Skip to content

Commit 275afd6

Browse files
committed
fix: consider outer binders when folding captured items' type
1 parent 4fb1df6 commit 275afd6

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

crates/hir-ty/src/infer/closure.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{cmp, collections::HashMap, convert::Infallible, mem};
55
use chalk_ir::{
66
cast::Cast,
77
fold::{FallibleTypeFolder, TypeFoldable},
8-
AliasEq, AliasTy, BoundVar, ConstData, DebruijnIndex, FnSubst, Mutability, TyKind, WhereClause,
8+
AliasEq, AliasTy, BoundVar, DebruijnIndex, FnSubst, Mutability, TyKind, WhereClause,
99
};
1010
use hir_def::{
1111
data::adt::VariantData,
@@ -26,8 +26,8 @@ use crate::{
2626
static_lifetime, to_chalk_trait_id,
2727
traits::FnTrait,
2828
utils::{self, generics, Generics},
29-
Adjust, Adjustment, Binders, BindingMode, ChalkTraitId, ClosureId, ConstValue, DynTy,
30-
FnPointer, FnSig, Interner, Substitution, Ty, TyExt,
29+
Adjust, Adjustment, Binders, BindingMode, ChalkTraitId, ClosureId, DynTy, FnPointer, FnSig,
30+
Interner, Substitution, Ty, TyExt,
3131
};
3232

3333
use super::{Expectation, InferenceContext};
@@ -266,24 +266,19 @@ impl CapturedItemWithoutTy {
266266
let Some(idx) = self.generics.param_idx(x) else {
267267
return Err(());
268268
};
269-
Ok(ConstData {
270-
ty,
271-
value: ConstValue::BoundVar(BoundVar::new(outer_binder, idx)),
272-
}
273-
.intern(Interner))
269+
Ok(BoundVar::new(outer_binder, idx).to_const(Interner, ty))
274270
}
275271

276272
fn try_fold_free_placeholder_ty(
277273
&mut self,
278274
idx: chalk_ir::PlaceholderIndex,
279-
_outer_binder: DebruijnIndex,
275+
outer_binder: DebruijnIndex,
280276
) -> std::result::Result<Ty, Self::Error> {
281277
let x = from_placeholder_idx(self.db, idx);
282278
let Some(idx) = self.generics.param_idx(x) else {
283279
return Err(());
284280
};
285-
Ok(TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx))
286-
.intern(Interner))
281+
Ok(BoundVar::new(outer_binder, idx).to_ty(Interner))
287282
}
288283
}
289284
let g_def = match owner {

crates/hir-ty/src/mir/eval/tests.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,37 @@ fn main() {
640640
"#,
641641
);
642642
}
643+
644+
#[test]
645+
fn regression_14966() {
646+
check_pass(
647+
r#"
648+
//- minicore: fn, copy, coerce_unsized
649+
trait A<T> {
650+
fn a(&self) {}
651+
}
652+
impl A<()> for () {}
653+
654+
struct B;
655+
impl B {
656+
pub fn b<T>(s: &dyn A<T>) -> Self {
657+
B
658+
}
659+
}
660+
struct C;
661+
impl C {
662+
fn c<T>(a: &dyn A<T>) -> Self {
663+
let mut c = C;
664+
let b = B::b(a);
665+
c.d(|| a.a());
666+
c
667+
}
668+
fn d(&mut self, f: impl FnOnce()) {}
669+
}
670+
671+
fn main() {
672+
C::c(&());
673+
}
674+
"#,
675+
);
676+
}

0 commit comments

Comments
 (0)