Skip to content

Commit 4196675

Browse files
committed
Auto merge of rust-lang#16045 - HKalbasi:rustc-tests-fixup, r=HKalbasi
Fix panic with closure inside array len I was working on rust-lang#15947 and found out that we panic on this test: ``` fn main() { let x = [(); &(&'static: loop { |x| {}; }) as *const _ as usize] } ``` This PR fixes the panic. Closures in array len are still broken, but closure in const eval is not stable anyway.
2 parents 49dd380 + c11a002 commit 4196675

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

crates/hir-ty/src/consteval.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use base_db::{salsa::Cycle, CrateId};
44
use chalk_ir::{cast::Cast, BoundVar, DebruijnIndex};
55
use hir_def::{
6-
hir::Expr,
6+
body::Body,
7+
hir::{Expr, ExprId},
78
path::Path,
89
resolver::{Resolver, ValueNs},
910
type_ref::LiteralConstRef,
@@ -280,21 +281,32 @@ pub(crate) fn const_eval_discriminant_variant(
280281
// get an `InferenceResult` instead of an `InferenceContext`. And we should remove `ctx.clone().resolve_all()` here
281282
// and make this function private. See the fixme comment on `InferenceContext::resolve_all`.
282283
pub(crate) fn eval_to_const(
283-
expr: Idx<Expr>,
284+
expr: ExprId,
284285
mode: ParamLoweringMode,
285286
ctx: &mut InferenceContext<'_>,
286287
args: impl FnOnce() -> Generics,
287288
debruijn: DebruijnIndex,
288289
) -> Const {
289290
let db = ctx.db;
290291
let infer = ctx.clone().resolve_all();
292+
fn has_closure(body: &Body, expr: ExprId) -> bool {
293+
if matches!(body[expr], Expr::Closure { .. }) {
294+
return true;
295+
}
296+
let mut r = false;
297+
body[expr].walk_child_exprs(|idx| r |= has_closure(body, idx));
298+
r
299+
}
300+
if has_closure(&ctx.body, expr) {
301+
// Type checking clousres need an isolated body (See the above FIXME). Bail out early to prevent panic.
302+
return unknown_const(infer[expr].clone());
303+
}
291304
if let Expr::Path(p) = &ctx.body.exprs[expr] {
292305
let resolver = &ctx.resolver;
293306
if let Some(c) = path_to_const(db, resolver, p, mode, args, debruijn, infer[expr].clone()) {
294307
return c;
295308
}
296309
}
297-
let infer = ctx.clone().resolve_all();
298310
if let Ok(mir_body) = lower_to_mir(ctx.db, ctx.owner, &ctx.body, &infer, expr) {
299311
if let Ok(result) = interpret_mir(db, Arc::new(mir_body), true, None).0 {
300312
return result;

crates/hir-ty/src/tests/regression.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2000,3 +2000,15 @@ fn test() {
20002000
"#,
20012001
);
20022002
}
2003+
2004+
#[test]
2005+
fn rustc_test_issue_52437() {
2006+
check_types(
2007+
r#"
2008+
fn main() {
2009+
let x = [(); &(&'static: loop { |x| {}; }) as *const _ as usize]
2010+
//^ [(); _]
2011+
}
2012+
"#,
2013+
);
2014+
}

0 commit comments

Comments
 (0)