File tree Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Original file line number Diff line number Diff line change @@ -1164,7 +1164,9 @@ impl InferenceContext<'_> {
1164
1164
expected : & Expectation ,
1165
1165
) -> chalk_ir:: Ty < Interner > {
1166
1166
let elem_ty = match expected. to_option ( & mut self . table ) . as_ref ( ) . map ( |t| t. kind ( Interner ) ) {
1167
- Some ( TyKind :: Array ( st, _) | TyKind :: Slice ( st) ) => st. clone ( ) ,
1167
+ // Avoid using a type variable as the coerce_to type, as it may resolve
1168
+ // during the first coercion instead of being based on the common type.
1169
+ Some ( TyKind :: Array ( st, _) | TyKind :: Slice ( st) ) if !st. is_ty_var ( ) => st. clone ( ) ,
1168
1170
_ => self . table . new_type_var ( ) ,
1169
1171
} ;
1170
1172
Original file line number Diff line number Diff line change @@ -96,7 +96,7 @@ fn foo<T>(x: &[T]) -> &[T] { x }
96
96
fn test() {
97
97
let x = if true {
98
98
foo(&[1])
99
- // ^^^^ adjustments: Deref(None), Borrow(Ref('?8 , Not)), Pointer(Unsize)
99
+ // ^^^^ adjustments: Deref(None), Borrow(Ref('?7 , Not)), Pointer(Unsize)
100
100
} else {
101
101
&[1]
102
102
};
@@ -148,7 +148,7 @@ fn foo<T>(x: &[T]) -> &[T] { x }
148
148
fn test(i: i32) {
149
149
let x = match i {
150
150
2 => foo(&[2]),
151
- // ^^^^ adjustments: Deref(None), Borrow(Ref('?8 , Not)), Pointer(Unsize)
151
+ // ^^^^ adjustments: Deref(None), Borrow(Ref('?7 , Not)), Pointer(Unsize)
152
152
1 => &[1],
153
153
_ => &[3],
154
154
};
@@ -957,3 +957,35 @@ fn f() {
957
957
"# ,
958
958
) ;
959
959
}
960
+
961
+ #[ test]
962
+ fn coerce_array_elements ( ) {
963
+ check_no_mismatches (
964
+ r#"
965
+ // Check that least upper bound coercions don't resolve type variable merely based on the first
966
+ // coercion. Check issue rust-lang/rust#136420.
967
+
968
+ fn foo() {}
969
+ fn bar() {}
970
+
971
+ fn infer<T>(_: T) {}
972
+
973
+ fn infer_array_element<T>(_: [T; 2]) {}
974
+
975
+ fn main() {
976
+ infer(if false {
977
+ foo
978
+ } else {
979
+ bar
980
+ });
981
+
982
+ infer(match false {
983
+ true => foo,
984
+ false => bar,
985
+ });
986
+
987
+ infer_array_element([foo, bar]);
988
+ }
989
+ "# ,
990
+ ) ;
991
+ }
You can’t perform that action at this time.
0 commit comments