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
2 changes: 1 addition & 1 deletion crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl<'a> InferenceContext<'a> {
}
}

/// Replaces Ty::Unknown by a new type var, so we can maybe still infer it.
/// Replaces `Ty::Error` by a new type var, so we can maybe still infer it.
fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
match ty.kind(Interner) {
TyKind::Error => self.table.new_type_var(),
Expand Down
17 changes: 13 additions & 4 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,20 @@ impl<'a> InferenceContext<'a> {
.1
}
Expr::TryBlock { body } => {
self.with_breakable_ctx(BreakableKind::Border, self.err_ty(), None, |this| {
let _inner = this.infer_expr(*body, expected);
// The type that is returned from the try block
let try_ty = self.table.new_type_var();
if let Some(ty) = expected.only_has_type(&mut self.table) {
self.unify(&try_ty, &ty);
}

// The ok-ish type that is expected from the last expression
let ok_ty = self.resolve_associated_type(try_ty.clone(), self.resolve_ops_try_ok());

self.with_breakable_ctx(BreakableKind::Block, ok_ty.clone(), None, |this| {
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that BreakableKind::Block is entirely correct, but as long as we do not check that Expr::Try is in a context where it can be used, this does not matter.

Copy link
Member

Choose a reason for hiding this comment

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

Ye I imagine we'd need to add a new variant here once we check whether Expr::Try is in a correct context (we did for a week with an implementation I did, but that caused too many problems due to some things missing).

this.infer_expr(*body, &Expectation::has_type(ok_ty));
});
// FIXME should be std::result::Result<{inner}, _>
self.err_ty()

try_ty
}
Expr::Async { body } => {
let ret_ty = self.table.new_type_var();
Expand Down
28 changes: 14 additions & 14 deletions crates/hir-ty/src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2064,17 +2064,17 @@ fn fn_pointer_return() {
fn block_modifiers_smoke_test() {
check_infer(
r#"
//- minicore: future
//- minicore: future, try
async fn main() {
let x = unsafe { 92 };
let y = async { async { () }.await };
let z = try { () };
let z: core::ops::ControlFlow<(), _> = try { () };
let w = const { 92 };
let t = 'a: { 92 };
}
"#,
expect![[r#"
16..162 '{ ...2 }; }': ()
16..193 '{ ...2 }; }': ()
26..27 'x': i32
30..43 'unsafe { 92 }': i32
30..43 'unsafe { 92 }': i32
Expand All @@ -2086,17 +2086,17 @@ async fn main() {
65..77 'async { () }': impl Future<Output = ()>
65..83 'async ....await': ()
73..75 '()': ()
95..96 'z': {unknown}
99..109 'try { () }': ()
99..109 'try { () }': {unknown}
105..107 '()': ()
119..120 'w': i32
123..135 'const { 92 }': i32
123..135 'const { 92 }': i32
131..133 '92': i32
145..146 't': i32
149..159 ''a: { 92 }': i32
155..157 '92': i32
95..96 'z': ControlFlow<(), ()>
130..140 'try { () }': ()
130..140 'try { () }': ControlFlow<(), ()>
136..138 '()': ()
150..151 'w': i32
154..166 'const { 92 }': i32
154..166 'const { 92 }': i32
162..164 '92': i32
176..177 't': i32
180..190 ''a: { 92 }': i32
186..188 '92': i32
"#]],
)
}
Expand Down
40 changes: 18 additions & 22 deletions crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ fn foo() {
}

#[test]
fn try_blocks_are_borders() {
fn async_blocks_are_borders() {
check_diagnostics(
r#"
fn foo() {
'a: loop {
try {
async {
break;
//^^^^^ error: break outside of loop
break 'a;
Expand All @@ -60,12 +60,12 @@ fn foo() {
}

#[test]
fn async_blocks_are_borders() {
fn closures_are_borders() {
check_diagnostics(
r#"
fn foo() {
'a: loop {
try {
|| {
break;
//^^^^^ error: break outside of loop
break 'a;
Expand All @@ -82,39 +82,35 @@ fn foo() {
}

#[test]
fn closures_are_borders() {
fn blocks_pass_through() {
check_diagnostics(
r#"
fn foo() {
'a: loop {
try {
break;
//^^^^^ error: break outside of loop
break 'a;
//^^^^^^^^ error: break outside of loop
continue;
//^^^^^^^^ error: continue outside of loop
continue 'a;
//^^^^^^^^^^^ error: continue outside of loop
};
{
break;
break 'a;
continue;
continue 'a;
}
}
}
"#,
);
}

#[test]
fn blocks_pass_through() {
fn try_blocks_pass_through() {
check_diagnostics(
r#"
fn foo() {
'a: loop {
{
break;
break 'a;
continue;
continue 'a;
}
try {
break;
break 'a;
continue;
continue 'a;
};
}
}
"#,
Expand Down