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

Rollup of 6 pull requests #133377

Merged
merged 16 commits into from
Nov 23, 2024
Merged
Changes from 2 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
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
@@ -729,7 +729,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let can_coerce = self.may_coerce(arg_ty, coerced_ty);
if !can_coerce {
return Compatibility::Incompatible(Some(ty::error::TypeError::Sorts(
ty::error::ExpectedFound::new(true, coerced_ty, arg_ty),
ty::error::ExpectedFound::new(coerced_ty, arg_ty),
)));
}

@@ -758,7 +758,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
expected_ty
};
TypeTrace::types(&self.misc(span), true, mismatched_ty, provided_ty)
TypeTrace::types(&self.misc(span), mismatched_ty, provided_ty)
};

// The algorithm here is inspired by levenshtein distance and longest common subsequence.
42 changes: 13 additions & 29 deletions compiler/rustc_infer/src/infer/at.rs
Original file line number Diff line number Diff line change
@@ -308,25 +308,22 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into())),
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
}
}
}

impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Regions(ExpectedFound::new(true, a, b)),
}
TypeTrace { cause: cause.clone(), values: ValuePairs::Regions(ExpectedFound::new(a, b)) }
}
}

impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into())),
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
}
}
}
@@ -337,13 +334,13 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
cause: cause.clone(),
values: match (a.unpack(), b.unpack()) {
(GenericArgKind::Lifetime(a), GenericArgKind::Lifetime(b)) => {
ValuePairs::Regions(ExpectedFound::new(true, a, b))
ValuePairs::Regions(ExpectedFound::new(a, b))
}
(GenericArgKind::Type(a), GenericArgKind::Type(b)) => {
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
}
(GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
}
_ => bug!("relating different kinds: {a:?} {b:?}"),
},
@@ -353,37 +350,28 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {

impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Terms(ExpectedFound::new(true, a, b)),
}
TypeTrace { cause: cause.clone(), values: ValuePairs::Terms(ExpectedFound::new(a, b)) }
}
}

impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::TraitRefs(ExpectedFound::new(true, a, b)),
}
TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
}
}

impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Aliases(ExpectedFound::new(true, a.into(), b.into())),
values: ValuePairs::Aliases(ExpectedFound::new(a.into(), b.into())),
}
}
}

impl<'tcx> ToTrace<'tcx> for ty::AliasTerm<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Aliases(ExpectedFound::new(true, a, b)),
}
TypeTrace { cause: cause.clone(), values: ValuePairs::Aliases(ExpectedFound::new(a, b)) }
}
}

@@ -392,7 +380,6 @@ impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::PolySigs(ExpectedFound::new(
true,
ty::Binder::dummy(a),
ty::Binder::dummy(b),
)),
@@ -402,18 +389,15 @@ impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {

impl<'tcx> ToTrace<'tcx> for ty::PolyFnSig<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::PolySigs(ExpectedFound::new(true, a, b)),
}
TypeTrace { cause: cause.clone(), values: ValuePairs::PolySigs(ExpectedFound::new(a, b)) }
}
}

impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialTraitRef<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(true, a, b)),
values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(a, b)),
}
}
}
@@ -422,7 +406,7 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialProjection<'tcx> {
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::ExistentialProjection(ExpectedFound::new(true, a, b)),
values: ValuePairs::ExistentialProjection(ExpectedFound::new(a, b)),
}
}
}
18 changes: 4 additions & 14 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
@@ -1478,39 +1478,29 @@ impl<'tcx> TypeTrace<'tcx> {
self.cause.span
}

pub fn types(
cause: &ObligationCause<'tcx>,
a_is_expected: bool,
a: Ty<'tcx>,
b: Ty<'tcx>,
) -> TypeTrace<'tcx> {
pub fn types(cause: &ObligationCause<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
}
}

pub fn trait_refs(
cause: &ObligationCause<'tcx>,
a_is_expected: bool,
a: ty::TraitRef<'tcx>,
b: ty::TraitRef<'tcx>,
) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::TraitRefs(ExpectedFound::new(a_is_expected, a, b)),
}
TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
}

