Skip to content

Commit 25ce315

Browse files
authored
Rollup merge of rust-lang#93728 - JulianKnodt:toterm, r=oli-obk
Add in ValuePair::Term This adds in an enum when matching on positions which can either be types or consts. It will default to emitting old special cased error messages for types. r? `@oli-obk` cc `@matthiaskrgr` Fixes rust-lang#93578
2 parents 1f90f4f + be236d7 commit 25ce315

File tree

6 files changed

+74
-69
lines changed

6 files changed

+74
-69
lines changed

compiler/rustc_infer/src/infer/at.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
268268
a: Self,
269269
b: Self,
270270
) -> TypeTrace<'tcx> {
271-
TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
271+
TypeTrace {
272+
cause: cause.clone(),
273+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
274+
}
272275
}
273276
}
274277

@@ -292,27 +295,22 @@ impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
292295
a: Self,
293296
b: Self,
294297
) -> TypeTrace<'tcx> {
295-
TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
298+
TypeTrace {
299+
cause: cause.clone(),
300+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
301+
}
296302
}
297303
}
298304

299305
impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
300306
fn to_trace(
301-
tcx: TyCtxt<'tcx>,
307+
_: TyCtxt<'tcx>,
302308
cause: &ObligationCause<'tcx>,
303309
a_is_expected: bool,
304310
a: Self,
305311
b: Self,
306312
) -> TypeTrace<'tcx> {
307-
match (a, b) {
308-
(ty::Term::Ty(a), ty::Term::Ty(b)) => {
309-
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
310-
}
311-
(ty::Term::Const(a), ty::Term::Const(b)) => {
312-
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
313-
}
314-
(_, _) => todo!(),
315-
}
313+
TypeTrace { cause: cause.clone(), values: Terms(ExpectedFound::new(a_is_expected, a, b)) }
316314
}
317315
}
318316

@@ -358,7 +356,7 @@ impl<'tcx> ToTrace<'tcx> for ty::ProjectionTy<'tcx> {
358356
let b_ty = tcx.mk_projection(b.item_def_id, b.substs);
359357
TypeTrace {
360358
cause: cause.clone(),
361-
values: Types(ExpectedFound::new(a_is_expected, a_ty, b_ty)),
359+
values: Terms(ExpectedFound::new(a_is_expected, a_ty.into(), b_ty.into())),
362360
}
363361
}
364362
}

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

