-
Notifications
You must be signed in to change notification settings - Fork 13.3k
refactor writeback: emit normalization errors with new solver #118751
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,16 @@ use rustc_errors::{ErrorGuaranteed, StashKey}; | |
use rustc_hir as hir; | ||
use rustc_hir::intravisit::{self, Visitor}; | ||
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282; | ||
use rustc_middle::traits::ObligationCause; | ||
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion}; | ||
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder}; | ||
use rustc_middle::ty::visit::TypeVisitableExt; | ||
use rustc_middle::ty::TypeSuperFoldable; | ||
use rustc_middle::ty::{self, Ty, TyCtxt}; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::Span; | ||
use rustc_trait_selection::solve; | ||
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt; | ||
|
||
use std::mem; | ||
|
||
|
@@ -695,24 +699,22 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { | |
} | ||
} | ||
|
||
fn resolve<T>(&mut self, x: T, span: &dyn Locatable) -> T | ||
fn resolve<T>(&mut self, value: T, span: &dyn Locatable) -> T | ||
where | ||
T: TypeFoldable<TyCtxt<'tcx>>, | ||
{ | ||
let mut resolver = Resolver::new(self.fcx, span, self.body); | ||
let x = x.fold_with(&mut resolver); | ||
if cfg!(debug_assertions) && x.has_infer() { | ||
span_bug!(span.to_span(self.fcx.tcx), "writeback: `{:?}` has inference variables", x); | ||
} | ||
let value = self.fcx.resolve_vars_if_possible(value); | ||
let value = value.fold_with(&mut Resolver::new(self.fcx, span, self.body)); | ||
assert!(!value.has_infer()); | ||
|
||
// We may have introduced e.g. `ty::Error`, if inference failed, make sure | ||
// to mark the `TypeckResults` as tainted in that case, so that downstream | ||
// users of the typeck results don't produce extra errors, or worse, ICEs. | ||
if let Some(e) = resolver.replaced_with_error { | ||
self.typeck_results.tainted_by_errors = Some(e); | ||
if let Err(guar) = value.error_reported() { | ||
self.typeck_results.tainted_by_errors = Some(guar); | ||
} | ||
|
||
x | ||
value | ||
} | ||
} | ||
|
||
|
@@ -732,15 +734,13 @@ impl Locatable for hir::HirId { | |
} | ||
} | ||
|
||
/// The Resolver. This is the type folding engine that detects | ||
/// unresolved types and so forth. | ||
struct Resolver<'cx, 'tcx> { | ||
fcx: &'cx FnCtxt<'cx, 'tcx>, | ||
span: &'cx dyn Locatable, | ||
body: &'tcx hir::Body<'tcx>, | ||
|
||
/// Set to `Some` if any `Ty` or `ty::Const` had to be replaced with an `Error`. | ||
replaced_with_error: Option<ErrorGuaranteed>, | ||
/// Whether we should normalize using the new solver, disabled | ||
/// both when using the old solver and when resolving predicates. | ||
should_normalize: bool, | ||
} | ||
|
||
impl<'cx, 'tcx> Resolver<'cx, 'tcx> { | ||
|
@@ -749,7 +749,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> { | |
span: &'cx dyn Locatable, | ||
body: &'tcx hir::Body<'tcx>, | ||
) -> Resolver<'cx, 'tcx> { | ||
Resolver { fcx, span, body, replaced_with_error: None } | ||
Resolver { fcx, span, body, should_normalize: fcx.next_trait_solver() } | ||
} | ||
|
||
fn report_error(&self, p: impl Into<ty::GenericArg<'tcx>>) -> ErrorGuaranteed { | ||
|
@@ -768,64 +768,71 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> { | |
.emit(), | ||
} | ||
} | ||
|
||
fn handle_term<T>( | ||
&mut self, | ||
value: T, | ||
outer_exclusive_binder: impl FnOnce(T) -> ty::DebruijnIndex, | ||
new_err: impl Fn(TyCtxt<'tcx>, ErrorGuaranteed) -> T, | ||
) -> T | ||
where | ||
T: Into<ty::GenericArg<'tcx>> + TypeSuperFoldable<TyCtxt<'tcx>> + Copy, | ||
{ | ||
let tcx = self.fcx.tcx; | ||
// We must deeply normalize in the new solver, since later lints | ||
// expect that types that show up in the typeck are fully | ||
// normalized. | ||
let value = if self.should_normalize { | ||
let body_id = tcx.hir().body_owner_def_id(self.body.id()); | ||
let cause = ObligationCause::misc(self.span.to_span(tcx), body_id); | ||
let at = self.fcx.at(&cause, self.fcx.param_env); | ||
let universes = vec![None; outer_exclusive_binder(value).as_usize()]; | ||
solve::deeply_normalize_with_skipped_universes(at, value, universes).unwrap_or_else( | ||
|errors| { | ||
let guar = self.fcx.err_ctxt().report_fulfillment_errors(errors); | ||
new_err(tcx, guar) | ||
}, | ||
) | ||
} else { | ||
value | ||
}; | ||
|
||
if value.has_non_region_infer() { | ||
let guar = self.report_error(value); | ||
new_err(tcx, guar) | ||
} else { | ||
tcx.fold_regions(value, |_, _| tcx.lifetimes.re_erased) | ||
} | ||
} | ||
} | ||
|
||
impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Resolver<'cx, 'tcx> { | ||
fn interner(&self) -> TyCtxt<'tcx> { | ||
self.fcx.tcx | ||
} | ||
|
||
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | ||
let tcx = self.fcx.tcx; | ||
match self.fcx.fully_resolve(t) { | ||
Ok(t) if self.fcx.next_trait_solver() => { | ||
// We must normalize erasing regions here, since later lints | ||
// expect that types that show up in the typeck are fully | ||
// normalized. | ||
if let Ok(t) = tcx.try_normalize_erasing_regions(self.fcx.param_env, t) { | ||
t | ||
} else { | ||
tcx.fold_regions(t, |_, _| tcx.lifetimes.re_erased) | ||
} | ||
} | ||
Ok(t) => { | ||
// Do not anonymize late-bound regions | ||
// (e.g. keep `for<'a>` named `for<'a>`). | ||
// This allows NLL to generate error messages that | ||
// refer to the higher-ranked lifetime names written by the user. | ||
tcx.fold_regions(t, |_, _| tcx.lifetimes.re_erased) | ||
} | ||
Err(_) => { | ||
debug!("Resolver::fold_ty: input type `{:?}` not fully resolvable", t); | ||
let e = self.report_error(t); | ||
self.replaced_with_error = Some(e); | ||
Ty::new_error(self.fcx.tcx, e) | ||
} | ||
} | ||
} | ||
|
||
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { | ||
debug_assert!(!r.is_bound(), "Should not be resolving bound region."); | ||
self.fcx.tcx.lifetimes.re_erased | ||
} | ||
|
||
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { | ||
self.handle_term(ty, Ty::outer_exclusive_binder, Ty::new_error) | ||
} | ||
|
||
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> { | ||
match self.fcx.fully_resolve(ct) { | ||
Ok(ct) => self.fcx.tcx.erase_regions(ct), | ||
Err(_) => { | ||
debug!("Resolver::fold_const: input const `{:?}` not fully resolvable", ct); | ||
let e = self.report_error(ct); | ||
self.replaced_with_error = Some(e); | ||
ty::Const::new_error(self.fcx.tcx, e, ct.ty()) | ||
} | ||
} | ||
self.handle_term(ct, ty::Const::outer_exclusive_binder, |tcx, guar| { | ||
ty::Const::new_error(tcx, guar, ct.ty()) | ||
}) | ||
} | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
// During type check, we store promises with the result of trait | ||
// lookup rather than the actual results (because the results are not | ||
// necessarily available immediately). These routines unwind the | ||
// promises. It is expected that we will have already reported any | ||
// errors that may be encountered, so if the promises store an error, | ||
// a dummy result is returned. | ||
fn fold_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When do we resolve predicates during writeback? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right |
||
// Do not normalize predicates in the new solver. The new solver is | ||
// supposed to handle unnormalized predicates and incorrectly normalizing | ||
// them can be unsound, e.g. for `WellFormed` predicates. | ||
let prev = mem::replace(&mut self.should_normalize, false); | ||
let predicate = predicate.super_fold_with(self); | ||
self.should_normalize = prev; | ||
predicate | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... seems a bit sketch. Idk if I have any better ideas other than this though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine.
We only did this to enable the use of
fully_resolve
in writeback to also resolve regions. But given that we resolve all regions to'erased
anyways, this is pretty pointless andresolve_vars_if_possible
is good enough.