Skip to content

Commit 653c45c

Browse files
committed
add transparent attribute for mod items
1 parent e22dab3 commit 653c45c

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
@@ -45,6 +45,7 @@ pub(crate) mod link_attrs;
4545
pub(crate) mod lint_helpers;
4646
pub(crate) mod loop_match;
4747
pub(crate) mod macro_attrs;
48+
pub(crate) mod modules;
4849
pub(crate) mod must_use;
4950
pub(crate) mod no_implicit_prelude;
5051
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
@@ -44,6 +44,7 @@ use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
4444
use crate::attributes::macro_attrs::{
4545
AllowInternalUnsafeParser, MacroEscapeParser, MacroExportParser, MacroUseParser,
4646
};
47+
use crate::attributes::modules::TransparentParser;
4748
use crate::attributes::must_use::MustUseParser;
4849
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
4950
use crate::attributes::non_exhaustive::NonExhaustiveParser;
@@ -247,6 +248,7 @@ attribute_parsers!(
247248
Single<WithoutArgs<SpecializationTraitParser>>,
248249
Single<WithoutArgs<StdInternalSymbolParser>>,
249250
Single<WithoutArgs<TrackCallerParser>>,
251+
Single<WithoutArgs<TransparentParser>>,
250252
Single<WithoutArgs<TypeConstParser>>,
251253
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,
252254
// 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
@@ -659,6 +659,8 @@ declare_features! (
659659
(unstable, trait_alias, "1.24.0", Some(41517)),
660660
/// Allows for transmuting between arrays with sizes that contain generic consts.
661661
(unstable, transmute_generic_consts, "1.70.0", Some(109929)),
662+
/// Allows #[transparent] on modules to inherit lexical scopes.
663+
(unstable, transparent_modules, "1.91.0", Some(79260)),
662664
/// Allows #[repr(transparent)] on unions (RFC 2645).
663665
(unstable, transparent_unions, "1.37.0", Some(60405)),
664666
/// 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
@@ -742,6 +742,9 @@ pub enum AttributeKind {
742742
/// Represents `#[track_caller]`
743743
TrackCaller(Span),
744744

745+
/// Represents `#[transparent]` mod attribute
746+
Transparent(Span),
747+
745748
/// Represents `#[type_const]`.
746749
TypeConst(Span),
747750

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl AttributeKind {
100100
StdInternalSymbol(..) => No,
101101
TargetFeature { .. } => No,
102102
TrackCaller(..) => Yes,
103+
Transparent(..) => No,
103104
TypeConst(..) => Yes,
104105
TypeLengthLimit { .. } => No,
105106
UnsafeSpecializationMarker(..) => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
222222
AttributeKind::BodyStability { .. }
223223
| AttributeKind::ConstStabilityIndirect
224224
| AttributeKind::MacroTransparency(_)
225+
| AttributeKind::Transparent(_)
225226
| AttributeKind::Pointee(..)
226227
| AttributeKind::Dummy
227228
| AttributeKind::RustcBuiltinMacro { .. }

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
152152
let expn_id = self.cstore().expn_that_defined_untracked(def_id, self.tcx.sess);
153153
return Some(self.new_extern_module(
154154
parent,
155-
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
155+
ModuleKind::Def(
156+
def_kind,
157+
def_id,
158+
Some((self.tcx.item_name(def_id), false)),
159+
),
156160
expn_id,
157161
self.def_span(def_id),
158162
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -630,14 +634,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
630634
// Disallow `use $crate;`
631635
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
632636
let crate_root = self.r.resolve_crate_root(source.ident);
633-
let crate_name = match crate_root.kind {
634-
ModuleKind::Def(.., name) => name,
637+
let crate_name_and_transparent = match crate_root.kind {
638+
ModuleKind::Def(.., name_and_transparent) => name_and_transparent,
635639
ModuleKind::Block => unreachable!(),
636640
};
637641
// HACK(eddyb) unclear how good this is, but keeping `$crate`
638642
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
639643
// while the current crate doesn't have a valid `crate_name`.
640-
if let Some(crate_name) = crate_name {
644+
if let Some((crate_name, _transparent)) = crate_name_and_transparent {
641645
// `crate_name` should not be interpreted as relative.
642646
module_path.push(Segment::from_ident_and_id(
643647
Ident::new(kw::PathRoot, source.ident.span),
@@ -819,9 +823,18 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
819823
{
820824
self.r.mods_with_parse_errors.insert(def_id);
821825
}
826+
let transparent = AttributeParser::parse_limited(
827+
self.r.tcx.sess,
828+
&item.attrs,
829+
sym::transparent,
830+
item.span,
831+
item.id,
832+
None,
833+
)
834+
.is_some();
822835
self.parent_scope.module = self.r.new_local_module(
823836
Some(parent),
824-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
837+
ModuleKind::Def(def_kind, def_id, Some((ident.name, transparent))),
825838
expansion.to_expn_id(),
826839
item.span,
827840
parent.no_implicit_prelude
@@ -854,7 +867,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
854867

855868
self.parent_scope.module = self.r.new_local_module(
856869
Some(parent),
857-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
870+
ModuleKind::Def(def_kind, def_id, Some((ident.name, false))),
858871
expansion.to_expn_id(),
859872
item.span,
860873
parent.no_implicit_prelude,

compiler/rustc_resolve/src/ident.rs

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

242+
if module.is_transparent() {
243+
return Some((module.parent.unwrap().nearest_item_scope(), None));
244+
}
245+
242246
// We need to support the next case under a deprecation warning
243247
// ```
244248
// struct MyStruct;
@@ -349,7 +353,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
349353
// Encountered a module item, abandon ribs and look into that module and preludes.
350354
let parent_scope = &ParentScope { module, ..*parent_scope };
351355
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
352-
return self
356+
let binding = self
353357
.cm()
354358
.resolve_ident_in_scope_set(
355359
orig_ident,
@@ -360,8 +364,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
360364
ignore_binding,
361365
None,
362366
)
363-
.ok()
364-
.map(LexicalScopeBinding::Item);
367+
.ok();
368+
match binding {
369+
Some(binding) => {
370+
return Some(LexicalScopeBinding::Item(binding));
371+
}
372+
None => {
373+
if !module.is_transparent() {
374+
return None;
375+
}
376+
}
377+
}
365378
}
366379

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

0 commit comments

Comments
 (0)