Skip to content

Commit 27ba290

Browse files
committed
add transparent attribute for mod items
1 parent fc546b9 commit 27ba290

18 files changed

Lines changed: 306 additions & 15 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub(crate) mod link_attrs;
4747
pub(crate) mod lint_helpers;
4848
pub(crate) mod loop_match;
4949
pub(crate) mod macro_attrs;
50+
pub(crate) mod modules;
5051
pub(crate) mod must_use;
5152
pub(crate) mod no_implicit_prelude;
5253
pub(crate) mod no_link;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use super::prelude::*;
2+
3+
pub(crate) struct TransparentParser;
4+
impl<S: Stage> NoArgsAttributeParser<S> for TransparentParser {
5+
const PATH: &[Symbol] = &[sym::transparent];
6+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
7+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Mod)]);
8+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Transparent;
9+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
5050
use crate::attributes::macro_attrs::{
5151
AllowInternalUnsafeParser, MacroEscapeParser, MacroExportParser, MacroUseParser,
5252
};
53+
use crate::attributes::modules::TransparentParser;
5354
use crate::attributes::must_use::MustUseParser;
5455
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
5556
use crate::attributes::no_link::NoLinkParser;
@@ -277,6 +278,7 @@ attribute_parsers!(
277278
Single<WithoutArgs<StdInternalSymbolParser>>,
278279
Single<WithoutArgs<ThreadLocalParser>>,
279280
Single<WithoutArgs<TrackCallerParser>>,
281+
Single<WithoutArgs<TransparentParser>>,
280282
Single<WithoutArgs<TypeConstParser>>,
281283
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,
282284
// tidy-alphabetical-end

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,12 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
917917
EncodeCrossCrate::Yes, pin_ergonomics, experimental!(pin_v2),
918918
),
919919

920+
gated!(
921+
transparent, Normal,
922+
template!(Word, "https://github.com/rust-lang/rust/issues/79260#issuecomment-731773625"),
923+
ErrorFollowing, EncodeCrossCrate::No, transparent_modules, experimental!(transparent)
924+
),
925+
920926
// ==========================================================================
921927
// Internal attributes: Stability, deprecation, and unsafe:
922928
// ==========================================================================

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ declare_features! (
660660
(unstable, trait_alias, "1.24.0", Some(41517)),
661661
/// Allows for transmuting between arrays with sizes that contain generic consts.
662662
(unstable, transmute_generic_consts, "1.70.0", Some(109929)),
663+
/// Allows #[transparent] on modules to inherit lexical scopes.
664+
(unstable, transparent_modules, "1.91.0", Some(79260)),
663665
/// Allows #[repr(transparent)] on unions (RFC 2645).
664666
(unstable, transparent_unions, "1.37.0", Some(60405)),
665667
/// Allows inconsistent bounds in where clauses.

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,9 @@ pub enum AttributeKind {
964964
/// Represents `#[track_caller]`
965965
TrackCaller(Span),
966966

967+
/// Represents `#[transparent]` mod attribute
968+
Transparent(Span),
969+
967970
/// Represents `#[type_const]`.
968971
TypeConst(Span),
969972

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl AttributeKind {
121121
TargetFeature { .. } => No,
122122
ThreadLocal => No,
123123
TrackCaller(..) => Yes,
124+
Transparent(..) => No,
124125
TypeConst(..) => Yes,
125126
TypeLengthLimit { .. } => No,
126127
UnsafeSpecializationMarker(..) => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
230230
| AttributeKind::ConstStabilityIndirect
231231
| AttributeKind::MacroTransparency(_)
232232
| AttributeKind::CfgTrace(..)
233+
| AttributeKind::Transparent(_)
233234
| AttributeKind::Pointee(..)
234235
| AttributeKind::Dummy
235236
| AttributeKind::RustcBuiltinMacro { .. }

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
164164
let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id);
165165
return Some(self.new_extern_module(
166166
parent,
167-
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
167+
ModuleKind::Def(
168+
def_kind,
169+
def_id,
170+
Some((self.tcx.item_name(def_id), false)),
171+
),
168172
expn_id,
169173
self.def_span(def_id),
170174
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -664,14 +668,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
664668
// Disallow `use $crate;`
665669
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
666670
let crate_root = self.r.resolve_crate_root(source.ident);
667-
let crate_name = match crate_root.kind {
668-
ModuleKind::Def(.., name) => name,
671+
let crate_name_and_transparent = match crate_root.kind {
672+
ModuleKind::Def(.., name_and_transparent) => name_and_transparent,
669673
ModuleKind::Block => unreachable!(),
670674
};
671675
// HACK(eddyb) unclear how good this is, but keeping `$crate`
672676
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
673677
// while the current crate doesn't have a valid `crate_name`.
674-
if let Some(crate_name) = crate_name {
678+
if let Some((crate_name, _transparent)) = crate_name_and_transparent {
675679
// `crate_name` should not be interpreted as relative.
676680
module_path.push(Segment::from_ident_and_id(
677681
Ident::new(kw::PathRoot, source.ident.span),
@@ -853,9 +857,18 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
853857
{
854858
self.r.mods_with_parse_errors.insert(def_id);
855859
}
860+
let transparent = AttributeParser::parse_limited(
861+
self.r.tcx.sess,
862+
&item.attrs,
863+
sym::transparent,
864+
item.span,
865+
item.id,
866+
None,
867+
)
868+
.is_some();
856869
self.parent_scope.module = self.r.new_local_module(
857870
Some(parent),
858-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
871+
ModuleKind::Def(def_kind, def_id, Some((ident.name, transparent))),
859872
expansion.to_expn_id(),
860873
item.span,
861874
parent.no_implicit_prelude
@@ -888,7 +901,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
888901

889902
self.parent_scope.module = self.r.new_local_module(
890903
Some(parent),
891-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
904+
ModuleKind::Def(def_kind, def_id, Some((ident.name, false))),
892905
expansion.to_expn_id(),
893906
item.span,
894907
parent.no_implicit_prelude,

compiler/rustc_resolve/src/ident.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
247247
return Some((module.parent.unwrap().nearest_item_scope(), None));
248248
}
249249

250+
if module.is_transparent() {
251+
return Some((module.parent.unwrap().nearest_item_scope(), None));
252+
}
253+
250254
// We need to support the next case under a deprecation warning
251255
// ```
252256
// struct MyStruct;
@@ -355,7 +359,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
355359
// Encountered a module item, abandon ribs and look into that module and preludes.
356360
let parent_scope = &ParentScope { module, ..*parent_scope };
357361
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
358-
return self
362+
let binding = self
359363
.cm()
360364
.resolve_ident_in_scope_set(
361365
orig_ident,
@@ -366,8 +370,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
366370
ignore_decl,
367371
None,
368372
)
369-
.ok()
370-
.map(LateDecl::Decl);
373+
.ok();
374+
match binding {
375+
Some(binding) => {
376+
return Some(LateDecl::Decl(binding));
377+
}
378+
None => {
379+
if !module.is_transparent() {
380+
return None;
381+
}
382+
}
383+
}
371384
}
372385

373386
if let RibKind::MacroDefinition(def) = rib.kind

0 commit comments

Comments
 (0)