8000 Match up array coercion for rust-lang/rust#140283 by Veykril · Pull Request #19911 · rust-lang/rust-analyzer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Match up array coercion for rust-lang/rust#140283 #19911

8000 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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,9 @@ impl InferenceContext<'_> {
expected: &Expectation,
) -> chalk_ir::Ty<Interner> {
let elem_ty = match expected.to_option(&mut self.table).as_ref().map(|t| t.kind(Interner)) {
Some(TyKind::Array(st, _) | TyKind::Slice(st)) => st.clone(),
// Avoid using a type variable as the coerce_to type, as it may resolve
// during the first coercion instead of being based on the common type.
Some(TyKind::Array(st, _) | TyKind::Slice(st)) if !st.is_ty_var() => st.clone(),
_ => self.table.new_type_var(),
};

Expand Down
36 changes: 34 additions & 2 deletions crates/hir-ty/src/tests/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn foo<T>(x: &[T]) -> &[T] { x }
fn test() {
let x = if true {
foo(&[1])
// ^^^^ adjustments: Deref(None), Borrow(Ref('?8, Not)), Pointer(Unsize)
// ^^^^ adjustments: Deref(None), Borrow(Ref('?7, Not)), Pointer(Unsize)
} else {
&[1]
};
Expand Down Expand Up @@ -148,7 +148,7 @@ fn foo<T>(x: &[T]) -> &[T] { x }
fn test(i: i32) {
let x = match i {
2 => foo(&[2]),
// ^^^^ adjustments: Deref(None), Borrow(Ref('?8, Not)), Pointer(Unsize)
// ^^^^ adjustments: Deref(None), Borrow(Ref('?7, Not)), Pointer(Unsize)
1 => &[1],
_ => &[3],
};
Expand Down Expand Up @@ -957,3 +957,35 @@ fn f() {
"#,
);
}

#[test]
fn coerce_array_elements() {
check_no_mismatches(
r#"
// Check that least upper bound coercions don't resolve type variable merely based on the first
// coercion. Check issue rust-lang/rust#136420.

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

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

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

fn main() {
infer(if false {
foo
} else {
bar
});

infer(match false {
true => foo,
false => bar,
});

infer_array_element([foo, bar]);
}
"#,
);
}
Loading
0