Skip to content

Commit 792a845

Browse files
Note binding obligation causes for const equate errors
1 parent beba7ca commit 792a845

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

compiler/rustc_middle/src/traits/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ impl<'tcx> ObligationCauseCode<'tcx> {
459459
_ => None,
460460
}
461461
}
462+
463+
pub fn peel_match_impls(&self) -> &Self {
464+
match self {
465+
MatchImpl(cause, _) => cause.code(),
466+
_ => self,
467+
}
468+
}
462469
}
463470

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

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -1483,13 +1483,26 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
14831483
.emit();
14841484
}
14851485
FulfillmentErrorCode::CodeConstEquateError(ref expected_found, ref err) => {
1486-
self.report_mismatched_consts(
1486+
let mut diag = self.report_mismatched_consts(
14871487
&error.obligation.cause,
14881488
expected_found.expected,
14891489
expected_found.found,
14901490
err.clone(),
1491-
)
1492-
.emit();
1491+
);
1492+
let code = error.obligation.cause.code().peel_derives().peel_match_impls();
1493+
if let ObligationCauseCode::BindingObligation(..)
1494+
| ObligationCauseCode::ItemObligation(..) = code
1495+
{
1496+
self.note_obligation_cause_code(
1497+
&mut diag,
1498+
&error.obligation.predicate,
1499+
error.obligation.param_env,
1500+
code,
1501+
&mut vec![],
1502+
&mut Default::default(),
1503+
);
1504+
}
1505+
diag.emit();
14931506
}
14941507
}
14951508
}

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

+40
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
2424
|
2525
= note: expected constant `{ N as u128 }`
2626
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:5
@@ -51,6 +56,11 @@ LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
5156
|
5257
= note: expected constant `{ N as _ }`
5358
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
@@ -60,6 +70,11 @@ LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
6070
|
6171
= note: expected constant `12`
6272
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
@@ -69,6 +84,11 @@ LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
6984
|
7085
= note: expected constant `13`
7186
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:5
@@ -96,6 +116,11 @@ LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
96116
|
97117
= note: expected constant `{ N as u128 }`
98118
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:5
@@ -123,6 +148,11 @@ LL | assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
123148
|
124149
= note: expected constant `{ N as _ }`
125150
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
@@ -132,6 +162,11 @@ LL | assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
132162
|
133163
= note: expected constant `12`
134164
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
@@ -141,6 +176,11 @@ LL | assert_impl::<HasCastInTraitImpl<14, 13>>();
141176
|
142177
= note: expected constant `13`
143178
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/issue-72819-generic-in-const-eval.full.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ LL | let x: Arr<{usize::MAX}> = Arr {};
66
|
77
= note: expected constant `false`
88
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
@@ -15,6 +22,13 @@ LL | let x: Arr<{usize::MAX}> = Arr {};
1522
|
1623
= note: expected constant `false`
1724
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/issues/issue-73260.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | let x: Arr<{usize::MAX}> = Arr {};
66
|
77
= note: expected constant `false`
88
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
@@ -15,6 +23,14 @@ LL | let x: Arr<{usize::MAX}> = Arr {};
1523
|
1624
= note: expected constant `false`
1725
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

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

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | requires_distinct("str", 12);
66
|
77
= note: expected constant `true`
88
found constant `false`
9+
note: required by a bound in `requires_distinct`
10+
--> $DIR/issue-79674.rs:23:37
11+
|
12+
LL | fn requires_distinct<A, B>(_a: A, _b: B) where
13+
| ----------------- required by a bound in this
14+
LL | A: MiniTypeId, B: MiniTypeId,
15+
LL | Lift<{is_same_type::<A, B>()}>: IsFalse {}
16+
| ^^^^^^^ required by this bound in `requires_distinct`
917

1018
error: aborting due to previous error
1119

0 commit comments

Comments
 (0)