Skip to content

Commit 11020b9

Browse files
authored
Rollup merge of #106361 - clubby789:int-literal-too-large, r=estebank
Note maximum integer literal for `IntLiteralTooLarge` Closes #105908 `@rustbot` label +A-diagnostics
2 parents d24b229 + 537c7f4 commit 11020b9

File tree

10 files changed

+84
-28
lines changed

10 files changed

+84
-28
lines changed

compiler/rustc_ast/src/util/literal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum LitError {
3434
InvalidIntSuffix,
3535
InvalidFloatSuffix,
3636
NonDecimalFloat(u32),
37-
IntTooLarge,
37+
IntTooLarge(u32),
3838
}
3939

4040
impl LitKind {
@@ -333,6 +333,6 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitErr
333333
// but these kinds of errors are already reported by the lexer.
334334
let from_lexer =
335335
base < 10 && s.chars().any(|c| c.to_digit(10).map_or(false, |d| d >= base));
336-
if from_lexer { LitError::LexerError } else { LitError::IntTooLarge }
336+
if from_lexer { LitError::LexerError } else { LitError::IntTooLarge(base) }
337337
})
338338
}

compiler/rustc_error_messages/locales/en-US/session.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ session_invalid_float_literal_suffix = invalid suffix `{$suffix}` for float lite
8585
.help = valid suffixes are `f32` and `f64`
8686
8787
session_int_literal_too_large = integer literal is too large
88+
.note = value exceeds limit of `{$limit}`
8889
8990
session_invalid_int_literal_width = invalid width `{$width}` for integer literal
9091
.help = valid widths are 8, 16, 32, 64 and 128

compiler/rustc_session/src/errors.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,11 @@ pub(crate) struct InvalidFloatLiteralSuffix {
260260

261261
#[derive(Diagnostic)]
262262
#[diag(session_int_literal_too_large)]
263+
#[note]
263264
pub(crate) struct IntLiteralTooLarge {
264265
#[primary_span]
265266
pub span: Span,
267+
pub limit: String,
266268
}
267269

268270
#[derive(Diagnostic)]
@@ -361,8 +363,15 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span:
361363
_ => unreachable!(),
362364
};
363365
}
364-
LitError::IntTooLarge => {
365-
sess.emit_err(IntLiteralTooLarge { span });
366+
LitError::IntTooLarge(base) => {
367+
let max = u128::MAX;
368+
let limit = match base {
369+
2 => format!("{max:#b}"),
370+
8 => format!("{max:#o}"),
371+
16 => format!("{max:#x}"),
372+
_ => format!("{max}"),
373+
};
374+
sess.emit_err(IntLiteralTooLarge { span, limit });
366375
}
367376
}
368377
}

src/test/ui/lexer/error-stage.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ error: integer literal is too large
4949
|
5050
LL | 999340282366920938463463374607431768211455999;
5151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
|
53+
= note: value exceeds limit of `340282366920938463463374607431768211455`
5254

5355
error: aborting due to 8 previous errors
5456

