Skip to content

Commit 3a22b8c

Browse files
committed
Port #[rustc_pub_transparent] to the new attribute system
1 parent 45acf54 commit 3a22b8c

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ pub enum AttributeKind {
224224
/// Represents `#[rustc_macro_transparency]`.
225225
MacroTransparency(Transparency),
226226

227+
/// Represents `#[rustc_pub_transparent]` (used by the `repr_transparent_external_private_fields` lint).
228+
PubTransparent(Span),
229+
227230
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
228231
Repr(ThinVec<(ReprAttr, Span)>),
229232

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ impl<S: Stage> SingleAttributeParser<S> for AsPtrParser {
1919
Some(AttributeKind::AsPtr(cx.attr_span))
2020
}
2121
}
22+
23+
pub(crate) struct PubTransparentParser;
24+
impl<S: Stage> SingleAttributeParser<S> for PubTransparentParser {
25+
const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
26+
27+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
28+
29+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
30+
31+
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
32+
// FIXME: check that there's no args (this is currently checked elsewhere)
33+
Some(AttributeKind::PubTransparent(cx.attr_span))
34+
}
35+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1818
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
1919
use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
21-
use crate::attributes::lint_helpers::AsPtrParser;
21+
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2222
use crate::attributes::repr::ReprParser;
2323
use crate::attributes::stability::{
2424
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -106,6 +106,7 @@ attribute_parsers!(
106106
Single<AsPtrParser>,
107107
Single<ConstStabilityIndirectParser>,
108108
Single<DeprecationParser>,
109+
Single<PubTransparentParser>,
109110
Single<TransparencyParser>,
110111
// tidy-alphabetical-end
111112
];

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
687687
),
688688
rustc_attr!(
689689
rustc_pub_transparent, Normal, template!(Word),
690-
WarnFollowing, EncodeCrossCrate::Yes,
690+
ErrorFollowing, EncodeCrossCrate::Yes,
691691
"used internally to mark types with a `transparent` representation when it is guaranteed by the documentation",
692692
),
693693

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cell::LazyCell;
22
use std::ops::ControlFlow;
33

44
use rustc_abi::FieldIdx;
5+
use rustc_attr_data_structures::AttributeKind;
56
use rustc_attr_data_structures::ReprAttr::ReprPacked;
67
use rustc_data_structures::unord::{UnordMap, UnordSet};
78
use rustc_errors::codes::*;
@@ -1384,7 +1385,11 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
13841385
ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)),
13851386
ty::Array(ty, _) => check_non_exhaustive(tcx, *ty),
13861387
ty::Adt(def, args) => {
1387-
if !def.did().is_local() && !tcx.has_attr(def.did(), sym::rustc_pub_transparent)
1388+
if !def.did().is_local()
1389+
&& !attrs::find_attr!(
1390+
tcx.get_all_attrs(def.did()),
1391+
AttributeKind::PubTransparent(_)
1392+
)
13881393
{
13891394
let non_exhaustive = def.is_variant_list_non_exhaustive()
13901395
|| def

compiler/rustc_passes/src/check_attr.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
150150
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => {
151151
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
152152
}
153+
&Attribute::Parsed(AttributeKind::PubTransparent(attr_span)) => {
154+
if !find_attr!(
155+
attrs,
156+
AttributeKind::Repr(r) => r.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent)
157+
).unwrap_or(false) {
158+
self.dcx().emit_err(errors::RustcPubTransparent { span, attr_span });
159+
}
160+
}
153161
Attribute::Unparsed(_) => {
154162
match attr.path().as_slice() {
155163
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -276,7 +284,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
276284
self.check_type_const(hir_id,attr, target);
277285
}
278286
[sym::linkage, ..] => self.check_linkage(attr, span, target),
279-
[sym::rustc_pub_transparent, ..] => self.check_rustc_pub_transparent(attr.span(), span, attrs),
280287
[
281288
// ok
282289
sym::allow
@@ -2623,14 +2630,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
26232630
}
26242631
}
26252632

2626-
fn check_rustc_pub_transparent(&self, attr_span: Span, span: Span, attrs: &[Attribute]) {
2627-
if !find_attr!(attrs, AttributeKind::Repr(r) => r.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent))
2628-
.unwrap_or(false)
2629-
{
2630-
self.dcx().emit_err(errors::RustcPubTransparent { span, attr_span });
2631-
}
2632-
}
2633-
26342633
fn check_rustc_force_inline(
26352634
&self,
26362635
hir_id: HirId,

0 commit comments

Comments
 (0)