Skip to content

Commit b3432e2

Browse files
committed
rustc_pub_transparent: validation
1 parent 814185a commit b3432e2

File tree

6 files changed

+1197
-0
lines changed

6 files changed

+1197
-0
lines changed

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,10 @@ passes_rustc_lint_opt_ty =
657657
`#[rustc_lint_opt_ty]` should be applied to a struct
658658
.label = not a struct
659659
660+
passes_rustc_pub_transparent =
661+
attribute should be applied to `#[repr(transparent)]` types
662+
.label = not a `#[repr(transparent)]` type
663+
660664
passes_rustc_safe_intrinsic =
661665
attribute should be applied to intrinsic functions
662666
.label = not an intrinsic function

compiler/rustc_passes/src/check_attr.rs

+13
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
245245
self.check_coroutine(attr, target);
246246
}
247247
[sym::linkage, ..] => self.check_linkage(attr, span, target),
248+
[sym::rustc_pub_transparent, ..] => self.check_rustc_pub_transparent( attr.span, span, attrs),
248249
[
249250
// ok
250251
sym::allow
@@ -2381,6 +2382,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23812382
}
23822383
}
23832384
}
2385+
2386+
fn check_rustc_pub_transparent(&self, attr_span: Span, span: Span, attrs: &[Attribute]) {
2387+
if !attrs
2388+
.iter()
2389+
.filter(|attr| attr.has_name(sym::repr))
2390+
.filter_map(|attr| attr.meta_item_list())
2391+
.flatten()
2392+
.any(|nmi| nmi.has_name(sym::transparent))
2393+
{
2394+
self.dcx().emit_err(errors::RustcPubTransparent { span, attr_span });
2395+
}
2396+
}
23842397
}
23852398

23862399
impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {

compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,15 @@ pub struct RustcStdInternalSymbol {
622622
pub span: Span,
623623
}
624624

625+
#[derive(Diagnostic)]
626+
#[diag(passes_rustc_pub_transparent)]
627+
pub struct RustcPubTransparent {
628+
#[primary_span]
629+
pub attr_span: Span,
630+
#[label]
631+
pub span: Span,
632+
}
633+
625634
#[derive(Diagnostic)]
626635
#[diag(passes_link_ordinal)]
627636
pub struct LinkOrdinal {

0 commit comments

Comments
 (0)