Skip to content

Commit 80d29cd

Browse files
committed
Make const_lit_matches_ty check literal suffixes for exact type match
1 parent 1d113d2 commit 80d29cd

File tree

10 files changed

+43
-81
lines changed

10 files changed

+43
-81
lines changed

compiler/rustc_middle/src/ty/consts/lit.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::LitKind;
1+
use rustc_ast::{LitFloatType, LitIntType, LitKind};
22
use rustc_hir;
33
use rustc_macros::HashStable;
44

@@ -44,10 +44,17 @@ pub fn const_lit_matches_ty<'tcx>(
4444
{
4545
true
4646
}
47-
(LitKind::Int(..), ty::Uint(_)) if !neg => true,
48-
(LitKind::Int(..), ty::Int(_)) => true,
47+
(LitKind::Int(_, LitIntType::Unsigned(lit_ty)), ty::Uint(expect_ty)) if !neg => {
48+
lit_ty == *expect_ty
49+
}
50+
(LitKind::Int(_, LitIntType::Signed(lit_ty)), ty::Int(expect_ty)) => lit_ty == *expect_ty,
51+
(LitKind::Int(_, LitIntType::Unsuffixed), ty::Uint(_)) if !neg => true,
52+
(LitKind::Int(_, LitIntType::Unsuffixed), ty::Int(_)) => true,
4953
(LitKind::Bool(..), ty::Bool) => true,
50-
(LitKind::Float(..), ty::Float(_)) => true,
54+
(LitKind::Float(_, LitFloatType::Suffixed(lit_ty)), ty::Float(expect_ty)) => {
55+
lit_ty == *expect_ty
56+
}
57+
(LitKind::Float(_, LitFloatType::Unsuffixed), ty::Float(_)) => true,
5158
(LitKind::Char(..), ty::Char) => true,
5259
(LitKind::Err(..), _) => true,
5360
_ => false,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(min_generic_const_args)]
2+
#![expect(incomplete_features)]
3+
4+
type const CONST: usize = 1_i32;
5+
//~^ ERROR the constant `1` is not of type `usize`
6+
//~| NOTE expected `usize`, found `i32`
7+
8+
fn main() {
9+
const { CONST };
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: the constant `1` is not of type `usize`
2+
--> $DIR/type_const-mismatched-literal-suffix.rs:4:1
3+
|
4+
LL | type const CONST: usize = 1_i32;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `i32`
6+
7+
error: aborting due to 1 previous error
8+
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
error: the constant `1` is not of type `u8`
2-
--> $DIR/type-mismatch.rs:8:27
3-
|
4-
LL | assert_eq!(R.method::<1u16>(), 1);
5-
| ^^^^ expected `u8`, found `u16`
6-
|
7-
note: required by a const generic parameter in `R::method`
8-
--> $DIR/type-mismatch.rs:5:15
9-
|
10-
LL | fn method<const N: u8>(&self) -> u8 { N }
11-
| ^^^^^^^^^^^ required by this const generic parameter in `R::method`
12-
131
error[E0308]: mismatched types
142
--> $DIR/type-mismatch.rs:8:27
153
|
@@ -22,6 +10,6 @@ LL - assert_eq!(R.method::<1u16>(), 1);
2210
LL + assert_eq!(R.method::<1u8>(), 1);
2311
|
2412

25-
error: aborting due to 2 previous errors
13+
error: aborting due to 1 previous error
2614

2715
For more information about this error, try `rustc --explain E0308`.
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
error: the constant `1` is not of type `u8`
2-
--> $DIR/type-mismatch.rs:8:27
3-
|
4-
LL | assert_eq!(R.method::<1u16>(), 1);
5-
| ^^^^ expected `u8`, found `u16`
6-
|
7-
note: required by a const generic parameter in `R::method`
8-
--> $DIR/type-mismatch.rs:5:15
9-
|
10-
LL | fn method<const N: u8>(&self) -> u8 { N }
11-
| ^^^^^^^^^^^ required by this const generic parameter in `R::method`
12-
131
error[E0308]: mismatched types
142
--> $DIR/type-mismatch.rs:8:27
153
|
@@ -22,6 +10,6 @@ LL - assert_eq!(R.method::<1u16>(), 1);
2210
LL + assert_eq!(R.method::<1u8>(), 1);
2311
|
2412

25-
error: aborting due to 2 previous errors
13+
error: aborting due to 1 previous error
2614

2715
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/type-dependent/type-mismatch.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ impl R {
66
}
77
fn main() {
88
assert_eq!(R.method::<1u16>(), 1);
9-
//~^ ERROR the constant `1` is not of type `u8`
10-
//~| ERROR mismatched types
9+
//~^ ERROR mismatched types
1110
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//! Regression test for <https://github.com/rust-lang/rust/issues/133966>
22
pub struct Data([[&'static str]; 5_i32]);
3-
//~^ ERROR the constant `5` is not of type `usize`
4-
//~| ERROR the size for values of type `[&'static str]` cannot be known at compilation time
5-
//~| ERROR mismatched types
3+
//~^ ERROR mismatched types
64
const _: &'static Data = unsafe { &*(&[] as *const Data) };
7-
//~^ ERROR the type `[[&str]; 5]` has an unknown layout
85
fn main() {}
Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
1-
error: the constant `5` is not of type `usize`
2-
--> $DIR/array-len-mismatch-type.rs:2:17
3-
|
4-
LL | pub struct Data([[&'static str]; 5_i32]);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `i32`
6-
|
7-
= note: the length of array `[[&'static str]; 5]` must be type `usize`
8-
9-
error[E0277]: the size for values of type `[&'static str]` cannot be known at compilation time
10-
--> $DIR/array-len-mismatch-type.rs:2:17
11-
|
12-
LL | pub struct Data([[&'static str]; 5_i32]);
13-
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
14-
|
15-
= help: the trait `Sized` is not implemented for `[&'static str]`
16-
= note: slice and array elements must have `Sized` type
17-
18-
error[E0080]: the type `[[&str]; 5]` has an unknown layout
19-
--> $DIR/array-len-mismatch-type.rs:6:39
20-
|
21-
LL | const _: &'static Data = unsafe { &*(&[] as *const Data) };
22-
| ^^ evaluation of `_` failed here
23-
241
error[E0308]: mismatched types
252
--> $DIR/array-len-mismatch-type.rs:2:34
263
|
@@ -33,7 +10,6 @@ LL - pub struct Data([[&'static str]; 5_i32]);
3310
LL + pub struct Data([[&'static str]; 5_usize]);
3411
|
3512

36-
error: aborting due to 4 previous errors
13+
error: aborting due to 1 previous error
3714

38-
Some errors have detailed explanations: E0080, E0277, E0308.
39-
For more information about an error, try `rustc --explain E0080`.
15+
For more information about this error, try `rustc --explain E0308`.

tests/ui/repeat-expr/repeat_count.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ fn main() {
2727
//~| NOTE expected `usize`, found `isize`
2828
//~| NOTE `-1_isize` cannot fit into type `usize`
2929
let h = [0; 4u8];
30-
//~^ ERROR the constant `4` is not of type `usize`
31-
//~| NOTE expected `usize`, found `u8`
32-
//~| NOTE the length of array `[{integer}; 4]` must be type `usize`
33-
//~| ERROR mismatched types
30+
//~^ ERROR mismatched types
3431
//~| NOTE expected `usize`, found `u8`
3532
struct I {
3633
i: (),

tests/ui/repeat-expr/repeat_count.stderr

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,6 @@ LL | let g = [0_usize; -1_isize];
5050
|
5151
= note: `-1_isize` cannot fit into type `usize`
5252

53-
error: the constant `4` is not of type `usize`
54-
--> $DIR/repeat_count.rs:29:13
55-
|
56-
LL | let h = [0; 4u8];
57-
| ^^^^^^^^ expected `usize`, found `u8`
58-
|
59-
= note: the length of array `[{integer}; 4]` must be type `usize`
60-
61-
error[E0308]: mismatched types
62-
--> $DIR/repeat_count.rs:38:17
63-
|
64-
LL | let i = [0; I { i: () }];
65-
| ^^^^^^^^^^^ expected `usize`, found `I`
66-
6753
error[E0308]: mismatched types
6854
--> $DIR/repeat_count.rs:29:17
6955
|
@@ -76,7 +62,13 @@ LL - let h = [0; 4u8];
7662
LL + let h = [0; 4usize];
7763
|
7864

79-
error: aborting due to 10 previous errors
65+
error[E0308]: mismatched types
66+
--> $DIR/repeat_count.rs:35:17
67+
|
68+
LL | let i = [0; I { i: () }];
69+
| ^^^^^^^^^^^ expected `usize`, found `I`
70+
71+
error: aborting due to 9 previous errors
8072

8173
Some errors have detailed explanations: E0308, E0435.
8274
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)