Skip to content

Commit 7b1ce6e

Browse files
authored
Rollup merge of #71203 - csmoe:issue-71137, r=csmoe
Correct await span for async-await error reporting Closes #71137 r? @tmandry
2 parents 707004c + 00d12ef commit 7b1ce6e

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

src/librustc_middle/ty/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,14 @@ pub struct ResolvedOpaqueTy<'tcx> {
298298
///
299299
/// ```ignore (pseudo-Rust)
300300
/// async move {
301-
/// let x: T = ...;
301+
/// let x: T = expr;
302302
/// foo.await
303303
/// ...
304304
/// }
305305
/// ```
306306
///
307-
/// Here, we would store the type `T`, the span of the value `x`, and the "scope-span" for
308-
/// the scope that contains `x`.
307+
/// Here, we would store the type `T`, the span of the value `x`, the "scope-span" for
308+
/// the scope that contains `x`, the expr `T` evaluated from, and the span of `foo.await`.
309309
#[derive(RustcEncodable, RustcDecodable, Clone, Debug, Eq, Hash, PartialEq, HashStable)]
310310
pub struct GeneratorInteriorTypeCause<'tcx> {
311311
/// Type of the captured binding.
@@ -314,6 +314,8 @@ pub struct GeneratorInteriorTypeCause<'tcx> {
314314
pub span: Span,
315315
/// Span of the scope of the captured binding.
316316
pub scope_span: Option<Span>,
317+
/// Span of `.await` or `yield` expression.
318+
pub yield_span: Span,
317319
/// Expr which the type evaluated from.
318320
pub expr: Option<hir::HirId>,
319321
}

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub trait InferCtxtExt<'tcx> {
125125
err: &mut DiagnosticBuilder<'_>,
126126
target_span: Span,
127127
scope_span: &Option<Span>,
128+
await_span: Span,
128129
expr: Option<hir::HirId>,
129130
snippet: String,
130131
inner_generator_body: Option<&hir::Body<'_>>,
@@ -1289,20 +1290,31 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
12891290
ty_matches(ty)
12901291
})
12911292
.map(|expr| expr.span);
1292-
let ty::GeneratorInteriorTypeCause { span, scope_span, expr, .. } = cause;
1293-
(span, source_map.span_to_snippet(*span), scope_span, expr, from_awaited_ty)
1293+
let ty::GeneratorInteriorTypeCause { span, scope_span, yield_span, expr, .. } =
1294+
cause;
1295+
(
1296+
span,
1297+
source_map.span_to_snippet(*span),
1298+
scope_span,
1299+
yield_span,
1300+
expr,
1301+
from_awaited_ty,
1302+
)
12941303
});
12951304

12961305
debug!(
12971306
"maybe_note_obligation_cause_for_async_await: target_ty={:?} \
12981307
generator_interior_types={:?} target_span={:?}",
12991308
target_ty, tables.generator_interior_types, target_span
13001309
);
1301-
if let Some((target_span, Ok(snippet), scope_span, expr, from_awaited_ty)) = target_span {
1310+
if let Some((target_span, Ok(snippet), scope_span, yield_span, expr, from_awaited_ty)) =
1311+
target_span
1312+
{
13021313
self.note_obligation_cause_for_async_await(
13031314
err,
13041315
*target_span,
13051316
scope_span,
1317+
*yield_span,
13061318
*expr,
13071319
snippet,
13081320
generator_body,
@@ -1327,6 +1339,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13271339
err: &mut DiagnosticBuilder<'_>,
13281340
target_span: Span,
13291341
scope_span: &Option<Span>,
1342+
yield_span: Span,
13301343
expr: Option<hir::HirId>,
13311344
snippet: String,
13321345
inner_generator_body: Option<&hir::Body<'_>>,
@@ -1418,10 +1431,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14181431
"note_obligation_cause_for_async_await generator_interior_types: {:#?}",
14191432
tables.generator_interior_types
14201433
);
1421-
let await_span = tables.generator_interior_types.iter().map(|t| t.span).last().unwrap();
1422-
let mut span = MultiSpan::from_span(await_span);
1434+
let mut span = MultiSpan::from_span(yield_span);
14231435
span.push_span_label(
1424-
await_span,
1436+
yield_span,
14251437
format!("{} occurs here, with `{}` maybe used later", await_or_yield, snippet),
14261438
);
14271439

src/librustc_typeck/check/generator_interior.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
9696
span: source_span,
9797
ty: &ty,
9898
scope_span,
99+
yield_span: yield_data.span,
99100
expr: expr.map(|e| e.hir_id),
100101
})
101102
.or_insert(entries);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// edition:2018
2+
3+
use std::future::Future;
4+
use std::sync::Mutex;
5+
6+
fn fake_spawn<F: Future + Send + 'static>(f: F) { }
7+
8+
async fn wrong_mutex() {
9+
let m = Mutex::new(1);
10+
{
11+
let mut guard = m.lock().unwrap();
12+
(async { "right"; }).await;
13+
*guard += 1;
14+
}
15+
16+
(async { "wrong"; }).await;
17+
}
18+
19+
fn main() {
20+
fake_spawn(wrong_mutex()); //~ Error future cannot be sent between threads safely
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: future cannot be sent between threads safely
2+
--> $DIR/issue-71137.rs:20:3
3+
|
4+
LL | fn fake_spawn<F: Future + Send + 'static>(f: F) { }
5+
| ---- required by this bound in `fake_spawn`
6+
...
7+
LL | fake_spawn(wrong_mutex());
8+
| ^^^^^^^^^^ future returned by `wrong_mutex` is not `Send`
9+
|
10+
= help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, i32>`
11+
note: future is not `Send` as this value is used across an await
12+
--> $DIR/issue-71137.rs:12:5
13+
|
14+
LL | let mut guard = m.lock().unwrap();
15+
| --------- has type `std::sync::MutexGuard<'_, i32>` which is not `Send`
16+
LL | (async { "right"; }).await;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here, with `mut guard` maybe used later
18+
LL | *guard += 1;
19+
LL | }
20+
| - `mut guard` is later dropped here
21+
22+
error: aborting due to previous error
23+

0 commit comments

Comments
 (0)