Skip to content

Commit 3a05db6

Browse files
committed
Simplify suggestions for errors in generators.
1 parent 825f8ed commit 3a05db6

File tree

1 file changed

+42
-51
lines changed
  • compiler/rustc_trait_selection/src/traits/error_reporting

1 file changed

+42
-51
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+42-51
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
4444
#[derive(Debug)]
4545
pub enum GeneratorInteriorOrUpvar {
4646
// span of interior type
47-
Interior(Span),
47+
Interior(Span, Option<(Option<Span>, Span, Option<hir::HirId>, Option<Span>)>),
4848
// span of upvar
4949
Upvar(Span),
5050
}
@@ -283,7 +283,6 @@ pub trait TypeErrCtxtExt<'tcx> {
283283
&self,
284284
err: &mut Diagnostic,
285285
interior_or_upvar_span: GeneratorInteriorOrUpvar,
286-
interior_extra_info: Option<(Option<Span>, Span, Option<hir::HirId>, Option<Span>)>,
287286
is_async: bool,
288287
outer_generator: Option<DefId>,
289288
trait_pred: ty::TraitPredicate<'tcx>,
@@ -2003,17 +2002,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20032002
.as_local()
20042003
.and_then(|def_id| hir.maybe_body_owned_by(def_id))
20052004
.map(|body_id| hir.body(body_id));
2006-
let is_async = match generator_did.as_local() {
2007-
Some(_) => generator_body
2008-
.and_then(|body| body.generator_kind())
2009-
.map(|generator_kind| matches!(generator_kind, hir::GeneratorKind::Async(..)))
2010-
.unwrap_or(false),
2011-
None => self
2012-
.tcx
2013-
.generator_kind(generator_did)
2014-
.map(|generator_kind| matches!(generator_kind, hir::GeneratorKind::Async(..)))
2015-
.unwrap_or(false),
2016-
};
2005+
let is_async = self
2006+
.tcx
2007+
.generator_kind(generator_did)
2008+
.map(|generator_kind| matches!(generator_kind, hir::GeneratorKind::Async(..)))
2009+
.unwrap_or(false);
20172010
let mut visitor = AwaitsVisitor::default();
20182011
if let Some(body) = generator_body {
20192012
visitor.visit_body(body);
@@ -2043,61 +2036,60 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20432036
eq
20442037
};
20452038

