add test for correct await span
This commit is contained in:
parent
bc1595473b
commit
00d12ef901
5 changed files with 62 additions and 12 deletions
|
@ -314,8 +314,8 @@ pub struct GeneratorInteriorTypeCause<'tcx> {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
/// Span of the scope of the captured binding.
|
/// Span of the scope of the captured binding.
|
||||||
pub scope_span: Option<Span>,
|
pub scope_span: Option<Span>,
|
||||||
/// Span of `.await` statement.
|
/// Span of `.await` or `yield` expression.
|
||||||
pub await_span: Span,
|
pub yield_span: Span,
|
||||||
/// Expr which the type evaluated from.
|
/// Expr which the type evaluated from.
|
||||||
pub expr: Option<hir::HirId>,
|
pub expr: Option<hir::HirId>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1261,13 +1261,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
ty_matches(ty)
|
ty_matches(ty)
|
||||||
})
|
})
|
||||||
.map(|expr| expr.span);
|
.map(|expr| expr.span);
|
||||||
let ty::GeneratorInteriorTypeCause { span, scope_span, await_span, expr, .. } =
|
let ty::GeneratorInteriorTypeCause { span, scope_span, yield_span, expr, .. } =
|
||||||
cause;
|
cause;
|
||||||
(
|
(
|
||||||
span,
|
span,
|
||||||
source_map.span_to_snippet(*span),
|
source_map.span_to_snippet(*span),
|
||||||
scope_span,
|
scope_span,
|
||||||
await_span,
|
yield_span,
|
||||||
expr,
|
expr,
|
||||||
from_awaited_ty,
|
from_awaited_ty,
|
||||||
)
|
)
|
||||||
|
@ -1275,17 +1275,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"maybe_note_obligation_cause_for_async_await: target_ty={:?} \
|
"maybe_note_obligation_cause_for_async_await: target_ty={:?} \
|
||||||
generator_interior_types={:?} target_span={:?} await_span={:?}",
|
generator_interior_types={:?} target_span={:?}",
|
||||||
target_ty, tables.generator_interior_types, target_span, await_span
|
target_ty, tables.generator_interior_types, target_span
|
||||||
);
|
);
|
||||||
if let Some((target_span, Ok(snippet), scope_span, await_span, expr, from_awaited_ty)) =
|
if let Some((target_span, Ok(snippet), scope_span, yield_span, expr, from_awaited_ty)) =
|
||||||
target_span
|
target_span
|
||||||
{
|
{
|
||||||
self.note_obligation_cause_for_async_await(
|
self.note_obligation_cause_for_async_await(
|
||||||
err,
|
err,
|
||||||
*target_span,
|
*target_span,
|
||||||
scope_span,
|
scope_span,
|
||||||
await_span,
|
*yield_span,
|
||||||
*expr,
|
*expr,
|
||||||
snippet,
|
snippet,
|
||||||
generator_body,
|
generator_body,
|
||||||
|
@ -1310,7 +1310,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
err: &mut DiagnosticBuilder<'_>,
|
err: &mut DiagnosticBuilder<'_>,
|
||||||
target_span: Span,
|
target_span: Span,
|
||||||
scope_span: &Option<Span>,
|
scope_span: &Option<Span>,
|
||||||
await_span: Span,
|
yield_span: Span,
|
||||||
expr: Option<hir::HirId>,
|
expr: Option<hir::HirId>,
|
||||||
snippet: String,
|
snippet: String,
|
||||||
inner_generator_body: Option<&hir::Body<'_>>,
|
inner_generator_body: Option<&hir::Body<'_>>,
|
||||||
|
@ -1386,6 +1386,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
if let Some(await_span) = from_awaited_ty {
|
if let Some(await_span) = from_awaited_ty {
|
||||||
// The type causing this obligation is one being awaited at await_span.
|
// The type causing this obligation is one being awaited at await_span.
|
||||||
let mut span = MultiSpan::from_span(await_span);
|
let mut span = MultiSpan::from_span(await_span);
|
||||||
|
|
||||||
|
span.push_span_label(
|
||||||
|
await_span,
|
||||||
|
format!("await occurs here on type `{}`, which {}", target_ty, trait_explanation),
|
||||||
|
);
|
||||||
|
|
||||||
err.span_note(
|
err.span_note(
|
||||||
span,
|
span,
|
||||||
&format!(
|
&format!(
|
||||||
|
@ -1399,9 +1405,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
"note_obligation_cause_for_async_await generator_interior_types: {:#?}",
|
"note_obligation_cause_for_async_await generator_interior_types: {:#?}",
|
||||||
tables.generator_interior_types
|
tables.generator_interior_types
|
||||||
);
|
);
|
||||||
let mut span = MultiSpan::from_span(await_span);
|
let mut span = MultiSpan::from_span(yield_span);
|
||||||
span.push_span_label(
|
span.push_span_label(
|
||||||
await_span,
|
yield_span,
|
||||||
format!("{} occurs here, with `{}` maybe used later", await_or_yield, snippet),
|
format!("{} occurs here, with `{}` maybe used later", await_or_yield, snippet),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,7 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
|
||||||
span: source_span,
|
span: source_span,
|
||||||
ty: &ty,
|
ty: &ty,
|
||||||
scope_span,
|
scope_span,
|
||||||
await_span: yield_data.span,
|
yield_span: yield_data.span,
|
||||||
expr: expr.map(|e| e.hir_id),
|
expr: expr.map(|e| e.hir_id),
|
||||||
})
|
})
|
||||||
.or_insert(entries);
|
.or_insert(entries);
|
||||||
|
|
21
src/test/ui/async-await/issue-71137.rs
Normal file
21
src/test/ui/async-await/issue-71137.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// edition:2018
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
fn fake_spawn<F: Future + Send + 'static>(f: F) { }
|
||||||
|
|
||||||
|
async fn wrong_mutex() {
|
||||||
|
let m = Mutex::new(1);
|
||||||
|
{
|
||||||
|
let mut guard = m.lock().unwrap();
|
||||||
|
(async { "right"; }).await;
|
||||||
|
*guard += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
(async { "wrong"; }).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
fake_spawn(wrong_mutex()); //~ Error future cannot be sent between threads safely
|
||||||
|
}
|
23
src/test/ui/async-await/issue-71137.stderr
Normal file
23
src/test/ui/async-await/issue-71137.stderr
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
error: future cannot be sent between threads safely
|
||||||
|
--> $DIR/issue-71137.rs:20:3
|
||||||
|
|
|
||||||
|
LL | fn fake_spawn<F: Future + Send + 'static>(f: F) { }
|
||||||
|
| ---- required by this bound in `fake_spawn`
|
||||||
|
...
|
||||||
|
LL | fake_spawn(wrong_mutex());
|
||||||
|
| ^^^^^^^^^^ future returned by `wrong_mutex` is not `Send`
|
||||||
|
|
|
||||||
|
= help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, i32>`
|
||||||
|
note: future is not `Send` as this value is used across an await
|
||||||
|
--> $DIR/issue-71137.rs:12:5
|
||||||
|
|
|
||||||
|
LL | let mut guard = m.lock().unwrap();
|
||||||
|
| --------- has type `std::sync::MutexGuard<'_, i32>` which is not `Send`
|
||||||
|
LL | (async { "right"; }).await;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ await occurs here, with `mut guard` maybe used later
|
||||||
|
LL | *guard += 1;
|
||||||
|
LL | }
|
||||||
|
| - `mut guard` is later dropped here
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue