Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 96e8c7c

Browse files
authoredNov 23, 2024
Rollup merge of #133366 - compiler-errors:expected-found, r=dtolnay
Remove unnecessary bool from `ExpectedFound::new` It's true almost everywhere, and the one place it's not can be replaced w/ an if statement.
2 parents 8036ff1 + d294e47 commit 96e8c7c

File tree

13 files changed

+58
-90
lines changed

13 files changed

+58
-90
lines changed
 

‎compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
729729
let can_coerce = self.may_coerce(arg_ty, coerced_ty);
730730
if !can_coerce {
731731
return Compatibility::Incompatible(Some(ty::error::TypeError::Sorts(
732-
ty::error::ExpectedFound::new(true, coerced_ty, arg_ty),
732+
ty::error::ExpectedFound::new(coerced_ty, arg_ty),
733733
)));
734734
}
735735

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

764764
// The algorithm here is inspired by levenshtein distance and longest common subsequence.

‎compiler/rustc_infer/src/infer/at.rs

+13-29
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,22 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
308308
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
309309
TypeTrace {
310310
cause: cause.clone(),
311-
values: ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into())),
311+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
312312
}
313313
}
314314
}
315315

316316
impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
317317
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
318-
TypeTrace {
319-
cause: cause.clone(),
320-
values: ValuePairs::Regions(ExpectedFound::new(true, a, b)),
321-
}
318+
TypeTrace { cause: cause.clone(), values: ValuePairs::Regions(ExpectedFound::new(a, b)) }
322319
}
323320
}
324321

325322
impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
326323
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
327324
TypeTrace {
328325
cause: cause.clone(),
329-
values: ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into())),
326+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
330327
}
331328
}
332329
}
@@ -337,13 +334,13 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
337334
cause: cause.clone(),
338335
values: match (a.unpack(), b.unpack()) {
339336
(GenericArgKind::Lifetime(a), GenericArgKind::Lifetime(b)) => {
340-
ValuePairs::Regions(ExpectedFound::new(true, a, b))
337+
ValuePairs::Regions(ExpectedFound::new(a, b))
341338
}
342339
(GenericArgKind::Type(a), GenericArgKind::Type(b)) => {
343-
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
340+
ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
344341
}
345342
(GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
346-
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
343+
ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
347344
}
348345
_ => bug!("relating different kinds: {a:?} {b:?}"),
349346
},
@@ -353,37 +350,28 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
353350

354351
impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
355352
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
356-
TypeTrace {
357-
cause: cause.clone(),
358-
values: ValuePairs::Terms(ExpectedFound::new(true, a, b)),
359-
}
353+
TypeTrace { cause: cause.clone(), values: ValuePairs::Terms(ExpectedFound::new(a, b)) }
360354
}
361355
}
362356

363357
impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
364358
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
365-
TypeTrace {
366-
cause: cause.clone(),
367-
values: ValuePairs::TraitRefs(ExpectedFound::new(true, a, b)),
368-
}
359+
TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
369360
}
370361
}
371362

372363
impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
373364
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
374365
TypeTrace {
375366
cause: cause.clone(),
376-
values: ValuePairs::Aliases(ExpectedFound::new(true, a.into(), b.into())),
367+
values: ValuePairs::Aliases(ExpectedFound::new(a.into(), b.into())),
377368
}
378369
}
379370
}
380371

381372
impl<'tcx> ToTrace<'tcx> for ty::AliasTerm<'tcx> {
382373
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
383-
TypeTrace {
384-
cause: cause.clone(),
385-
values: ValuePairs::Aliases(ExpectedFound::new(true, a, b)),
386-
}
374+
TypeTrace { cause: cause.clone(), values: ValuePairs::Aliases(ExpectedFound::new(a, b)) }
387375
}
388376
}
389377

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

403390
impl<'tcx> ToTrace<'tcx> for ty::PolyFnSig<'tcx> {
404391
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
405-
TypeTrace {
406-
cause: cause.clone(),
407-
values: ValuePairs::PolySigs(ExpectedFound::new(true, a, b)),
408-
}
392+
TypeTrace { cause: cause.clone(), values: ValuePairs::PolySigs(ExpectedFound::new(a, b)) }
409393
}
410394
}
411395

