Skip to content

Commit b2275e2

Browse files
committed
resolve: Introduce RibKind::Block
to avoid confusing module items, blocks with items, and blocks without items.
1 parent 321a89b commit b2275e2

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
326326
}
327327

328328
module = match rib.kind {
329-
RibKind::Module(module) => module,
329+
RibKind::Module(module) | RibKind::Block(Some(module)) => module,
330330
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
331331
// If an invocation of this macro created `ident`, give up on `ident`
332332
// and switch to `ident`'s source from the macro definition.
@@ -1147,6 +1147,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11471147
for rib in ribs {
11481148
match rib.kind {
11491149
RibKind::Normal
1150+
| RibKind::Block(..)
11501151
| RibKind::FnOrCoroutine
11511152
| RibKind::Module(..)
11521153
| RibKind::MacroDefinition(..)
@@ -1239,6 +1240,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12391240
for rib in ribs {
12401241
let (has_generic_params, def_kind) = match rib.kind {
12411242
RibKind::Normal
1243+
| RibKind::Block(..)
12421244
| RibKind::FnOrCoroutine
12431245
| RibKind::Module(..)
12441246
| RibKind::MacroDefinition(..)
@@ -1332,6 +1334,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13321334
for rib in ribs {
13331335
let (has_generic_params, def_kind) = match rib.kind {
13341336
RibKind::Normal
1337+
| RibKind::Block(..)
13351338
| RibKind::FnOrCoroutine
13361339
| RibKind::Module(..)
13371340
| RibKind::MacroDefinition(..)

compiler/rustc_resolve/src/late.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ pub(crate) enum RibKind<'ra> {
193193
/// No restriction needs to be applied.
194194
Normal,
195195

196+
/// We passed through an `ast::Block`.
197+
/// Behaves like `Normal`, but also partially like `Module` if the block contains items.
198+
/// `Block(None)` must be always processed in the same way as `Block(Some(module))`
199+
/// with empty `module`. The module can be `None` only because creation of some definitely
200+
/// empty modules is skipped as an optimization.
201+
Block(Option<Module<'ra>>),
202+
196203
/// We passed through an impl or trait and are now in one of its
197204
/// methods or associated types. Allow references to ty params that impl or trait
198205
/// binds. Disallow any other upvars (including other ty params that are
@@ -211,7 +218,7 @@ pub(crate) enum RibKind<'ra> {
211218
/// All other constants aren't allowed to use generic params at all.
212219
ConstantItem(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>),
213220

214-
/// We passed through a module.
221+
/// We passed through a module item.
215222
Module(Module<'ra>),
216223

217224
/// We passed through a `macro_rules!` statement
@@ -243,6 +250,7 @@ impl RibKind<'_> {
243250
pub(crate) fn contains_params(&self) -> bool {
244251
match self {
245252
RibKind::Normal
253+
| RibKind::Block(..)
246254
| RibKind::FnOrCoroutine
247255
| RibKind::ConstantItem(..)
248256
| RibKind::Module(_)
@@ -259,15 +267,8 @@ impl RibKind<'_> {
259267
fn is_label_barrier(self) -> bool {
260268
match self {
261269
RibKind::Normal | RibKind::MacroDefinition(..) => false,
262-
263-
RibKind::AssocItem
264-
| RibKind::FnOrCoroutine
265-
| RibKind::Item(..)
266-
| RibKind::ConstantItem(..)
267-
| RibKind::Module(..)
268-
| RibKind::ForwardGenericParamBan(_)
269-
| RibKind::ConstParamTy
270-
| RibKind::InlineAsmSym => true,
270+
RibKind::FnOrCoroutine | RibKind::ConstantItem(..) => true,
271+
kind => bug!("unexpected rib kind: {kind:?}"),
271272
}
272273
}
273274
}
@@ -2820,9 +2821,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28202821
// We also can't shadow bindings from associated parent items.
28212822
for ns in [ValueNS, TypeNS] {
28222823
for parent_rib in self.ribs[ns].iter().rev() {
2823-
// Break at mod level, to account for nested items which are
2824+
// Break at module or block level, to account for nested items which are
28242825
// allowed to shadow generic param names.
2825-
if matches!(parent_rib.kind, RibKind::Module(..)) {
2826+
if matches!(parent_rib.kind, RibKind::Module(..) | RibKind::Block(..)) {
28262827
break;
28272828
}
28282829

@@ -4657,16 +4658,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46574658
debug!("(resolving block) entering block");
46584659
// Move down in the graph, if there's an anonymous module rooted here.
46594660
let orig_module = self.parent_scope.module;
4660-
let anonymous_module = self.r.block_map.get(&block.id).cloned(); // clones a reference
4661+
let anonymous_module = self.r.block_map.get(&block.id).copied();
46614662

46624663
let mut num_macro_definition_ribs = 0;
46634664
if let Some(anonymous_module) = anonymous_module {
46644665
debug!("(resolving block) found anonymous module, moving down");
4665-
self.ribs[ValueNS].push(Rib::new(RibKind::Module(anonymous_module)));
4666-
self.ribs[TypeNS].push(Rib::new(RibKind::Module(anonymous_module)));
4666+
self.ribs[ValueNS].push(Rib::new(RibKind::Block(Some(anonymous_module))));
4667+
self.ribs[TypeNS].push(Rib::new(RibKind::Block(Some(anonymous_module))));
46674668
self.parent_scope.module = anonymous_module;
46684669
} else {
4669-
self.ribs[ValueNS].push(Rib::new(RibKind::Normal));
4670+
self.ribs[ValueNS].push(Rib::new(RibKind::Block(None)));
46704671
}
46714672

46724673
// Descend into the block.

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
851851
}
852852

853853
// Try to find in last block rib
854-
if let Some(rib) = &self.last_block_rib
855-
&& let RibKind::Normal = rib.kind
856-
{
854+
if let Some(rib) = &self.last_block_rib {
857855
for (ident, &res) in &rib.bindings {
858856
if let Res::Local(_) = res
859857
&& path.len() == 1
@@ -902,7 +900,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
902900
if path.len() == 1 {
903901
for rib in self.ribs[ns].iter().rev() {
904902
let item = path[0].ident;
905-
if let RibKind::Module(module) = rib.kind
903+
if let RibKind::Module(module) | RibKind::Block(Some(module)) = rib.kind
906904
&& let Some(did) = find_doc_alias_name(self.r, module, item.name)
907905
{
908906
return Some((did, item));
@@ -2470,7 +2468,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24702468
}
24712469

24722470
// Items in scope
2473-
if let RibKind::Module(module) = rib.kind {
2471+
if let RibKind::Module(module) | RibKind::Block(Some(module)) = rib.kind {
24742472
// Items from this module
24752473
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
24762474

0 commit comments

Comments
 (0)