Skip to content

Commit bc15954

Browse files
committed
record await span inside GeneratorInteriorTypeCause
1 parent 868b51b commit bc15954

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/librustc_middle/ty/context.rs

+5-3
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` statement.
318+
pub await_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

+18-12
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<'_>>,
@@ -1260,20 +1261,31 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
12601261
ty_matches(ty)
12611262
})
12621263
.map(|expr| expr.span);
1263-
let ty::GeneratorInteriorTypeCause { span, scope_span, expr, .. } = cause;
1264-
(span, source_map.span_to_snippet(*span), scope_span, expr, from_awaited_ty)
1264+
let ty::GeneratorInteriorTypeCause { span, scope_span, await_span, expr, .. } =
1265+
cause;
1266+
(
1267+
span,
1268+
source_map.span_to_snippet(*span),
1269+
scope_span,
1270+
await_span,
1271+
expr,
1272+
from_awaited_ty,
1273+
)
12651274
});
12661275

12671276
debug!(
12681277
"maybe_note_obligation_cause_for_async_await: target_ty={:?} \
1269-
generator_interior_types={:?} target_span={:?}",
1270-
target_ty, tables.generator_interior_types, target_span
1278+
generator_interior_types={:?} target_span={:?} await_span={:?}",
1279+
target_ty, tables.generator_interior_types, target_span, await_span
12711280
);
1272-
if let Some((target_span, Ok(snippet), scope_span, expr, from_awaited_ty)) = target_span {
1281+
if let Some((target_span, Ok(snippet), scope_span, await_span, expr, from_awaited_ty)) =
1282+
target_span
1283+
{
12731284
self.note_obligation_cause_for_async_await(
12741285
err,
12751286
*target_span,
12761287
scope_span,
1288+
await_span,
12771289
*expr,
12781290
snippet,
12791291
generator_body,
@@ -1298,6 +1310,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
12981310
err: &mut DiagnosticBuilder<'_>,
12991311
target_span: Span,
13001312
scope_span: &Option<Span>,
1313+
await_span: Span,
13011314
expr: Option<hir::HirId>,
13021315
snippet: String,
13031316
inner_generator_body: Option<&hir::Body<'_>>,
@@ -1373,12 +1386,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13731386
if let Some(await_span) = from_awaited_ty {
13741387
// The type causing this obligation is one being awaited at await_span.
13751388
let mut span = MultiSpan::from_span(await_span);
1376-
1377-
span.push_span_label(
1378-
await_span,
1379-
format!("await occurs here on type `{}`, which {}", target_ty, trait_explanation),
1380-
);
1381-
13821389
err.span_note(
13831390
span,
13841391
&format!(
@@ -1392,7 +1399,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13921399
"note_obligation_cause_for_async_await generator_interior_types: {:#?}",
13931400
tables.generator_interior_types
13941401
);
1395-
let await_span = tables.generator_interior_types.iter().map(|t| t.span).last().unwrap();
13961402
let mut span = MultiSpan::from_span(await_span);
13971403
span.push_span_label(
13981404
await_span,

src/librustc_typeck/check/generator_interior.rs

+1
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+
await_span: yield_data.span,
99100
expr: expr.map(|e| e.hir_id),
100101
})
101102
.or_insert(entries);

0 commit comments

Comments
 (0)