1
Fork 0

Improve cause information for NLL higher-ranked errors

This PR has several interconnected pieces:

1. In some of the NLL region error code, we now pass
   around an `ObligationCause`, instead of just a plain `Span`.
   This gets forwarded into `fulfill_cx.register_predicate_obligation`
   during error reporting.
2. The general InferCtxt error reporting code is extended to
   handle `ObligationCauseCode::BindingObligation`
3. A new enum variant `ConstraintCategory::Predicate` is added.
   We try to avoid using this as the 'best blame constraint' - instead,
   we use it to enhance the `ObligationCause` of the `BlameConstraint`
   that we do end up choosing.

As a result, several NLL error messages now contain the same
"the lifetime requirement is introduced here" message as non-NLL
errors.

Having an `ObligationCause` available will likely prove useful
for future improvements to NLL error messages.
This commit is contained in:
Aaron Hill 2021-08-28 18:45:37 -05:00
parent 3e8f32e1c5
commit 93ab12eeab
No known key found for this signature in database
GPG key ID: B4087E510E98B164
16 changed files with 194 additions and 82 deletions

View file

@ -19,7 +19,7 @@ mod normalize_erasing_regions;
mod normalize_projection_ty;
mod type_op;
pub use type_op::{type_op_ascribe_user_type_with_span, type_op_prove_predicate_with_span};
pub use type_op::{type_op_ascribe_user_type_with_span, type_op_prove_predicate_with_cause};
use rustc_middle::ty::query::Providers;

View file

@ -257,7 +257,7 @@ fn type_op_prove_predicate<'tcx>(
canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
type_op_prove_predicate_with_span(infcx, fulfill_cx, key, None);
type_op_prove_predicate_with_cause(infcx, fulfill_cx, key, ObligationCause::dummy());
Ok(())
})
}
@ -265,17 +265,12 @@ fn type_op_prove_predicate<'tcx>(
/// The core of the `type_op_prove_predicate` query: for diagnostics purposes in NLL HRTB errors,
/// this query can be re-run to better track the span of the obligation cause, and improve the error
/// message. Do not call directly unless you're in that very specific context.
pub fn type_op_prove_predicate_with_span<'a, 'tcx: 'a>(
pub fn type_op_prove_predicate_with_cause<'a, 'tcx: 'a>(
infcx: &'a InferCtxt<'a, 'tcx>,
fulfill_cx: &'a mut dyn TraitEngine<'tcx>,
key: ParamEnvAnd<'tcx, ProvePredicate<'tcx>>,
span: Option<Span>,
cause: ObligationCause<'tcx>,
) {
let cause = if let Some(span) = span {
ObligationCause::dummy_with_span(span)
} else {
ObligationCause::dummy()
};
let (param_env, ProvePredicate { predicate }) = key.into_parts();
fulfill_cx.register_predicate_obligation(infcx, Obligation::new(cause, param_env, predicate));
}