Auto merge of #17174 - Kohei316:fix-infer-async-block-with-tail-retur… · rust-lang/rust@6e8646d

File tree

2 files changed

lines changed

  • src/tools/rust-analyzer/crates

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -933,8 +933,24 @@ impl InferenceContext<'_> {

933933

let prev_ret_coercion =

934934

mem::replace(&mut self.return_coercion, Some(CoerceMany::new(ret_ty.clone())));

935935
936+

// FIXME: We should handle async blocks like we handle closures

937+

let expected = &Expectation::has_type(ret_ty);

936938

let (_, inner_ty) = self.with_breakable_ctx(BreakableKind::Border, None, None, |this| {

937-

this.infer_block(tgt_expr, *id, statements, *tail, None, &Expectation::has_type(ret_ty))

939+

let ty = this.infer_block(tgt_expr, *id, statements, *tail, None, expected);

940+

if let Some(target) = expected.only_has_type(&mut this.table) {

941+

match this.coerce(Some(tgt_expr), &ty, &target) {

942+

Ok(res) => res,

943+

Err(_) => {

944+

this.result.type_mismatches.insert(

945+

tgt_expr.into(),

946+

TypeMismatch { expected: target.clone(), actual: ty.clone() },

947+

);

948+

target

949+

}

950+

}

951+

} else {

952+

ty

953+

}

938954

});

939955
940956

self.diverges = prev_diverges;

Original file line numberDiff line numberDiff line change

@@ -1120,4 +1120,30 @@ fn test() {

11201120

"#,

11211121

);

11221122

}

1123+
1124+

#[test]

1125+

fn type_hints_async_block() {

1126+

check_types(

1127+

r#"

1128+

//- minicore: future

1129+

async fn main() {

1130+

let _x = async { 8_i32 };

1131+

//^^ impl Future<Output = i32>

1132+

}"#,

1133+

);

1134+

}

1135+
1136+

#[test]

1137+

fn type_hints_async_block_with_tail_return_exp() {

1138+

check_types(

1139+

r#"

1140+

//- minicore: future

1141+

async fn main() {

1142+

let _x = async {

1143+

//^^ impl Future<Output = i32>

1144+

return 8_i32;

1145+

};

1146+

}"#,

1147+

);

1148+

}

11231149

}