Skip to content

Commit ea2e810

Browse files
authored
Rollup merge of rust-lang#100437 - compiler-errors:better-const-mismatch-err, r=oli-obk
Improve const mismatch `FulfillmentError` Fixes rust-lang#100414
2 parents 2fbc9a4 + 8189a45 commit ea2e810

16 files changed

+194
-45
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1588,9 +1588,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15881588
Mismatch::Variable(infer::ExpectedFound { expected, found }),
15891589
)
15901590
}
1591+
ValuePairs::Terms(infer::ExpectedFound {
1592+
expected: ty::Term::Const(_),
1593+
found: ty::Term::Const(_),
1594+
}) => (false, Mismatch::Fixed("constant")),
15911595
ValuePairs::TraitRefs(_) | ValuePairs::PolyTraitRefs(_) => {
15921596
(false, Mismatch::Fixed("trait"))
15931597
}
1598+
ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")),
15941599
_ => (false, Mismatch::Fixed("type")),
15951600
};
15961601
let vals = match self.values_str(values) {

compiler/rustc_middle/src/traits/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ impl<'tcx> ObligationCauseCode<'tcx> {
469469
_ => None,
470470
}
471471
}
472+
473+
pub fn peel_match_impls(&self) -> &Self {
474+
match self {
475+
MatchImpl(cause, _) => cause.code(),
476+
_ => self,
477+
}
478+
}
472479
}
473480

474481
// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -1506,13 +1506,28 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
15061506
.emit();
15071507
}
15081508
FulfillmentErrorCode::CodeConstEquateError(ref expected_found, ref err) => {
1509-
self.report_mismatched_consts(
1509+
let mut diag = self.report_mismatched_consts(
15101510
&error.obligation.cause,
15111511
expected_found.expected,
15121512
expected_found.found,
15131513
err.clone(),
1514-
)
1515-
.emit();
1514+
);
1515+
let code = error.obligation.cause.code().peel_derives().peel_match_impls();
1516+
if let ObligationCauseCode::BindingObligation(..)
1517+
| ObligationCauseCode::ItemObligation(..)
1518+
| ObligationCauseCode::ExprBindingObligation(..)
1519+
| ObligationCauseCode::ExprItemObligation(..) = code
1520+
{
1521+
self.note_obligation_cause_code(
1522+
&mut diag,
1523+
&error.obligation.predicate,
1524+
error.obligation.param_env,
1525+
code,
1526+
&mut vec![],
1527+
&mut Default::default(),
1528+
);
1529+
}
1530+
diag.emit();
15161531
}
15171532
}
15181533
}

src/test/ui/const-generics/defaults/generic-expr-default-concrete.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | Foo::<10, 12>
55
| ^^^^^^^^^^^^^ expected `11`, found `12`
66
|
7-
= note: expected type `11`
8-
found type `12`
7+
= note: expected constant `11`
8+
found constant `12`
99

1010
error: aborting due to previous error
1111

src/test/ui/const-generics/defaults/generic-expr-default-mismatched-types.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | Foo::<N, { N + 2 }>
55
| ^^^^^^^^^^^^^^^^^^^ expected `{ N + 1 }`, found `{ N + 2 }`
66
|
7-
= note: expected type `{ N + 1 }`
8-
found type `{ N + 2 }`
7+
= note: expected constant `{ N + 1 }`
8+
found constant `{ N + 2 }`
99

1010
error: aborting due to previous error
1111

src/test/ui/const-generics/generic_const_exprs/abstract-const-as-cast-3.stderr

+56-16
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ error[E0308]: mismatched types
2222
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as u128 }`, found `{ O as u128 }`
2424
|
25-
= note: expected type `{ N as u128 }`
26-
found type `{ O as u128 }`
25+
= note: expected constant `{ N as u128 }`
26+
found constant `{ O as u128 }`
27+
note: required by a bound in `use_trait_impl::assert_impl`
28+
--> $DIR/abstract-const-as-cast-3.rs:14:23
29+
|
30+
LL | fn assert_impl<T: Trait>() {}
31+
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`
2732

2833
error: unconstrained generic constant
2934
--> $DIR/abstract-const-as-cast-3.rs:20:19
@@ -49,26 +54,41 @@ error[E0308]: mismatched types
4954
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
5055
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as _ }`, found `{ O as u128 }`
5156
|
52-
= note: expected type `{ N as _ }`
53-
found type `{ O as u128 }`
57+
= note: expected constant `{ N as _ }`
58+
found constant `{ O as u128 }`
59+
note: required by a bound in `use_trait_impl::assert_impl`
60+
--> $DIR/abstract-const-as-cast-3.rs:14:23
61+
|
62+
LL | fn assert_impl<T: Trait>() {}
63+
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`
5464

