Skip to content

Commit 3564a09

Browse files
Wip
1 parent e5d5083 commit 3564a09

16 files changed

+67
-30
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ attr_parsing_invalid_attr_unsafe = `{$name}` is not an unsafe attribute
174174
.suggestion = remove the `unsafe(...)`
175175
.note = extraneous unsafe is not allowed in attributes
176176
177-
attr_parsing_invalid_meta_item = expected unsuffixed literal, found {$descr}
177+
attr_parsing_invalid_meta_item = expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found {$descr}
178+
.remove_neg_sugg = negative numbers are not literals, try removing the `-` sign
178179
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
179180
180181
attr_parsing_suffixed_literal_in_attribute = suffixed literals are not allowed in attributes

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ use rustc_session::errors::report_lit_error;
1818
use rustc_session::parse::ParseSess;
1919
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
2020
use thin_vec::ThinVec;
21-
2221
use crate::ShouldEmit;
23-
use crate::session_diagnostics::{
24-
InvalidMetaItem, InvalidMetaItemQuoteIdentSugg, SuffixedLiteralInAttribute,
25-
};
22+
use crate::session_diagnostics::{InvalidMetaItem, InvalidMetaItemQuoteIdentSugg, InvalidMetaItemRemoveNegSugg, SuffixedLiteralInAttribute};
2623

