Skip to content

Commit aba522c

Browse files
committed
add transparent attribute for mod items
1 parent 2850ca8 commit aba522c

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
@@ -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_not_suspend;
5152
pub(crate) mod must_use;
5253
pub(crate) mod no_implicit_prelude;
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
@@ -51,6 +51,7 @@ use crate::attributes::macro_attrs::{
5151
AllowInternalUnsafeParser, CollapseDebugInfoParser, MacroEscapeParser, MacroExportParser,
5252
MacroUseParser,
5353
};
54+
use crate::attributes::modules::TransparentParser;
5455
use crate::attributes::must_not_suspend::MustNotSuspendParser;
5556
use crate::attributes::must_use::MustUseParser;
5657
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
@@ -281,6 +282,7 @@ attribute_parsers!(
281282
Single<WithoutArgs<StdInternalSymbolParser>>,
282283
Single<WithoutArgs<ThreadLocalParser>>,
283284
Single<WithoutArgs<TrackCallerParser>>,
285+
Single<WithoutArgs<TransparentParser>>,
284286
Single<WithoutArgs<TypeConstParser>>,
285287
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,
286288
// 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
@@ -1006,6 +1006,9 @@ pub enum AttributeKind {
10061006
/// Represents `#[track_caller]`
10071007
TrackCaller(Span),
10081008

1009+
/// Represents `#[transparent]` mod attribute
1010+
Transparent(Span),
1011+
10091012
/// Represents `#[type_const]`.
10101013
TypeConst(Span),
10111014

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl AttributeKind {
124124
TargetFeature { .. } => No,
125125
ThreadLocal => No,
126126
TrackCaller(..) => Yes,
127+
Transparent(..) => No,
127128
TypeConst(..) => Yes,
128129
TypeLengthLimit { .. } => No,
129130
UnsafeSpecializationMarker(..) => No,

compiler/rustc_passes/src/check_attr.rs

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

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
165165
let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id);
166166
return Some(self.new_extern_module(
167167
parent,
168-
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
168+
ModuleKind::Def(
169+
def_kind,
170+
def_id,
171+
Some((self.tcx.item_name(def_id), false)),
172+
),
169173
expn_id,
170174
self.def_span(def_id),
171175
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -666,14 +670,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
666670
// Disallow `use $crate;`
667671
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
668672
let crate_root = self.r.resolve_crate_root(source.ident);
669-
let crate_name = match crate_root.kind {
670-
ModuleKind::Def(.., name) => name,
673+
let crate_name_and_transparent = match crate_root.kind {
674+
ModuleKind::Def(.., name_and_transparent) => name_and_transparent,
671675
ModuleKind::Block => unreachable!(),
672676
};
673677
// HACK(eddyb) unclear how good this is, but keeping `$crate`
674678
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
675679
// while the current crate doesn't have a valid `crate_name`.
676-
if let Some(crate_name) = crate_name {
680+
if let Some((crate_name, _transparent)) = crate_name_and_transparent {
677681
// `crate_name` should not be interpreted as relative.
678682
module_path.push(Segment::from_ident_and_id(
679683
Ident::new(kw::PathRoot, source.ident.span),
@@ -855,9 +859,18 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
855859
{
856860
self.r.mods_with_parse_errors.insert(def_id);
857861
}
862+
let transparent = AttributeParser::parse_limited(
863+
self.r.tcx.sess,
864+
&item.attrs,
865+
sym::transparent,
866+
item.span,
867+
item.id,
868+
None,
869+
)
870+
.is_some();
858871
self.parent_scope.module = self.r.new_local_module(
859872
Some(parent),
860-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
873+
ModuleKind::Def(def_kind, def_id, Some((ident.name, transparent))),
861874
expansion.to_expn_id(),
862875
item.span,
863876
parent.no_implicit_prelude
@@ -890,7 +903,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
890903

891904
self.parent_scope.module = self.r.new_local_module(
892905
Some(parent),
893-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
906+
ModuleKind::Def(def_kind, def_id, Some((ident.name, false))),
894907
expansion.to_expn_id(),
895908
item.span,
896909
parent.no_implicit_prelude,

compiler/rustc_resolve/src/ident.rs

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

252+
if module.is_transparent() {
253+
return Some((module.parent.unwrap().nearest_item_scope(), None));
254+
}
255+
252256
// We need to support the next case under a deprecation warning
253257
// ```
254258
// struct MyStruct;
@@ -357,7 +361,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
357361
// Encountered a module item, abandon ribs and look into that module and preludes.
358362
let parent_scope = &ParentScope { module, ..*parent_scope };
359363
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
360-
return self
364+
let binding = self
361365
.cm()
362366
.resolve_ident_in_scope_set(
363367
orig_ident,
@@ -368,8 +372,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
368372
ignore_decl,
369373
None,
370374
)
371-
.ok()
372-
.map(LateDecl::Decl);
375+
.ok();
376+
match binding {
377+
Some(binding) => {
378+
return Some(LateDecl::Decl(binding));
379+
}
380+
None => {
381+
if !module.is_transparent() {
382+
return None;
383+
}
384+
}
385+
}
373386
}
374387

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

0 commit comments

Comments
 (0)