5565
error[E0308]: mismatched types
5666
--> $DIR/abstract-const-as-cast-3.rs:23:5
5767
|
5868
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
5969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `12`, found `13`
6070
|
61-
= note: expected type `12`
62-
found type `13`
71+
= note: expected constant `12`
72+
found constant `13`
73+
note: required by a bound in `use_trait_impl::assert_impl`
74+
--> $DIR/abstract-const-as-cast-3.rs:14:23
75+
|
76+
LL | fn assert_impl<T: Trait>() {}
77+
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`
6378

6479
error[E0308]: mismatched types
6580
--> $DIR/abstract-const-as-cast-3.rs:25:5
6681
|
6782
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
6883
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `13`, found `14`
6984
|
70-
= note: expected type `13`
71-
found type `14`
85+
= note: expected constant `13`
86+
found constant `14`
87+
note: required by a bound in `use_trait_impl::assert_impl`
88+
--> $DIR/abstract-const-as-cast-3.rs:14:23
89+
|
90+
LL | fn assert_impl<T: Trait>() {}
91+
| ^^^^^ required by this bound in `use_trait_impl::assert_impl`
7292

7393
error: unconstrained generic constant
7494
--> $DIR/abstract-const-as-cast-3.rs:35:19
@@ -94,8 +114,13 @@ error[E0308]: mismatched types
94114
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
95115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as u128 }`, found `{ O as u128 }`
96116
|
97-
= note: expected type `{ N as u128 }`
98-
found type `{ O as u128 }`
117+
= note: expected constant `{ N as u128 }`
118+
found constant `{ O as u128 }`
119+
note: required by a bound in `use_trait_impl_2::assert_impl`
120+
--> $DIR/abstract-const-as-cast-3.rs:32:23
121+
|
122+
LL | fn assert_impl<T: Trait>() {}
123+
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`
99124

100125
error: unconstrained generic constant
101126
--> $DIR/abstract-const-as-cast-3.rs:38:19
@@ -121,26 +146,41 @@ error[E0308]: mismatched types
121146
LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
122147
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{ N as _ }`, found `{ O as u128 }`
123148
|
124-
= note: expected type `{ N as _ }`
125-
found type `{ O as u128 }`
149+
= note: expected constant `{ N as _ }`
150+
found constant `{ O as u128 }`
151+
note: required by a bound in `use_trait_impl_2::assert_impl`
152+
--> $DIR/abstract-const-as-cast-3.rs:32:23
153+
|
154+
LL | fn assert_impl<T: Trait>() {}
155+
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`
126156

127157
error[E0308]: mismatched types
128158
--> $DIR/abstract-const-as-cast-3.rs:41:5
129159
|
130160
LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
131161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `12`, found `13`
132162
|
133-
= note: expected type `12`
134-
found type `13`
163+
= note: expected constant `12`
164+
found constant `13`
165+
note: required by a bound in `use_trait_impl_2::assert_impl`
166+
--> $DIR/abstract-const-as-cast-3.rs:32:23
167+
|
168+
LL | fn assert_impl<T: Trait>() {}
169+
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`
135170

136171
error[E0308]: mismatched types
137172
--> $DIR/abstract-const-as-cast-3.rs:43:5
138173
|
139174
LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
140175
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `13`, found `14`
141176
|
142-
= note: expected type `13`
143-
found type `14`
177+
= note: expected constant `13`
178+
found constant `14`
179+
note: required by a bound in `use_trait_impl_2::assert_impl`
180+
--> $DIR/abstract-const-as-cast-3.rs:32:23
181+
|
182+
LL | fn assert_impl<T: Trait>() {}
183+
| ^^^^^ required by this bound in `use_trait_impl_2::assert_impl`
144184

145185
error: aborting due to 12 previous errors
146186

src/test/ui/const-generics/generic_const_exprs/different-fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | [0; size_of::<Foo<T>>()]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `size_of::<T>()`, found `size_of::<Foo<T>>()`
66
|
7-
= note: expected type `size_of::<T>()`
8-
found type `size_of::<Foo<T>>()`
7+
= note: expected constant `size_of::<T>()`
8+
found constant `size_of::<Foo<T>>()`
99

1010
error: unconstrained generic constant
1111
--> $DIR/different-fn.rs:10:9

src/test/ui/const-generics/generic_const_exprs/issue-62504.full.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | ArrayHolder([0; Self::SIZE])
55
| ^^^^^^^^^^^^^^^ expected `X`, found `Self::SIZE`
66
|
7-
= note: expected type `X`
8-
found type `Self::SIZE`
7+
= note: expected constant `X`
8+
found constant `Self::SIZE`
99

1010
error: unconstrained generic constant
1111
--> $DIR/issue-62504.rs:18:25

