Skip to content

Commit

Permalink
rustc_pub_transparent: validation
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigorenkoPV committed Aug 24, 2024
1 parent 814185a commit fc022ad
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ passes_rustc_lint_opt_ty =
`#[rustc_lint_opt_ty]` should be applied to a struct
.label = not a struct
passes_rustc_pub_transparent =
attribute should be applied to `#[repr(transparent)]` types
.label = not a `#[repr(transparent)]` type
passes_rustc_safe_intrinsic =
attribute should be applied to intrinsic functions
.label = not an intrinsic function
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_coroutine(attr, target);
}
[sym::linkage, ..] => self.check_linkage(attr, span, target),
[sym::rustc_pub_transparent, ..] => self.check_rustc_pub_transparent( attr.span, span, attrs),
[
// ok
sym::allow
Expand Down Expand Up @@ -2381,6 +2382,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
}

fn check_rustc_pub_transparent(&self, attr_span: Span, span: Span, attrs: &[Attribute]) {
if !attrs
.iter()
.filter(|attr| attr.has_name(sym::repr))
.filter_map(|attr| attr.meta_item_list())
.flatten()
.any(|nmi| nmi.has_name(sym::transparent))
{
self.dcx().emit_err(errors::RustcPubTransparent { span, attr_span });
}
}
}

impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,15 @@ pub struct RustcStdInternalSymbol {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_rustc_pub_transparent)]
pub struct RustcPubTransparent {
#[primary_span]
pub attr_span: Span,
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_link_ordinal)]
pub struct LinkOrdinal {
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/attributes/rustc_pub_transparent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![feature(rustc_attrs, transparent_unions)]

#[rustc_pub_transparent]
#[repr(transparent)]
union E<T: Copy> {
value: T,
uninit: (),
}

#[repr(transparent)]
#[rustc_pub_transparent]
struct S<T>(T);

#[rustc_pub_transparent] //~ ERROR attribute should be applied to `#[repr(transparent)]` types
#[repr(C)]
struct S1 {
A: u8,
}

#[rustc_pub_transparent] //~ ERROR attribute should be applied to `#[repr(transparent)]` types
struct S2<T> {
value: T,
}

fn main() {}
23 changes: 23 additions & 0 deletions tests/ui/attributes/rustc_pub_transparent.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: attribute should be applied to `#[repr(transparent)]` types
--> $DIR/rustc_pub_transparent.rs:14:1
|
LL | #[rustc_pub_transparent]
| ^^^^^^^^^^^^^^^^^^^^^^^^
LL | #[repr(C)]
LL | / struct S1 {
LL | | A: u8,
LL | | }
| |_- not a `#[repr(transparent)]` type

error: attribute should be applied to `#[repr(transparent)]` types
--> $DIR/rustc_pub_transparent.rs:20:1
|
LL | #[rustc_pub_transparent]
| ^^^^^^^^^^^^^^^^^^^^^^^^
LL | / struct S2<T> {
LL | | value: T,
LL | | }
| |_- not a `#[repr(transparent)]` type

error: aborting due to 2 previous errors

0 comments on commit fc022ad

Please sign in to comment.