Skip to content

Commit 6dbaf86

Browse files
authored
Rollup merge of #104864 - chenyukang:yukang/fix-104700-binding, r=estebank
Account for item-local in inner scope for E0425 Fixes #104700
2 parents ed97493 + 68ea516 commit 6dbaf86

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

compiler/rustc_resolve/src/late.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ struct LateResolutionVisitor<'a, 'b, 'ast> {
566566
/// FIXME #4948: Reuse ribs to avoid allocation.
567567
ribs: PerNS<Vec<Rib<'a>>>,
568568

569+
/// Previous poped `rib`, only used for diagnostic.
570+
last_block_rib: Option<Rib<'a>>,
571+
569572
/// The current set of local scopes, for labels.
570573
label_ribs: Vec<Rib<'a, NodeId>>,
571574

@@ -873,6 +876,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
873876
// Ignore errors in function bodies if this is rustdoc
874877
// Be sure not to set this until the function signature has been resolved.
875878
let previous_state = replace(&mut this.in_func_body, true);
879+
// We only care block in the same function
880+
this.last_block_rib = None;
876881
// Resolve the function body, potentially inside the body of an async closure
877882
this.with_lifetime_rib(
878883
LifetimeRibKind::Elided(LifetimeRes::Infer),
@@ -1168,6 +1173,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11681173
type_ns: vec![Rib::new(start_rib_kind)],
11691174
macro_ns: vec![Rib::new(start_rib_kind)],
11701175
},
1176+
last_block_rib: None,
11711177
label_ribs: Vec::new(),
11721178
lifetime_ribs: Vec::new(),
11731179
lifetime_elision_candidates: None,
@@ -3769,7 +3775,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
37693775
self.ribs[ValueNS].pop();
37703776
self.label_ribs.pop();
37713777
}
3772-
self.ribs[ValueNS].pop();
3778+
self.last_block_rib = self.ribs[ValueNS].pop();
37733779
if anonymous_module.is_some() {
37743780
self.ribs[TypeNS].pop();
37753781
}

compiler/rustc_resolve/src/late/diagnostics.rs

+16
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,22 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
623623
return (true, candidates);
624624
}
625625
}
626+
627+
// Try to find in last block rib
628+
if let Some(rib) = &self.last_block_rib && let RibKind::NormalRibKind = rib.kind {
629+
for (ident, &res) in &rib.bindings {
630+
if let Res::Local(_) = res && path.len() == 1 &&
631+
ident.span.eq_ctxt(path[0].ident.span) &&
632+
ident.name == path[0].ident.name {
633+
err.span_help(
634+
ident.span,
635+
&format!("the binding `{}` is available in a different scope in the same function", path_str),
636+
);
637+
return (true, candidates);
638+
}
639+
}
640+
}
641+
626642
return (false, candidates);
627643
}
628644

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let foo = 1;
3+
{
4+
let bar = 2;
5+
let test_func = |x| x > 3;
6+
}
7+
if bar == 2 { //~ ERROR cannot find value
8+
println!("yes");
9+
}
10+
test_func(1); //~ ERROR cannot find function
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0425]: cannot find value `bar` in this scope
2+
--> $DIR/issue-104700-inner_scope.rs:7:8
3+
|
4+
LL | if bar == 2 {
5+
| ^^^
6+
|
7+
help: the binding `bar` is available in a different scope in the same function
8+
--> $DIR/issue-104700-inner_scope.rs:4:13
9+
|
10+
LL | let bar = 2;
11+
| ^^^
12+
13+
error[E0425]: cannot find function `test_func` in this scope
14+
--> $DIR/issue-104700-inner_scope.rs:10:5
15+
|
16+
LL | test_func(1);
17+
| ^^^^^^^^^ not found in this scope
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)