2046-
let mut interior_or_upvar_span = None;
2047-
let mut interior_extra_info = None;
2048-
20492039
// Get the typeck results from the infcx if the generator is the function we are currently
20502040
// type-checking; otherwise, get them by performing a query. This is needed to avoid
20512041
// cycles. If we can't use resolved types because the generator comes from another crate,
20522042
// we still provide a targeted error but without all the relevant spans.
2053-
let generator_data: Option<GeneratorData<'tcx, '_>> = match &self.typeck_results {
2054-
Some(t) if t.hir_owner.to_def_id() == generator_did_root => {
2055-
Some(GeneratorData::Local(&t))
2056-
}
2043+
let generator_data = match &self.typeck_results {
2044+
Some(t) if t.hir_owner.to_def_id() == generator_did_root => GeneratorData::Local(&t),
20572045
_ if generator_did.is_local() => {
2058-
Some(GeneratorData::Local(self.tcx.typeck(generator_did.expect_local())))
2046+
GeneratorData::Local(self.tcx.typeck(generator_did.expect_local()))
20592047
}
2060-
_ => self
2061-
.tcx
2062-
.generator_diagnostic_data(generator_did)
2063-
.as_ref()
2064-
.map(|generator_diag_data| GeneratorData::Foreign(generator_diag_data)),
2048+
_ if let Some(generator_diag_data) = self.tcx.generator_diagnostic_data(generator_did) => {
2049+
GeneratorData::Foreign(generator_diag_data)
2050+
}
2051+
_ => return false,
20652052
};
20662053

2067-
if let Some(generator_data) = generator_data.as_ref() {
2068-
interior_or_upvar_span =
2069-
generator_data.try_get_upvar_span(&self, generator_did, ty_matches);
2054+
let mut interior_or_upvar_span = None;
20702055

2071-
// The generator interior types share the same binders
2072-
if let Some(cause) =
2073-
generator_data.get_generator_interior_types().skip_binder().iter().find(
2074-
|ty::GeneratorInteriorTypeCause { ty, .. }| {
2075-
ty_matches(generator_data.get_generator_interior_types().rebind(*ty))
2076-
},
2077-
)
2078-
{
2079-
let from_awaited_ty = generator_data.get_from_await_ty(visitor, hir, ty_matches);
2080-
let ty::GeneratorInteriorTypeCause { span, scope_span, yield_span, expr, .. } =
2081-
cause;
2056+
let from_awaited_ty = generator_data.get_from_await_ty(visitor, hir, ty_matches);
2057+
debug!(?from_awaited_ty);
20822058

2083-
interior_or_upvar_span = Some(GeneratorInteriorOrUpvar::Interior(*span));
2084-
interior_extra_info = Some((*scope_span, *yield_span, *expr, from_awaited_ty));
2085-
}
2059+
// The generator interior types share the same binders
2060+
if let Some(cause) =
2061+
generator_data.get_generator_interior_types().skip_binder().iter().find(
2062+
|ty::GeneratorInteriorTypeCause { ty, .. }| {
2063+
ty_matches(generator_data.get_generator_interior_types().rebind(*ty))
2064+
},
2065+
)
2066+
{
2067+
let ty::GeneratorInteriorTypeCause { span, scope_span, yield_span, expr, .. } = cause;
20862068

2087-
if interior_or_upvar_span.is_none() && generator_data.is_foreign() {
2088-
interior_or_upvar_span = Some(GeneratorInteriorOrUpvar::Interior(span));
2089-
}
2069+
interior_or_upvar_span = Some(GeneratorInteriorOrUpvar::Interior(
2070+
*span,
2071+
Some((*scope_span, *yield_span, *expr, from_awaited_ty)),
2072+
));
20902073
}
20912074

2075+
if interior_or_upvar_span.is_none() {
2076+
interior_or_upvar_span =
2077+
generator_data.try_get_upvar_span(&self, generator_did, ty_matches);
2078+
}
2079+
2080+
if interior_or_upvar_span.is_none() && generator_data.is_foreign() {
2081+
interior_or_upvar_span = Some(GeneratorInteriorOrUpvar::Interior(span, None));
2082+
}
2083+
2084+
debug!(?interior_or_upvar_span);
20922085
if let Some(interior_or_upvar_span) = interior_or_upvar_span {
2093-
let typeck_results = generator_data.and_then(|generator_data| match generator_data {
2086+
let typeck_results = match generator_data {
20942087
GeneratorData::Local(typeck_results) => Some(typeck_results),
20952088
GeneratorData::Foreign(_) => None,
2096-
});
2089+
};
20972090
self.note_obligation_cause_for_async_await(
20982091
err,
20992092
interior_or_upvar_span,
2100-
interior_extra_info,
21012093
is_async,
21022094
outer_generator,
21032095
trait_ref,
@@ -2119,7 +2111,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21192111
&self,
21202112
err: &mut Diagnostic,
21212113
interior_or_upvar_span: GeneratorInteriorOrUpvar,
2122-
interior_extra_info: Option<(Option<Span>, Span, Option<hir::HirId>, Option<Span>)>,
21232114
is_async: bool,
21242115
outer_generator: Option<DefId>,
21252116
trait_pred: ty::TraitPredicate<'tcx>,
@@ -2241,7 +2232,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
22412232
}
22422233
};
22432234
match interior_or_upvar_span {
2244-
GeneratorInteriorOrUpvar::Interior(interior_span) => {
2235+
GeneratorInteriorOrUpvar::Interior(interior_span, interior_extra_info) => {
22452236
if let Some((scope_span, yield_span, expr, from_awaited_ty)) = interior_extra_info {
22462237
if let Some(await_span) = from_awaited_ty {
22472238
// The type causing this obligation is one being awaited at await_span.

0 commit comments

Comments
 (0)