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

Use ocx.normalize in report_projection_error #104903

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
18 changes: 18 additions & 0 deletions compiler/rustc_trait_selection/src/traits/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
self.register_infer_ok_obligations(infer_ok)
}

/// Makes `expected <: actual`.
pub fn eq_exp<T>(
&self,
cause: &ObligationCause<'tcx>,
param_env: ty::ParamEnv<'tcx>,
a_is_expected: bool,
a: T,
b: T,
) -> Result<(), TypeError<'tcx>>
where
T: ToTrace<'tcx>,
{
self.infcx
.at(cause, param_env)
.eq_exp(a_is_expected, a, b)
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
}

pub fn eq<T: ToTrace<'tcx>>(
&self,
cause: &ObligationCause<'tcx>,
Expand Down
51 changes: 30 additions & 21 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,32 +1577,26 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}

self.probe(|_| {
let mut err = error.err;
let mut values = None;
let ocx = ObligationCtxt::new_in_snapshot(self);

// try to find the mismatched types to report the error with.
//
// this can fail if the problem was higher-ranked, in which
// cause I have no idea for a good error message.
let bound_predicate = predicate.kind();
if let ty::PredicateKind::Clause(ty::Clause::Projection(data)) =
let (values, err) = if let ty::PredicateKind::Clause(ty::Clause::Projection(data)) =
bound_predicate.skip_binder()
{
let mut selcx = SelectionContext::new(self);
let data = self.replace_bound_vars_with_fresh_vars(
obligation.cause.span,
infer::LateBoundRegionConversionTime::HigherRankedType,
bound_predicate.rebind(data),
);
let mut obligations = vec![];
// FIXME(normalization): Change this to use `At::normalize`
let normalized_ty = super::normalize_projection_type(
&mut selcx,
let normalized_ty = ocx.normalize(
spastorino marked this conversation as resolved.
Show resolved Hide resolved
&obligation.cause,
obligation.param_env,
data.projection_ty,
obligation.cause.clone(),
0,
&mut obligations,
self.tcx
.mk_projection(data.projection_ty.item_def_id, data.projection_ty.substs),
);

debug!(?obligation.cause, ?obligation.param_env);
Expand All @@ -1618,19 +1612,34 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
| ObligationCauseCode::ObjectCastObligation(..)
| ObligationCauseCode::OpaqueType
);
if let Err(new_err) = self.at(&obligation.cause, obligation.param_env).eq_exp(
let expected_ty = data.term.ty().unwrap();

// constrain inference variables a bit more to nested obligations from normalize so
// we can have more helpful errors.
ocx.select_where_possible();

if let Err(new_err) = ocx.eq_exp(
spastorino marked this conversation as resolved.
Show resolved Hide resolved
&obligation.cause,
obligation.param_env,
is_normalized_ty_expected,
normalized_ty,
data.term,
expected_ty,
) {
values = Some((data, is_normalized_ty_expected, normalized_ty, data.term));
err = new_err;
(Some((data, is_normalized_ty_expected, normalized_ty, expected_ty)), new_err)
} else {
(None, error.err)
}
}
} else {
(None, error.err)
};

let msg = values
.and_then(|(predicate, _, normalized_ty, expected_ty)| {
self.maybe_detailed_projection_msg(predicate, normalized_ty, expected_ty)
self.maybe_detailed_projection_msg(
predicate,
normalized_ty.into(),
expected_ty.into(),
)
})
.unwrap_or_else(|| format!("type mismatch resolving `{}`", predicate));
let mut diag = struct_span_err!(self.tcx.sess, obligation.cause.span, E0271, "{msg}");
Expand Down Expand Up @@ -1672,11 +1681,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
&mut diag,
&obligation.cause,
secondary_span,
values.map(|(_, is_normalized_ty_expected, normalized_ty, term)| {
values.map(|(_, is_normalized_ty_expected, normalized_ty, expected_ty)| {
infer::ValuePairs::Terms(ExpectedFound::new(
is_normalized_ty_expected,
normalized_ty,
term,
normalized_ty.into(),
expected_ty.into(),
))
}),
err,
Expand Down