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

+5-8
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

-13
This file was deleted.

tests/crashes/121585-2.rs

-30
This file was deleted.

tests/crashes/121858-2.rs

-20
This file was deleted.

tests/crashes/124151.rs

-14
This file was deleted.

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

+1-1
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
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

+1-1
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

+4-10
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`.
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
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: the constant `A` is not of type `i64`
2+
--> $DIR/eval_type_mismatch.rs:5:38
3+
|
4+
LL | impl<const A: usize, const B: usize> Outer<A, B>
5+
| ^^^^^^^^^^^ expected `i64`, found `usize`
6+
|
7+
note: required by a bound in `Outer`
8+
--> $DIR/eval_type_mismatch.rs:4:14
9+
|
10+
LL | struct Outer<const A: i64, const B: usize>();
11+
| ^^^^^^^^^^^^ required by this bound in `Outer`
12+
13+
error[E0599]: no function or associated item named `o` found for struct `Outer<1, 1>` in the current scope
14+
--> $DIR/eval_type_mismatch.rs:15:20
15+
|
16+
LL | struct Outer<const A: i64, const B: usize>();
17+
| ------------------------------------------ function or associated item `o` not found for this struct
18+
...
19+
LL | Outer::<1, 1>::o();
20+
| ^ function or associated item not found in `Outer<1, 1>`
21+
|
22+
= note: the function or associated item was found for
23+
- `Outer<A, B>`
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/eval_type_mismatch.rs:5:44
27+
|
28+
LL | impl<const A: usize, const B: usize> Outer<A, B>
29+
| ^ expected `i64`, found `usize`
30+
31+
error: aborting due to 3 previous errors
32+
33+
Some errors have detailed explanations: E0308, E0599.
34+
For more information about an error, try `rustc --explain E0308`.

tests/ui/specialization/min_specialization/bad-const-wf-doesnt-specialize.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ struct S<const L: usize>;
77

88
impl<const N: i32> Copy for S<N> {}
99
//~^ ERROR: mismatched types
10+
//~| ERROR: the trait bound `S<N>: Clone` is not satisfied
11+
//~| ERROR: the constant `N` is not of type `usize`
1012
impl<const M: usize> Copy for S<M> {}
11-
//~^ ERROR: conflicting implementations of trait `Copy` for type `S<_>`
1213

1314
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
1-
error[E0119]: conflicting implementations of trait `Copy` for type `S<_>`
2-
--> $DIR/bad-const-wf-doesnt-specialize.rs:10:1
1+
error[E0277]: the trait bound `S<N>: Clone` is not satisfied
2+
--> $DIR/bad-const-wf-doesnt-specialize.rs:8:29
33
|
44
LL | impl<const N: i32> Copy for S<N> {}
5-
| -------------------------------- first implementation here
6-
LL |
7-
LL | impl<const M: usize> Copy for S<M> {}
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `S<_>`
5+
| ^^^^ the trait `Clone` is not implemented for `S<N>`
6+
|
7+
= help: the trait `Clone` is implemented for `S<L>`
8+
note: required by a bound in `Copy`
9+
--> $SRC_DIR/core/src/marker.rs:LL:COL
10+
help: consider annotating `S<N>` with `#[derive(Clone)]`
11+
|
12+
LL + #[derive(Clone)]
13+
LL | struct S<const L: usize>;
14+
|
15+
16+
error: the constant `N` is not of type `usize`
17+
--> $DIR/bad-const-wf-doesnt-specialize.rs:8:29
18+
|
19+
LL | impl<const N: i32> Copy for S<N> {}
20+
| ^^^^ expected `usize`, found `i32`
21+
|
22+
note: required by a bound in `S`
23+
--> $DIR/bad-const-wf-doesnt-specialize.rs:6:10
24+
|
25+
LL | struct S<const L: usize>;
26+
| ^^^^^^^^^^^^^^ required by this bound in `S`
927

1028
error[E0308]: mismatched types
1129
--> $DIR/bad-const-wf-doesnt-specialize.rs:8:31
1230
|
1331
LL | impl<const N: i32> Copy for S<N> {}
1432
| ^ expected `usize`, found `i32`
1533

16-
error: aborting due to 2 previous errors
34+
error: aborting due to 3 previous errors
1735

18-
Some errors have detailed explanations: E0119, E0308.
19-
For more information about an error, try `rustc --explain E0119`.
36+
Some errors have detailed explanations: E0277, E0308.
37+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)