Skip to content

Commit d6c7cf7

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

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.
@@ -1167,6 +1167,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11671167
for rib in ribs {
11681168
match rib.kind {
11691169
RibKind::Normal
1170+
| RibKind::Block(..)
11701171
| RibKind::FnOrCoroutine
11711172
| RibKind::Module(..)
11721173
| RibKind::MacroDefinition(..)
@@ -1259,6 +1260,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12591260
for rib in ribs {
12601261
let (has_generic_params, def_kind) = match rib.kind {
12611262
RibKind::Normal
1263+
| RibKind::Block(..)
12621264
| RibKind::FnOrCoroutine
12631265
| RibKind::Module(..)
12641266
| RibKind::MacroDefinition(..)
@@ -1352,6 +1354,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13521354
for rib in ribs {
13531355
let (has_generic_params, def_kind) = match rib.kind {
13541356
RibKind::Normal
1357+
| RibKind::Block(..)
13551358
| RibKind::FnOrCoroutine
13561359
| RibKind::Module(..)
13571360
| RibKind::MacroDefinition(..)

compiler/rustc_resolve/src/late.rs

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

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

213-
/// We passed through a module.
220+
/// We passed through a module item.
214221
Module(Module<'ra>),
215222

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

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

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

46794680
// 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
@@ -850,9 +850,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
850850
}
851851

852852
// Try to find in last block rib
853-
if let Some(rib) = &self.last_block_rib
854-
&& let RibKind::Normal = rib.kind
855-
{
853+
if let Some(rib) = &self.last_block_rib {
856854
for (ident, &res) in &rib.bindings {
857855
if let Res::Local(_) = res
858856
&& path.len() == 1
@@ -901,7 +899,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
901899
if path.len() == 1 {
902900
for rib in self.ribs[ns].iter().rev() {
903901
let item = path[0].ident;
904-
if let RibKind::Module(module) = rib.kind
902+
if let RibKind::Module(module) | RibKind::Block(Some(module)) = rib.kind
905903
&& let Some(did) = find_doc_alias_name(self.r, module, item.name)
906904
{
907905
return Some((did, item));
@@ -2469,7 +2467,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24692467
}
24702468

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

0 commit comments

Comments
 (0)