Fix misleading message when using a named constant as a struct alignment/pack

This commit is contained in:
yukang 2024-03-02 22:42:13 +08:00
parent e612d079a1
commit 5a5c6dfb33
8 changed files with 126 additions and 22 deletions

View File

@ -27,10 +27,16 @@ attr_incorrect_meta_item =
attr_incorrect_repr_format_align_one_arg = attr_incorrect_repr_format_align_one_arg =
incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
attr_incorrect_repr_format_expect_literal_integer =
incorrect `repr(align)` attribute format: `align` expect a literal integer as argument
attr_incorrect_repr_format_generic = attr_incorrect_repr_format_generic =
incorrect `repr({$repr_arg})` attribute format incorrect `repr({$repr_arg})` attribute format
.suggestion = use parentheses instead .suggestion = use parentheses instead
attr_incorrect_repr_format_packed_expect_integer =
incorrect `repr(packed)` attribute format: `packed` expect a literal integer as argument
attr_incorrect_repr_format_packed_one_or_zero_arg = attr_incorrect_repr_format_packed_one_or_zero_arg =
incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all

View File

@ -1039,21 +1039,37 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
}); });
} }
} }
MetaItemKind::List(_) => { MetaItemKind::List(nested_items) => {
if meta_item.has_name(sym::align) { if meta_item.has_name(sym::align) {
recognised = true; recognised = true;
if nested_items.len() == 1 {
sess.dcx().emit_err(
session_diagnostics::IncorrectReprFormatExpectInteger {
span: nested_items[0].span(),
},
);
} else {
sess.dcx().emit_err( sess.dcx().emit_err(
session_diagnostics::IncorrectReprFormatAlignOneArg { session_diagnostics::IncorrectReprFormatAlignOneArg {
span: meta_item.span, span: meta_item.span,
}, },
); );
}
} else if meta_item.has_name(sym::packed) { } else if meta_item.has_name(sym::packed) {
recognised = true; recognised = true;
if nested_items.len() == 1 {
sess.dcx().emit_err(
session_diagnostics::IncorrectReprFormatPackedExpectInteger {
span: nested_items[0].span(),
},
);
} else {
sess.dcx().emit_err( sess.dcx().emit_err(
session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg { session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
span: meta_item.span, span: meta_item.span,
}, },
); );
}
} else if matches!( } else if matches!(
meta_item.name_or_empty(), meta_item.name_or_empty(),
sym::Rust | sym::C | sym::simd | sym::transparent sym::Rust | sym::C | sym::simd | sym::transparent

View File

@ -170,6 +170,12 @@ pub(crate) struct IncorrectReprFormatPackedOneOrZeroArg {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
} }
#[derive(Diagnostic)]
#[diag(attr_incorrect_repr_format_packed_expect_integer, code = E0552)]
pub(crate) struct IncorrectReprFormatPackedExpectInteger {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(attr_invalid_repr_hint_no_paren, code = E0552)] #[diag(attr_invalid_repr_hint_no_paren, code = E0552)]
@ -252,6 +258,13 @@ pub(crate) struct IncorrectReprFormatAlignOneArg {
pub span: Span, pub span: Span,
} }
#[derive(Diagnostic)]
#[diag(attr_incorrect_repr_format_expect_literal_integer, code = E0693)]
pub(crate) struct IncorrectReprFormatExpectInteger {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(attr_incorrect_repr_format_generic, code = E0693)] #[diag(attr_incorrect_repr_format_generic, code = E0693)]
pub(crate) struct IncorrectReprFormatGeneric<'a> { pub(crate) struct IncorrectReprFormatGeneric<'a> {

View File

@ -441,7 +441,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
.map_err(|msg| { .map_err(|msg| {
struct_span_code_err!( struct_span_code_err!(
tcx.dcx(), tcx.dcx(),
attr.span, literal.span,
E0589, E0589,
"invalid `repr(align)` attribute: {}", "invalid `repr(align)` attribute: {}",
msg msg

View File

@ -0,0 +1,32 @@
//@ compile-flags: -Zdeduplicate-diagnostics=yes
const N: usize = 8;
#[repr(align(N))]
//~^ ERROR: incorrect `repr(align)` attribute format
struct T;
#[repr(align('a'))]
//~^ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer [E0589]
struct H;
#[repr(align("str"))]
//~^ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer [E0589]
struct L;
#[repr(align())]
//~^ ERROR: attribute format: `align` takes exactly one argument in parentheses
struct X;
const P: usize = 8;
#[repr(packed(P))]
//~^ ERROR: attribute format: `packed` expect a literal integer as argument
struct A;
#[repr(packed())]
//~^ ERROR: attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
struct B;
#[repr(packed)]
struct C;
fn main() {}

View File

@ -0,0 +1,40 @@
error[E0693]: incorrect `repr(align)` attribute format: `align` expect a literal integer as argument
--> $DIR/arg-error-issue-121425.rs:4:14
|
LL | #[repr(align(N))]
| ^
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
--> $DIR/arg-error-issue-121425.rs:8:8
|
LL | #[repr(align('a'))]
| ^^^^^^^^^^
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
--> $DIR/arg-error-issue-121425.rs:12:8
|
LL | #[repr(align("str"))]
| ^^^^^^^^^^^^
error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
--> $DIR/arg-error-issue-121425.rs:16:8
|
LL | #[repr(align())]
| ^^^^^^^
error[E0552]: incorrect `repr(packed)` attribute format: `packed` expect a literal integer as argument
--> $DIR/arg-error-issue-121425.rs:21:15
|
LL | #[repr(packed(P))]
| ^
error[E0552]: incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
--> $DIR/arg-error-issue-121425.rs:25:8
|
LL | #[repr(packed())]
| ^^^^^^^^
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0552, E0589, E0693.
For more information about an error, try `rustc --explain E0552`.

View File

@ -1,10 +1,11 @@
//@ compile-flags: -Zdeduplicate-diagnostics=yes
// Macros were previously expanded in `Expr` nonterminal tokens, now they are not. // Macros were previously expanded in `Expr` nonterminal tokens, now they are not.
macro_rules! pass_nonterminal { macro_rules! pass_nonterminal {
($n:expr) => { ($n:expr) => {
#[repr(align($n))] #[repr(align($n))]
//~^ ERROR expected unsuffixed literal or identifier, found `n!()` //~^ ERROR expected unsuffixed literal or identifier, found `n!()`
//~| ERROR incorrect `repr(align)` attribute format
struct S; struct S;
}; };
} }
@ -14,5 +15,6 @@ macro_rules! n {
} }
pass_nonterminal!(n!()); pass_nonterminal!(n!());
//~^ ERROR incorrect `repr(align)` attribute format: `align` expect a literal integer as argument [E0693]
fn main() {} fn main() {}

View File

@ -1,5 +1,5 @@
error: expected unsuffixed literal or identifier, found `n!()` error: expected unsuffixed literal or identifier, found `n!()`
--> $DIR/nonterminal-expansion.rs:5:22 --> $DIR/nonterminal-expansion.rs:7:22
| |
LL | #[repr(align($n))] LL | #[repr(align($n))]
| ^^ | ^^
@ -9,16 +9,11 @@ LL | pass_nonterminal!(n!());
| |
= note: this error originates in the macro `pass_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `pass_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses error[E0693]: incorrect `repr(align)` attribute format: `align` expect a literal integer as argument
--> $DIR/nonterminal-expansion.rs:5:16 --> $DIR/nonterminal-expansion.rs:17:19
| |
LL | #[repr(align($n))]
| ^^^^^^^^^
...
LL | pass_nonterminal!(n!()); LL | pass_nonterminal!(n!());
| ----------------------- in this macro invocation | ^
|
= note: this error originates in the macro `pass_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors error: aborting due to 2 previous errors