412396
impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialTraitRef<'tcx> {
413397
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
414398
TypeTrace {
415399
cause: cause.clone(),
416-
values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(true, a, b)),
400+
values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(a, b)),
417401
}
418402
}
419403
}
@@ -422,7 +406,7 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialProjection<'tcx> {
422406
fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
423407
TypeTrace {
424408
cause: cause.clone(),
425-
values: ValuePairs::ExistentialProjection(ExpectedFound::new(true, a, b)),
409+
values: ValuePairs::ExistentialProjection(ExpectedFound::new(a, b)),
426410
}
427411
}
428412
}

‎compiler/rustc_infer/src/infer/mod.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1478,39 +1478,29 @@ impl<'tcx> TypeTrace<'tcx> {
14781478
self.cause.span
14791479
}
14801480

1481-
pub fn types(
1482-
cause: &ObligationCause<'tcx>,
1483-
a_is_expected: bool,
1484-
a: Ty<'tcx>,
1485-
b: Ty<'tcx>,
1486-
) -> TypeTrace<'tcx> {
1481+
pub fn types(cause: &ObligationCause<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> TypeTrace<'tcx> {
14871482
TypeTrace {
14881483
cause: cause.clone(),
1489-
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1484+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
14901485
}
14911486
}
14921487

14931488
pub fn trait_refs(
14941489
cause: &ObligationCause<'tcx>,
1495-
a_is_expected: bool,
14961490
a: ty::TraitRef<'tcx>,
14971491
b: ty::TraitRef<'tcx>,
14981492
) -> TypeTrace<'tcx> {
1499-
TypeTrace {
1500-
cause: cause.clone(),
1501-
values: ValuePairs::TraitRefs(ExpectedFound::new(a_is_expected, a, b)),
1502-
}
1493+
TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
15031494
}
15041495

15051496
pub fn consts(
15061497
cause: &ObligationCause<'tcx>,
1507-
a_is_expected: bool,
15081498
a: ty::Const<'tcx>,
15091499
b: ty::Const<'tcx>,
15101500
) -> TypeTrace<'tcx> {
15111501
TypeTrace {
15121502
cause: cause.clone(),
1513-
values: ValuePairs::Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1503+
values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
15141504
}
15151505
}
15161506
}

‎compiler/rustc_infer/src/infer/opaque_types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'tcx> InferCtxt<'tcx> {
177177
res
178178
} else {
179179
let (a, b) = self.resolve_vars_if_possible((a, b));
180-
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
180+
Err(TypeError::Sorts(ExpectedFound::new(a, b)))
181181
}
182182
}
183183

‎compiler/rustc_middle/src/ty/relate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'tcx> Relate<TyCtxt<'tcx>> for &'tcx ty::List<ty::PolyExistentialPredicate<
9292
b_v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
9393
b_v.dedup();
9494
if a_v.len() != b_v.len() {
95-
return Err(TypeError::ExistentialMismatch(ExpectedFound::new(true, a, b)));
95+
return Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b)));
9696
}
9797

9898
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<
112112
ty::ExistentialPredicate::AutoTrait(a),
113113
ty::ExistentialPredicate::AutoTrait(b),
114114
) if a == b => Ok(ep_a.rebind(ty::ExistentialPredicate::AutoTrait(a))),
115-
_ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(true, a, b))),
115+
_ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b))),
116116
}
117117
});
118118
tcx.mk_poly_existential_predicates_from_iter(v)

‎compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
152152
err: TypeError<'tcx>,
153153
) -> Diag<'a> {
154154
self.report_and_explain_type_error(
155-
TypeTrace::types(cause, true, expected, actual),
155+
TypeTrace::types(cause, expected, actual),
156156
param_env,
157157
err,
158158
)
@@ -167,7 +167,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
167167
err: TypeError<'tcx>,
168168
) -> Diag<'a> {
169169
self.report_and_explain_type_error(
170-
TypeTrace::consts(cause, true, expected, actual),
170+
TypeTrace::consts(cause, expected, actual),
171171
param_env,
172172
err,
173173
)

