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
> [^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
@@ -96,10 +94,12 @@ The value of the expression is determined from the string representation of the
96
94
97
95
* If the radix is not 10, the first two characters are removed from the string.
98
96
97
+
* Any suffix is removed from the string.
98
+
99
99
* Any underscores are removed from the string.
100
100
101
101
* The string is converted to a `u128` value as if by [`u128::from_str_radix`] with the chosen radix.
102
-
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.
103
103
104
104
* The `u128` value is converted to the expression's type via a [numeric cast].
105
105
@@ -111,9 +111,11 @@ If the value does not fit in `u128`, the expression is rejected by the parser.
111
111
112
112
## Floating-point literal expressions
113
113
114
-
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
115
117
116
-
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.
117
119
118
120
If the token has no suffix, the expression's type is determined by type inference:
119
121
@@ -136,6 +138,8 @@ let x: f64 = 2.; // type f64
136
138
137
139
The value of the expression is determined from the string representation of the token as follows:
138
140
141
+
* Any suffix is removed from the string.
142
+
139
143
* Any underscores are removed from the string.
140
144
141
145
* The string is converted to the expression's type as if by [`f32::from_str`] or [`f64::from_str`].
`*` 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
@@ -110,7 +119,7 @@ and numeric literal tokens are accepted only with suffixes from the list below.
@@ -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:
468
468
469
469
* A _decimal literal_ followed by a period character `U+002E` (`.`). This is
470
470
optionally followed by another decimal literal, with an optional _exponent_.
471
471
* A single _decimal literal_ followed by an _exponent_.
472
-
* A single _decimal literal_ (in which case a suffix is required).
473
472
474
473
Like integer literals, a floating-point literal may be followed by a
475
474
suffix, so long as the pre-suffix part does not end with `U+002E` (`.`).
476
-
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.
477
476
See [literal expressions] for the effect of these suffixes.
478
477
479
-
Examples of floating-point literals of various forms:
478
+
Examples of floating-point literals which are accepted as literal expressions:
480
479
481
480
```rust
482
481
123.0f64;
483
482
0.1f64;
484
483
0.1f32;
485
484
12E+99_f64;
486
-
5f32;
487
485
letx:f64=2.;
488
486
```
489
487
@@ -493,39 +491,16 @@ to call a method named `f64` on `2`.
493
491
494
492
Note that `-1.0`, for example, is analyzed as two tokens: `-` followed by `1.0`.
> | `0b``_`<sup>\*</sup> _end of input or not BIN_DIGIT_\
541
516
> | `0o``_`<sup>\*</sup> _end of input or not OCT_DIGIT_\
542
517
> | `0x``_`<sup>\*</sup> _end of input or not HEX_DIGIT_\
@@ -549,7 +524,7 @@ Due to the possible ambiguity these raise, they are rejected by the tokenizer in
549
524
550
525
* An unsuffixed binary, octal, or hexadecimal literal followed, without intervening whitespace, by a period character (with the same restrictions on what follows the period as for floating-point literals).
551
526
552
-
* An unsuffixed binary or octal literal followed, without intervening whitespace, by the character `e`.
527
+
* An unsuffixed binary or octal literal followed, without intervening whitespace, by the character `e` or `E`.
553
528
554
529
* Input which begins with one of the radix prefixes but is not a valid binary, octal, or hexadecimal literal (because it contains no digits).
555
530
@@ -561,13 +536,13 @@ Examples of reserved forms:
561
536
0b0102; // this is not `0b010` followed by `2`
562
537
0o1279; // this is not `0o127` followed by `9`
563
538
0x80.0; // this is not `0x80` followed by `.` and `0`
564
-
0b101e; // this is not a pseudoliteral, or `0b101` followed by `e`
565
-
0b; // this is not a pseudoliteral, or `0` followed by `b`
566
-
0b_; // this is not a pseudoliteral, or `0` followed by `b_`
567
-
2e; // this is not a pseudoliteral, or `2` followed by `e`
568
-
2.0e; // this is not a pseudoliteral, or `2.0` followed by `e`
569
-
2em; // this is not a pseudoliteral, or `2` followed by `em`
570
-
2.0em; // this is not a pseudoliteral, or `2.0` followed by `em`
539
+
0b101e; // this is not a suffixed literal, or `0b101` followed by `e`
540
+
0b; // this is not an integer literal, or `0` followed by `b`
541
+
0b_; // this is not an integer literal, or `0` followed by `b_`
542
+
2e; // this is not a floating-point literal, or `2` followed by `e`
543
+
2.0e; // this is not a floating-point literal, or `2.0` followed by `e`
544
+
2em; // this is not a suffixed literal, or `2` followed by `em`
545
+
2.0em; // this is not a suffixed literal, or `2.0` followed by `em`
0 commit comments