Skip to content

Commit 2fd9260

Browse files
committed
Auto merge of rust-lang#14897 - HKalbasi:dev, r=HKalbasi
Insert type vars in function arguments follow up rust-lang#14891
2 parents b4e3fec + c21d09f commit 2fd9260

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,8 @@ impl<'a> InferenceContext<'a> {
10251025
)
10261026
}
10271027
};
1028-
1028+
// Try to evaluate unevaluated constant, and insert variable if is not possible.
1029+
let len = self.table.insert_const_vars_shallow(len);
10291030
TyKind::Array(elem_ty, len).intern(Interner)
10301031
}
10311032

@@ -1681,9 +1682,10 @@ impl<'a> InferenceContext<'a> {
16811682
} else {
16821683
param_ty
16831684
};
1684-
if !coercion_target.is_unknown()
1685-
&& self.coerce(Some(arg), &ty, &coercion_target).is_err()
1686-
{
1685+
// The function signature may contain some unknown types, so we need to insert
1686+
// type vars here to avoid type mismatch false positive.
1687+
let coercion_target = self.insert_type_vars(coercion_target);
1688+
if self.coerce(Some(arg), &ty, &coercion_target).is_err() {
16871689
self.result.type_mismatches.insert(
16881690
arg.into(),
16891691
TypeMismatch { expected: coercion_target, actual: ty.clone() },

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

+20
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,26 @@ fn main() {
18881888
_ = Outer {
18891889
inner: Inner::<1>(),
18901890
};
1891+
}
1892+
"#,
1893+
);
1894+
check_no_mismatches(
1895+
r#"
1896+
pub const N: usize = 2 + 2;
1897+
1898+
fn f(t: [u8; N]) {}
1899+
1900+
fn main() {
1901+
let a = [1, 2, 3, 4];
1902+
f(a);
1903+
let b = [1; 4];
1904+
let c: [u8; N] = b;
1905+
let d = [1; N];
1906+
let e: [u8; N] = d;
1907+
let f = [1; N];
1908+
let g = match f {
1909+
[a, b, c, d] => a + b + c + d,
1910+
};
18911911
}
18921912
"#,
18931913
);

crates/ide-diagnostics/src/handlers/type_mismatch.rs

+19
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,25 @@ fn h() {
644644
);
645645
}
646646

647+
#[test]
648+
fn unknown_type_in_function_signature() {
649+
check_diagnostics(
650+
r#"
651+
struct X<T>(T);
652+
653+
fn foo(x: X<Unknown>) {}
654+
fn test1() {
655+
// Unknown might be `i32`, so we should not emit type mismatch here.
656+
foo(X(42));
657+
}
658+
fn test2() {
659+
foo(42);
660+
//^^ error: expected X<{unknown}>, found i32
661+
}
662+
"#,
663+
);
664+
}
665+
647666
#[test]
648667
fn evaluate_const_generics_in_types() {
649668
check_diagnostics(

0 commit comments

Comments
 (0)