Skip to content

Commit f8b7b64

Browse files
committed
WIP use params for APIT
1 parent 33aa2f8 commit f8b7b64

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

crates/ra_hir_ty/src/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
480480
fn collect_fn(&mut self, data: &FunctionData) {
481481
let body = Arc::clone(&self.body); // avoid borrow checker problem
482482
for (type_ref, pat) in data.params.iter().zip(body.params.iter()) {
483-
let ty = self.make_ty_with_mode(type_ref, ImplTraitLoweringMode::Opaque);
483+
let ty = self.make_ty_with_mode(type_ref, ImplTraitLoweringMode::Param);
484484

485485
self.infer_pat(*pat, &ty, BindingMode::default());
486486
}

crates/ra_hir_ty/src/lower.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::{
3030
Binders, FnSig, GenericPredicate, PolyFnSig, ProjectionPredicate, ProjectionTy, Substs,
3131
TraitEnvironment, TraitRef, Ty, TypeCtor,
3232
};
33+
use hir_expand::name::Name;
3334

3435
#[derive(Debug)]
3536
pub struct TyLoweringContext<'a, DB: HirDatabase> {
@@ -69,6 +70,10 @@ pub enum ImplTraitLoweringMode {
6970
/// i.e. for arguments of the function we're currently checking, and return
7071
/// types of functions we're calling.
7172
Opaque,
73+
/// `impl Trait` gets lowered into a type variable. Used for argument
74+
/// position impl Trait currently, since it allows us to support that
75+
/// without Chalk.
76+
Param,
7277
/// `impl Trait` gets lowered into a variable that can unify with some
7378
/// type. This is used in places where values flow 'in', i.e. for arguments
7479
/// of functions we're calling, and the return type of the function we're
@@ -137,6 +142,11 @@ impl Ty {
137142
.collect();
138143
Ty::Opaque(predicates)
139144
}
145+
ImplTraitLoweringMode::Param => {
146+
let idx = ctx.impl_trait_counter.get();
147+
ctx.impl_trait_counter.set(idx + 1);
148+
Ty::Param { idx: idx as u32, name: Name::missing() }
149+
}
140150
ImplTraitLoweringMode::Variable => {
141151
let idx = ctx.impl_trait_counter.get();
142152
ctx.impl_trait_counter.set(idx + 1);

crates/ra_hir_ty/src/tests/traits.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,59 @@ fn test<T: ApplyL>(t: T) {
849849
assert_eq!(t, "{unknown}");
850850
}
851851

852+
#[test]
853+
fn argument_impl_trait() {
854+
assert_snapshot!(
855+
infer_with_mismatches(r#"
856+
trait Trait<T> {
857+
fn foo(&self) -> T;
858+
fn foo2(&self) -> i64;
859+
}
860+
fn bar(impl Trait<u64>) {}
861+
struct S<T>(T);
862+
impl<T> Trait<T> for S<T> {}
863+
864+
fn test(x: impl Trait<u64>, y: &impl Trait<u64>) {
865+
x;
866+
y;
867+
let z = S(1);
868+
bar(z);
869+
x.foo();
870+
y.foo();
871+
z.foo();
872+
x.foo2();
873+
y.foo2();
874+
z.foo2();
875+
}
876+
"#, true),
877+
@r###"
878+
[30; 34) 'self': &Self
879+
[55; 59) 'self': &Self
880+
[99; 101) '{}': ()
881+
[111; 112) 'x': impl Trait<u64>
882+
[131; 132) 'y': &impl Trait<u64>
883+
[152; 269) '{ ...2(); }': ()
884+
[158; 159) 'x': impl Trait<u64>
885+
[165; 166) 'y': &impl Trait<u64>
886+
[176; 177) 'z': impl Trait<u64>
887+
[180; 183) 'bar': fn bar() -> impl Trait<u64>
888+
[180; 185) 'bar()': impl Trait<u64>
889+
[191; 192) 'x': impl Trait<u64>
890+
[191; 198) 'x.foo()': u64
891+
[204; 205) 'y': &impl Trait<u64>
892+
[204; 211) 'y.foo()': u64
893+
[217; 218) 'z': impl Trait<u64>
894+
[217; 224) 'z.foo()': u64
895+
[230; 231) 'x': impl Trait<u64>
896+
[230; 238) 'x.foo2()': i64
897+
[244; 245) 'y': &impl Trait<u64>
898+
[244; 252) 'y.foo2()': i64
899+
[258; 259) 'z': impl Trait<u64>
900+
[258; 266) 'z.foo2()': i64
901+
"###
902+
);
903+
}
904+
852905
#[test]
853906
#[ignore]
854907
fn impl_trait() {

0 commit comments

Comments
 (0)