Skip to content

fresh binding should shadow the def in expand #143141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,9 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {

fn block_needs_anonymous_module(&self, block: &Block) -> bool {
// If any statements are items, we need to create an anonymous module
block
.stmts
.iter()
.any(|statement| matches!(statement.kind, StmtKind::Item(_) | StmtKind::MacCall(_)))
block.stmts.iter().any(|statement| {
matches!(statement.kind, StmtKind::Item(_) | StmtKind::MacCall(_) | StmtKind::Let(_))
})
}

// Add an import to the current module.
Expand Down
180 changes: 160 additions & 20 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use crate::late::{ConstantHasGenerics, NoConstantGenericsReason, PathSource, Rib
use crate::macros::{MacroRulesScope, sub_namespace_match};
use crate::{
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, CmResolver, Determinacy,
Finalize, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot,
NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res, ResolutionError,
Resolver, Scope, ScopeSet, Segment, Stage, Used, Weak, errors,
Finalize, ImportKind, LexicalScopeBinding, LookaheadItemInBlock, Module, ModuleKind,
ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res,
ResolutionError, Resolver, Scope, ScopeSet, Segment, Stage, Used, Weak, errors,
};

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -328,20 +328,160 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
*original_rib_ident_def,
ribs,
)));
} else if let RibKind::Block(Some(module)) = rib.kind
&& let Ok(binding) = self.cm().resolve_ident_in_module_unadjusted(
ModuleOrUniformRoot::Module(module),
ident,
ns,
parent_scope,
Shadowing::Unrestricted,
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
ignore_binding,
None,
)
{
// The ident resolves to an item in a block.
return Some(LexicalScopeBinding::Item(binding));
} else if let RibKind::Block { module, id: block_id } = rib.kind {
fn resolve_ident_in_forward_macro_of_block<'ra>(
r: &mut Resolver<'ra, '_>,
expansion: &mut Option<NodeId>, // macro_def_id
module: Module<'ra>,
resolving_block: NodeId,
ident: &mut Ident,
i: usize,
finalize: Option<Finalize>,
ribs: &[Rib<'ra>],
) -> Option<Res> {
let items = r.lookahead_items_in_block.get(&resolving_block)?;
for (node_id, item) in items.iter().rev() {
match item {
LookaheadItemInBlock::MacroDef { def_id, bindings } => {
if *def_id != r.macro_def(ident.span.ctxt()) {
continue;
}
expansion.get_or_insert(*node_id);
ident.span.remove_mark();
if let Some((original_rib_ident_def, (module_of_res, res))) =
bindings.get_key_value(ident)
&& module_of_res.is_ancestor_of(module)
{
// The ident resolves to a type parameter or local variable.
return Some(r.validate_res_from_ribs(
i,
*ident,
*res,
finalize.map(|finalize| finalize.path_span),
*original_rib_ident_def,
ribs,
));
}
}
LookaheadItemInBlock::Block => {
// resolve child block later
}
LookaheadItemInBlock::Binding { .. } => {}
}
}

let subs = items
.iter()
.filter_map(|(node_id, item)| {
if matches!(item, LookaheadItemInBlock::Block) {
Some(*node_id)
} else {
None
}
})
.collect::<Vec<_>>();
for node_id in subs {
if let Some(res) = resolve_ident_in_forward_macro_of_block(
r, expansion, module, node_id, ident, i, finalize, ribs,
) {
return Some(res);
}
}

None
}

fn is_defined_later(
r: &Resolver<'_, '_>,
expansion: NodeId, // macro_def_id
block_id: NodeId,
ident: &Ident,
) -> bool {
let Some(items) = r.lookahead_items_in_block.get(&block_id) else {
return false;
};
for (node_id, item) in items {
match item {
LookaheadItemInBlock::Binding { name } => {
if name.name == ident.name {
return true;
}
}
LookaheadItemInBlock::Block => {
if is_defined_later(r, expansion, *node_id, ident) {
return true;
}
}
LookaheadItemInBlock::MacroDef { .. } => {
if expansion.eq(node_id) {
return false;
}
}
}
}

false
}

let mut expansion = None;
if let Some(res) = resolve_ident_in_forward_macro_of_block(
self,
&mut expansion,
parent_scope.module,
block_id,
&mut ident,
i,
finalize,
ribs,
) {
return Some(LexicalScopeBinding::Res(res));
}

if let Some(expansion) = expansion
&& is_defined_later(self, expansion, block_id, &ident)
{
// return `None` for this case:
//
// ```
// let a = m!();
// let b = 1;
// macro_rules! m { () => { b } }
// use b;
// ```
return None;
}

if let Some(module) = module
&& let Ok(binding) = self.cm().resolve_ident_in_module_unadjusted(
ModuleOrUniformRoot::Module(module),
ident,
ns,
parent_scope,
Shadowing::Unrestricted,
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
ignore_binding,
None,
)
{
// The ident resolves to an item in a block.
if ns == TypeNS {
return Some(LexicalScopeBinding::Item(binding));
}
let macro_def_id = self.macro_def(ident.span.ctxt());
if !matches!(self.tcx.def_kind(macro_def_id), DefKind::Macro(_)) {
return Some(LexicalScopeBinding::Item(binding));
}
return if binding
.span
.ctxt()
.outer_expn()
.outer_expn_is_descendant_of(ident.span.ctxt())
{
Some(LexicalScopeBinding::Item(binding))
} else {
None
};
}
} else if let RibKind::Module(module) = rib.kind {
// Encountered a module item, abandon ribs and look into that module and preludes.
let parent_scope = &ParentScope { module, ..*parent_scope };
Expand Down Expand Up @@ -1163,7 +1303,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
for rib in ribs {
match rib.kind {
RibKind::Normal
| RibKind::Block(..)
| RibKind::Block { .. }
| RibKind::FnOrCoroutine
| RibKind::Module(..)
| RibKind::MacroDefinition(..)
Expand Down Expand Up @@ -1256,7 +1396,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
for rib in ribs {
let (has_generic_params, def_kind) = match rib.kind {
RibKind::Normal
| RibKind::Block(..)
| RibKind::Block { .. }
| RibKind::FnOrCoroutine
| RibKind::Module(..)
| RibKind::MacroDefinition(..)
Expand Down Expand Up @@ -1350,7 +1490,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
for rib in ribs {
let (has_generic_params, def_kind) = match rib.kind {
RibKind::Normal
| RibKind::Block(..)
| RibKind::Block { .. }
| RibKind::FnOrCoroutine
| RibKind::Module(..)
| RibKind::MacroDefinition(..)
Expand Down
Loading
Loading