Skip to content

Commit 4ff5872

Browse files
Note binding obligation causes for const equate errors
1 parent 4e97626 commit 4ff5872

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
@@ -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

+16-3
Original file line numberDiff line numberDiff line change
@@ -1506,13 +1506,26 @@ 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(..) = code
1518+
{
1519+
self.note_obligation_cause_code(
1520+
&mut diag,
1521+
&error.obligation.predicate,
1522+
error.obligation.param_env,
1523+
code,
1524+
&mut vec![],
1525+
&mut Default::default(),
1526+
);
1527+
}
1528+
diag.emit();
15161529
}
15171530
}
15181531
}

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:19
@@ -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:19
@@ -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:19
@@ -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)