‎compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
725725
&obligation.cause,
726726
None,
727727
None,
728-
TypeError::Sorts(ty::error::ExpectedFound::new(true, expected_ty, ct_ty)),
728+
TypeError::Sorts(ty::error::ExpectedFound::new(expected_ty, ct_ty)),
729729
false,
730730
);
731731
diag
@@ -1449,7 +1449,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
14491449
secondary_span,
14501450
values.map(|(_, normalized_ty, expected_ty)| {
14511451
obligation.param_env.and(infer::ValuePairs::Terms(ExpectedFound::new(
1452-
true,
14531452
expected_ty,
14541453
normalized_ty,
14551454
)))
@@ -2755,7 +2754,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27552754
(obligation.cause.clone(), terr)
27562755
};
27572756
self.report_and_explain_type_error(
2758-
TypeTrace::trait_refs(&cause, true, expected_trait_ref, found_trait_ref),
2757+
TypeTrace::trait_refs(&cause, expected_trait_ref, found_trait_ref),
27592758
obligation.param_env,
27602759
terr,
27612760
)
@@ -2846,7 +2845,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
28462845
if Some(expected_trait_ref.def_id) != self.tcx.lang_items().coroutine_trait() && not_tupled
28472846
{
28482847
return Ok(self.report_and_explain_type_error(
2849-
TypeTrace::trait_refs(&obligation.cause, true, expected_trait_ref, found_trait_ref),
2848+
TypeTrace::trait_refs(&obligation.cause, expected_trait_ref, found_trait_ref),
28502849
obligation.param_env,
28512850
ty::error::TypeError::Mismatch,
28522851
));

‎compiler/rustc_trait_selection/src/solve/fulfill.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,14 @@ fn fulfillment_error_for_no_solution<'tcx>(
264264
let (a, b) = infcx.enter_forall_and_leak_universe(
265265
obligation.predicate.kind().rebind((pred.a, pred.b)),
266266
);
267-
let expected_found = ExpectedFound::new(true, a, b);
267+
let expected_found = ExpectedFound::new(a, b);
268268
FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
269269
}
270270
ty::PredicateKind::Coerce(pred) => {
271271
let (a, b) = infcx.enter_forall_and_leak_universe(
272272
obligation.predicate.kind().rebind((pred.a, pred.b)),
273273
);
274-
let expected_found = ExpectedFound::new(false, a, b);
274+
let expected_found = ExpectedFound::new(b, a);
275275
FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
276276
}
277277
ty::PredicateKind::Clause(_)

‎compiler/rustc_trait_selection/src/traits/fulfill.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,11 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
557557
ProcessResult::Changed(mk_pending(ok.obligations))
558558
}
559559
Ok(Err(err)) => {
560-
let expected_found =
561-
ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
560+
let expected_found = if subtype.a_is_expected {
561+
ExpectedFound::new(subtype.a, subtype.b)
562+
} else {
563+
ExpectedFound::new(subtype.b, subtype.a)
564+
};
562565
ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err))
563566
}
564567
}
@@ -578,7 +581,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
578581
}
579582
Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
580583
Ok(Err(err)) => {
581-
let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
584+
let expected_found = ExpectedFound::new(coerce.b, coerce.a);
582585
ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err))
583586
}
584587
}
@@ -703,7 +706,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
703706
}
704707
Err(err) => {
705708
ProcessResult::Error(FulfillmentErrorCode::ConstEquate(
706-
ExpectedFound::new(true, c1, c2),
709+
ExpectedFound::new(c1, c2),
707710
err,
708711
))
709712
}
@@ -727,7 +730,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
727730
ProcessResult::Unchanged
728731
} else {
729732
// Two different constants using generic parameters ~> error.
730-
let expected_found = ExpectedFound::new(true, c1, c2);
733+
let expected_found = ExpectedFound::new(c1, c2);
731734
ProcessResult::Error(FulfillmentErrorCode::ConstEquate(
732735
expected_found,
733736
TypeError::ConstMismatch(expected_found),

‎compiler/rustc_trait_selection/src/traits/select/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstFreshVars<'tcx> {
7070
) => Ok(a),
7171

7272
(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
73-
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
73+
Err(TypeError::Sorts(ExpectedFound::new(a, b)))
7474
}
7575

7676
(&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> {
9595
}
9696

9797
(ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
98-
return Err(TypeError::ConstMismatch(ExpectedFound::new(true, a, b)));
98+
return Err(TypeError::ConstMismatch(ExpectedFound::new(a, b)));
9999
}
100100

101101
_ => {}

‎compiler/rustc_type_ir/src/error.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ pub struct ExpectedFound<T> {
1212
}
1313

1414
impl<T> ExpectedFound<T> {
15-
pub fn new(a_is_expected: bool, a: T, b: T) -> Self {
16-
if a_is_expected {
17-
ExpectedFound { expected: a, found: b }
18-
} else {
19-
ExpectedFound { expected: b, found: a }
20-
}
15+
pub fn new(expected: T, found: T) -> Self {
16+
ExpectedFound { expected, found }
2117
}
2218
}
2319

There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.