Skip to content

Fix accidental type inference in array coercion #140283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1817,12 +1817,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let element_ty = if !args.is_empty() {
let coerce_to = expected
.to_option(self)
.and_then(|uty| self.try_structurally_resolve_type(expr.span, uty).builtin_index())
.and_then(|uty| {
self.try_structurally_resolve_type(expr.span, uty)
.builtin_index()
// Avoid using the original type variable as the coerce_to type, as it may resolve
// during the first coercion instead of being the LUB type.
.filter(|t| !self.try_structurally_resolve_type(expr.span, *t).is_ty_var())
})
.unwrap_or_else(|| self.next_ty_var(expr.span));
let element_expected = Expectation::ExpectHasType(coerce_to)
.try_structurally_resolve_and_adjust_for_branches(self, expr.span);
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
assert_eq!(self.diverges.get(), Diverges::Maybe);
for e in args {
let e_ty = self.check_expr_with_hint(e, coerce_to);
let e_ty = self.check_expr_with_expectation(e, element_expected);
let cause = self.misc(e.span);
coerce.coerce(self, &cause, e, e_ty);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/ui/coercion/coerce-many-with-ty-var.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//@ run-pass
// Check that least upper bound coercions don't resolve type variable merely based on the first
// coercion. Check issue #136420.

fn foo() {}
fn bar() {}

fn infer<T>(_: T) {}

fn infer_array_element<T>(_: [T; 2]) {}

fn main() {
// Previously the element type's ty var will be unified with `foo`.
let _: [_; 2] = [foo, bar];
infer_array_element([foo, bar]);
let _ = [foo, if false { bar } else { foo }];

let _ = if false {
foo
} else {
bar
};
infer(if false {
foo
} else {
bar
});
let _ = if false {
foo
} else {
if false {
bar
} else {
foo
}
};


let _ = match false {
true => foo,
false => bar,
};
infer(match false {
true => foo,
false => bar,
});
let _ = match 1 {
2 => foo,
_ => if false {
bar
} else {
foo
},
};
}
3 changes: 0 additions & 3 deletions tests/ui/coercion/coerce-unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,4 @@ pub fn main() {
let a = [1, 2, 3];
let v = vec![1, 2, 3];
check2!(&a[..], &v);

// Make sure in-array coercion still works.
let _ = [("a", Default::default()), (Default::default(), "b"), (&s, &s)];
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ error[E0308]: mismatched types
--> $DIR/typo-in-repeat-expr-issue-80173.rs:57:23
|
LL | let k = vec!['c', 10];
| ^^ expected `char`, found `u8`
| ^^ expected `char`, found integer
|
help: replace the comma with a semicolon to create a vector
|
Expand Down
Loading