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

Improve const mismatch FulfillmentError #100437

Merged
merged 4 commits into from
Aug 29, 2022
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
5 changes: 5 additions & 0 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,9 +1588,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
Mismatch::Variable(infer::ExpectedFound { expected, found }),
)
}
ValuePairs::Terms(infer::ExpectedFound {
expected: ty::Term::Const(_),
found: ty::Term::Const(_),
}) => (false, Mismatch::Fixed("constant")),
ValuePairs::TraitRefs(_) | ValuePairs::PolyTraitRefs(_) => {
(false, Mismatch::Fixed("trait"))
}
ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")),
_ => (false, Mismatch::Fixed("type")),
};
let vals = match self.values_str(values) {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ impl<'tcx> ObligationCauseCode<'tcx> {
_ => None,
}
}

pub fn peel_match_impls(&self) -> &Self {
match self {
MatchImpl(cause, _) => cause.code(),
_ => self,
}
}
}

// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
Expand Down
21 changes: 18 additions & 3 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,13 +1506,28 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
.emit();
}
FulfillmentErrorCode::CodeConstEquateError(ref expected_found, ref err) => {
self.report_mismatched_consts(
let mut diag = self.report_mismatched_consts(
&error.obligation.cause,
expected_found.expected,
expected_found.found,
err.clone(),
)
.emit();
);
let code = error.obligation.cause.code().peel_derives().peel_match_impls();
if let ObligationCauseCode::BindingObligation(..)
| ObligationCauseCode::ItemObligation(..)
| ObligationCauseCode::ExprBindingObligation(..)
| ObligationCauseCode::ExprItemObligation(..) = code
{
self.note_obligation_cause_code(
&mut diag,
&error.obligation.predicate,
error.obligation.param_env,
code,
&mut vec![],
&mut Default::default(),
);
}
diag.emit();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | Foo::<10, 12>
| ^^^^^^^^^^^^^ expected `11`, found `12`
|
= note: expected type `11`
found type `12`
= note: expected constant `11`
found constant `12`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | Foo::<N, { N + 2 }>
| ^^^^^^^^^^^^^^^^^^^ expected `{ N + 1 }`, found `{ N + 2 }`
|
= note: expected type `{ N + 1 }`
found type `{ N + 2 }`
= note: expected constant `{ N + 1 }`
found constant `{ N + 2 }`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ error[E0308]: mismatched types
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as u128 }`, found `{ O as u128 }`
|
= note: expected type `{ N as u128 }`
found type `{ O as u128 }`
= note: expected constant `{ N as u128 }`
found constant `{ O as u128 }`
note: required by a bound in `use_trait_impl::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:14:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`

error: unconstrained generic constant
--> $DIR/abstract-const-as-cast-3.rs:20:19
Expand All @@ -49,26 +54,41 @@ error[E0308]: mismatched types
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as _ }`, found `{ O as u128 }`
|
= note: expected type `{ N as _ }`
found type `{ O as u128 }`
= note: expected constant `{ N as _ }`
found constant `{ O as u128 }`
note: required by a bound in `use_trait_impl::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:14:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:23:5
|
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `12`, found `13`
|
= note: expected type `12`
found type `13`
= note: expected constant `12`
found constant `13`
note: required by a bound in `use_trait_impl::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:14:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:25:5
|
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `13`, found `14`
|
= note: expected type `13`
found type `14`
= note: expected constant `13`
found constant `14`
note: required by a bound in `use_trait_impl::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:14:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`

error: unconstrained generic constant
--> $DIR/abstract-const-as-cast-3.rs:35:19
Expand All @@ -94,8 +114,13 @@ error[E0308]: mismatched types
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as u128 }`, found `{ O as u128 }`
|
= note: expected type `{ N as u128 }`
found type `{ O as u128 }`
= note: expected constant `{ N as u128 }`
found constant `{ O as u128 }`
note: required by a bound in `use_trait_impl_2::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:32:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`

error: unconstrained generic constant
--> $DIR/abstract-const-as-cast-3.rs:38:19
Expand All @@ -121,26 +146,41 @@ error[E0308]: mismatched types
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as _ }`, found `{ O as u128 }`
|
= note: expected type `{ N as _ }`
found type `{ O as u128 }`
= note: expected constant `{ N as _ }`
found constant `{ O as u128 }`
note: required by a bound in `use_trait_impl_2::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:32:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:41:5
|
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `12`, found `13`
|
= note: expected type `12`
found type `13`
= note: expected constant `12`
found constant `13`
note: required by a bound in `use_trait_impl_2::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:32:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`

error[E0308]: mismatched types
--> $DIR/abstract-const-as-cast-3.rs:43:5
|
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `13`, found `14`
|
= note: expected type `13`
found type `14`
= note: expected constant `13`
found constant `14`
note: required by a bound in `use_trait_impl_2::assert_impl`
--> $DIR/abstract-const-as-cast-3.rs:32:23
|
LL | fn assert_impl<T: Trait>() {}
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`

