diff options
| author | yukang <moorekang@gmail.com> | 2024-03-02 22:42:13 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2024-03-02 23:15:39 +0800 |
| commit | 5a5c6dfb33ec9f17416ac96ca66554a033ecd387 (patch) | |
| tree | 78cef499e2ae9fff0c4921b7fb137142a2c6b7e8 /compiler/rustc_attr/src | |
| parent | e612d079a1102803fd2cae5dcd7f7f277e493b8e (diff) | |
| download | rust-5a5c6dfb33ec9f17416ac96ca66554a033ecd387.tar.gz rust-5a5c6dfb33ec9f17416ac96ca66554a033ecd387.zip | |
Fix misleading message when using a named constant as a struct alignment/pack
Diffstat (limited to 'compiler/rustc_attr/src')
| -rw-r--r-- | compiler/rustc_attr/src/builtin.rs | 38 | ||||
| -rw-r--r-- | compiler/rustc_attr/src/session_diagnostics.rs | 13 |
2 files changed, 40 insertions, 11 deletions
diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index f414ff746bb..6b6eefc64f8 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -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) { recognised = true; - sess.dcx().emit_err( - session_diagnostics::IncorrectReprFormatAlignOneArg { - span: meta_item.span, - }, - ); + if nested_items.len() == 1 { + sess.dcx().emit_err( + session_diagnostics::IncorrectReprFormatExpectInteger { + span: nested_items[0].span(), + }, + ); + } else { + sess.dcx().emit_err( + session_diagnostics::IncorrectReprFormatAlignOneArg { + span: meta_item.span, + }, + ); + } } else if meta_item.has_name(sym::packed) { recognised = true; - sess.dcx().emit_err( - session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg { - span: meta_item.span, - }, - ); + if nested_items.len() == 1 { + sess.dcx().emit_err( + session_diagnostics::IncorrectReprFormatPackedExpectInteger { + span: nested_items[0].span(), + }, + ); + } else { + sess.dcx().emit_err( + session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg { + span: meta_item.span, + }, + ); + } } else if matches!( meta_item.name_or_empty(), sym::Rust | sym::C | sym::simd | sym::transparent diff --git a/compiler/rustc_attr/src/session_diagnostics.rs b/compiler/rustc_attr/src/session_diagnostics.rs index 8cbd401d300..f489cc87bc7 100644 --- a/compiler/rustc_attr/src/session_diagnostics.rs +++ b/compiler/rustc_attr/src/session_diagnostics.rs @@ -170,6 +170,12 @@ pub(crate) struct IncorrectReprFormatPackedOneOrZeroArg { #[primary_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)] #[diag(attr_invalid_repr_hint_no_paren, code = E0552)] @@ -253,6 +259,13 @@ pub(crate) struct IncorrectReprFormatAlignOneArg { } #[derive(Diagnostic)] +#[diag(attr_incorrect_repr_format_expect_literal_integer, code = E0693)] +pub(crate) struct IncorrectReprFormatExpectInteger { + #[primary_span] + pub span: Span, +} + +#[derive(Diagnostic)] #[diag(attr_incorrect_repr_format_generic, code = E0693)] pub(crate) struct IncorrectReprFormatGeneric<'a> { #[primary_span] |
