Skip to content

Commit 7787c54

Browse files
committed
make error codes reflect reality better
1 parent 12a5073 commit 7787c54

34 files changed

+377
-303
lines changed

compiler/rustc_attr_parsing/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ attr_parsing_unsupported_literal_cfg_boolean =
121121
literal in `cfg` predicate value must be a boolean
122122
attr_parsing_unsupported_literal_cfg_string =
123123
literal in `cfg` predicate value must be a string
124-
attr_parsing_unsupported_literal_deprecated_kv_pair =
125-
item in `deprecated` must be a key/value pair
126-
attr_parsing_unsupported_literal_deprecated_string =
127-
literal in `deprecated` value must be a string
128124
attr_parsing_unsupported_literal_generic =
129125
unsupported literal
130126
attr_parsing_unsupported_literal_suggestion =

compiler/rustc_attr_parsing/src/attributes/confusables.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ impl<S: Stage> AttributeParser<S> for ConfusablesParser {
3030
for param in list.mixed() {
3131
let span = param.span();
3232

33-
let Some(lit) = param.lit() else {
34-
cx.expected_string_literal(span);
33+
let Some(lit) = param.lit().and_then(|i| i.value_str()) else {
34+
cx.expected_string_literal(span, param.lit());
3535
continue;
3636
};
3737

38-
this.confusables.push(lit.symbol);
38+
this.confusables.push(lit);
3939
}
4040

4141
this.first_span.get_or_insert(cx.attr_span);

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

+47-52
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use super::{AttributeOrder, OnDuplicate, SingleAttributeParser};
88
use crate::context::{AcceptContext, Stage};
99
use crate::parser::ArgParser;
1010
use crate::session_diagnostics;
11-
use crate::session_diagnostics::UnsupportedLiteralReason;
1211

1312
pub(crate) struct DeprecationParser;
1413

@@ -27,13 +26,7 @@ fn get<S: Stage>(
2726
if let Some(value_str) = v.value_as_str() {
2827
Some(value_str)
2928
} else {
30-
let lit = v.value_as_lit();
31-
cx.emit_err(session_diagnostics::UnsupportedLiteral {
32-
span: v.value_span,
33-
reason: UnsupportedLiteralReason::DeprecatedString,
34-
is_bytestr: lit.kind.is_bytestr(),
35-
start_point_span: cx.sess().source_map().start_point(lit.span),
36-
});
29+
cx.expected_string_literal(v.value_span, Some(&v.value_as_lit()));
3730
None
3831
}
3932
} else {
@@ -61,57 +54,59 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
6154

6255
let is_rustc = features.staged_api();
6356

64-
if let Some(value) = args.name_value()
65-
&& let Some(value_str) = value.value_as_str()
66-
{
67-
note = Some(value_str)
68-
} else if let Some(list) = args.list() {
69-
for param in list.mixed() {
70-
let param_span = param.span();
71-
let Some(param) = param.meta_item() else {
72-
cx.emit_err(session_diagnostics::UnsupportedLiteral {
73-
span: param_span,
74-
reason: UnsupportedLiteralReason::DeprecatedKvPair,
75-
is_bytestr: false,
76-
start_point_span: cx.sess().source_map().start_point(param_span),
77-
});
78-
return None;
79-
};
57+
match args {
58+
ArgParser::NoArgs => {
59+
// ok
60+
}
61+
ArgParser::List(list) => {
62+
for param in list.mixed() {
63+
let Some(param) = param.meta_item() else {
64+
cx.unexpected_literal(param.span());
65+
return None;
66+
};
8067

81-
let (ident, arg) = param.word_or_empty();
68+
let (ident, arg) = param.word_or_empty();
8269

83-
match ident.name {
84-
sym::since => {
85-
since = Some(get(cx, ident, param_span, arg, &since)?);
86-
}
87-
sym::note => {
88-
note = Some(get(cx, ident, param_span, arg, &note)?);
89-
}
90-
sym::suggestion => {
91-
if !features.deprecated_suggestion() {
92-
cx.emit_err(session_diagnostics::DeprecatedItemSuggestion {
93-
span: param_span,
94-
is_nightly: cx.sess().is_nightly_build(),
95-
details: (),
96-
});
70+
match ident.name {
71+
sym::since => {
72+
since = Some(get(cx, ident, param.span(), arg, &since)?);
73+
}
74+
sym::note => {
75+
note = Some(get(cx, ident, param.span(), arg, &note)?);
9776
}
77+
sym::suggestion => {
78+
if !features.deprecated_suggestion() {
79+
cx.emit_err(session_diagnostics::DeprecatedItemSuggestion {
80+
span: param.span(),
81+
is_nightly: cx.sess().is_nightly_build(),
82+
details: (),
83+
});
84+
}
9885

99-
suggestion = Some(get(cx, ident, param_span, arg, &suggestion)?);
100-
}
101-
_ => {
102-
cx.unknown_key(
103-
param_span,
104-
ident.to_string(),
105-
if features.deprecated_suggestion() {
106-
&["since", "note", "suggestion"]
107-
} else {
108-
&["since", "note"]
109-
},
110-
);
111-
return None;
86+
suggestion = Some(get(cx, ident, param.span(), arg, &suggestion)?);
87+
}
88+
_ => {
89+
cx.unknown_key(
90+
param.span(),
91+
ident.to_string(),
92+
if features.deprecated_suggestion() {
93+
&["since", "note", "suggestion"]
94+
} else {
95+
&["since", "note"]
96+
},
97+
);
98+
return None;
99+
}
112100
}
113101
}
114102
}
103+
ArgParser::NameValue(v) => {
104+
let Some(value) = v.value_as_str() else {
105+
cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
106+
return None;
107+
};
108+
note = Some(value);
109+
}
115110
}
116111

117112
let since = if let Some(since) = since {

compiler/rustc_attr_parsing/src/attributes/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
8181
};
8282

8383
let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
84-
cx.expected_string_literal(l.span());
84+
cx.expected_string_literal(l.span(), l.lit());
8585
return None;
8686
};
8787

8888
Some(reason)
8989
}
9090
ArgParser::NameValue(v) => {
9191
let Some(reason) = v.value_as_str() else {
92-
cx.expected_string_literal(v.value_span);
92+
cx.expected_string_literal(v.value_span, Some(v.value_as_lit()));
9393
return None;
9494
};
9595

compiler/rustc_attr_parsing/src/attributes/repr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl<S: Stage> CombineAttributeParser<S> for ReprParser {
3434
let mut reprs = Vec::new();
3535

3636
let Some(list) = args.list() else {
37+
cx.expected_list(cx.attr_span);
3738
return reprs;
3839
};
3940

compiler/rustc_attr_parsing/src/context.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::{Deref, DerefMut};
44
use std::sync::LazyLock;
55

66
use private::Sealed;
7-
use rustc_ast::{self as ast, DelimArgs, NodeId};
7+
use rustc_ast::{self as ast, DelimArgs, MetaItemLit, NodeId};
88
use rustc_attr_data_structures::AttributeKind;
99
use rustc_data_structures::sync::DynSend;
1010
use rustc_errors::{DiagCtxtHandle, Diagnostic, LintDiagnostic};
@@ -250,13 +250,25 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
250250
self.emit_err(UnknownMetaItem { span, item: found, expected: options })
251251
}
252252

253-
pub(crate) fn expected_string_literal(&self, span: Span) -> ErrorGuaranteed {
253+
/// error that a string literal was expected.
254+
/// You can optionally give the literal you did find (which you found not to be a string literal)
255+
/// which can make better errors. For example, if the literal was a byte string it will suggest
256+
/// removing the `b` prefix.
257+
pub(crate) fn expected_string_literal(
258+
&self,
259+
span: Span,
260+
actual_literal: Option<&MetaItemLit>,
261+
) -> ErrorGuaranteed {
254262
self.emit_err(AttributeParseError {
255263
span,
256264
attr_span: self.attr_span,
257265
template: self.template.clone(),
258266
attribute: self.attr_path.clone(),
259-
reason: AttributeParseErrorReason::ExpectedStringLiteral,
267+
reason: AttributeParseErrorReason::ExpectedStringLiteral {
268+
byte_string: actual_literal.and_then(|i| {
269+
i.kind.is_bytestr().then(|| self.sess().source_map().start_point(i.span))
270+
}),
271+
},
260272
})
261273
}
262274

@@ -293,6 +305,18 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
293305
})
294306
}
295307

308+
/// an error that should be emitted when a [`MetaItemOrLitParser`](crate::parser::MetaItemOrLitParser)
309+
/// was expected *not* to be a literal, but instead a meta item.
310+
pub(crate) fn unexpected_literal(&self, span: Span) -> ErrorGuaranteed {
311+
self.emit_err(AttributeParseError {
312+
span,
313+
attr_span: self.attr_span,
314+
template: self.template.clone(),
315+
attribute: self.attr_path.clone(),
316+
reason: AttributeParseErrorReason::UnexpectedLiteral,
317+
})
318+
}
319+
296320
pub(crate) fn expected_single_argument(&self, span: Span) -> ErrorGuaranteed {
297321
self.emit_err(AttributeParseError {
298322
span,

compiler/rustc_attr_parsing/src/session_diagnostics.rs

+30-19
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ pub(crate) enum UnsupportedLiteralReason {
1616
Generic,
1717
CfgString,
1818
CfgBoolean,
19-
DeprecatedString,
20-
DeprecatedKvPair,
2119
}
2220

2321
#[derive(Diagnostic)]
@@ -190,6 +188,7 @@ pub(crate) struct InvalidReprHintNoValue {
190188
}
191189

192190
/// Error code: E0565
191+
// FIXME(jdonszelmann): slowly phased out
193192
pub(crate) struct UnsupportedLiteral {
194193
pub span: Span,
195194
pub reason: UnsupportedLiteralReason,
@@ -212,12 +211,6 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnsupportedLiteral {
212211
UnsupportedLiteralReason::CfgBoolean => {
213212
fluent::attr_parsing_unsupported_literal_cfg_boolean
214213
}
215-
UnsupportedLiteralReason::DeprecatedString => {
216-
fluent::attr_parsing_unsupported_literal_deprecated_string
217-
}
218-
UnsupportedLiteralReason::DeprecatedKvPair => {
219-
fluent::attr_parsing_unsupported_literal_deprecated_kv_pair
220-
}
221214
},
222215
);
223216
diag.span(self.span);
@@ -473,9 +466,10 @@ pub(crate) struct UnrecognizedReprHint {
473466
}
474467

475468
pub(crate) enum AttributeParseErrorReason {
476-
ExpectedStringLiteral,
469+
ExpectedStringLiteral { byte_string: Option<Span> },
477470
ExpectedSingleArgument,
478471
ExpectedList,
472+
UnexpectedLiteral,
479473
ExpectedNameValue(Option<Symbol>),
480474
DuplicateKey(Symbol),
481475
ExpectedSpecificArgument { possibilities: Vec<&'static str>, strings: bool },
@@ -497,27 +491,44 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
497491
diag.span(self.attr_span);
498492
diag.code(E0539);
499493
match self.reason {
500-
AttributeParseErrorReason::ExpectedStringLiteral => {
501-
diag.span_note(self.span, "expected a string literal here");
494+
AttributeParseErrorReason::ExpectedStringLiteral { byte_string } => {
495+
if let Some(start_point_span) = byte_string {
496+
diag.span_suggestion(
497+
start_point_span,
498+
fluent::attr_parsing_unsupported_literal_suggestion,
499+
"",
500+
Applicability::MaybeIncorrect,
501+
);
502+
diag.note("expected a normal string literal, not a byte string literal");
503+
504+
return diag;
505+
} else {
506+
diag.span_label(self.span, "expected a string literal here");
507+
}
502508
}
503509
AttributeParseErrorReason::ExpectedSingleArgument => {
504-
diag.span_note(self.span, "expected a single argument here");
510+
diag.span_label(self.span, "expected a single argument here");
511+
diag.code(E0540);
505512
}
506513
AttributeParseErrorReason::ExpectedList => {
507-
diag.span_note(self.span, "expected this to be a list");
514+
diag.span_label(self.span, "expected this to be a list");
508515
}
509516
AttributeParseErrorReason::DuplicateKey(key) => {
510-
diag.span_note(self.span, format!("found `{key}` used as a key more than once"));
517+
diag.span_label(self.span, format!("found `{key}` used as a key more than once"));
511518
diag.code(E0538);
512519
}
520+
AttributeParseErrorReason::UnexpectedLiteral => {
521+
diag.span_label(self.span, format!("didn't expect a literal here"));
522+
diag.code(E0565);
523+
}
513524
AttributeParseErrorReason::ExpectedNameValue(None) => {
514-
diag.span_note(
525+
diag.span_label(
515526
self.span,
516527
format!("expected this to be of the form `{name} = \"...\"`"),
517528
);
518529
}
519530
AttributeParseErrorReason::ExpectedNameValue(Some(name)) => {
520-
diag.span_note(
531+
diag.span_label(
521532
self.span,
522533
format!("expected this to be of the form `{name} = \"...\"`"),
523534
);
@@ -527,13 +538,13 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
527538
match possibilities.as_slice() {
528539
&[] => {}
529540
&[x] => {
530-
diag.span_note(
541+
diag.span_label(
531542
self.span,
532543
format!("the only valid argument here is {quote}{x}{quote}"),
533544
);
534545
}
535546
[first, second] => {
536-
diag.span_note(self.span, format!("valid arguments are {quote}{first}{quote} or {quote}{second}{quote}"));
547+
diag.span_label(self.span, format!("valid arguments are {quote}{first}{quote} or {quote}{second}{quote}"));
537548
}
538549
[first @ .., second_to_last, last] => {
539550
let mut res = String::new();
@@ -544,7 +555,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
544555
"{quote}{second_to_last}{quote} or {quote}{last}{quote}"
545556
));
546557

547-
diag.span_note(self.span, format!("valid arguments are {res}"));
558+
diag.span_label(self.span, format!("valid arguments are {res}"));
548559
}
549560
}
550561
}

compiler/rustc_error_codes/src/error_codes/E0534.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
#### Note: this error code is no longer emitted by the compiler
2+
3+
This is because it was too specific to the `inline` attribute.
4+
Similar diagnostics occur for other attributes too.
5+
The example here will now emit `E0540`
6+
17
The `inline` attribute was malformed.
28

39
Erroneous code example:
410

5-
```compile_fail,E0534
11+
```compile_fail,E0540
612
#[inline()] // error: expected one argument
713
pub fn something() {}
814

compiler/rustc_error_codes/src/error_codes/E0535.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
An unknown argument was given to the `inline` attribute.
1+
#### Note: this error code is no longer emitted by the compiler
2+
3+
This is because it was too specific to the `inline` attribute.
4+
Similar diagnostics occur for other attributes too.
5+
The example here will now emit `E0539`
6+
27

38
Erroneous code example:
49

5-
```compile_fail,E0535
10+
```compile_fail,E0539
611
#[inline(unknown)] // error: invalid argument
712
pub fn something() {}
813

0 commit comments

Comments
 (0)