You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Numeric literals: say that the parser is now more lenient
This reflects the changes made in in rust-lang/rust#102944 .
Previously, unknown suffixes were rejected by the parser. Now they are
accepted by the parser and rejected at a later stage.
Similarly, integer literals too large to fit in u128 are now accepted by the
parser.
Forms like 5f32 are now INTEGER_LITERAL rather than FLOAT_LITERAL.
The notion of a 'pseudoliteral' is no longer required.
> [^out-of-range]: A value ≥ 2<sup>128</sup> is not allowed.
16
14
17
15
A _literal expression_ is an expression consisting of a single token, rather than a sequence of tokens, that immediately and directly denotes the value it evaluates to, rather than referring to it by name or some other evaluation rule.
18
16
@@ -54,7 +52,7 @@ A string literal expression consists of a single [BYTE_STRING_LITERAL] or [RAW_B
54
52
55
53
An integer literal expression consists of a single [INTEGER_LITERAL] token.
56
54
57
-
If the token has a [suffix], the suffix will be the name of one of the [primitive integer types][numeric types]: `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`, `u128`, `i128`, `usize`, or `isize`, and the expression has that type.
55
+
If the token has a [suffix], the suffix must be the name of one of the [primitive integer types][numeric types]: `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`, `u128`, `i128`, `usize`, or `isize`, and the expression has that type.
58
56
59
57
If the token has no suffix, the expression's type is determined by type inference:
60
58
@@ -101,7 +99,7 @@ The value of the expression is determined from the string representation of the
101
99
* Any underscores are removed from the string.
102
100
103
101
* The string is converted to a `u128` value as if by [`u128::from_str_radix`] with the chosen radix.
104
-
If the value does not fit in `u128`, the expression is rejected by the parser.
102
+
If the value does not fit in `u128`, it is a compiler error.
105
103
106
104
* The `u128` value is converted to the expression's type via a [numeric cast].
107
105
@@ -113,9 +111,11 @@ If the value does not fit in `u128`, the expression is rejected by the parser.
113
111
114
112
## Floating-point literal expressions
115
113
116
-
A floating-point literal expression consists of a single [FLOAT_LITERAL] token.
114
+
A floating-point literal expression has one of two forms:
115
+
* a single [FLOAT_LITERAL] token
116
+
* a single [INTEGER_LITERAL] token which has a suffix and no radix indicator
117
117
118
-
If the token has a [suffix], the suffix will be the name of one of the [primitive floating-point types][floating-point types]: `f32` or `f64`, and the expression has that type.
118
+
If the token has a [suffix], the suffix must be the name of one of the [primitive floating-point types][floating-point types]: `f32` or `f64`, and the expression has that type.
119
119
120
120
If the token has no suffix, the expression's type is determined by type inference:
`*` All number literals allow `_` as a visual separator: `1_234.0E+18f64`
84
84
85
85
#### Suffixes
86
86
87
87
A suffix is a sequence of characters following the primary part of a literal (without intervening whitespace), of the same form as a non-raw identifier or keyword.
88
88
89
-
Any kind of literal (string, integer, etc) with any suffix is valid as a token,
90
-
and can be passed to a macro without producing an error.
89
+
90
+
> **<sup>Lexer</sup>**\
91
+
> SUFFIX : IDENTIFIER_OR_KEYWORD\
92
+
> SUFFIX_NO_E : SUFFIX <sub>_not beginning with `e` or `E`_</sub>
93
+
94
+
Any kind of literal (string, integer, etc) with any suffix is valid as a token.
95
+
96
+
A literal token with any suffix can be passed to a macro without producing an error.
91
97
The macro itself will decide how to interpret such a token and whether to produce an error or not.
98
+
In particular, the `literal` fragment specifier for by-example macros matches literal tokens with arbitrary suffixes.
92
99
93
100
```rust
94
101
macro_rules!blackhole { ($tt:tt) => () }
102
+
macro_rules!blackhole_lit { ($l:literal) => () }
95
103
96
104
blackhole!("string"suffix); // OK
105
+
blackhole_lit!(1suffix); // OK
97
106
```
98
107
99
-
However, suffixes on literal tokens parsed as Rust code are restricted.
108
+
However, suffixes on literal tokens which are interpreted as literal expressions or patterns are restricted.
100
109
Any suffixes are rejected on non-numeric literal tokens,
101
110
and numeric literal tokens are accepted only with suffixes from the list below.
102
111
@@ -329,7 +338,7 @@ literal_. The grammar for recognizing the two kinds of literals is mixed.
@@ -369,11 +374,11 @@ An _integer literal_ has one of four forms:
369
374
(`0b`) and continues as any mixture (with at least one digit) of binary digits
370
375
and underscores.
371
376
372
-
Like any literal, an integer literal may be followed (immediately, without any spaces) by an _integer suffix_, which must be the name of one of the [primitive integer types][numeric types]:
A _floating-point literal_ has one of three forms:
467
+
A _floating-point literal_ has one of two forms:
454
468
455
469
* A _decimal literal_ followed by a period character `U+002E` (`.`). This is
456
470
optionally followed by another decimal literal, with an optional _exponent_.
457
471
* A single _decimal literal_ followed by an _exponent_.
458
-
* A single _decimal literal_ (in which case a suffix is required).
459
472
460
473
Like integer literals, a floating-point literal may be followed by a
461
474
suffix, so long as the pre-suffix part does not end with `U+002E` (`.`).
462
-
There are two valid _floating-point suffixes_: `f32` and `f64` (the names of the 32-bit and 64-bit [primitive floating-point types][floating-point types]).
475
+
The suffix may not begin with `e` or `E` if the literal does not include an exponent.
463
476
See [literal expressions] for the effect of these suffixes.
464
477
465
-
Examples of floating-point literals of various forms:
478
+
Examples of floating-point literals which are accepted as literal expressions:
466
479
467
480
```rust
468
481
123.0f64;
469
482
0.1f64;
470
483
0.1f32;
471
484
12E+99_f64;
472
-
5f32;
473
485
letx:f64=2.;
474
486
```
475
487
@@ -479,39 +491,16 @@ to call a method named `f64` on `2`.
479
491
480
492
Note that `-1.0`, for example, is analyzed as two tokens: `-` followed by `1.0`.
0 commit comments