pub fn consts(
cause: &ObligationCause<'tcx>,
a_is_expected: bool,
a: ty::Const<'tcx>,
b: ty::Const<'tcx>,
) -> TypeTrace<'tcx> {
TypeTrace {
cause: cause.clone(),
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
}
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
@@ -177,7 +177,7 @@ impl<'tcx> InferCtxt<'tcx> {
res
} else {
let (a, b) = self.resolve_vars_if_possible((a, b));
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
Err(TypeError::Sorts(ExpectedFound::new(a, b)))
}
}

4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ impl<'tcx> Relate<TyCtxt<'tcx>> for &'tcx ty::List<ty::PolyExistentialPredicate<
b_v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
b_v.dedup();
if a_v.len() != b_v.len() {
return Err(TypeError::ExistentialMismatch(ExpectedFound::new(true, a, b)));
return Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b)));
}

let v = iter::zip(a_v, b_v).map(|(ep_a, ep_b)| {
@@ -112,7 +112,7 @@ impl<'tcx> Relate<TyCtxt<'tcx>> for &'tcx ty::List<ty::PolyExistentialPredicate<
ty::ExistentialPredicate::AutoTrait(a),
ty::ExistentialPredicate::AutoTrait(b),
) if a == b => Ok(ep_a.rebind(ty::ExistentialPredicate::AutoTrait(a))),
_ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(true, a, b))),
_ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b))),
}
});
tcx.mk_poly_existential_predicates_from_iter(v)
Original file line number Diff line number Diff line change
@@ -152,7 +152,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
err: TypeError<'tcx>,
) -> Diag<'a> {
self.report_and_explain_type_error(
TypeTrace::types(cause, true, expected, actual),
TypeTrace::types(cause, expected, actual),
param_env,
err,
)
@@ -167,7 +167,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
err: TypeError<'tcx>,
) -> Diag<'a> {
self.report_and_explain_type_error(
TypeTrace::consts(cause, true, expected, actual),
TypeTrace::consts(cause, expected, actual),
param_env,
err,
)
Original file line number Diff line number Diff line change
@@ -725,7 +725,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&obligation.cause,
None,
None,
TypeError::Sorts(ty::error::ExpectedFound::new(true, expected_ty, ct_ty)),
TypeError::Sorts(ty::error::ExpectedFound::new(expected_ty, ct_ty)),
false,
);
diag
@@ -1449,7 +1449,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
secondary_span,
values.map(|(_, normalized_ty, expected_ty)| {
obligation.param_env.and(infer::ValuePairs::Terms(ExpectedFound::new(
true,
expected_ty,
normalized_ty,
)))
@@ -2755,7 +2754,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
(obligation.cause.clone(), terr)
};
self.report_and_explain_type_error(
TypeTrace::trait_refs(&cause, true, expected_trait_ref, found_trait_ref),
TypeTrace::trait_refs(&cause, expected_trait_ref, found_trait_ref),
obligation.param_env,
terr,
)
@@ -2846,7 +2845,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if Some(expected_trait_ref.def_id) != self.tcx.lang_items().coroutine_trait() && not_tupled
{
return Ok(self.report_and_explain_type_error(
TypeTrace::trait_refs(&obligation.cause, true, expected_trait_ref, found_trait_ref),
TypeTrace::trait_refs(&obligation.cause, expected_trait_ref, found_trait_ref),
obligation.param_env,
ty::error::TypeError::Mismatch,
));
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/solve/fulfill.rs
Original file line number Diff line number Diff line change
@@ -264,14 +264,14 @@ fn fulfillment_error_for_no_solution<'tcx>(
let (a, b) = infcx.enter_forall_and_leak_universe(
obligation.predicate.kind().rebind((pred.a, pred.b)),
);
let expected_found = ExpectedFound::new(true, a, b);
let expected_found = ExpectedFound::new(a, b);
FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
}
ty::PredicateKind::Coerce(pred) => {
let (a, b) = infcx.enter_forall_and_leak_universe(
obligation.predicate.kind().rebind((pred.a, pred.b)),
);
let expected_found = ExpectedFound::new(false, a, b);
let expected_found = ExpectedFound::new(b, a);
FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
}
ty::PredicateKind::Clause(_)
13 changes: 8 additions & 5 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
@@ -557,8 +557,11 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
ProcessResult::Changed(mk_pending(ok.obligations))
}
Ok(Err(err)) => {
let expected_found =
ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
let expected_found = if subtype.a_is_expected {
ExpectedFound::new(subtype.a, subtype.b)
} else {
ExpectedFound::new(subtype.b, subtype.a)
};
ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err))
}
}
@@ -578,7 +581,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
}
Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
Ok(Err(err)) => {
let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
let expected_found = ExpectedFound::new(coerce.b, coerce.a);
ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err))
}
}
@@ -703,7 +706,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
}
Err(err) => {
ProcessResult::Error(FulfillmentErrorCode::ConstEquate(
ExpectedFound::new(true, c1, c2),
ExpectedFound::new(c1, c2),
err,
))
}
@@ -727,7 +730,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
ProcessResult::Unchanged
} else {
// Two different constants using generic parameters ~> error.
let expected_found = ExpectedFound::new(true, c1, c2);
let expected_found = ExpectedFound::new(c1, c2);
ProcessResult::Error(FulfillmentErrorCode::ConstEquate(
expected_found,
TypeError::ConstMismatch(expected_found),
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/traits/select/_match.rs
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstFreshVars<'tcx> {
) => Ok(a),

(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
Err(TypeError::Sorts(ExpectedFound::new(a, b)))
}

(&ty::Error(guar), _) | (_, &ty::Error(guar)) => Ok(Ty::new_error(self.cx(), guar)),
@@ -95,7 +95,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstFreshVars<'tcx> {
}

(ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
return Err(TypeError::ConstMismatch(ExpectedFound::new(true, a, b)));
return Err(TypeError::ConstMismatch(ExpectedFound::new(a, b)));
}

_ => {}
8 changes: 2 additions & 6 deletions compiler/rustc_type_ir/src/error.rs
Original file line number Diff line number Diff line change
@@ -12,12 +12,8 @@ pub struct ExpectedFound<T> {
}

impl<T> ExpectedFound<T> {
pub fn new(a_is_expected: bool, a: T, b: T) -> Self {
if a_is_expected {
ExpectedFound { expected: a, found: b }
} else {
ExpectedFound { expected: b, found: a }
}
pub fn new(expected: T, found: T) -> Self {
ExpectedFound { expected, found }
}
}

34 changes: 16 additions & 18 deletions compiler/rustc_type_ir/src/relate.rs
Original file line number Diff line number Diff line change
@@ -171,16 +171,16 @@ impl<I: Interner> Relate<I> for ty::FnSig<I> {
return Err(TypeError::VariadicMismatch({
let a = a.c_variadic;
let b = b.c_variadic;
ExpectedFound::new(true, a, b)
ExpectedFound::new(a, b)
}));
}

if a.safety != b.safety {
return Err(TypeError::SafetyMismatch(ExpectedFound::new(true, a.safety, b.safety)));
return Err(TypeError::SafetyMismatch(ExpectedFound::new(a.safety, b.safety)));
}

if a.abi != b.abi {
return Err(TypeError::AbiMismatch(ExpectedFound::new(true, a.abi, b.abi)));
return Err(TypeError::AbiMismatch(ExpectedFound::new(a.abi, b.abi)));
};

let a_inputs = a.inputs();
@@ -233,7 +233,7 @@ impl<I: Interner> Relate<I> for ty::AliasTy<I> {
Err(TypeError::ProjectionMismatched({
let a = a.def_id;
let b = b.def_id;
ExpectedFound::new(true, a, b)
ExpectedFound::new(a, b)
}))
} else {
let args = match a.kind(relation.cx()) {
@@ -274,7 +274,7 @@ impl<I: Interner> Relate<I> for ty::AliasTerm<I> {
Err(TypeError::ProjectionMismatched({
let a = a.def_id;
let b = b.def_id;
ExpectedFound::new(true, a, b)
ExpectedFound::new(a, b)
}))
} else {
let args = match a.kind(relation.cx()) {
@@ -309,7 +309,7 @@ impl<I: Interner> Relate<I> for ty::ExistentialProjection<I> {
Err(TypeError::ProjectionMismatched({
let a = a.def_id;
let b = b.def_id;
ExpectedFound::new(true, a, b)
ExpectedFound::new(a, b)
}))
} else {
let term = relation.relate_with_variance(
@@ -340,7 +340,7 @@ impl<I: Interner> Relate<I> for ty::TraitRef<I> {
Err(TypeError::Traits({
let a = a.def_id;
let b = b.def_id;
ExpectedFound::new(true, a, b)
ExpectedFound::new(a, b)
}))
} else {
let args = relate_args_invariantly(relation, a.args, b.args)?;
@@ -360,7 +360,7 @@ impl<I: Interner> Relate<I> for ty::ExistentialTraitRef<I> {
Err(TypeError::Traits({
let a = a.def_id;
let b = b.def_id;
ExpectedFound::new(true, a, b)
ExpectedFound::new(a, b)
}))
} else {
let args = relate_args_invariantly(relation, a.args, b.args)?;
@@ -508,9 +508,9 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
let sz_b = sz_b.try_to_target_usize(cx);

match (sz_a, sz_b) {
(Some(sz_a_val), Some(sz_b_val)) if sz_a_val != sz_b_val => Err(
TypeError::FixedArraySize(ExpectedFound::new(true, sz_a_val, sz_b_val)),
),
(Some(sz_a_val), Some(sz_b_val)) if sz_a_val != sz_b_val => {
Err(TypeError::FixedArraySize(ExpectedFound::new(sz_a_val, sz_b_val)))
}
_ => Err(err),
}
}
@@ -529,9 +529,9 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
iter::zip(as_.iter(), bs.iter()).map(|(a, b)| relation.relate(a, b)),
)?)
} else if !(as_.is_empty() || bs.is_empty()) {
Err(TypeError::TupleSize(ExpectedFound::new(true, as_.len(), bs.len())))
Err(TypeError::TupleSize(ExpectedFound::new(as_.len(), bs.len())))
} else {
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
Err(TypeError::Sorts(ExpectedFound::new(a, b)))
}
}

@@ -558,7 +558,7 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
Ok(Ty::new_pat(cx, ty, pat))
}

_ => Err(TypeError::Sorts(ExpectedFound::new(true, a, b))),
_ => Err(TypeError::Sorts(ExpectedFound::new(a, b))),
}
}

@@ -637,7 +637,7 @@ pub fn structurally_relate_consts<I: Interner, R: TypeRelation<I>>(
}
_ => false,
};
if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(ExpectedFound::new(true, a, b))) }
if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(ExpectedFound::new(a, b))) }
}

