From 363addc79c7a2cc782c88646ac90ebcc5836fb92 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 22 Aug 2024 14:22:19 -0400 Subject: [PATCH] Gate repr(Rust) correctly on non-ADT items --- compiler/rustc_passes/src/check_attr.rs | 9 ++++ ...sue-43106-gating-of-builtin-attrs-error.rs | 24 +++++++++++ ...43106-gating-of-builtin-attrs-error.stderr | 41 ++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c93fb5c23b1..f12e33728d8 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1791,6 +1791,15 @@ fn check_repr( match hint.name_or_empty() { sym::Rust => { is_explicit_rust = true; + match target { + Target::Struct | Target::Union | Target::Enum => continue, + _ => { + self.dcx().emit_err(errors::AttrApplication::StructEnumUnion { + hint_span: hint.span(), + span, + }); + } + } } sym::C => { is_c = true; diff --git a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs index ca60bc62b51..afffb3b1443 100644 --- a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs +++ b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.rs @@ -164,4 +164,28 @@ mod inner { #![repr(C)] } //~| NOTE not a struct, enum, or union } + +#[repr(Rust)] +//~^ ERROR: attribute should be applied to a struct, enum, or union +mod repr_rust { +//~^ NOTE not a struct, enum, or union + mod inner { #![repr(Rust)] } + //~^ ERROR: attribute should be applied to a struct, enum, or union + //~| NOTE not a struct, enum, or union + + #[repr(Rust)] fn f() { } + //~^ ERROR: attribute should be applied to a struct, enum, or union + //~| NOTE not a struct, enum, or union + + struct S; + + #[repr(Rust)] type T = S; + //~^ ERROR: attribute should be applied to a struct, enum, or union + //~| NOTE not a struct, enum, or union + + #[repr(Rust)] impl S { } + //~^ ERROR: attribute should be applied to a struct, enum, or union + //~| NOTE not a struct, enum, or union +} + fn main() {} diff --git a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr index ac2bf78157d..fe764ff4925 100644 --- a/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr +++ b/tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr @@ -107,6 +107,21 @@ LL | | LL | | } | |_- not a struct, enum, or union +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:168:8 + | +LL | #[repr(Rust)] + | ^^^^ +LL | +LL | / mod repr_rust { +LL | | +LL | | mod inner { #![repr(Rust)] } +LL | | +... | +LL | | +LL | | } + | |_- not a struct, enum, or union + error: attribute should be applied to an `extern crate` item --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:26:1 | @@ -329,7 +344,31 @@ error[E0517]: attribute should be applied to a struct, enum, or union LL | #[repr(C)] impl S { } | ^ ---------- not a struct, enum, or union -error: aborting due to 39 previous errors +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:172:25 + | +LL | mod inner { #![repr(Rust)] } + | --------------------^^^^---- not a struct, enum, or union + +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:176:12 + | +LL | #[repr(Rust)] fn f() { } + | ^^^^ ---------- not a struct, enum, or union + +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:182:12 + | +LL | #[repr(Rust)] type T = S; + | ^^^^ ----------- not a struct, enum, or union + +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:186:12 + | +LL | #[repr(Rust)] impl S { } + | ^^^^ ---------- not a struct, enum, or union + +error: aborting due to 44 previous errors Some errors have detailed explanations: E0517, E0518, E0658. For more information about an error, try `rustc --explain E0517`.