Skip to content

Commit 74e1b33

Browse files
committed
add transparent attribute for mod items
1 parent e100792 commit 74e1b33

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
@@ -44,6 +44,7 @@ pub(crate) mod link_attrs;
4444
pub(crate) mod lint_helpers;
4545
pub(crate) mod loop_match;
4646
pub(crate) mod macro_attrs;
47+
pub(crate) mod modules;
4748
pub(crate) mod must_use;
4849
pub(crate) mod no_implicit_prelude;
4950
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
@@ -43,6 +43,7 @@ use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
4343
use crate::attributes::macro_attrs::{
4444
AllowInternalUnsafeParser, MacroEscapeParser, MacroExportParser, MacroUseParser,
4545
};
46+
use crate::attributes::modules::TransparentParser;
4647
use crate::attributes::must_use::MustUseParser;
4748
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
4849
use crate::attributes::non_exhaustive::NonExhaustiveParser;
@@ -241,6 +242,7 @@ attribute_parsers!(
241242
Single<WithoutArgs<SpecializationTraitParser>>,
242243
Single<WithoutArgs<StdInternalSymbolParser>>,
243244
Single<WithoutArgs<TrackCallerParser>>,
245+
Single<WithoutArgs<TransparentParser>>,
244246
Single<WithoutArgs<TypeConstParser>>,
245247
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,
246248
// tidy-alphabetical-end

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,12 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
888888
EncodeCrossCrate::No, loop_match, experimental!(loop_match)
889889
),
890890

891+
gated!(
892+
transparent, Normal,
893+
template!(Word, "https://github.com/rust-lang/rust/issues/79260#issuecomment-731773625"),
894+
ErrorFollowing, EncodeCrossCrate::No, transparent_modules, experimental!(transparent)
895+
),
896+
891897
// ==========================================================================
892898
// Internal attributes: Stability, deprecation, and unsafe:
893899
// ==========================================================================

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ declare_features! (
658658
(unstable, trait_alias, "1.24.0", Some(41517)),
659659
/// Allows for transmuting between arrays with sizes that contain generic consts.
660660
(unstable, transmute_generic_consts, "1.70.0", Some(109929)),
661+
/// Allows #[transparent] on modules to inherit lexical scopes.
662+
(unstable, transparent_modules, "1.91.0", Some(79260)),
661663
/// Allows #[repr(transparent)] on unions (RFC 2645).
662664
(unstable, transparent_unions, "1.37.0", Some(60405)),
663665
/// 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
@@ -708,6 +708,9 @@ pub enum AttributeKind {
708708
/// Represents `#[track_caller]`
709709
TrackCaller(Span),
710710

711+
/// Represents `#[transparent]` mod attribute
712+
Transparent(Span),
713+
711714
/// Represents `#[type_const]`.
712715
TypeConst(Span),
713716

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl AttributeKind {
9898
StdInternalSymbol(..) => No,
9999
TargetFeature { .. } => No,
100100
TrackCaller(..) => Yes,
101+
Transparent(..) => No,
101102
TypeConst(..) => Yes,
102103
TypeLengthLimit { .. } => No,
103104
UnsafeSpecializationMarker(..) => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
225225
AttributeKind::BodyStability { .. }
226226
| AttributeKind::ConstStabilityIndirect
227227
| AttributeKind::MacroTransparency(_)
228+
| AttributeKind::Transparent(_)
228229
| AttributeKind::Pointee(..)
229230
| AttributeKind::Dummy
230231
| AttributeKind::RustcBuiltinMacro { .. }

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
154154
let expn_id = self.cstore().expn_that_defined_untracked(def_id, self.tcx.sess);
155155
return Some(self.new_extern_module(
156156
parent,
157-
ModuleKind::Def(def_kind, def_id, Some(self.tcx.item_name(def_id))),
157+
ModuleKind::Def(
158+
def_kind,
159+
def_id,
160+
Some((self.tcx.item_name(def_id), false)),
161+
),
158162
expn_id,
159163
self.def_span(def_id),
160164
// FIXME: Account for `#[no_implicit_prelude]` attributes.
@@ -629,14 +633,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
629633
// Disallow `use $crate;`
630634
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
631635
let crate_root = self.r.resolve_crate_root(source.ident);
632-
let crate_name = match crate_root.kind {
633-
ModuleKind::Def(.., name) => name,
636+
let crate_name_and_transparent = match crate_root.kind {
637+
ModuleKind::Def(.., name_and_transparent) => name_and_transparent,
634638
ModuleKind::Block => unreachable!(),
635639
};
636640
// HACK(eddyb) unclear how good this is, but keeping `$crate`
637641
// in `source` breaks `tests/ui/imports/import-crate-var.rs`,
638642
// while the current crate doesn't have a valid `crate_name`.
639-
if let Some(crate_name) = crate_name {
643+
if let Some((crate_name, _transparent)) = crate_name_and_transparent {
640644
// `crate_name` should not be interpreted as relative.
641645
module_path.push(Segment::from_ident_and_id(
642646
Ident::new(kw::PathRoot, source.ident.span),
@@ -818,9 +822,18 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
818822
{
819823
self.r.mods_with_parse_errors.insert(def_id);
820824
}
825+
let transparent = AttributeParser::parse_limited(
826+
self.r.tcx.sess,
827+
&item.attrs,
828+
sym::transparent,
829+
item.span,
830+
item.id,
831+
None,
832+
)
833+
.is_some();
821834
self.parent_scope.module = self.r.new_local_module(
822835
Some(parent),
823-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
836+
ModuleKind::Def(def_kind, def_id, Some((ident.name, transparent))),
824837
expansion.to_expn_id(),
825838
item.span,
826839
parent.no_implicit_prelude
@@ -852,7 +865,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
852865

853866
self.parent_scope.module = self.r.new_local_module(
854867
Some(parent),
855-
ModuleKind::Def(def_kind, def_id, Some(ident.name)),
868+
ModuleKind::Def(def_kind, def_id, Some((ident.name, false))),
856869
expansion.to_expn_id(),
857870
item.span,
858871
parent.no_implicit_prelude,

compiler/rustc_resolve/src/ident.rs

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

240+
if module.is_transparent() {
241+
return Some((module.parent.unwrap().nearest_item_scope(), None));
242+
}
243+
240244
// We need to support the next case under a deprecation warning
241245
// ```
242246
// struct MyStruct;
@@ -345,7 +349,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
345349
// Encountered a module item, abandon ribs and look into that module and preludes.
346350
let parent_scope = &ParentScope { module, ..*parent_scope };
347351
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
348-
return self
352+
let binding = self
349353
.cm()
350354
.resolve_ident_in_scope_set(
351355
orig_ident,
@@ -356,8 +360,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
356360
ignore_binding,
357361
None,
358362
)
359-
.ok()
360-
.map(LexicalScopeBinding::Item);
363+
.ok();
364+
match binding {
365+
Some(binding) => {
366+
return Some(LexicalScopeBinding::Item(binding));
367+
}
368+
None => {
369+
if !module.is_transparent() {
370+
return None;
371+
}
372+
}
373+
}
361374
}
362375

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

0 commit comments

Comments
 (0)