mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
fix: disregard type variable expectation for if expressions
This commit is contained in:
parent
d022e0ec53
commit
db8c7523f8
2 changed files with 34 additions and 0 deletions
|
@ -85,6 +85,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
let ty = match &self.body[tgt_expr] {
|
let ty = match &self.body[tgt_expr] {
|
||||||
Expr::Missing => self.err_ty(),
|
Expr::Missing => self.err_ty(),
|
||||||
&Expr::If { condition, then_branch, else_branch } => {
|
&Expr::If { condition, then_branch, else_branch } => {
|
||||||
|
let expected = &expected.adjust_for_branches(&mut self.table);
|
||||||
self.infer_expr(
|
self.infer_expr(
|
||||||
condition,
|
condition,
|
||||||
&Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(Interner)),
|
&Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(Interner)),
|
||||||
|
|
|
@ -122,6 +122,23 @@ fn test() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_else_adjust_for_branches_discard_type_var() {
|
||||||
|
check_no_mismatches(
|
||||||
|
r#"
|
||||||
|
fn test() {
|
||||||
|
let f = || {
|
||||||
|
if true {
|
||||||
|
&""
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn match_first_coerce() {
|
fn match_first_coerce() {
|
||||||
check_no_mismatches(
|
check_no_mismatches(
|
||||||
|
@ -182,6 +199,22 @@ fn test() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn match_adjust_for_branches_discard_type_var() {
|
||||||
|
check_no_mismatches(
|
||||||
|
r#"
|
||||||
|
fn test() {
|
||||||
|
let f = || {
|
||||||
|
match 0i32 {
|
||||||
|
0i32 => &"",
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn return_coerce_unknown() {
|
fn return_coerce_unknown() {
|
||||||
check_types(
|
check_types(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue