Skip to content

Commit 2b40fdb

Browse files
authored
Rollup merge of rust-lang#130349 - ShE3py:break_up_float, r=fmease
Fix `Parser::break_up_float`'s right span ```rs use std::mem::offset_of; fn main() { offset_of!((u8,), 0.0); } ``` Before: ``` error[E0609]: no field `0` on type `u8` --> ./main.rs:4:25 | 4 | offset_of!((u8,), 0.0); | _____--------------------^- | | | | | in this macro invocation 5 | | } ... | | = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error ``` After: ``` error[E0609]: no field `0` on type `u8` --> ./main.rs:4:25 | 4 | offset_of!((u8,), 0.0); | ^ error: aborting due to 1 previous error ``` --- `@rustbot` label +A-parser +D-imprecise-spans
2 parents d1701a5 + 4cb5849 commit 2b40fdb

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

compiler/rustc_parse/src/parser/expr.rs

+32-34
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ enum DestructuredFloat {
4949
/// 1.2 | 1.2e3
5050
MiddleDot(Symbol, Span, Span, Symbol, Span),
5151
/// Invalid
52-
Error,
52+
Error(ErrorGuaranteed),
5353
}
5454

5555
impl<'a> Parser<'a> {
@@ -1008,7 +1008,7 @@ impl<'a> Parser<'a> {
10081008
self.mk_expr_tuple_field_access(lo, ident1_span, base, sym1, None);
10091009
self.mk_expr_tuple_field_access(lo, ident2_span, base1, sym2, suffix)
10101010
}
1011-
DestructuredFloat::Error => base,
1011+
DestructuredFloat::Error(_) => base,
10121012
})
10131013
}
10141014
_ => {
@@ -1018,7 +1018,7 @@ impl<'a> Parser<'a> {
10181018
}
10191019
}
10201020

1021-
fn error_unexpected_after_dot(&self) {
1021+
fn error_unexpected_after_dot(&self) -> ErrorGuaranteed {
10221022
let actual = pprust::token_to_string(&self.token);
10231023
let span = self.token.span;
10241024
let sm = self.psess.source_map();
@@ -1028,17 +1028,19 @@ impl<'a> Parser<'a> {
10281028
}
10291029
_ => (span, actual),
10301030
};
1031-
self.dcx().emit_err(errors::UnexpectedTokenAfterDot { span, actual });
1031+
self.dcx().emit_err(errors::UnexpectedTokenAfterDot { span, actual })
10321032
}
10331033

1034-
// We need an identifier or integer, but the next token is a float.
1035-
// Break the float into components to extract the identifier or integer.
1034+
/// We need an identifier or integer, but the next token is a float.
1035+
/// Break the float into components to extract the identifier or integer.
1036+
///
1037+
/// See also [`TokenKind::break_two_token_op`] which does similar splitting of `>>` into `>`.
1038+
//
10361039
// FIXME: With current `TokenCursor` it's hard to break tokens into more than 2
1037-
// parts unless those parts are processed immediately. `TokenCursor` should either
1038-
// support pushing "future tokens" (would be also helpful to `break_and_eat`), or
1039-
// we should break everything including floats into more basic proc-macro style
1040-
// tokens in the lexer (probably preferable).
1041-
// See also `TokenKind::break_two_token_op` which does similar splitting of `>>` into `>`.
1040+
// parts unless those parts are processed immediately. `TokenCursor` should either
1041+
// support pushing "future tokens" (would be also helpful to `break_and_eat`), or
1042+
// we should break everything including floats into more basic proc-macro style
1043+
// tokens in the lexer (probably preferable).
10421044
fn break_up_float(&self, float: Symbol, span: Span) -> DestructuredFloat {
10431045
#[derive(Debug)]
10441046
enum FloatComponent {
@@ -1078,34 +1080,30 @@ impl<'a> Parser<'a> {
10781080
DestructuredFloat::Single(Symbol::intern(i), span)
10791081
}
10801082
// 1.
1081-
[IdentLike(i), Punct('.')] => {
1082-
let (ident_span, dot_span) = if can_take_span_apart() {
1083-
let (span, ident_len) = (span.data(), BytePos::from_usize(i.len()));
1084-
let ident_span = span.with_hi(span.lo + ident_len);
1085-
let dot_span = span.with_lo(span.lo + ident_len);
1086-
(ident_span, dot_span)
1083+
[IdentLike(left), Punct('.')] => {
1084+
let (left_span, dot_span) = if can_take_span_apart() {
1085+
let left_span = span.with_hi(span.lo() + BytePos::from_usize(left.len()));
1086+
let dot_span = span.with_lo(left_span.hi());
1087+
(left_span, dot_span)
10871088
} else {
10881089
(span, span)
10891090
};
1090-
let symbol = Symbol::intern(i);
1091-
DestructuredFloat::TrailingDot(symbol, ident_span, dot_span)
1091+
let left = Symbol::intern(left);
1092+
DestructuredFloat::TrailingDot(left, left_span, dot_span)
10921093
}
10931094
// 1.2 | 1.2e3
1094-
[IdentLike(i1), Punct('.'), IdentLike(i2)] => {
1095-
let (ident1_span, dot_span, ident2_span) = if can_take_span_apart() {
1096-
let (span, ident1_len) = (span.data(), BytePos::from_usize(i1.len()));
1097-
let ident1_span = span.with_hi(span.lo + ident1_len);
1098-
let dot_span = span
1099-
.with_lo(span.lo + ident1_len)
1100-
.with_hi(span.lo + ident1_len + BytePos(1));
1101-
let ident2_span = self.token.span.with_lo(span.lo + ident1_len + BytePos(1));
1102-
(ident1_span, dot_span, ident2_span)
1095+
[IdentLike(left), Punct('.'), IdentLike(right)] => {
1096+
let (left_span, dot_span, right_span) = if can_take_span_apart() {
1097+
let left_span = span.with_hi(span.lo() + BytePos::from_usize(left.len()));
1098+
let dot_span = span.with_lo(left_span.hi()).with_hi(left_span.hi() + BytePos(1));
1099+
let right_span = span.with_lo(dot_span.hi());
1100+
(left_span, dot_span, right_span)
11031101
} else {
11041102
(span, span, span)
11051103
};
1106-
let symbol1 = Symbol::intern(i1);
1107-
let symbol2 = Symbol::intern(i2);
1108-
DestructuredFloat::MiddleDot(symbol1, ident1_span, dot_span, symbol2, ident2_span)
1104+
let left = Symbol::intern(left);
1105+
let right = Symbol::intern(right);
1106+
DestructuredFloat::MiddleDot(left, left_span, dot_span, right, right_span)
11091107
}
11101108
// 1e+ | 1e- (recovered)
11111109
[IdentLike(_), Punct('+' | '-')] |
@@ -1116,8 +1114,8 @@ impl<'a> Parser<'a> {
11161114
// 1.2e+3 | 1.2e-3
11171115
[IdentLike(_), Punct('.'), IdentLike(_), Punct('+' | '-'), IdentLike(_)] => {
11181116
// See the FIXME about `TokenCursor` above.
1119-
self.error_unexpected_after_dot();
1120-
DestructuredFloat::Error
1117+
let guar = self.error_unexpected_after_dot();
1118+
DestructuredFloat::Error(guar)
11211119
}
11221120
_ => panic!("unexpected components in a float token: {components:?}"),
11231121
}
@@ -1183,7 +1181,7 @@ impl<'a> Parser<'a> {
11831181
fields.insert(start_idx, Ident::new(symbol2, span2));
11841182
fields.insert(start_idx, Ident::new(symbol1, span1));
11851183
}
1186-
DestructuredFloat::Error => {
1184+
DestructuredFloat::Error(_) => {
11871185
trailing_dot = None;
11881186
fields.insert(start_idx, Ident::new(symbol, self.prev_token.span));
11891187
}

tests/ui/offset-of/offset-of-tuple.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type ComplexTup = (((u8, u8), u8), u8);
2828

2929
fn nested() {
3030
offset_of!(((u8, u16), (u32, u16, u8)), 0.2); //~ ERROR no field `2`
31+
offset_of!(((u8, u16), (u32, u16, u8)), 0.1e2); //~ ERROR no field `1e2`
3132
offset_of!(((u8, u16), (u32, u16, u8)), 1.2);
3233
offset_of!(((u8, u16), (u32, u16, u8)), 1.2.0); //~ ERROR no field `0`
3334

tests/ui/offset-of/offset-of-tuple.stderr

+23-24
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,43 @@ LL | { builtin # offset_of((u8, u8), 1 .) };
2929
| ^
3030

3131
error: unexpected token: `)`
32-
--> $DIR/offset-of-tuple.rs:46:45
32+
--> $DIR/offset-of-tuple.rs:47:45
3333
|
3434
LL | { builtin # offset_of(ComplexTup, 0.0.1.) };
3535
| ^
3636

3737
error: unexpected token: `)`
38-
--> $DIR/offset-of-tuple.rs:47:46
38+
--> $DIR/offset-of-tuple.rs:48:46
3939
|
4040
LL | { builtin # offset_of(ComplexTup, 0 .0.1.) };
4141
| ^
4242

4343
error: unexpected token: `)`
44-
--> $DIR/offset-of-tuple.rs:48:47
44+
--> $DIR/offset-of-tuple.rs:49:47
4545
|
4646
LL | { builtin # offset_of(ComplexTup, 0 . 0.1.) };
4747
| ^
4848

4949
error: unexpected token: `)`
50-
--> $DIR/offset-of-tuple.rs:49:46
50+
--> $DIR/offset-of-tuple.rs:50:46
5151
|
5252
LL | { builtin # offset_of(ComplexTup, 0. 0.1.) };
5353
| ^
5454

5555
error: unexpected token: `)`
56-
--> $DIR/offset-of-tuple.rs:50:46
56+
--> $DIR/offset-of-tuple.rs:51:46
5757
|
5858
LL | { builtin # offset_of(ComplexTup, 0.0 .1.) };
5959
| ^
6060

6161
error: unexpected token: `)`
62-
--> $DIR/offset-of-tuple.rs:51:47
62+
--> $DIR/offset-of-tuple.rs:52:47
6363
|
6464
LL | { builtin # offset_of(ComplexTup, 0.0 . 1.) };
6565
| ^
6666

6767
error: unexpected token: `)`
68-
--> $DIR/offset-of-tuple.rs:52:46
68+
--> $DIR/offset-of-tuple.rs:53:46
6969
|
7070
LL | { builtin # offset_of(ComplexTup, 0.0. 1.) };
7171
| ^
@@ -104,43 +104,43 @@ LL | offset_of!((u8, u8), 1 .);
104104
| ^
105105

106106
error: unexpected token: `)`
107-
--> $DIR/offset-of-tuple.rs:35:34
107+
--> $DIR/offset-of-tuple.rs:36:34
108108
|
109109
LL | offset_of!(ComplexTup, 0.0.1.);
110110
| ^
111111

112112
error: unexpected token: `)`
113-
--> $DIR/offset-of-tuple.rs:36:35
113+
--> $DIR/offset-of-tuple.rs:37:35
114114
|
115115
LL | offset_of!(ComplexTup, 0 .0.1.);
116116
| ^
117117

118118
error: unexpected token: `)`
119-
--> $DIR/offset-of-tuple.rs:37:36
119+
--> $DIR/offset-of-tuple.rs:38:36
120120
|
121121
LL | offset_of!(ComplexTup, 0 . 0.1.);
122122
| ^
123123

124124
error: unexpected token: `)`
125-
--> $DIR/offset-of-tuple.rs:38:35
125+
--> $DIR/offset-of-tuple.rs:39:35
126126
|
127127
LL | offset_of!(ComplexTup, 0. 0.1.);
128128
| ^
129129

130130
error: unexpected token: `)`
131-
--> $DIR/offset-of-tuple.rs:39:35
131+
--> $DIR/offset-of-tuple.rs:40:35
132132
|
133133
LL | offset_of!(ComplexTup, 0.0 .1.);
134134
| ^
135135

136136
error: unexpected token: `)`
137-
--> $DIR/offset-of-tuple.rs:40:36
137+
--> $DIR/offset-of-tuple.rs:41:36
138138
|
139139
LL | offset_of!(ComplexTup, 0.0 . 1.);
140140
| ^
141141

142142
error: unexpected token: `)`
143-
--> $DIR/offset-of-tuple.rs:41:35
143+
--> $DIR/offset-of-tuple.rs:42:35
144144
|
145145
LL | offset_of!(ComplexTup, 0.0. 1.);
146146
| ^
@@ -196,22 +196,21 @@ LL | builtin # offset_of((u8, u8), 1_u8);
196196
error[E0609]: no field `2` on type `(u8, u16)`
197197
--> $DIR/offset-of-tuple.rs:30:47
198198
|
199-
LL | offset_of!(((u8, u16), (u32, u16, u8)), 0.2);
200-
| _____------------------------------------------^-
201-
| | |
202-
| | in this macro invocation
203-
LL | | offset_of!(((u8, u16), (u32, u16, u8)), 1.2);
204-
LL | | offset_of!(((u8, u16), (u32, u16, u8)), 1.2.0);
205-
... |
199+
LL | offset_of!(((u8, u16), (u32, u16, u8)), 0.2);
200+
| ^
201+
202+
error[E0609]: no field `1e2` on type `(u8, u16)`
203+
--> $DIR/offset-of-tuple.rs:31:47
206204
|
207-
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
205+
LL | offset_of!(((u8, u16), (u32, u16, u8)), 0.1e2);
206+
| ^^^
208207

209208
error[E0609]: no field `0` on type `u8`
210-
--> $DIR/offset-of-tuple.rs:32:49
209+
--> $DIR/offset-of-tuple.rs:33:49
211210
|
212211
LL | offset_of!(((u8, u16), (u32, u16, u8)), 1.2.0);
213212
| ^
214213

215-
error: aborting due to 33 previous errors
214+
error: aborting due to 34 previous errors
216215

217216
For more information about this error, try `rustc --explain E0609`.

0 commit comments

Comments
 (0)