Skip to content

Commit 8b043af

Browse files
committed
use the parsed literal as the span source for errors
1 parent f0d5956 commit 8b043af

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

macros/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use proc_macro::TokenStream;
1212
use proc_macro2::TokenStream as TokenStream2;
1313
use quote::{quote, quote_spanned};
1414
use std::fmt;
15-
use syn::spanned::Spanned;
15+
use syn::{LitStr, spanned::Spanned};
1616

1717
mod error;
1818
mod parser;
@@ -22,10 +22,9 @@ mod parser;
2222
pub fn parse_lit(input: TokenStream) -> TokenStream {
2323
build_uuid(input.clone()).unwrap_or_else(|e| {
2424
let msg = e.to_string();
25-
let ts = TokenStream2::from(input);
2625
let span =
2726
match e {
28-
Error::UuidParse(error::Error(error::ErrorKind::Char {
27+
Error::UuidParse(lit, error::Error(error::ErrorKind::Char {
2928
character,
3029
index,
3130
})) => {
@@ -36,19 +35,20 @@ pub fn parse_lit(input: TokenStream) -> TokenStream {
3635
width += 1;
3736
}
3837
let mut s = proc_macro2::Literal::string("");
39-
s.set_span(ts.span());
38+
s.set_span(lit.span());
4039
s.subspan(index..index + width - 1)
4140
}
42-
Error::UuidParse(error::Error(
41+
Error::UuidParse(lit, error::Error(
4342
error::ErrorKind::GroupLength { index, len, .. },
4443
)) => {
4544
let mut s = proc_macro2::Literal::string("");
46-
s.set_span(ts.span());
45+
s.set_span(lit.span());
4746
s.subspan(index..index + len)
4847
}
4948
_ => None,
5049
}
51-
.unwrap_or_else(|| ts.span());
50+
.unwrap_or_else(|| TokenStream2::from(input).span());
51+
5252
TokenStream::from(quote_spanned! {span=>
5353
compile_error!(#msg)
5454
})
@@ -57,26 +57,26 @@ pub fn parse_lit(input: TokenStream) -> TokenStream {
5757

5858
enum Error {
5959
NonStringLiteral,
60-
UuidParse(error::Error),
60+
UuidParse(LitStr, error::Error),
6161
}
6262

6363
impl fmt::Display for Error {
6464
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6565
match *self {
6666
Error::NonStringLiteral => f.write_str("expected string literal"),
67-
Error::UuidParse(ref e) => write!(f, "{}", e),
67+
Error::UuidParse(_, ref e) => write!(f, "{}", e),
6868
}
6969
}
7070
}
7171

7272
fn build_uuid(input: TokenStream) -> Result<TokenStream, Error> {
73-
let string = match syn::parse::<syn::Lit>(input) {
74-
Ok(syn::Lit::Str(literal)) => literal.value(),
73+
let str_lit = match syn::parse::<syn::Lit>(input) {
74+
Ok(syn::Lit::Str(literal)) => literal,
7575
_ => return Err(Error::NonStringLiteral),
7676
};
7777

78-
let bytes = parser::try_parse(&string)
79-
.map_err(|e| Error::UuidParse(e.into_err()))?;
78+
let bytes = parser::try_parse(&str_lit.value())
79+
.map_err(|e| Error::UuidParse(str_lit, e.into_err()))?;
8080

8181
let tokens = bytes
8282
.iter()

tests/ui/compile_fail/invalid_parse.stderr

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error: invalid length: expected length 32 for simple format, found 0
2-
--> tests/ui/compile_fail/invalid_parse.rs:3:23
2+
--> tests/ui/compile_fail/invalid_parse.rs:3:17
33
|
44
3 | const _: Uuid = uuid!("");
5-
| ^^
5+
| ^^^^^^^^^
6+
|
7+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
68

79
error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-zA-Z], found `!` at 1
810
--> tests/ui/compile_fail/invalid_parse.rs:4:24
@@ -29,16 +31,20 @@ error: invalid character: expected an optional prefix of `urn:uuid:` followed by
2931
| ^
3032

3133
error: invalid group count: expected 5, found 4
32-
--> tests/ui/compile_fail/invalid_parse.rs:8:23
34+
--> tests/ui/compile_fail/invalid_parse.rs:8:17
3335
|
3436
8 | const _: Uuid = uuid!("F9168C5E-CEB2-4faa-B6BFF329BF39FA1E4");
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
|
39+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
3640

3741
error: invalid group count: expected 5, found 3
38-
--> tests/ui/compile_fail/invalid_parse.rs:9:23
42+
--> tests/ui/compile_fail/invalid_parse.rs:9:17
3943
|
4044
9 | const _: Uuid = uuid!("F9168C5E-CEB2-4faa");
41-
| ^^^^^^^^^^^^^^^^^^^^
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
|
47+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
4248

4349
error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-zA-Z], found `X` at 19
4450
--> tests/ui/compile_fail/invalid_parse.rs:10:42
@@ -59,10 +65,12 @@ error: invalid group length in group 4: expected 12, found 8
5965
| ^^^^^^^^
6066

6167
error: invalid length: expected length 32 for simple format, found 33
62-
--> tests/ui/compile_fail/invalid_parse.rs:13:23
68+
--> tests/ui/compile_fail/invalid_parse.rs:13:17
6369
|
6470
13 | const _: Uuid = uuid!("67e5504410b1426f9247bb680e5fe0c88");
65-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72+
|
73+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
6674

6775
error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-zA-Z], found `g` at 32
6876
--> tests/ui/compile_fail/invalid_parse.rs:14:55
@@ -71,10 +79,12 @@ error: invalid character: expected an optional prefix of `urn:uuid:` followed by
7179
| ^
7280

7381
error: invalid length: expected length 32 for simple format, found 31
74-
--> tests/ui/compile_fail/invalid_parse.rs:18:23
82+
--> tests/ui/compile_fail/invalid_parse.rs:18:17
7583
|
7684
18 | const _: Uuid = uuid!("67e5504410b1426f9247bb680e5fe0c");
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86+
|
87+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
7888

7989
error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-zA-Z], found `X` at 7
8090
--> tests/ui/compile_fail/invalid_parse.rs:19:30
@@ -83,10 +93,12 @@ error: invalid character: expected an optional prefix of `urn:uuid:` followed by
8393
| ^
8494

8595
error: invalid group count: expected 5, found 2
86-
--> tests/ui/compile_fail/invalid_parse.rs:20:23
96+
--> tests/ui/compile_fail/invalid_parse.rs:20:17
8797
|
8898
20 | const _: Uuid = uuid!("67e550-4105b1426f9247bb680e5fe0c");
89-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100+
|
101+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
90102

91103
error: invalid group length in group 3: expected 4, found 5
92104
--> tests/ui/compile_fail/invalid_parse.rs:21:43
@@ -113,10 +125,12 @@ error: invalid group length in group 4: expected 12, found 8
113125
| ^^^^^^^^
114126

115127
error: invalid group count: expected 5, found 4
116-
--> tests/ui/compile_fail/invalid_parse.rs:27:23
128+
--> tests/ui/compile_fail/invalid_parse.rs:27:17
117129
|
118130
27 | const _: Uuid = uuid!("F9168C5E-CEB2-4faa-B6BFF329BF39FA1E4");
119-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
132+
|
133+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
120134

121135
error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-zA-Z], found `G` at 30
122136
--> tests/ui/compile_fail/invalid_parse.rs:28:53
@@ -143,10 +157,12 @@ error: invalid character: expected an optional prefix of `urn:uuid:` followed by
143157
| ^
144158

145159
error: invalid group count: expected 5, found 2
146-
--> tests/ui/compile_fail/invalid_parse.rs:35:23
160+
--> tests/ui/compile_fail/invalid_parse.rs:35:17
147161
|
148162
35 | const _: Uuid = uuid!("67e550-4105b1426f9247bb680e5fe0c");
149-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164+
|
165+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
150166

151167
error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-zA-Z], found `岡` at 7
152168
--> tests/ui/compile_fail/invalid_parse.rs:37:30
@@ -167,10 +183,12 @@ error: invalid character: expected an optional prefix of `urn:uuid:` followed by
167183
| ^^
168184

169185
error: invalid length: expected length 32 for simple format, found 4
170-
--> tests/ui/compile_fail/invalid_parse.rs:42:23
186+
--> tests/ui/compile_fail/invalid_parse.rs:42:17
171187
|
172188
42 | const _: Uuid = uuid!("F916");
173-
| ^^^^^^
189+
| ^^^^^^^^^^^^^
190+
|
191+
= note: this error originates in the macro `uuid` (in Nightly builds, run with -Z macro-backtrace for more info)
174192

175193
error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-zA-Z], found `x` at 5
176194
--> tests/ui/compile_fail/invalid_parse.rs:43:28

0 commit comments

Comments
 (0)