Skip to content
Merged
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
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
47 changes: 47 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,50 @@ 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
"#]],
);
}

#[test]
fn infer_nested_inner_type() {
check_infer(
r#"
fn foo() {
{
let s = S { field: 0 };
let f = s.field;
}
struct S { field: u32 }
}
"#,
expect![[r#"
9..109 '{ ...32 } }': ()
15..79 '{ ... }': ()
29..30 's': S
33..47 'S { field: 0 }': S
44..45 '0': u32
61..62 'f': u32
65..66 's': S
65..72 's.field': u32
"#]],
);
}
51 changes: 51 additions & 0 deletions crates/hir_ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3151,3 +3151,54 @@ fn test() {
"#,
);
}

#[test]
fn inner_use() {
check_types(
r#"
mod m {
pub trait Tr {
fn method(&self) -> u8 { 0 }
}

impl Tr for () {}
}

fn f() {
use m::Tr;

().method();
//^^^^^^^^^^^ u8
}
"#,
);
}

#[test]
fn inner_use_in_block() {
check_types(
r#"
mod m {
pub trait Tr {
fn method(&self) -> u8 { 0 }
}

impl Tr for () {}
}

fn f() {
{
use m::Tr;

().method();
//^^^^^^^^^^^ u8
}

{
().method();
//^^^^^^^^^^^ {unknown}
}
}
"#,
);
}
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