Skip to content

Commit c23698f

Browse files
committed
resolve: Introduce RibKind::Block
to avoid confusing module items, blocks with items, and blocks without items.
1 parent 2886b36 commit c23698f

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
@@ -331,7 +331,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
331331
}
332332

333333
module = match rib.kind {
334-
RibKind::Module(module) => module,
334+
RibKind::Module(module) | RibKind::Block(Some(module)) => module,
335335
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
336336
// If an invocation of this macro created `ident`, give up on `ident`
337337
// and switch to `ident`'s source from the macro definition.
@@ -1154,6 +1154,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11541154
for rib in ribs {
11551155
match rib.kind {
11561156
RibKind::Normal
1157+
| RibKind::Block(..)
11571158
| RibKind::FnOrCoroutine
11581159
| RibKind::Module(..)
11591160
| RibKind::MacroDefinition(..)
@@ -1246,6 +1247,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12461247
for rib in ribs {
12471248
let (has_generic_params, def_kind) = match rib.kind {
12481249
RibKind::Normal
1250+
| RibKind::Block(..)
12491251
| RibKind::FnOrCoroutine
12501252
| RibKind::Module(..)
12511253
| RibKind::MacroDefinition(..)
@@ -1339,6 +1341,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13391341
for rib in ribs {
13401342
let (has_generic_params, def_kind) = match rib.kind {
13411343
RibKind::Normal
1344+
| RibKind::Block(..)
13421345
| RibKind::FnOrCoroutine
13431346
| RibKind::Module(..)
13441347
| 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

@@ -4663,16 +4664,16 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46634664
debug!("(resolving block) entering block");
46644665
// Move down in the graph, if there's an anonymous module rooted here.
46654666
let orig_module = self.parent_scope.module;
4666-
let anonymous_module = self.r.block_map.get(&block.id).cloned(); // clones a reference
4667+
let anonymous_module = self.r.block_map.get(&block.id).copied();
46674668

46684669
let mut num_macro_definition_ribs = 0;
46694670
if let Some(anonymous_module) = anonymous_module {
46704671
debug!("(resolving block) found anonymous module, moving down");
4671-
self.ribs[ValueNS].push(Rib::new(RibKind::Module(anonymous_module)));
4672-
self.ribs[TypeNS].push(Rib::new(RibKind::Module(anonymous_module)));
4672+
self.ribs[ValueNS].push(Rib::new(RibKind::Block(Some(anonymous_module))));
4673+
self.ribs[TypeNS].push(Rib::new(RibKind::Block(Some(anonymous_module))));
46734674
self.parent_scope.module = anonymous_module;
46744675
} else {
4675-
self.ribs[ValueNS].push(Rib::new(RibKind::Normal));
4676+
self.ribs[ValueNS].push(Rib::new(RibKind::Block(None)));
46764677
}
46774678

46784679
// 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)