Skip to content

Commit 4a9607f

Browse files
committed
add block kind during name resolution
1 parent cd43430 commit 4a9607f

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
327327

328328
module = match rib.kind {
329329
RibKind::Module(module) => module,
330+
RibKind::Block { module } if let Some(module) = module => module,
330331
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
331332
// If an invocation of this macro created `ident`, give up on `ident`
332333
// and switch to `ident`'s source from the macro definition.
@@ -1149,6 +1150,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11491150
RibKind::Normal
11501151
| RibKind::FnOrCoroutine
11511152
| RibKind::Module(..)
1153+
| RibKind::Block { .. }
11521154
| RibKind::MacroDefinition(..)
11531155
| RibKind::ForwardGenericParamBan(_) => {
11541156
// Nothing to do. Continue.
@@ -1241,6 +1243,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12411243
RibKind::Normal
12421244
| RibKind::FnOrCoroutine
12431245
| RibKind::Module(..)
1246+
| RibKind::Block { .. }
12441247
| RibKind::MacroDefinition(..)
12451248
| RibKind::InlineAsmSym
12461249
| RibKind::AssocItem
@@ -1334,6 +1337,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13341337
RibKind::Normal
13351338
| RibKind::FnOrCoroutine
13361339
| RibKind::Module(..)
1340+
| RibKind::Block { .. }
13371341
| RibKind::MacroDefinition(..)
13381342
| RibKind::InlineAsmSym
13391343
| RibKind::AssocItem

compiler/rustc_resolve/src/late.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ pub(crate) enum RibKind<'ra> {
211211
/// All other constants aren't allowed to use generic params at all.
212212
ConstantItem(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>),
213213

214+
Block {
215+
module: Option<Module<'ra>>,
216+
},
217+
214218
/// We passed through a module.
215219
Module(Module<'ra>),
216220

@@ -243,6 +247,7 @@ impl RibKind<'_> {
243247
pub(crate) fn contains_params(&self) -> bool {
244248
match self {
245249
RibKind::Normal
250+
| RibKind::Block { .. }
246251
| RibKind::FnOrCoroutine
247252
| RibKind::ConstantItem(..)
248253
| RibKind::Module(_)
@@ -258,13 +263,16 @@ impl RibKind<'_> {
258263
/// This rib forbids referring to labels defined in upwards ribs.
259264
fn is_label_barrier(self) -> bool {
260265
match self {
261-
RibKind::Normal | RibKind::MacroDefinition(..) => false,
266+
RibKind::Normal | RibKind::MacroDefinition(..) | RibKind::Block { module: None } => {
267+
false
268+
}
262269

263270
RibKind::AssocItem
264271
| RibKind::FnOrCoroutine
265272
| RibKind::Item(..)
266273
| RibKind::ConstantItem(..)
267274
| RibKind::Module(..)
275+
| RibKind::Block { module: Some(_) }
268276
| RibKind::ForwardGenericParamBan(_)
269277
| RibKind::ConstParamTy
270278
| RibKind::InlineAsmSym => true,
@@ -2822,7 +2830,10 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28222830
for parent_rib in self.ribs[ns].iter().rev() {
28232831
// Break at mod level, to account for nested items which are
28242832
// allowed to shadow generic param names.
2825-
if matches!(parent_rib.kind, RibKind::Module(..)) {
2833+
if matches!(
2834+
parent_rib.kind,
2835+
RibKind::Module(..) | RibKind::Block { module: Some(_) }
2836+
) {
28262837
break;
28272838
}
28282839

@@ -4662,11 +4673,11 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
46624673
let mut num_macro_definition_ribs = 0;
46634674
if let Some(anonymous_module) = anonymous_module {
46644675
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)));
4676+
self.ribs[ValueNS].push(Rib::new(RibKind::Block { module: Some(anonymous_module) }));
4677+
self.ribs[TypeNS].push(Rib::new(RibKind::Block { module: Some(anonymous_module) }));
46674678
self.parent_scope.module = anonymous_module;
46684679
} else {
4669-
self.ribs[ValueNS].push(Rib::new(RibKind::Normal));
4680+
self.ribs[ValueNS].push(Rib::new(RibKind::Block { module: None }));
46704681
}
46714682

46724683
// Descend into the block.

compiler/rustc_resolve/src/late/diagnostics.rs

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

853853
// Try to find in last block rib
854854
if let Some(rib) = &self.last_block_rib
855-
&& let RibKind::Normal = rib.kind
855+
&& let RibKind::Block { module: None } = rib.kind
856856
{
857857
for (ident, &res) in &rib.bindings {
858858
if let Res::Local(_) = res
@@ -2467,7 +2467,8 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24672467
}
24682468

24692469
// Items in scope
2470-
if let RibKind::Module(module) = rib.kind {
2470+
if let RibKind::Module(module) | RibKind::Block { module: Some(module) } = rib.kind
2471+
{
24712472
// Items from this module
24722473
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
24732474

0 commit comments

Comments
 (0)