Skip to content

Commit a32e0f3

Browse files
authored
Rollup merge of #94849 - ouz-a:master4, r=oli-obk
Check var scope if it exist Fixes #92893. Added helper function to check the scope of a variable, if it doesn't have a scope call delay_span_bug, which avoids us trying to get a block/scope that doesn't exist. Had to increase `ROOT_ENTRY_LIMIT` was getting tidy error
2 parents 20bf34f + c20bb1d commit a32e0f3

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

compiler/rustc_middle/src/middle/region.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,9 @@ impl ScopeTree {
362362
self.parent_map.get(&id).cloned().map(|(p, _)| p)
363363
}
364364

365-
/// Returns the lifetime of the local variable `var_id`
366-
pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope {
367-
self.var_map
368-
.get(&var_id)
369-
.cloned()
370-
.unwrap_or_else(|| bug!("no enclosing scope for id {:?}", var_id))
365+
/// Returns the lifetime of the local variable `var_id`, if any.
366+
pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Option<Scope> {
367+
self.var_map.get(&var_id).cloned()
371368
}
372369

373370
/// Returns the scope when the temp created by `expr_id` will be cleaned up.

compiler/rustc_mir_build/src/build/matches/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -701,17 +701,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
701701
let local_id = self.var_local_id(var, for_guard);
702702
let source_info = self.source_info(span);
703703
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
704-
let region_scope = self.region_scope_tree.var_scope(var.local_id);
705-
if schedule_drop {
704+
// Altough there is almost always scope for given variable in corner cases
705+
// like #92893 we might get variable with no scope.
706+
if let Some(region_scope) = self.region_scope_tree.var_scope(var.local_id) && schedule_drop{
706707
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
707708
}
708709
Place::from(local_id)
709710
}
710711

711712
crate fn schedule_drop_for_binding(&mut self, var: HirId, span: Span, for_guard: ForGuard) {
712713
let local_id = self.var_local_id(var, for_guard);
713-
let region_scope = self.region_scope_tree.var_scope(var.local_id);
714-
self.schedule_drop(span, region_scope, local_id, DropKind::Value);
714+
if let Some(region_scope) = self.region_scope_tree.var_scope(var.local_id) {
715+
self.schedule_drop(span, region_scope, local_id, DropKind::Value);
716+
}
715717
}
716718

717719
/// Visit all of the primary bindings in a patterns, that is, visit the

compiler/rustc_typeck/src/check/generator_interior.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
319319
self.expr_count += 1;
320320

321321
if let PatKind::Binding(..) = pat.kind {
322-
let scope = self.region_scope_tree.var_scope(pat.hir_id.local_id);
322+
let scope = self.region_scope_tree.var_scope(pat.hir_id.local_id).unwrap();
323323
let ty = self.fcx.typeck_results.borrow().pat_ty(pat);
324324
self.record(ty, pat.hir_id, Some(scope), None, pat.span, false);
325325
}

src/test/ui/mir/issue-92893.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Bug<A = [(); (let a = (), 1).1]> {
2+
//~^ `let` expressions are not supported here
3+
//~^^ `let` expressions in this position are unstable [E0658]
4+
a: A
5+
}
6+
7+
fn main() {}

src/test/ui/mir/issue-92893.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: `let` expressions are not supported here
2+
--> $DIR/issue-92893.rs:1:22
3+
|
4+
LL | struct Bug<A = [(); (let a = (), 1).1]> {
5+
| ^^^^^^^^^^
6+
|
7+
= note: only supported directly in conditions of `if` and `while` expressions
8+
9+
error[E0658]: `let` expressions in this position are unstable
10+
--> $DIR/issue-92893.rs:1:22
11+
|
12+
LL | struct Bug<A = [(); (let a = (), 1).1]> {
13+
| ^^^^^^^^^^
14+
|
15+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
16+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0658`.

src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(super) fn check<'tcx>(
5959
if let Some(indexed_extent) = indexed_extent {
6060
let parent_def_id = cx.tcx.hir().get_parent_item(expr.hir_id);
6161
let region_scope_tree = cx.tcx.region_scope_tree(parent_def_id);
62-
let pat_extent = region_scope_tree.var_scope(pat.hir_id.local_id);
62+
let pat_extent = region_scope_tree.var_scope(pat.hir_id.local_id).unwrap();
6363
if region_scope_tree.is_subscope_of(indexed_extent, pat_extent) {
6464
return;
6565
}
@@ -262,7 +262,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
262262
match res {
263263
Res::Local(hir_id) => {
264264
let parent_def_id = self.cx.tcx.hir().get_parent_item(expr.hir_id);
265-
let extent = self.cx.tcx.region_scope_tree(parent_def_id).var_scope(hir_id.local_id);
265+
let extent = self.cx.tcx.region_scope_tree(parent_def_id).var_scope(hir_id.local_id).unwrap();
266266
if index_used_directly {
267267
self.indexed_directly.insert(
268268
seqvar.segments[0].ident.name,

src/tools/clippy/clippy_lints/src/shadow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
160160

161161
fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second: ItemLocalId) -> bool {
162162
let scope_tree = cx.tcx.region_scope_tree(owner.to_def_id());
163-
let first_scope = scope_tree.var_scope(first);
164-
let second_scope = scope_tree.var_scope(second);
163+
let first_scope = scope_tree.var_scope(first).unwrap();
164+
let second_scope = scope_tree.var_scope(second).unwrap();
165165
scope_tree.is_subscope_of(second_scope, first_scope)
166166
}
167167

0 commit comments

Comments
 (0)