src/test/ui/const-generics/generic_const_exprs/issue-72819-generic-in-const-eval.full.stderr

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,31 @@ error[E0308]: mismatched types
44
LL | let x: Arr<{usize::MAX}> = Arr {};
55
| ^^^^^^^^^^^^^^^^^ expected `false`, found `true`
66
|
7-
= note: expected type `false`
8-
found type `true`
7+
= note: expected constant `false`
8+
found constant `true`
9+
note: required by a bound in `Arr`
10+
--> $DIR/issue-72819-generic-in-const-eval.rs:8:39
11+
|
12+
LL | struct Arr<const N: usize>
13+
| --- required by a bound in this
14+
LL | where Assert::<{N < usize::MAX / 2}>: IsTrue,
15+
| ^^^^^^ required by this bound in `Arr`
916

1017
error[E0308]: mismatched types
1118
--> $DIR/issue-72819-generic-in-const-eval.rs:20:32
1219
|
1320
LL | let x: Arr<{usize::MAX}> = Arr {};
1421
| ^^^ expected `false`, found `true`
1522
|
16-
= note: expected type `false`
17-
found type `true`
23+
= note: expected constant `false`
24+
found constant `true`
25+
note: required by a bound in `Arr`
26+
--> $DIR/issue-72819-generic-in-const-eval.rs:8:39
27+
|
28+
LL | struct Arr<const N: usize>
29+
| --- required by a bound in this
30+
LL | where Assert::<{N < usize::MAX / 2}>: IsTrue,
31+
| ^^^^^^ required by this bound in `Arr`
1832

1933
error: aborting due to 2 previous errors
2034

src/test/ui/const-generics/generic_const_exprs/issue-83765.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: method not compatible with trait
44
LL | fn size(&self) -> [usize; DIM] {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
66
|
7-
= note: expected type `Self::DIM`
8-
found type `DIM`
7+
= note: expected constant `Self::DIM`
8+
found constant `DIM`
99

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

3232
error: aborting due to 3 previous errors
3333

src/test/ui/const-generics/generic_const_exprs/issue-85848.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ error[E0308]: mismatched types
5454
LL | writes_to_specific_path(&cap);
5555
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `true`, found `{ contains::<T, U>() }`
5656
|
57-
= note: expected type `true`
58-
found type `{ contains::<T, U>() }`
57+
= note: expected constant `true`
58+
found constant `{ contains::<T, U>() }`
5959

6060
error: aborting due to 3 previous errors
6161

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![allow(incomplete_features)]
2+
#![feature(generic_const_exprs)]
3+
4+
trait True {}
5+
6+
struct Is<const V: bool>;
7+
8+
impl True for Is<true> {}
9+
10+
fn g<T>()
11+
//~^ NOTE required by a bound in this
12+
where
13+
Is<{ std::mem::size_of::<T>() == 0 }>: True,
14+
//~^ NOTE required by a bound in `g`
15+
//~| NOTE required by this bound in `g`
16+
{
17+
}
18+
19+
fn main() {
20+
g::<usize>();
21+
//~^ ERROR mismatched types
22+
//~| NOTE expected `false`, found `true`
23+
//~| NOTE expected constant `false`
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/obligation-cause.rs:20:5
3+
|
4+
LL | g::<usize>();
5+
| ^^^^^^^^^^ expected `false`, found `true`
6+
|
7+
= note: expected constant `false`
8+
found constant `true`
9+
note: required by a bound in `g`
10+
--> $DIR/obligation-cause.rs:13:44
11+
|
12+
LL | fn g<T>()
13+
| - required by a bound in this
14+
...
15+
LL | Is<{ std::mem::size_of::<T>() == 0 }>: True,
16+
| ^^^^ required by this bound in `g`
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/const-generics/issues/issue-73260.stderr

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@ error[E0308]: mismatched types
44
LL | let x: Arr<{usize::MAX}> = Arr {};
55
| ^^^^^^^^^^^^^^^^^ expected `false`, found `true`
66
|
7-
= note: expected type `false`
8-
found type `true`
7+
= note: expected constant `false`
8+
found constant `true`
9+
note: required by a bound in `Arr`
10+
--> $DIR/issue-73260.rs:6:37
11+
|
12+
LL | struct Arr<const N: usize>
13+
| --- required by a bound in this
14+
LL | where
15+
LL | Assert::<{N < usize::MAX / 2}>: IsTrue,
16+
| ^^^^^^ required by this bound in `Arr`
917

1018
error[E0308]: mismatched types
1119
--> $DIR/issue-73260.rs:16:32
1220
|
1321
LL | let x: Arr<{usize::MAX}> = Arr {};
1422
| ^^^ expected `false`, found `true`
1523
|
16-
= note: expected type `false`
17-
found type `true`
24+
= note: expected constant `false`
25+
found constant `true`
26+
note: required by a bound in `Arr`
27+
--> $DIR/issue-73260.rs:6:37
28+
|
29+
LL | struct Arr<const N: usize>
30+
| --- required by a bound in this
31+
LL | where
32+
LL | Assert::<{N < usize::MAX / 2}>: IsTrue,
33+
| ^^^^^^ required by this bound in `Arr`
1834

1935
error: aborting due to 2 previous errors
2036

0 commit comments

Comments
 (0)