impl<I: Interner, T: Relate<I>> Relate<I> for ty::Binder<I, T> {
@@ -658,9 +658,7 @@ impl<I: Interner> Relate<I> for ty::TraitPredicate<I> {
) -> RelateResult<I, ty::TraitPredicate<I>> {
let trait_ref = relation.relate(a.trait_ref, b.trait_ref)?;
if a.polarity != b.polarity {
return Err(TypeError::PolarityMismatch(ExpectedFound::new(
true, a.polarity, b.polarity,
)));
return Err(TypeError::PolarityMismatch(ExpectedFound::new(a.polarity, b.polarity)));
}
Ok(ty::TraitPredicate { trait_ref, polarity: a.polarity })
}
4 changes: 1 addition & 3 deletions compiler/rustc_type_ir/src/relate/combine.rs
Original file line number Diff line number Diff line change
@@ -123,9 +123,7 @@ where
}

// All other cases of inference are errors
(ty::Infer(_), _) | (_, ty::Infer(_)) => {
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
}
(ty::Infer(_), _) | (_, ty::Infer(_)) => Err(TypeError::Sorts(ExpectedFound::new(a, b))),

(ty::Alias(ty::Opaque, _), _) | (_, ty::Alias(ty::Opaque, _)) => {
assert!(!infcx.next_trait_solver());
Loading