Skip to content

Commit 71f756d

Browse files
committed
add transparent attribute for mod items
1 parent 3391c01 commit 71f756d

File tree

18 files changed

+306
-15
lines changed

18 files changed

+306
-15
lines changed

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub(crate) mod link_attrs;
4646
pub(crate) mod lint_helpers;
4747
pub(crate) mod loop_match;
4848
pub(crate) mod macro_attrs;
49+
pub(crate) mod modules;
4950
pub(crate) mod must_use;
5051
pub(crate) mod no_implicit_prelude;
5152
pub(crate) mod non_exhaustive;
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
@@ -48,6 +48,7 @@ use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
4848
use crate::attributes::macro_attrs::{
4949
AllowInternalUnsafeParser, MacroEscapeParser, MacroExportParser, MacroUseParser,
5050
};
51+
use crate::attributes::modules::TransparentParser;
5152
use crate::attributes::must_use::MustUseParser;
5253
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
5354
use crate::attributes::non_exhaustive::NonExhaustiveParser;
@@ -254,6 +255,7 @@ attribute_parsers!(
254255
Single<WithoutArgs<SpecializationTraitParser>>,
255256
Single<WithoutArgs<StdInternalSymbolParser>>,
256257
Single<WithoutArgs<TrackCallerParser>>,
258+
Single<WithoutArgs<TransparentParser>>,
257259
Single<WithoutArgs<TypeConstParser>>,
258260
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,
259261
// 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
@@ -657,6 +657,8 @@ declare_features! (
657657
(unstable, trait_alias, "1.24.0", Some(41517)),
658658
/// Allows for transmuting between arrays with sizes that contain generic consts.
659659
(unstable, transmute_generic_consts, "1.70.0", Some(109929)),
660+
/// Allows #[transparent] on modules to inherit lexical scopes.
661+
(unstable, transparent_modules, "1.91.0", Some(79260)),
660662
/// Allows #[repr(transparent)] on unions (RFC 2645).
661663
(unstable, transparent_unions, "1.37.0", Some(60405)),
662664
/// 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
@@ -896,6 +896,9 @@ pub enum AttributeKind {
896896
/// Represents `#[track_caller]`
897897
TrackCaller(Span),
898898

899+
/// Represents `#[transparent]` mod attribute
900+
Transparent(Span),
901+
899902
/// Represents `#[type_const]`.
900903
TypeConst(Span),
901904

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl AttributeKind {
102102
StdInternalSymbol(..) => No,
103103
TargetFeature { .. } => No,
104104
TrackCaller(..) => Yes,
105+
Transparent(..) => No,
105106
TypeConst(..) => Yes,
106107
TypeLengthLimit { .. } => No,
107108
UnsafeSpecializationMarker(..) => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
216216
AttributeKind::BodyStability { .. }
217217
| AttributeKind::ConstStabilityIndirect
218218
| AttributeKind::MacroTransparency(_)
219+
| AttributeKind::Transparent(_)
219220
| AttributeKind::Pointee(..)
220221
| AttributeKind::Dummy
221222
| AttributeKind::RustcBuiltinMacro { .. }

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
161161
let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id);
162162
return Some(self.new_extern_module(
163163
parent,
164-
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
164+
ModuleKind::Def(
165+
def_kind,
166+
def_id,
167+
Some((self.tcx.item_name(def_id), false)),
168+
),
165169
expn_id,
166170
self.def_span(def_id),
167171
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -665,14 +669,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
665669
// Disallow `use $crate;`
666670
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
667671
let crate_root = self.r.resolve_crate_root(source.ident);
668-
let crate_name = match crate_root.kind {
669-
ModuleKind::Def(.., name) => name,
672+
let crate_name_and_transparent = match crate_root.kind {
673+
ModuleKind::Def(.., name_and_transparent) => name_and_transparent,
670674
ModuleKind::Block => unreachable!(),
671675
};
672676
// HACK(eddyb) unclear how good this is, but keeping `$crate`
673677
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
674678
// while the current crate doesn't have a valid `crate_name`.
675-
if let Some(crate_name) = crate_name {
679+
if let Some((crate_name, _transparent)) = crate_name_and_transparent {
676680
// `crate_name` should not be interpreted as relative.
677681
module_path.push(Segment::from_ident_and_id(
678682
Ident::new(kw::PathRoot, source.ident.span),
@@ -854,9 +858,18 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
854858
{
855859
self.r.mods_with_parse_errors.insert(def_id);
856860
}
861+
let transparent = AttributeParser::parse_limited(
862+
self.r.tcx.sess,
863+
&item.attrs,
864+
sym::transparent,
865+
item.span,
866+
item.id,
867+
None,
868+
)
869+
.is_some();
857870
self.parent_scope.module = self.r.new_local_module(
858871
Some(parent),
859-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
872+
ModuleKind::Def(def_kind, def_id, Some((ident.name, transparent))),
860873
expansion.to_expn_id(),
861874
item.span,
862875
parent.no_implicit_prelude
@@ -889,7 +902,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
889902

890903
self.parent_scope.module = self.r.new_local_module(
891904
Some(parent),
892-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
905+
ModuleKind::Def(def_kind, def_id, Some((ident.name, false))),
893906
expansion.to_expn_id(),
894907
item.span,
895908
parent.no_implicit_prelude,

compiler/rustc_resolve/src/ident.rs

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

257+
if module.is_transparent() {
258+
return Some((module.parent.unwrap().nearest_item_scope(), None));
259+
}
260+
257261
// We need to support the next case under a deprecation warning
258262
// ```
259263
// struct MyStruct;
@@ -364,7 +368,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
364368
// Encountered a module item, abandon ribs and look into that module and preludes.
365369
let parent_scope = &ParentScope { module, ..*parent_scope };
366370
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
367-
return self
371+
let binding = self
368372
.cm()
369373
.resolve_ident_in_scope_set(
370374
orig_ident,
@@ -375,8 +379,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
375379
ignore_binding,
376380
None,
377381
)
378-
.ok()
379-
.map(LexicalScopeBinding::Item);
382+
.ok();
383+
match binding {
384+
Some(binding) => {
385+
return Some(LexicalScopeBinding::Item(binding));
386+
}
387+
None => {
388+
if !module.is_transparent() {
389+
return None;
390+
}
391+
}
392+
}
380393
}
381394

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

0 commit comments

Comments
 (0)