Skip to content

Commit 9dc7620

Browse files
committed
Fail relating constants of different types
1 parent 7c54789 commit 9dc7620

File tree

13 files changed

+84
-119
lines changed

13 files changed

+84
-119
lines changed

compiler/rustc_infer/src/infer/relate/combine.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'tcx> InferCtxt<'tcx> {
168168
// ourselves with a check to find bugs being required for code to compile because it made inference progress.
169169
self.probe(|_| {
170170
if a.ty() == b.ty() {
171-
return;
171+
return Ok(());
172172
}
173173

174174
// We don't have access to trait solving machinery in `rustc_infer` so the logic for determining if the
@@ -178,18 +178,15 @@ impl<'tcx> InferCtxt<'tcx> {
178178
relation.param_env().and((a.ty(), b.ty())),
179179
&mut OriginalQueryValues::default(),
180180
);
181-
self.tcx.check_tys_might_be_eq(canonical).unwrap_or_else(|_| {
181+
self.tcx.check_tys_might_be_eq(canonical).map_err(|_| {
182182
// The error will only be reported later. If we emit an ErrorGuaranteed
183183
// here, then we will never get to the code that actually emits the error.
184184
self.tcx.dcx().delayed_bug(format!(
185185
"cannot relate consts of different types (a={a:?}, b={b:?})",
186186
));
187-
// We treat these constants as if they were of the same type, so that any
188-
// such constants being used in impls make these impls match barring other mismatches.
189-
// This helps with diagnostics down the road.
190-
});
191-
});
192-
187+
TypeError::Mismatch
188+
})
189+
})?;
193190
match (a.kind(), b.kind()) {
194191
(
195192
ty::ConstKind::Infer(InferConst::Var(a_vid)),

tests/crashes/121585-1.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/crashes/121585-2.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/crashes/121858-2.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/crashes/124151.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/ui/const-generics/bad-subst-const-kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ impl<const N: u64> Q for [u8; N] {
1111
}
1212

1313
pub fn test() -> [u8; <[u8; 13] as Q>::ASSOC] { todo!() }
14-
//~^ ERROR: the constant `13` is not of type `u64`
14+
//~^ ERROR: `[u8; 13]: Q` is not satisfied
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
error: the constant `13` is not of type `u64`
1+
error[E0277]: the trait bound `[u8; 13]: Q` is not satisfied
22
--> $DIR/bad-subst-const-kind.rs:13:24
33
|
44
LL | pub fn test() -> [u8; <[u8; 13] as Q>::ASSOC] { todo!() }
5-
| ^^^^^^^^ expected `u64`, found `usize`
5+
| ^^^^^^^^ the trait `Q` is not implemented for `[u8; 13]`
66
|
7-
note: required for `[u8; 13]` to implement `Q`
8-
--> $DIR/bad-subst-const-kind.rs:8:20
9-
|
10-
LL | impl<const N: u64> Q for [u8; N] {
11-
| ------------ ^ ^^^^^^^
12-
| |
13-
| unsatisfied trait bound introduced here
7+
= help: the trait `Q` is implemented for `[u8; N]`
148

159
error[E0308]: mismatched types
1610
--> $DIR/bad-subst-const-kind.rs:8:31
@@ -20,4 +14,5 @@ LL | impl<const N: u64> Q for [u8; N] {
2014

2115
error: aborting due to 2 previous errors
2216

23-
For more information about this error, try `rustc --explain E0308`.
17+
Some errors have detailed explanations: E0277, E0308.
18+
For more information about an error, try `rustc --explain E0277`.

tests/ui/const-generics/generic_const_exprs/type_mismatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl<const N: u64> Q for [u8; N] {}
1010
//~| ERROR mismatched types
1111

1212
pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
13-
//~^ ERROR the constant `13` is not of type `u64`
13+
//~^ ERROR `[u8; 13]: Q` is not satisfied
1414
//~| ERROR mismatched types
1515

1616
pub fn main() {}

tests/ui/const-generics/generic_const_exprs/type_mismatch.stderr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@ LL | const ASSOC: usize;
77
LL | impl<const N: u64> Q for [u8; N] {}
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `ASSOC` in implementation
99

10-
error: the constant `13` is not of type `u64`
10+
error[E0277]: the trait bound `[u8; 13]: Q` is not satisfied
1111
--> $DIR/type_mismatch.rs:12:26
1212
|
1313
LL | pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {}
14-
| ^^^^^^^^ expected `u64`, found `usize`
14+
| ^^^^^^^^ the trait `Q` is not implemented for `[u8; 13]`
1515
|
16-
note: required for `[u8; 13]` to implement `Q`
17-
--> $DIR/type_mismatch.rs:8:20
18-
|
19-
LL | impl<const N: u64> Q for [u8; N] {}
20-
| ------------ ^ ^^^^^^^
21-
| |
22-
| unsatisfied trait bound introduced here
16+
= help: the trait `Q` is implemented for `[u8; N]`
2317

2418
error[E0308]: mismatched types
2519
--> $DIR/type_mismatch.rs:12:20
@@ -37,5 +31,5 @@ LL | impl<const N: u64> Q for [u8; N] {}
3731

3832
error: aborting due to 4 previous errors
3933

40-
Some errors have detailed explanations: E0046, E0308.
34+
Some errors have detailed explanations: E0046, E0277, E0308.
4135
For more information about an error, try `rustc --explain E0046`.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
//@ known-bug: #121858
21
#![feature(generic_const_exprs)]
2+
#![allow(incomplete_features)]
33

44
struct Outer<const A: i64, const B: usize>();
55
impl<const A: usize, const B: usize> Outer<A, B>
6+
//~^ ERROR: `A` is not of type `i64`
7+
//~| ERROR: mismatched types
68
where
79
[(); A + (B * 2)]:,
810
{
9-
fn o() -> Union {}
11+
fn o() {}
1012
}
1113

1214
fn main() {
1315
Outer::<1, 1>::o();
16+
//~^ ERROR: no function or associated item named `o` found
1417
}

0 commit comments

Comments
 (0)