error: aborting due to 12 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | [0; size_of::<Foo<T>>()]
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `size_of::<T>()`, found `size_of::<Foo<T>>()`
|
= note: expected type `size_of::<T>()`
found type `size_of::<Foo<T>>()`
= note: expected constant `size_of::<T>()`
found constant `size_of::<Foo<T>>()`

error: unconstrained generic constant
--> $DIR/different-fn.rs:10:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | ArrayHolder([0; Self::SIZE])
| ^^^^^^^^^^^^^^^ expected `X`, found `Self::SIZE`
|
= note: expected type `X`
found type `Self::SIZE`
= note: expected constant `X`
found constant `Self::SIZE`

error: unconstrained generic constant
--> $DIR/issue-62504.rs:18:25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ error[E0308]: mismatched types
LL | let x: Arr<{usize::MAX}> = Arr {};
| ^^^^^^^^^^^^^^^^^ expected `false`, found `true`
|
= note: expected type `false`
found type `true`
= note: expected constant `false`
found constant `true`
note: required by a bound in `Arr`
--> $DIR/issue-72819-generic-in-const-eval.rs:8:39
|
LL | struct Arr<const N: usize>
| --- required by a bound in this
LL | where Assert::<{N < usize::MAX / 2}>: IsTrue,
| ^^^^^^ required by this bound in `Arr`

error[E0308]: mismatched types
--> $DIR/issue-72819-generic-in-const-eval.rs:20:32
|
LL | let x: Arr<{usize::MAX}> = Arr {};
| ^^^ expected `false`, found `true`
|
= note: expected type `false`
found type `true`
= note: expected constant `false`
found constant `true`
note: required by a bound in `Arr`
--> $DIR/issue-72819-generic-in-const-eval.rs:8:39
|
LL | struct Arr<const N: usize>
| --- required by a bound in this
LL | where Assert::<{N < usize::MAX / 2}>: IsTrue,
| ^^^^^^ required by this bound in `Arr`

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: method not compatible with trait
LL | fn size(&self) -> [usize; DIM] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
|
= note: expected type `Self::DIM`
found type `DIM`
= note: expected constant `Self::DIM`
found constant `DIM`

error: unconstrained generic constant
--> $DIR/issue-83765.rs:32:24
Expand All @@ -26,8 +26,8 @@ error[E0308]: mismatched types
LL | self.reference.size()
| ^^^^^^^^^^^^^^^^^^^^^ expected `DIM`, found `Self::DIM`
|
= note: expected type `DIM`
found type `Self::DIM`
= note: expected constant `DIM`
found constant `Self::DIM`

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ error[E0308]: mismatched types
LL | writes_to_specific_path(&cap);
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `true`, found `{ contains::<T, U>() }`
|
= note: expected type `true`
found type `{ contains::<T, U>() }`
= note: expected constant `true`
found constant `{ contains::<T, U>() }`

error: aborting due to 3 previous errors

Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/const-generics/generic_const_exprs/obligation-cause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

trait True {}

struct Is<const V: bool>;

impl True for Is<true> {}

fn g<T>()
//~^ NOTE required by a bound in this
where
Is<{ std::mem::size_of::<T>() == 0 }>: True,
//~^ NOTE required by a bound in `g`
//~| NOTE required by this bound in `g`
{
}

fn main() {
g::<usize>();
//~^ ERROR mismatched types
//~| NOTE expected `false`, found `true`
//~| NOTE expected constant `false`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0308]: mismatched types
--> $DIR/obligation-cause.rs:20:5
|
LL | g::<usize>();
| ^^^^^^^^^^ expected `false`, found `true`
|
= note: expected constant `false`
found constant `true`
note: required by a bound in `g`
--> $DIR/obligation-cause.rs:13:44
|
LL | fn g<T>()
| - required by a bound in this
...
LL | Is<{ std::mem::size_of::<T>() == 0 }>: True,
| ^^^^ required by this bound in `g`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
24 changes: 20 additions & 4 deletions src/test/ui/const-generics/issues/issue-73260.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ error[E0308]: mismatched types
LL | let x: Arr<{usize::MAX}> = Arr {};
| ^^^^^^^^^^^^^^^^^ expected `false`, found `true`
|
= note: expected type `false`
found type `true`
= note: expected constant `false`
found constant `true`
note: required by a bound in `Arr`
--> $DIR/issue-73260.rs:6:37
|
LL | struct Arr<const N: usize>
| --- required by a bound in this
LL | where
LL | Assert::<{N < usize::MAX / 2}>: IsTrue,
| ^^^^^^ required by this bound in `Arr`

error[E0308]: mismatched types
--> $DIR/issue-73260.rs:16:32
|
LL | let x: Arr<{usize::MAX}> = Arr {};
| ^^^ expected `false`, found `true`
|
= note: expected type `false`
found type `true`
= note: expected constant `false`
found constant `true`
note: required by a bound in `Arr`
--> $DIR/issue-73260.rs:6:37
|
LL | struct Arr<const N: usize>
| --- required by a bound in this
LL | where
LL | Assert::<{N < usize::MAX / 2}>: IsTrue,
| ^^^^^^ required by this bound in `Arr`

error: aborting due to 2 previous errors

Expand Down
Loading