Skip to content

Commit

Permalink
Add in ValuePair::Term
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JulianKnodt committed Feb 7, 2022
1 parent f624427 commit fdd6f4e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
12 changes: 2 additions & 10 deletions compiler/rustc_infer/src/infer/at.rs
Expand Up @@ -288,21 +288,13 @@ impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {

impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
fn to_trace(
tcx: TyCtxt<'tcx>,
_: TyCtxt<'tcx>,
cause: &ObligationCause<'tcx>,
a_is_expected: bool,
a: Self,
b: Self,
) -> TypeTrace<'tcx> {
match (a, b) {
(ty::Term::Ty(a), ty::Term::Ty(b)) => {
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
}
(ty::Term::Const(a), ty::Term::Const(b)) => {
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
}
(_, _) => todo!(),
}
TypeTrace { cause: cause.clone(), values: Terms(ExpectedFound::new(a_is_expected, a, b)) }
}
}

Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Expand Up @@ -2127,6 +2127,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
infer::Types(exp_found) => self.expected_found_str_ty(exp_found),
infer::Regions(exp_found) => self.expected_found_str(exp_found),
infer::Consts(exp_found) => self.expected_found_str(exp_found),
infer::Terms(exp_found) => self.expected_found_str_term(exp_found),
infer::TraitRefs(exp_found) => {
let pretty_exp_found = ty::error::ExpectedFound {
expected: exp_found.expected.print_only_trait_path(),
Expand Down Expand Up @@ -2166,6 +2167,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
Some(self.cmp(exp_found.expected, exp_found.found))
}

fn expected_found_str_term(
&self,
exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
) -> Option<(DiagnosticStyledString, DiagnosticStyledString)> {
let exp_found = self.resolve_vars_if_possible(exp_found);
if exp_found.references_error() {
return None;
}

Some(match (exp_found.expected, exp_found.found) {
(ty::Term::Ty(expected), ty::Term::Ty(found)) => self.cmp(expected, found),
(expected, found) => (
DiagnosticStyledString::highlighted(expected.to_string()),
DiagnosticStyledString::highlighted(found.to_string()),
),
})
}

/// Returns a string of the form "expected `{}`, found `{}`".
fn expected_found_str<T: fmt::Display + TypeFoldable<'tcx>>(
&self,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/mod.rs
Expand Up @@ -371,6 +371,7 @@ pub enum ValuePairs<'tcx> {
Types(ExpectedFound<Ty<'tcx>>),
Regions(ExpectedFound<ty::Region<'tcx>>),
Consts(ExpectedFound<&'tcx ty::Const<'tcx>>),
Terms(ExpectedFound<ty::Term<'tcx>>),
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
}
Expand Down
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | foo(());
| ^^^ lifetime mismatch
|
= note: expected reference `&'a ()`
found type `&()`
= note: expected type `&'a ()`
found type `&()`
note: the lifetime requirement is introduced here
--> $DIR/higher-ranked-projection.rs:15:33
|
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/lifetimes/issue-79187-2.stderr
Expand Up @@ -23,8 +23,8 @@ error[E0308]: mismatched types
LL | take_foo(|a: &i32| a);
| ^^^^^^^^ lifetime mismatch
|
= note: expected reference `&i32`
found reference `&i32`
= note: expected type `&i32`
found type `&i32`
note: the anonymous lifetime #1 defined here doesn't meet the lifetime requirements
--> $DIR/issue-79187-2.rs:9:14
|
Expand All @@ -42,8 +42,8 @@ error[E0308]: mismatched types
LL | take_foo(|a: &i32| -> &i32 { a });
| ^^^^^^^^ lifetime mismatch
|
= note: expected reference `&i32`
found reference `&i32`
= note: expected type `&i32`
found type `&i32`
note: the anonymous lifetime #1 defined here doesn't meet the lifetime requirements
--> $DIR/issue-79187-2.rs:10:14
|
Expand Down

0 comments on commit fdd6f4e

Please sign in to comment.