Skip to content

infer: update resolver when descending into block #7627

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

Merged
merged 4 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 14 additions & 15 deletions crates/hir_ty/src/diagnostics/match_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,20 @@ fn main(f: Foo) {
);
}

#[test]
fn internal_or() {
check_diagnostics(
r#"
fn main() {
enum Either { A(bool), B }
match Either::B {
//^^^^^^^^^ Missing match arm
Either::A(true | false) => (),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is what this test intends to check

Copy link
Member

@lnicola lnicola Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it checks that there's a "missing match arm" diagnostic because Either::B isn't handled. But the previous comment was indeed a bit strange. Is it actually working now?

}
}
"#,
);
}
mod false_negatives {
//! The implementation of match checking here is a work in progress. As we roll this out, we
//! prefer false negatives to false positives (ideally there would be no false positives). This
Expand All @@ -1518,21 +1532,6 @@ fn main() {
11..20 => (),
}
}
"#,
);
}

#[test]
fn internal_or() {
// We do not currently handle patterns with internal `or`s.
check_diagnostics(
r#"
fn main() {
enum Either { A(bool), B }
match Either::B {
Either::A(true | false) => (),
}
}
"#,
);
}
Expand Down
43 changes: 26 additions & 17 deletions crates/hir_ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,33 @@ impl<'a> InferenceContext<'a> {

self.coerce_merge_branch(&then_ty, &else_ty)
}
Expr::Block { statements, tail, label, id: _ } => match label {
Some(_) => {
let break_ty = self.table.new_type_var();
self.breakables.push(BreakableContext {
may_break: false,
break_ty: break_ty.clone(),
label: label.map(|label| self.body[label].name.clone()),
});
let ty = self.infer_block(statements, *tail, &Expectation::has_type(break_ty));
let ctxt = self.breakables.pop().expect("breakable stack broken");
if ctxt.may_break {
ctxt.break_ty
} else {
ty
Expr::Block { statements, tail, label, id: _ } => {
let old_resolver = mem::replace(
&mut self.resolver,
resolver_for_expr(self.db.upcast(), self.owner, tgt_expr),
);
let ty = match label {
Some(_) => {
let break_ty = self.table.new_type_var();
self.breakables.push(BreakableContext {
may_break: false,
break_ty: break_ty.clone(),
label: label.map(|label| self.body[label].name.clone()),
});
let ty =
self.infer_block(statements, *tail, &Expectation::has_type(break_ty));
let ctxt = self.breakables.pop().expect("breakable stack broken");
if ctxt.may_break {
ctxt.break_ty
} else {
ty
}
}
}
None => self.infer_block(statements, *tail, expected),
},
None => self.infer_block(statements, *tail, expected),
};
self.resolver = old_resolver;
ty
}
Expr::Unsafe { body } | Expr::Const { body } => self.infer_expr(*body, expected),
Expr::TryBlock { body } => {
let _inner = self.infer_expr(*body, expected);
Expand Down
22 changes: 22 additions & 0 deletions crates/hir_ty/src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,3 +2415,25 @@ fn infer_const_params() {
"#]],
);
}

#[test]
fn infer_inner_type() {
check_infer(
r#"
fn foo() {
struct S { field: u32 }
let s = S { field: 0 };
let f = s.field;
}
"#,
expect![[r#"
9..89 '{ ...eld; }': ()
47..48 's': S
51..65 'S { field: 0 }': S
62..63 '0': u32
75..76 'f': u32
79..80 's': S
79..86 's.field': u32
"#]],
);
}
1 change: 1 addition & 0 deletions crates/ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ fn main() {
let test = "test";
//^^^^ &str
let test = InnerStruct {};
//^^^^ InnerStruct

let test = unresolved();

Expand Down