+23-20
Original file line numberDiff line numberDiff line change
@@ -1582,18 +1582,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15821582
None => (None, Mismatch::Fixed("type"), false),
15831583
Some(values) => {
15841584
let (is_simple_error, exp_found) = match values {
1585-
ValuePairs::Types(exp_found) => {
1586-
let is_simple_err =
1587-
exp_found.expected.is_simple_text() && exp_found.found.is_simple_text();
1588-
OpaqueTypesVisitor::visit_expected_found(
1589-
self.tcx,
1590-
exp_found.expected,
1591-
exp_found.found,
1592-
span,
1593-
)
1594-
.report(diag);
1585+
ValuePairs::Terms(infer::ExpectedFound {
1586+
expected: ty::Term::Ty(expected),
1587+
found: ty::Term::Ty(found),
1588+
}) => {
1589+
let is_simple_err = expected.is_simple_text() && found.is_simple_text();
1590+
OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
1591+
.report(diag);
15951592

1596-
(is_simple_err, Mismatch::Variable(exp_found))
1593+
(
1594+
is_simple_err,
1595+
Mismatch::Variable(infer::ExpectedFound { expected, found }),
1596+
)
15971597
}
15981598
ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
15991599
_ => (false, Mismatch::Fixed("type")),
@@ -1624,7 +1624,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16241624
};
16251625
if let Some((sp, msg)) = secondary_span {
16261626
if swap_secondary_and_primary {
1627-
let terr = if let Some(infer::ValuePairs::Types(infer::ExpectedFound {
1627+
let terr = if let Some(infer::ValuePairs::Terms(infer::ExpectedFound {
16281628
expected,
16291629
..
16301630
})) = values
@@ -2036,9 +2036,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20362036
}
20372037
FailureCode::Error0308(failure_str) => {
20382038
let mut err = struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str);
2039-
if let ValuePairs::Types(ty::error::ExpectedFound { expected, found }) =
2040-
trace.values
2041-
{
2039+
if let Some((expected, found)) = trace.values.ty() {
20422040
match (expected.kind(), found.kind()) {
20432041
(ty::Tuple(_), ty::Tuple(_)) => {}
20442042
// If a tuple of length one was expected and the found expression has
@@ -2148,9 +2146,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21482146
values: ValuePairs<'tcx>,
21492147
) -> Option<(DiagnosticStyledString, DiagnosticStyledString)> {
21502148
match values {
2151-
infer::Types(exp_found) => self.expected_found_str_ty(exp_found),
21522149
infer::Regions(exp_found) => self.expected_found_str(exp_found),
2153-
infer::Consts(exp_found) => self.expected_found_str(exp_found),
2150+
infer::Terms(exp_found) => self.expected_found_str_term(exp_found),
21542151
infer::TraitRefs(exp_found) => {
21552152
let pretty_exp_found = ty::error::ExpectedFound {
21562153
expected: exp_found.expected.print_only_trait_path(),
@@ -2178,16 +2175,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21782175
}
21792176
}
21802177

2181-
fn expected_found_str_ty(
2178+
fn expected_found_str_term(
21822179
&self,
2183-
exp_found: ty::error::ExpectedFound<Ty<'tcx>>,
2180+
exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
21842181
) -> Option<(DiagnosticStyledString, DiagnosticStyledString)> {
21852182
let exp_found = self.resolve_vars_if_possible(exp_found);
21862183
if exp_found.references_error() {
21872184
return None;
21882185
}
21892186

2190-
Some(self.cmp(exp_found.expected, exp_found.found))
2187+
Some(match (exp_found.expected, exp_found.found) {
2188+
(ty::Term::Ty(expected), ty::Term::Ty(found)) => self.cmp(expected, found),
2189+
(expected, found) => (
2190+
DiagnosticStyledString::highlighted(expected.to_string()),
2191+
DiagnosticStyledString::highlighted(found.to_string()),
2192+
),
2193+
})
21912194
}
21922195

21932196
/// Returns a string of the form "expected `{}`, found `{}`".

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
44
use crate::infer::lexical_region_resolve::RegionResolutionError;
5-
use crate::infer::{SubregionOrigin, Subtype, ValuePairs};
5+
use crate::infer::{SubregionOrigin, Subtype};
66
use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
77
use rustc_errors::ErrorReported;
88
use rustc_hir as hir;
@@ -34,16 +34,16 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3434
{
3535
if let (&Subtype(ref sup_trace), &Subtype(ref sub_trace)) = (&sup_origin, &sub_origin) {
3636
if let (
37-
ValuePairs::Types(sub_expected_found),
38-
ValuePairs::Types(sup_expected_found),
37+
sub_expected_found @ Some((sub_expected, sub_found)),
38+
sup_expected_found @ Some(_),
3939
CompareImplMethodObligation { trait_item_def_id, .. },
40-
) = (&sub_trace.values, &sup_trace.values, sub_trace.cause.code())
40+
) = (&sub_trace.values.ty(), &sup_trace.values.ty(), sub_trace.cause.code())
4141
{
4242
if sup_expected_found == sub_expected_found {
4343
self.emit_err(
4444
var_origin.span(),
45-
sub_expected_found.expected,
46-
sub_expected_found.found,
45+
sub_expected,
46+
sub_found,
4747
*trait_item_def_id,
4848
);
4949
return Some(ErrorReported);

compiler/rustc_infer/src/infer/mod.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,26 @@ pub struct InferCtxt<'a, 'tcx> {
366366
/// See the `error_reporting` module for more details.
367367
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable)]
368368
pub enum ValuePairs<'tcx> {
369-
Types(ExpectedFound<Ty<'tcx>>),
370369
Regions(ExpectedFound<ty::Region<'tcx>>),
371-
Consts(ExpectedFound<&'tcx ty::Const<'tcx>>),
370+
Terms(ExpectedFound<ty::Term<'tcx>>),
372371
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
373372
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
374373
}
375374

375+
impl<'tcx> ValuePairs<'tcx> {
376+
pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
377+
if let ValuePairs::Terms(ExpectedFound {
378+
expected: ty::Term::Ty(expected),
379+
found: ty::Term::Ty(found),
380+
}) = self
381+
{
382+
Some((expected, found))
383+
} else {
384+
None
385+
}
386+
}
387+
}
388+
376389
/// The trace designates the path through inference that we took to
377390
/// encounter an error or subtyping constraint.
378391
///
@@ -1817,7 +1830,10 @@ impl<'tcx> TypeTrace<'tcx> {
18171830
a: Ty<'tcx>,
18181831
b: Ty<'tcx>,
18191832
) -> TypeTrace<'tcx> {
1820-
TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
1833+
TypeTrace {
1834+
cause: cause.clone(),
1835+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1836+
}
18211837
}
18221838

18231839
pub fn consts(
@@ -1826,7 +1842,10 @@ impl<'tcx> TypeTrace<'tcx> {
18261842
a: &'tcx ty::Const<'tcx>,
18271843
b: &'tcx ty::Const<'tcx>,
18281844
) -> TypeTrace<'tcx> {
1829-
TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
1845+
TypeTrace {
1846+
cause: cause.clone(),
1847+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1848+
}
18301849
}
18311850
}
18321851

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -1382,26 +1382,11 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
13821382
normalized_ty,
13831383
data.term,
13841384
) {
1385-
values = Some(match (normalized_ty, data.term) {
1386-
(ty::Term::Ty(normalized_ty), ty::Term::Ty(ty)) => {
1387-
infer::ValuePairs::Types(ExpectedFound::new(
1388-
is_normalized_ty_expected,
1389-
normalized_ty,
1390-
ty,
1391-
))
1392-
}
1393-
(ty::Term::Const(normalized_ct), ty::Term::Const(ct)) => {
1394-
infer::ValuePairs::Consts(ExpectedFound::new(
1395-
is_normalized_ty_expected,
1396-
normalized_ct,
1397-
ct,
1398-
))
1399-
}
1400-
(_, _) => span_bug!(
1401-
obligation.cause.span,
1402-
"found const or type where other expected"
1403-
),
1404-
});
1385+
values = Some(infer::ValuePairs::Terms(ExpectedFound::new(
1386+
is_normalized_ty_expected,
1387+
normalized_ty,
1388+
data.term,
1389+
)));
14051390
err_buf = error;
14061391
err = &err_buf;
14071392
}

compiler/rustc_typeck/src/check/compare_method.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,9 @@ fn compare_predicate_entailment<'tcx>(
377377
&mut diag,
378378
&cause,
379379
trait_err_span.map(|sp| (sp, "type in trait".to_owned())),
380-
Some(infer::ValuePairs::Types(ExpectedFound {
381-
expected: trait_fty,
382-
found: impl_fty,
380+
Some(infer::ValuePairs::Terms(ExpectedFound {
381+
expected: trait_fty.into(),
382+
found: impl_fty.into(),
383383
})),
384384
&terr,
385385
false,
@@ -1068,9 +1068,9 @@ crate fn compare_const_impl<'tcx>(
10681068
&mut diag,
10691069
&cause,
10701070
trait_c_span.map(|span| (span, "type in trait".to_owned())),
1071-
Some(infer::ValuePairs::Types(ExpectedFound {
1072-
expected: trait_ty,
1073-
found: impl_ty,
1071+
Some(infer::ValuePairs::Terms(ExpectedFound {
1072+
expected: trait_ty.into(),
1073+
found: impl_ty.into(),
10741074
})),
10751075
&terr,
10761076
false,

0 commit comments

Comments
 (0)