]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lexer/error-stage.rs
Rollup merge of #105955 - Nilstrieb:no-trivial-opt-wrappers-we-have-field-accesses...
[rust.git] / src / test / ui / lexer / error-stage.rs
1 // This test is about the treatment of invalid literals. In particular, some
2 // literals are only considered invalid if they survive to HIR lowering.
3 //
4 // Literals with bad suffixes
5 // --------------------------
6 // Literals consist of a primary part and an optional suffix.
7 // https://doc.rust-lang.org/reference/tokens.html#suffixes says:
8 //
9 //   Any kind of literal (string, integer, etc) with any suffix is valid as a
10 //   token, and can be passed to a macro without producing an error. The macro
11 //   itself will decide how to interpret such a token and whether to produce an
12 //   error or not.
13 //
14 //   ```
15 //   macro_rules! blackhole { ($tt:tt) => () }
16 //   blackhole!("string"suffix); // OK
17 //   ```
18 //
19 //   However, suffixes on literal tokens parsed as Rust code are restricted.
20 //   Any suffixes are rejected on non-numeric literal tokens, and numeric
21 //   literal tokens are accepted only with suffixes from the list below.
22 //
23 //   Integer: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize
24 //   Floating-point: f32, f64
25 //
26 // This means that something like `"string"any_suffix` is a token accepted by
27 // the lexer, but rejected later for being an invalid combination of primary
28 // part and suffix.
29 //
30 // `0b10f32` is a similar case. `0b10` is a valid primary part that is a valid
31 // *integer* literal when no suffix is present. It only causes an error later
32 // when combined with the `f32` float suffix.
33 //
34 // However, `0b10.0f32` is different. It is rejected by the lexer because
35 // `0b10.0` is not a valid token even on its own.
36 //
37 // This difference is unfortunate, but it's baked into the language now.
38 //
39 // Too-large integer literals
40 // --------------------------
41 // https://doc.rust-lang.org/reference/tokens.html#integer-literals says that
42 // literals like `128_i8` and `256_u8` "are too big for their type, but are
43 // still valid tokens".
44
45 macro_rules! sink {
46     ($($x:tt;)*) => {()}
47 }
48
49 // The invalid literals are ignored because the macro consumes them. Except for
50 // `0b10.0f32` because it's a lexer error.
51 const _: () = sink! {
52     "string"any_suffix; // OK
53     10u123; // OK
54     10.0f123; // OK
55     0b10f32; // OK
56     0b10.0f32; //~ ERROR binary float literal is not supported
57     999340282366920938463463374607431768211455999; // OK
58 };
59
60 // The invalid literals used to cause errors, but this was changed by #102944.
61 // Except for `0b010.0f32`, because it's a lexer error.
62 #[cfg(FALSE)]
63 fn configured_out() {
64     "string"any_suffix; // OK
65     10u123; // OK
66     10.0f123; // OK
67     0b10f32; // OK
68     0b10.0f32; //~ ERROR binary float literal is not supported
69     999340282366920938463463374607431768211455999; // OK
70 }
71
72 // All the invalid literals cause errors.
73 fn main() {
74     "string"any_suffix; //~ ERROR suffixes on string literals are invalid
75     10u123; //~ ERROR invalid width `123` for integer literal
76     10.0f123; //~ ERROR invalid width `123` for float literal
77     0b10f32; //~ ERROR binary float literal is not supported
78     0b10.0f32; //~ ERROR binary float literal is not supported
79     999340282366920938463463374607431768211455999; //~ ERROR integer literal is too large
80 }