src/test/ui/lexer/lex-bad-numeric-literals.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-tidy-linelength
2+
13
fn main() {
24
0o1.0; //~ ERROR: octal float literal is not supported
35
0o2f32; //~ ERROR: octal float literal is not supported
@@ -15,6 +17,12 @@ fn main() {
1517
//~^ ERROR: integer literal is too large
1618
9900000000000000000000000000999999999999999999999999999999;
1719
//~^ ERROR: integer literal is too large
20+
0b111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110;
21+
//~^ ERROR: integer literal is too large
22+
0o37777777777777777777777777777777777777777770;
23+
//~^ ERROR: integer literal is too large
24+
0xffffffffffffffffffffffffffffffff0;
25+
//~^ ERROR: integer literal is too large
1826
0x; //~ ERROR: no valid digits
1927
0xu32; //~ ERROR: no valid digits
2028
0ou32; //~ ERROR: no valid digits
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,169 @@
11
error: octal float literal is not supported
2-
--> $DIR/lex-bad-numeric-literals.rs:2:5
2+
--> $DIR/lex-bad-numeric-literals.rs:4:5
33
|
44
LL | 0o1.0;
55
| ^^^^^
66

77
error: octal float literal is not supported
8-
--> $DIR/lex-bad-numeric-literals.rs:4:5
8+
--> $DIR/lex-bad-numeric-literals.rs:6:5
99
|
1010
LL | 0o3.0f32;
1111
| ^^^^^
1212

1313
error: octal float literal is not supported
14-
--> $DIR/lex-bad-numeric-literals.rs:5:5
14+
--> $DIR/lex-bad-numeric-literals.rs:7:5
1515
|
1616
LL | 0o4e4;
1717
| ^^^^^
1818

1919
error: octal float literal is not supported
20-
--> $DIR/lex-bad-numeric-literals.rs:6:5
20+
--> $DIR/lex-bad-numeric-literals.rs:8:5
2121
|
2222
LL | 0o5.0e5;
2323
| ^^^^^^^
2424

2525
error: octal float literal is not supported
26-
--> $DIR/lex-bad-numeric-literals.rs:7:5
26+
--> $DIR/lex-bad-numeric-literals.rs:9:5
2727
|
2828
LL | 0o6e6f32;
2929
| ^^^^^
3030

3131
error: octal float literal is not supported
32-
--> $DIR/lex-bad-numeric-literals.rs:8:5
32+
--> $DIR/lex-bad-numeric-literals.rs:10:5
3333
|
3434
LL | 0o7.0e7f64;
3535
| ^^^^^^^
3636

3737
error: hexadecimal float literal is not supported
38-
--> $DIR/lex-bad-numeric-literals.rs:9:5
38+
--> $DIR/lex-bad-numeric-literals.rs:11:5
3939
|
4040
LL | 0x8.0e+9;
4141
| ^^^^^^^^
4242

4343
error: hexadecimal float literal is not supported
44-
--> $DIR/lex-bad-numeric-literals.rs:10:5
44+
--> $DIR/lex-bad-numeric-literals.rs:12:5
4545
|
4646
LL | 0x9.0e-9;
4747
| ^^^^^^^^
4848

4949
error[E0768]: no valid digits found for number
50-
--> $DIR/lex-bad-numeric-literals.rs:11:5
50+
--> $DIR/lex-bad-numeric-literals.rs:13:5
5151
|
5252
LL | 0o;
5353
| ^^
5454

5555
error: expected at least one digit in exponent
56-
--> $DIR/lex-bad-numeric-literals.rs:12:5
56+
--> $DIR/lex-bad-numeric-literals.rs:14:5
5757
|
5858
LL | 1e+;
5959
| ^^^
6060

6161
error: hexadecimal float literal is not supported
62-
--> $DIR/lex-bad-numeric-literals.rs:13:5
62+
--> $DIR/lex-bad-numeric-literals.rs:15:5
6363
|
6464
LL | 0x539.0;
6565
| ^^^^^^^
6666

6767
error[E0768]: no valid digits found for number
68-
--> $DIR/lex-bad-numeric-literals.rs:18:5
68+
--> $DIR/lex-bad-numeric-literals.rs:26:5
6969
|
7070
LL | 0x;
7171
| ^^
7272

7373
error[E0768]: no valid digits found for number
74-
--> $DIR/lex-bad-numeric-literals.rs:19:5
74+
--> $DIR/lex-bad-numeric-literals.rs:27:5
7575
|
7676
LL | 0xu32;
7777
| ^^
7878

7979
error[E0768]: no valid digits found for number
80-
--> $DIR/lex-bad-numeric-literals.rs:20:5
80+
--> $DIR/lex-bad-numeric-literals.rs:28:5
8181
|
8282
LL | 0ou32;
8383
| ^^
8484

8585
error[E0768]: no valid digits found for number
86-
--> $DIR/lex-bad-numeric-literals.rs:21:5
86+
--> $DIR/lex-bad-numeric-literals.rs:29:5
8787
|
8888
LL | 0bu32;
8989
| ^^
9090

9191
error[E0768]: no valid digits found for number
92-
--> $DIR/lex-bad-numeric-literals.rs:22:5
92+
--> $DIR/lex-bad-numeric-literals.rs:30:5
9393
|
9494
LL | 0b;
9595
| ^^
9696

9797
error: octal float literal is not supported
98-
--> $DIR/lex-bad-numeric-literals.rs:24:5
98+
--> $DIR/lex-bad-numeric-literals.rs:32:5
9999
|
100100
LL | 0o123.456;
101101
| ^^^^^^^^^
102102

103103
error: binary float literal is not supported
104-
--> $DIR/lex-bad-numeric-literals.rs:26:5
104+
--> $DIR/lex-bad-numeric-literals.rs:34:5
105105
|
106106
LL | 0b111.101;
107107
| ^^^^^^^^^
108108

109109
error: octal float literal is not supported
110-
--> $DIR/lex-bad-numeric-literals.rs:3:5
110+
--> $DIR/lex-bad-numeric-literals.rs:5:5
111111
|
112112
LL | 0o2f32;
113113
| ^^^^^^ not supported
114114

115115
error: integer literal is too large
116-
--> $DIR/lex-bad-numeric-literals.rs:14:5
116+
--> $DIR/lex-bad-numeric-literals.rs:16:5
117117
|
118118
LL | 9900000000000000000000000000999999999999999999999999999999;
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120+
|
121+
= note: value exceeds limit of `340282366920938463463374607431768211455`
120122

121123
error: integer literal is too large
122-
--> $DIR/lex-bad-numeric-literals.rs:16:5
124+
--> $DIR/lex-bad-numeric-literals.rs:18:5
123125
|
124126
LL | 9900000000000000000000000000999999999999999999999999999999;
125127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128+
|
129+
= note: value exceeds limit of `340282366920938463463374607431768211455`
130+
131+
error: integer literal is too large
132+
--> $DIR/lex-bad-numeric-literals.rs:20:5
133+
|
134+
LL | 0b111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110;
135+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
136+
|
137+
= note: value exceeds limit of `0b11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111`
138+
139+
error: integer literal is too large
140+
--> $DIR/lex-bad-numeric-literals.rs:22:5
141+
|
142+
LL | 0o37777777777777777777777777777777777777777770;
143+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
144+
|
145+
= note: value exceeds limit of `0o3777777777777777777777777777777777777777777`
146+
147+
error: integer literal is too large
148+
--> $DIR/lex-bad-numeric-literals.rs:24:5
149+
|
150+
LL | 0xffffffffffffffffffffffffffffffff0;
151+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152+
|
153+
= note: value exceeds limit of `0xffffffffffffffffffffffffffffffff`
126154

127155
error: octal float literal is not supported
128-
--> $DIR/lex-bad-numeric-literals.rs:23:5
156+
--> $DIR/lex-bad-numeric-literals.rs:31:5
129157
|
130158
LL | 0o123f64;
131159
| ^^^^^^^^ not supported
132160

133161
error: binary float literal is not supported
134-
--> $DIR/lex-bad-numeric-literals.rs:25:5
162+
--> $DIR/lex-bad-numeric-literals.rs:33:5
135163
|
136164
LL | 0b101f64;
137165
| ^^^^^^^^ not supported
138166

139-
error: aborting due to 23 previous errors
167+
error: aborting due to 26 previous errors
140168

141169
For more information about this error, try `rustc --explain E0768`.

src/test/ui/macros/issue-104769-concat_bytes-invalid-literal.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ error: integer literal is too large
1111
|
1212
LL | concat_bytes!(888888888888888888888888888888888888888);
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: value exceeds limit of `340282366920938463463374607431768211455`
1416

1517
error: aborting due to 2 previous errors
1618

src/test/ui/parser/int-literal-too-large-span.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: integer literal is too large
33
|
44
LL | 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: value exceeds limit of `340282366920938463463374607431768211455`
68

79
error: aborting due to previous error
810

src/test/ui/parser/issues/issue-5544-a.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: integer literal is too large
33
|
44
LL | let __isize = 340282366920938463463374607431768211456; // 2^128
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: value exceeds limit of `340282366920938463463374607431768211455`
68

79
error: aborting due to previous error
810

src/test/ui/parser/issues/issue-5544-b.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: integer literal is too large
33
|
44
LL | let __isize = 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff_ff;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: value exceeds limit of `0xffffffffffffffffffffffffffffffff`
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)