2724
#[derive(Clone, Debug)]
2825
pub struct PathParser<'a>(pub Cow<'a, Path>);
@@ -313,15 +310,7 @@ fn expr_to_lit(
313310
match res {
314311
Ok(lit) => {
315312
if token_lit.suffix.is_some() {
316-
let mut err = psess.dcx().struct_span_err(
317-
expr.span,
318-
"suffixed literals are not allowed in attributes",
319-
);
320-
err.help(
321-
"instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), \
322-
use an unsuffixed version (`1`, `1.0`, etc.)",
323-
);
324-
err.emit_unless_delay(!should_emit.should_emit());
313+
psess.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }).emit_unless_delay(!should_emit.should_emit());
325314
None
326315
} else {
327316
if should_emit.should_emit() && !lit.kind.is_unsuffixed() {
@@ -355,6 +344,9 @@ fn expr_to_lit(
355344
if let ExprKind::Err(_) = expr.kind {
356345
err.downgrade_to_delayed_bug();
357346
}
347+
348+
// If this literal is a macro call and we're not currently emitting errors, don't emit a big
349+
// This macro call might still be expanded later and become a literal
358350
if matches!(expr.kind, ExprKind::MacCall(..)) && !should_emit.should_emit() {
359351
err.cancel();
360352
} else {
@@ -415,6 +407,8 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
415407
}
416408

417409
let path = self.parser.parse_path(PathStyle::Mod)?;
410+
411+
// Check style of arguments that this meta item has
418412
let args = if self.parser.check(exp!(OpenParen)) {
419413
let start = self.parser.token.span;
420414
let (sub_parsers, _) = self.parser.parse_paren_comma_seq(|parser| {
@@ -450,6 +444,7 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
450444
span: self.parser.token.span,
451445
descr: token_descr(&self.parser.token),
452446
quote_ident_sugg: None,
447+
remove_neg_sugg: None
453448
};
454449

455450
// Suggest quoting idents, e.g. in `#[cfg(key = value)]`. We don't use `Token::ident` and
@@ -468,6 +463,14 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
468463
});
469464
}
470465

466+
if self.parser.token == token::Minus && self.parser.look_ahead(1, |t| matches!(t.kind, rustc_ast::token::TokenKind::Literal {..} ) ) {
467+
err.remove_neg_sugg = Some(InvalidMetaItemRemoveNegSugg {
468+
negative_sign: self.parser.token.span,
469+
});
470+
self.parser.bump();
471+
self.parser.bump();
472+
}
473+
471474
Err(self.parser.dcx().create_err(err))
472475
}
473476

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ pub(crate) struct InvalidMetaItem {
768768
pub descr: String,
769769
#[subdiagnostic]
770770
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
771+
#[subdiagnostic]
772+
pub remove_neg_sugg: Option<InvalidMetaItemRemoveNegSugg>
771773
}
772774

773775
#[derive(Subdiagnostic)]
@@ -779,6 +781,13 @@ pub(crate) struct InvalidMetaItemQuoteIdentSugg {
779781
pub after: Span,
780782
}
781783

784+
#[derive(Subdiagnostic)]
785+
#[multipart_suggestion(attr_parsing_remove_neg_sugg, applicability = "machine-applicable")]
786+
pub(crate) struct InvalidMetaItemRemoveNegSugg {
787+
#[suggestion_part(code = "")]
788+
pub negative_sign: Span,
789+
}
790+
782791
#[derive(Diagnostic)]
783792
#[diag(attr_parsing_suffixed_literal_in_attribute)]
784793
#[help]

tests/ui/attributes/key-value-expansion-on-mac.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@ macro_rules! bar {
99
#[rustc_dummy = stringify!(b)]
1010
bar!();
1111

12+
macro_rules! becomes_lit {
13+
() => { 5 };
14+
}
15+
16+
macro_rules! becomes_invalid_lit {
17+
() => { 1 + 1 };
18+
}
19+
20+
#[rustc_dummy = becomes_lit!()]
21+
#[rustc_dummy = becomes_invalid_lit!()]
22+
bar!();
23+
1224
fn main() {}

tests/ui/attributes/malformed-fn-align.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn f3() {}
2626
#[repr(align(16))] //~ ERROR `#[repr(align(...))]` is not supported on function items
2727
fn f4() {}
2828

29-
#[rustc_align(-1)] //~ ERROR expected unsuffixed literal, found `-`
29+
#[rustc_align(-1)] //~ ERROR expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `-`
3030
fn f5() {}
3131

3232
#[rustc_align(3)] //~ ERROR invalid alignment value: not a power of two

tests/ui/attributes/malformed-fn-align.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ error[E0589]: invalid alignment value: not a power of two
3737
LL | #[rustc_align(0)]
3838
| ^
3939

40-
error: expected unsuffixed literal, found `-`
40+
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `-`
4141
--> $DIR/malformed-fn-align.rs:29:15
4242
|
4343
LL | #[rustc_align(-1)]
4444
| ^
45+
|
46+
help: negative numbers are not literals, try removing the `-` sign
47+
|
48+
LL - #[rustc_align(-1)]
49+
LL + #[rustc_align(1)]
50+
|
4551

4652
error[E0589]: invalid alignment value: not a power of two
4753
--> $DIR/malformed-fn-align.rs:32:15

tests/ui/attributes/nonterminal-expansion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
macro_rules! pass_nonterminal {
66
($n:expr) => {
77
#[repr(align($n))]
8-
//~^ ERROR expected unsuffixed literal, found `expr` metavariable
8+
//~^ ERROR expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `expr` metavariable
99
struct S;
1010
};
1111
}

tests/ui/attributes/nonterminal-expansion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected unsuffixed literal, found `expr` metavariable
1+
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `expr` metavariable
22
--> $DIR/nonterminal-expansion.rs:7:22
33
|
44
LL | #[repr(align($n))]

tests/ui/conditional-compilation/cfg-attr-syntax-validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct S9;
3838
macro_rules! generate_s10 {
3939
($expr: expr) => {
4040
#[cfg(feature = $expr)]
41-
//~^ ERROR expected unsuffixed literal, found `expr` metavariable
41+
//~^ ERROR expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `expr` metavariable
4242
struct S10;
4343
}
4444
}

tests/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ LL | #[cfg(a = b"hi")]
7171
|
7272
= note: expected a normal string literal, not a byte string literal
7373

74-
error: expected unsuffixed literal, found `expr` metavariable
74+
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `expr` metavariable
7575
--> $DIR/cfg-attr-syntax-validation.rs:40:25
7676
|
7777
LL | #[cfg(feature = $expr)]

0 commit comments

Comments
 (0)