Skip to content

Commit a76690f

Browse files
committed
optimize unsupported literal diag message
1 parent fb7c76b commit a76690f

File tree

6 files changed

+87
-50
lines changed

6 files changed

+87
-50
lines changed

src/libsyntax/attr/builtin.rs

+70-39
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ enum AttrError {
2424
MissingSince,
2525
MissingFeature,
2626
MultipleStabilityLevels,
27-
UnsupportedLiteral
27+
UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
2828
}
2929

30-
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError, is_bytestr: bool) {
30+
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
3131
let diag = &sess.span_diagnostic;
3232
match error {
3333
AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
@@ -45,13 +45,11 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError, is_bytestr: boo
4545
AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
4646
AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
4747
"multiple stability levels"),
48-
AttrError::UnsupportedLiteral => {
49-
let mut err = struct_span_err!(
50-
diag,
51-
span,
52-
E0565,
53-
"unsupported literal",
54-
);
48+
AttrError::UnsupportedLiteral(
49+
msg,
50+
is_bytestr,
51+
) => {
52+
let mut err = struct_span_err!(diag, span, E0565, "{}", msg);
5553
if is_bytestr {
5654
if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
5755
err.span_suggestion_with_applicability(
@@ -222,7 +220,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
222220
let meta = meta.as_ref().unwrap();
223221
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
224222
if item.is_some() {
225-
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
223+
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
226224
return false
227225
}
228226
if let Some(v) = meta.value_str() {
@@ -252,13 +250,19 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
252250
sess,
253251
mi.span,
254252
AttrError::UnknownMetaItem(mi.name(), expected),
255-
false,
256253
);
257254
continue 'outer
258255
}
259256
}
260257
} else {
261-
handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
258+
handle_errors(
259+
sess,
260+
meta.span,
261+
AttrError::UnsupportedLiteral(
262+
"unsupported literal",
263+
false,
264+
),
265+
);
262266
continue 'outer
263267
}
264268
}
@@ -283,7 +287,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
283287
})
284288
}
285289
(None, _) => {
286-
handle_errors(sess, attr.span(), AttrError::MissingSince, false);
290+
handle_errors(sess, attr.span(), AttrError::MissingSince);
287291
continue
288292
}
289293
_ => {
@@ -309,7 +313,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
309313
}
310314
"unstable" => {
311315
if stab.is_some() {
312-
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
316+
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
313317
break
314318
}
315319

@@ -330,13 +334,19 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
330334
mi.name(),
331335
&["feature", "reason", "issue"]
332336
),
333-
false,
334337
);
335338
continue 'outer
336339
}
337340
}
338341
} else {
339-
handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
342+
handle_errors(
343+
sess,
344+
meta.span,
345+
AttrError::UnsupportedLiteral(
346+
"unsupported literal",
347+
false,
348+
),
349+
);
340350
continue 'outer
341351
}
342352
}
@@ -363,7 +373,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
363373
})
364374
}
365375
(None, _, _) => {
366-
handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
376+
handle_errors(sess, attr.span(), AttrError::MissingFeature);
367377
continue
368378
}
369379
_ => {
@@ -374,7 +384,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
374384
}
375385
"stable" => {
376386
if stab.is_some() {
377-
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
387+
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
378388
break
379389
}
380390

@@ -393,7 +403,6 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
393403
AttrError::UnknownMetaItem(
394404
mi.name(), &["since", "note"],
395405
),
396-
false,
397406
);
398407
continue 'outer
399408
}
@@ -402,9 +411,11 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
402411
NestedMetaItemKind::Literal(lit) => {
403412
handle_errors(
404413
sess,
405-
meta.span,
406-
AttrError::UnsupportedLiteral,
407-
lit.node.is_bytestr()
414+
lit.span,
415+
AttrError::UnsupportedLiteral(
416+
"unsupported literal",
417+
false,
418+
),
408419
);
409420
continue 'outer
410421
}
@@ -424,11 +435,11 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
424435
})
425436
}
426437
(None, _) => {
427-
handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
438+
handle_errors(sess, attr.span(), AttrError::MissingFeature);
428439
continue
429440
}
430441
_ => {
431-
handle_errors(sess, attr.span(), AttrError::MissingSince, false);
442+
handle_errors(sess, attr.span(), AttrError::MissingSince);
432443
continue
433444
}
434445
}
@@ -498,8 +509,11 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
498509
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
499510
handle_errors(
500511
sess,
501-
lit.span, AttrError::UnsupportedLiteral,
502-
lit.node.is_bytestr(),
512+
lit.span,
513+
AttrError::UnsupportedLiteral(
514+
"literal in `cfg` predicate value must be a string",
515+
lit.node.is_bytestr()
516+
),
503517
);
504518
true
505519
}
@@ -520,7 +534,14 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
520534
ast::MetaItemKind::List(ref mis) => {
521535
for mi in mis.iter() {
522536
if !mi.is_meta_item() {
523-
handle_errors(sess, mi.span, AttrError::UnsupportedLiteral, false);
537+
handle_errors(
538+
sess,
539+
mi.span,
540+
AttrError::UnsupportedLiteral(
541+
"unsupported literal",
542+
false
543+
),
544+
);
524545
return false;
525546
}
526547
}
@@ -591,7 +612,7 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
591612
depr = if let Some(metas) = attr.meta_item_list() {
592613
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
593614
if item.is_some() {
594-
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
615+
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
595616
return false
596617
}
597618
if let Some(v) = meta.value_str() {
@@ -602,8 +623,11 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
602623
handle_errors(
603624
sess,
604625
lit.span,
605-
AttrError::UnsupportedLiteral,
606-
lit.node.is_bytestr(),
626+
AttrError::UnsupportedLiteral(
627+
"literal in `deprecated` \
628+
value must be a string",
629+
lit.node.is_bytestr()
630+
),
607631
);
608632
} else {
609633
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
@@ -626,15 +650,20 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
626650
sess,
627651
meta.span,
628652
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
629-
false,
630653
);
631654
continue 'outer
632655
}
633656
}
634657
}
635658
NestedMetaItemKind::Literal(lit) => {
636-
let is_bytestr = lit.node.is_bytestr();
637-
handle_errors(sess, lit.span, AttrError::UnsupportedLiteral, is_bytestr);
659+
handle_errors(
660+
sess,
661+
lit.span,
662+
AttrError::UnsupportedLiteral(
663+
"item in `deprecated` must be a key/value pair",
664+
false,
665+
),
666+
);
638667
continue 'outer
639668
}
640669
}
@@ -694,12 +723,14 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
694723
mark_used(attr);
695724
for item in items {
696725
if !item.is_meta_item() {
697-
let (span, is_bytestr) = if let Some(lit) = item.literal() {
698-
(lit.span, lit.node.is_bytestr())
699-
} else {
700-
(item.span, false)
701-
};
702-
handle_errors(sess, span, AttrError::UnsupportedLiteral, is_bytestr);
726+
handle_errors(
727+
sess,
728+
item.span,
729+
AttrError::UnsupportedLiteral(
730+
"meta item in `repr` must be an identifier",
731+
false,
732+
),
733+
);
703734
continue
704735
}
705736

src/test/ui/conditional-compilation/cfg-attr-syntax-validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ struct S6;
1919
#[cfg(a())] //~ ERROR invalid predicate `a`
2020
struct S7;
2121

22-
#[cfg(a = 10)] //~ ERROR unsupported literal
22+
#[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
2323
struct S8;
2424

25-
#[deprecated(since = b"1.30", note = "hi")] //~ ERROR E0565
25+
#[cfg(a = b"hi")]
2626
struct S9;
2727

2828
macro_rules! generate_s10 {

src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

+12-6
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,28 @@ error[E0537]: invalid predicate `a`
4040
LL | #[cfg(a())] //~ ERROR invalid predicate `a`
4141
| ^^^
4242

43-
error[E0565]: unsupported literal
43+
error[E0565]: literal in `cfg` predicate value must be a string
4444
--> $DIR/cfg-attr-syntax-validation.rs:22:11
4545
|
46-
LL | #[cfg(a = 10)] //~ ERROR unsupported literal
46+
LL | #[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
4747
| ^^
4848

49+
error[E0565]: literal in `cfg` predicate value must be a string
50+
--> $DIR/cfg-attr-syntax-validation.rs:25:11
51+
|
52+
LL | #[cfg(a = b"hi")]
53+
| ^^^^^ help: consider removing the prefix: `"hi"`
54+
4955
error: `cfg` is not a well-formed meta-item
50-
--> $DIR/cfg-attr-syntax-validation.rs:27:9
56+
--> $DIR/cfg-attr-syntax-validation.rs:30:9
5157
|
5258
LL | #[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
5359
| ^^^^^^^^^^^^^^^^^^^^^^^ help: expected syntax is: `#[cfg(/* predicate */)]`
5460
...
55-
LL | generate_s9!(concat!("nonexistent"));
56-
| ------------------------------------- in this macro invocation
61+
LL | generate_s10!(concat!("nonexistent"));
62+
| -------------------------------------- in this macro invocation
5763

58-
error: aborting due to 9 previous errors
64+
error: aborting due to 10 previous errors
5965

6066
Some errors occurred: E0537, E0565.
6167
For more information about an error, try `rustc --explain E0537`.

src/test/ui/error-codes/E0565-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0565]: unsupported literal
1+
error[E0565]: item in `deprecated` must be a key/value pair
22
--> $DIR/E0565-1.rs:12:14
33
|
44
LL | #[deprecated("since")] //~ ERROR E0565

src/test/ui/error-codes/E0565-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0565]: unsupported literal
1+
error[E0565]: literal in `deprecated` value must be a string
22
--> $DIR/E0565-2.rs:12:22
33
|
44
LL | #[deprecated(since = b"1.29", note = "hi")] //~ ERROR E0565

src/test/ui/error-codes/E0565.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0565]: unsupported literal
1+
error[E0565]: meta item in `repr` must be an identifier
22
--> $DIR/E0565.rs:12:8
33
|
44
LL | #[repr("C")] //~ ERROR E0565

0 commit comments

Comments
 (0)