From 5363f350887b1e5b5dd21a86f88c8af9d7fea6da Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:25 +0200 Subject: Merging upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- src/test/ui/lexer/error-stage.rs | 80 ++++++++++++++++++++++++ src/test/ui/lexer/error-stage.stderr | 54 ++++++++++++++++ src/test/ui/lexer/lex-bad-char-literals-6.stderr | 18 +++--- 3 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 src/test/ui/lexer/error-stage.rs create mode 100644 src/test/ui/lexer/error-stage.stderr (limited to 'src/test/ui/lexer') diff --git a/src/test/ui/lexer/error-stage.rs b/src/test/ui/lexer/error-stage.rs new file mode 100644 index 000000000..c8d88f745 --- /dev/null +++ b/src/test/ui/lexer/error-stage.rs @@ -0,0 +1,80 @@ +// This test is about the treatment of invalid literals. In particular, some +// literals are only considered invalid if they survive to HIR lowering. +// +// Literals with bad suffixes +// -------------------------- +// Literals consist of a primary part and an optional suffix. +// https://doc.rust-lang.org/reference/tokens.html#suffixes says: +// +// Any kind of literal (string, integer, etc) with any suffix is valid as a +// token, and can be passed to a macro without producing an error. The macro +// itself will decide how to interpret such a token and whether to produce an +// error or not. +// +// ``` +// macro_rules! blackhole { ($tt:tt) => () } +// blackhole!("string"suffix); // OK +// ``` +// +// However, suffixes on literal tokens parsed as Rust code are restricted. +// Any suffixes are rejected on non-numeric literal tokens, and numeric +// literal tokens are accepted only with suffixes from the list below. +// +// Integer: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize +// Floating-point: f32, f64 +// +// This means that something like `"string"any_suffix` is a token accepted by +// the lexer, but rejected later for being an invalid combination of primary +// part and suffix. +// +// `0b10f32` is a similar case. `0b10` is a valid primary part that is a valid +// *integer* literal when no suffix is present. It only causes an error later +// when combined with the `f32` float suffix. +// +// However, `0b10.0f32` is different. It is rejected by the lexer because +// `0b10.0` is not a valid token even on its own. +// +// This difference is unfortunate, but it's baked into the language now. +// +// Too-large integer literals +// -------------------------- +// https://doc.rust-lang.org/reference/tokens.html#integer-literals says that +// literals like `128_i8` and `256_u8` "are too big for their type, but are +// still valid tokens". + +macro_rules! sink { + ($($x:tt;)*) => {()} +} + +// The invalid literals are ignored because the macro consumes them. Except for +// `0b10.0f32` because it's a lexer error. +const _: () = sink! { + "string"any_suffix; // OK + 10u123; // OK + 10.0f123; // OK + 0b10f32; // OK + 0b10.0f32; //~ ERROR binary float literal is not supported + 999340282366920938463463374607431768211455999; // OK +}; + +// The invalid literals used to cause errors, but this was changed by #102944. +// Except for `0b010.0f32`, because it's a lexer error. +#[cfg(FALSE)] +fn configured_out() { + "string"any_suffix; // OK + 10u123; // OK + 10.0f123; // OK + 0b10f32; // OK + 0b10.0f32; //~ ERROR binary float literal is not supported + 999340282366920938463463374607431768211455999; // OK +} + +// All the invalid literals cause errors. +fn main() { + "string"any_suffix; //~ ERROR suffixes on string literals are invalid + 10u123; //~ ERROR invalid width `123` for integer literal + 10.0f123; //~ ERROR invalid width `123` for float literal + 0b10f32; //~ ERROR binary float literal is not supported + 0b10.0f32; //~ ERROR binary float literal is not supported + 999340282366920938463463374607431768211455999; //~ ERROR integer literal is too large +} diff --git a/src/test/ui/lexer/error-stage.stderr b/src/test/ui/lexer/error-stage.stderr new file mode 100644 index 000000000..697a7c28d --- /dev/null +++ b/src/test/ui/lexer/error-stage.stderr @@ -0,0 +1,54 @@ +error: binary float literal is not supported + --> $DIR/error-stage.rs:56:5 + | +LL | 0b10.0f32; + | ^^^^^^ + +error: binary float literal is not supported + --> $DIR/error-stage.rs:68:5 + | +LL | 0b10.0f32; + | ^^^^^^ + +error: binary float literal is not supported + --> $DIR/error-stage.rs:78:5 + | +LL | 0b10.0f32; + | ^^^^^^ + +error: suffixes on string literals are invalid + --> $DIR/error-stage.rs:74:5 + | +LL | "string"any_suffix; + | ^^^^^^^^^^^^^^^^^^ invalid suffix `any_suffix` + +error: invalid width `123` for integer literal + --> $DIR/error-stage.rs:75:5 + | +LL | 10u123; + | ^^^^^^ + | + = help: valid widths are 8, 16, 32, 64 and 128 + +error: invalid width `123` for float literal + --> $DIR/error-stage.rs:76:5 + | +LL | 10.0f123; + | ^^^^^^^^ + | + = help: valid widths are 32 and 64 + +error: binary float literal is not supported + --> $DIR/error-stage.rs:77:5 + | +LL | 0b10f32; + | ^^^^^^^ not supported + +error: integer literal is too large + --> $DIR/error-stage.rs:79:5 + | +LL | 999340282366920938463463374607431768211455999; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 8 previous errors + diff --git a/src/test/ui/lexer/lex-bad-char-literals-6.stderr b/src/test/ui/lexer/lex-bad-char-literals-6.stderr index afef0cb60..ce4194246 100644 --- a/src/test/ui/lexer/lex-bad-char-literals-6.stderr +++ b/src/test/ui/lexer/lex-bad-char-literals-6.stderr @@ -42,12 +42,11 @@ LL | if x == y {} <&'a str as PartialEq> <&'a str as PartialEq> <&'b str as PartialEq>> - > - >> - > - >> - and 4 others + > + > + > + error[E0308]: mismatched types --> $DIR/lex-bad-char-literals-6.rs:15:20 @@ -68,12 +67,11 @@ LL | if x == z {} <&'a str as PartialEq> <&'a str as PartialEq> <&'b str as PartialEq>> - > - >> - > - >> - and 4 others + > + > + > + error: aborting due to 6 previous errors -- cgit v1.2.3