Skip to content

Commit c8bd109

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

12 files changed

Lines changed: 77 additions & 83 deletions

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ compile-flags: --emit=link
2+
3+
#![feature(min_generic_const_args)]
4+
#![expect(incomplete_features)]
5+
6+
type const CONST: usize = 1_i32;
7+
//~^ ERROR the constant `1` is not of type `usize`
8+
//~| NOTE expected `usize`, found `i32`
9+
10+
fn main() {
11+
const { CONST };
12+
//~^ ERROR transmuting from 4-byte type to 8-byte type: `i32` -> `usize`
13+
//~| NOTE evaluation of `main::{constant#0}` failed here
14+
//~| NOTE erroneous constant encountered
15+
//~| NOTE erroneous constant encountered
16+
//~| NOTE duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: the constant `1` is not of type `usize`
2+
--> $DIR/type_const-mismatched-literal-suffix.rs:8:1
3+
|
4+
LL | type const CONST: usize = 1_i32;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `i32`
6+
7+
error[E0080]: transmuting from 4-byte type to 8-byte type: `i32` -> `usize`
8+
--> $DIR/type_const-mismatched-literal-suffix.rs:13:13
9+
|
10+
LL | const { CONST };
11+
| ^^^^^ evaluation of `main::{constant#0}` failed here
12+
13+
note: erroneous constant encountered
14+
--> $DIR/type_const-mismatched-literal-suffix.rs:13:5
15+
|
16+
LL | const { CONST };
17+
| ^^^^^^^^^^^^^^^
18+
19+
note: erroneous constant encountered
20+
--> $DIR/type_const-mismatched-literal-suffix.rs:13:5
21+
|
22+
LL | const { CONST };
23+
| ^^^^^^^^^^^^^^^
24+
|
25+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0080`.

tests/ui/const-generics/mgca/type_const-mismatched-types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
type const FREE: u32 = 5_usize;
55
//~^ ERROR the constant `5` is not of type `u32`
6+
//~| NOTE expected `u32`, found `usize`
67

78
type const FREE2: isize = FREE;
89
//~^ ERROR the constant `5` is not of type `isize`
10+
//~| NOTE expected `isize`, found `usize`
911

1012
trait Tr {
1113
type const N: usize;
@@ -14,6 +16,8 @@ trait Tr {
1416
impl Tr for () {
1517
type const N: usize = false;
1618
//~^ ERROR the constant `false` is not of type `usize`
19+
//~| NOTE expected `usize`, found `bool`
1720
}
1821

1922
fn main() {}
23+

tests/ui/const-generics/mgca/type_const-mismatched-types.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ LL | type const FREE: u32 = 5_usize;
55
| ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `usize`
66

77
error: the constant `5` is not of type `isize`
8-
--> $DIR/type_const-mismatched-types.rs:7:1
8+
--> $DIR/type_const-mismatched-types.rs:8:1
99
|
1010
LL | type const FREE2: isize = FREE;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `usize`
1212

1313
error: the constant `false` is not of type `usize`
14-
--> $DIR/type_const-mismatched-types.rs:15:5
14+
--> $DIR/type_const-mismatched-types.rs:17:5
1515
|
1616
LL | type const N: usize = false;
1717
| ^^^^^^^^^^^^^^^^^^^ expected `usize`, found `bool`
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`.

0 commit comments

Comments
 (0)