Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct await span for async-await error reporting #71203

Merged
merged 2 commits into from
Apr 22, 2020
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
8 changes: 5 additions & 3 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,14 @@ pub struct ResolvedOpaqueTy<'tcx> {
///
/// ```ignore (pseudo-Rust)
/// async move {
/// let x: T = ...;
/// let x: T = expr;
/// foo.await
/// ...
/// }
/// ```
///
/// Here, we would store the type `T`, the span of the value `x`, and the "scope-span" for
/// the scope that contains `x`.
/// Here, we would store the type `T`, the span of the value `x`, the "scope-span" for
/// the scope that contains `x`, the expr `T` evaluated from, and the span of `foo.await`.
#[derive(RustcEncodable, RustcDecodable, Clone, Debug, Eq, Hash, PartialEq, HashStable)]
pub struct GeneratorInteriorTypeCause<'tcx> {
/// Type of the captured binding.
Expand All @@ -314,6 +314,8 @@ pub struct GeneratorInteriorTypeCause<'tcx> {
pub span: Span,
/// Span of the scope of the captured binding.
pub scope_span: Option<Span>,
/// Span of `.await` or `yield` expression.
pub yield_span: Span,
/// Expr which the type evaluated from.
pub expr: Option<hir::HirId>,
}
Expand Down
24 changes: 18 additions & 6 deletions src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub trait InferCtxtExt<'tcx> {
err: &mut DiagnosticBuilder<'_>,
target_span: Span,
scope_span: &Option<Span>,
await_span: Span,
expr: Option<hir::HirId>,
snippet: String,
inner_generator_body: Option<&hir::Body<'_>>,
Expand Down Expand Up @@ -1260,20 +1261,31 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
ty_matches(ty)
})
.map(|expr| expr.span);
let ty::GeneratorInteriorTypeCause { span, scope_span, expr, .. } = cause;
(span, source_map.span_to_snippet(*span), scope_span, expr, from_awaited_ty)
let ty::GeneratorInteriorTypeCause { span, scope_span, yield_span, expr, .. } =
cause;
(
span,
source_map.span_to_snippet(*span),
scope_span,
yield_span,
expr,
from_awaited_ty,
)
});

debug!(
"maybe_note_obligation_cause_for_async_await: target_ty={:?} \
generator_interior_types={:?} target_span={:?}",
target_ty, tables.generator_interior_types, target_span
);
if let Some((target_span, Ok(snippet), scope_span, expr, from_awaited_ty)) = target_span {
if let Some((target_span, Ok(snippet), scope_span, yield_span, expr, from_awaited_ty)) =
target_span
{
self.note_obligation_cause_for_async_await(
err,
*target_span,
scope_span,
*yield_span,
*expr,
snippet,
generator_body,
Expand All @@ -1298,6 +1310,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
err: &mut DiagnosticBuilder<'_>,
target_span: Span,
scope_span: &Option<Span>,
yield_span: Span,
expr: Option<hir::HirId>,
snippet: String,
inner_generator_body: Option<&hir::Body<'_>>,
Expand Down Expand Up @@ -1392,10 +1405,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
"note_obligation_cause_for_async_await generator_interior_types: {:#?}",
tables.generator_interior_types
);
let await_span = tables.generator_interior_types.iter().map(|t| t.span).last().unwrap();
let mut span = MultiSpan::from_span(await_span);
let mut span = MultiSpan::from_span(yield_span);
span.push_span_label(
await_span,
yield_span,
format!("{} occurs here, with `{}` maybe used later", await_or_yield, snippet),
);

Expand Down
1 change: 1 addition & 0 deletions src/librustc_typeck/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
span: source_span,
ty: &ty,
scope_span,
yield_span: yield_data.span,
expr: expr.map(|e| e.hir_id),
})
.or_insert(entries);
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/async-await/issue-71137.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions src/test/ui/async-await/issue-71137.stderr
Original file line number Diff line number Diff line change
@@ -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