Skip to content

Commit 5559732

Browse files
authored
Rollup merge of rust-lang#142600 - GrigorenkoPV:attributes/rustc_pub_transparent, r=jdonszelmann
Port `#[rustc_pub_transparent]` to the new attribute system Very similar to rust-lang#142498. This is a part of rust-lang#131229, so r? `@jdonszelmann` --- For reference, the `#[rustc_pub_transparent]` attribute was created by me back in rust-lang#129487. As mentioned back in rust-lang#129487 (comment), this attribute does not check that it is applied to an ADT, because it checks that `#[repr(transparent)]` is also applied to the same item, which, in turn, should check for ADT.
2 parents 73996f3 + d86d3f3 commit 5559732

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ pub enum AttributeKind {
240240
/// Represents `#[optimize(size|speed)]`
241241
Optimize(OptimizeAttr, Span),
242242

243+
/// Represents `#[rustc_pub_transparent]` (used by the `repr_transparent_external_private_fields` lint).
244+
PubTransparent(Span),
245+
243246
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
244247
Repr(ThinVec<(ReprAttr, Span)>),
245248

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ 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+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
27+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
28+
const TEMPLATE: AttributeTemplate = template!(Word);
29+
30+
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
31+
// FIXME: check that there's no args (this is currently checked elsewhere)
32+
Some(AttributeKind::PubTransparent(cx.attr_span))
33+
}
34+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::attributes::codegen_attrs::{ColdParser, OptimizeParser};
1919
use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
2121
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
22-
use crate::attributes::lint_helpers::AsPtrParser;
22+
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2323
use crate::attributes::repr::{AlignParser, ReprParser};
2424
use crate::attributes::semantics::MayDangleParser;
2525
use crate::attributes::stability::{
@@ -113,6 +113,7 @@ attribute_parsers!(
113113
Single<InlineParser>,
114114
Single<MayDangleParser>,
115115
Single<OptimizeParser>,
116+
Single<PubTransparentParser>,
116117
Single<RustcForceInlineParser>,
117118
Single<TransparencyParser>,
118119
// tidy-alphabetical-end

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
710710
),
711711
rustc_attr!(
712712
rustc_pub_transparent, Normal, template!(Word),
713-
WarnFollowing, EncodeCrossCrate::Yes,
713+
ErrorFollowing, EncodeCrossCrate::Yes,
714714
"used internally to mark types with a `transparent` representation when it is guaranteed by the documentation",
715715
),
716716

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
149149
}
150150
Attribute::Parsed(AttributeKind::Repr(_)) => { /* handled below this loop and elsewhere */
151151
}
152+
153+
&Attribute::Parsed(AttributeKind::PubTransparent(attr_span)) => {
154+
self.check_rustc_pub_transparent(attr_span, span, attrs)
155+
}
152156
Attribute::Parsed(AttributeKind::Cold(attr_span)) => {
153157
self.check_cold(hir_id, *attr_span, span, target)
154158
}
@@ -288,7 +292,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
288292
self.check_type_const(hir_id,attr, target);
289293
}
290294
[sym::linkage, ..] => self.check_linkage(attr, span, target),
291-
[sym::rustc_pub_transparent, ..] => self.check_rustc_pub_transparent(attr.span(), span, attrs),
292295
[
293296
// ok
294297
sym::allow

0 commit comments

Comments
 (0)