Skip to content

Commit 515bdcd

Browse files
Rollup merge of #130551 - nnethercote:fix-break-last-token, r=petrochenkov
Fix `break_last_token`. It currently doesn't handle the three-char tokens `>>=` and `<<=` correctly. These can be broken twice, resulting in three individual tokens. This is a latent bug that currently doesn't cause any problems, but does cause problems for #124141, because that PR increases the usage of lazy token streams. r? `@petrochenkov`
2 parents c58e3cb + 73cc575 commit 515bdcd

4 files changed

Lines changed: 91 additions & 64 deletions

File tree

‎compiler/rustc_ast/src/token.rs‎

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -385,35 +385,41 @@ impl TokenKind {
385385
Literal(Lit::new(kind, symbol, suffix))
386386
}
387387

388-
/// An approximation to proc-macro-style single-character operators used by rustc parser.
389-
/// If the operator token can be broken into two tokens, the first of which is single-character,
390-
/// then this function performs that operation, otherwise it returns `None`.
391-
pubfnbreak_two_token_op(&self) -> Option<(TokenKind,TokenKind)>{
392-
Some(match*self{
393-
Le => (Lt,Eq),
394-
EqEq => (Eq,Eq),
395-
Ne => (Not,Eq),
396-
Ge => (Gt,Eq),
397-
AndAnd => (BinOp(And),BinOp(And)),
398-
OrOr => (BinOp(Or),BinOp(Or)),
399-
BinOp(Shl) => (Lt,Lt),
400-
BinOp(Shr) => (Gt,Gt),
401-
BinOpEq(Plus) => (BinOp(Plus),Eq),
402-
BinOpEq(Minus) => (BinOp(Minus),Eq),
403-
BinOpEq(Star) => (BinOp(Star),Eq),
404-
BinOpEq(Slash) => (BinOp(Slash),Eq),
405-
BinOpEq(Percent) => (BinOp(Percent),Eq),
406-
BinOpEq(Caret) => (BinOp(Caret),Eq),
407-
BinOpEq(And) => (BinOp(And),Eq),
408-
BinOpEq(Or) => (BinOp(Or),Eq),
409-
BinOpEq(Shl) => (Lt,Le),
410-
BinOpEq(Shr) => (Gt,Ge),
411-
DotDot => (Dot,Dot),
412-
DotDotDot => (Dot,DotDot),
413-
PathSep => (Colon,Colon),
414-
RArrow => (BinOp(Minus),Gt),
415-
LArrow => (Lt,BinOp(Minus)),
416-
FatArrow => (Eq,Gt),
388+
/// An approximation to proc-macro-style single-character operators used by
389+
/// rustc parser. If the operator token can be broken into two tokens, the
390+
/// first of which has `n` (1 or 2) chars, then this function performs that
391+
/// operation, otherwise it returns `None`.
392+
pubfnbreak_two_token_op(&self,n:u32) -> Option<(TokenKind,TokenKind)>{
393+
assert!(n == 1 || n == 2);
394+
Some(match(self, n){
395+
(Le,1) => (Lt,Eq),
396+
(EqEq,1) => (Eq,Eq),
397+
(Ne,1) => (Not,Eq),
398+
(Ge,1) => (Gt,Eq),
399+
(AndAnd,1) => (BinOp(And),BinOp(And)),
400+
(OrOr,1) => (BinOp(Or),BinOp(Or)),
401+
(BinOp(Shl),1) => (Lt,Lt),
402+
(BinOp(Shr),1) => (Gt,Gt),
403+
(BinOpEq(Plus),1) => (BinOp(Plus),Eq),
404+
(BinOpEq(Minus),1) => (BinOp(Minus),Eq),
405+
(BinOpEq(Star),1) => (BinOp(Star),Eq),
406+
(BinOpEq(Slash),1) => (BinOp(Slash),Eq),
407+
(BinOpEq(Percent),1) => (BinOp(Percent),Eq),
408+
(BinOpEq(Caret),1) => (BinOp(Caret),Eq),
409+
(BinOpEq(And),1) => (BinOp(And),Eq),
410+
(BinOpEq(Or),1) => (BinOp(Or),Eq),
411+
(BinOpEq(Shl),1) => (Lt,Le),// `<` + `<=`
412+
(BinOpEq(Shl),2) => (BinOp(Shl),Eq),// `<<` + `=`
413+
(BinOpEq(Shr),1) => (Gt,Ge),// `>` + `>=`
414+
(BinOpEq(Shr),2) => (BinOp(Shr),Eq),// `>>` + `=`
415+
(DotDot,1) => (Dot,Dot),
416+
(DotDotDot,1) => (Dot,DotDot),// `.` + `..`
417+
(DotDotDot,2) => (DotDot,Dot),// `..` + `.`
418+
(DotDotEq,2) => (DotDot,Eq),
419+
(PathSep,1) => (Colon,Colon),
420+
(RArrow,1) => (BinOp(Minus),Gt),
421+
(LArrow,1) => (Lt,BinOp(Minus)),
422+
(FatArrow,1) => (Eq,Gt),
417423
_ => returnNone,
418424
})
419425
}

‎compiler/rustc_parse/src/parser/attr_wrapper.rs‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct LazyAttrTokenStreamImpl {
108108
start_token:(Token,Spacing),
109109
cursor_snapshot:TokenCursor,
110110
num_calls:u32,
111-
break_last_token:bool,
111+
break_last_token:u32,
112112
node_replacements:Box<[NodeReplacement]>,
113113
}
114114

@@ -339,17 +339,20 @@ impl<'a> Parser<'a> {
339339
let parser_replacements_end = self.capture_state.parser_replacements.len();
340340

341341
assert!(
342-
!(self.break_last_token && matches!(capture_trailing,Trailing::Yes)),
343-
"Cannot set break_last_token and have trailing token"
342+
!(self.break_last_token > 0&& matches!(capture_trailing,Trailing::Yes)),
343+
"Cannot have break_last_token > 0 and have trailing token"
344344
);
345+
assert!(self.break_last_token <= 2,"cannot break token more than twice");
345346

346347
let end_pos = self.num_bump_calls
347348
+ capture_trailing asu32
348-
// If we 'broke' the last token (e.g. breaking a '>>' token to two '>' tokens), then
349-
// extend the range of captured tokens to include it, since the parser was not actually
350-
// bumped past it. When the `LazyAttrTokenStream` gets converted into an
351-
// `AttrTokenStream`, we will create the proper token.
352-
+ self.break_last_tokenasu32;
349+
// If we "broke" the last token (e.g. breaking a `>>` token once into `>` + `>`, or
350+
// breaking a `>>=` token twice into `>` + `>` + `=`), then extend the range of
351+
// captured tokens to include it, because the parser was not actually bumped past it.
352+
// (Even if we broke twice, it was still just one token originally, hence the `1`.)
353+
// When the `LazyAttrTokenStream` gets converted into an `AttrTokenStream`, we will
354+
// rebreak that final token once or twice.
355+
+ ifself.break_last_token == 0{0}else{1};
353356

354357
let num_calls = end_pos - collect_pos.start_pos;
355358

@@ -425,7 +428,7 @@ impl<'a> Parser<'a> {
425428
// for the `#[cfg]` and/or `#[cfg_attr]` attrs. This allows us to run
426429
// eager cfg-expansion on the captured token stream.
427430
if definite_capture_mode {
428-
assert!(!self.break_last_token,"Should not have unglued last token with cfg attr");
431+
assert!(self.break_last_token == 0,"Should not have unglued last token with cfg attr");
429432

430433
// What is the status here when parsing the example code at the top of this method?
431434
//
@@ -471,7 +474,7 @@ impl<'a> Parser<'a> {
471474
/// close delims.
472475
fnmake_attr_token_stream(
473476
iter:implIterator<Item = FlatToken>,
474-
break_last_token:bool,
477+
break_last_token:u32,
475478
) -> AttrTokenStream{
476479
#[derive(Debug)]
477480
structFrameData{
@@ -513,18 +516,17 @@ fn make_attr_token_stream(
513516
}
514517
}
515518

516-
if break_last_token {
519+
if break_last_token > 0{
517520
let last_token = stack_top.inner.pop().unwrap();
518521
ifletAttrTokenTree::Token(last_token, spacing) = last_token {
519-
letunglued_first = last_token.kind.break_two_token_op().unwrap().0;
522+
let(unglued, _)= last_token.kind.break_two_token_op(break_last_token).unwrap();
520523

521-
// An 'unglued' token is always two ASCII characters
524+
// Tokens are always ASCII chars, so we can use byte arithmetic here.
522525
letmut first_span = last_token.span.shrink_to_lo();
523-
first_span = first_span.with_hi(first_span.lo() + rustc_span::BytePos(1));
526+
first_span =
527+
first_span.with_hi(first_span.lo() + rustc_span::BytePos(break_last_token));
524528

525-
stack_top
526-
.inner
527-
.push(AttrTokenTree::Token(Token::new(unglued_first, first_span), spacing));
529+
stack_top.inner.push(AttrTokenTree::Token(Token::new(unglued, first_span), spacing));
528530
}else{
529531
panic!("Unexpected last token {last_token:?}")
530532
}

‎compiler/rustc_parse/src/parser/mod.rs‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,25 @@ pub struct Parser<'a> {
146146
token_cursor:TokenCursor,
147147
// The number of calls to `bump`, i.e. the position in the token stream.
148148
num_bump_calls:u32,
149-
// During parsing we may sometimes need to 'unglue' a glued token into two
150-
// component tokens (e.g. '>>' into '>' and '>), so the parser can consume
151-
// them one at a time. This process bypasses the normal capturing mechanism
152-
// (e.g. `num_bump_calls` will not be incremented), since the 'unglued'
153-
// tokens due not exist in the original `TokenStream`.
149+
// During parsing we may sometimes need to "unglue" a glued token into two
150+
// or three component tokens (e.g. `>>` into `>` and `>`, or `>>=` into `>`
151+
// and `>` and `=`), so the parser can consume them one at a time. This
152+
// process bypasses the normal capturing mechanism (e.g. `num_bump_calls`
153+
// will not be incremented), since the "unglued" tokens due not exist in
154+
// the original `TokenStream`.
154155
//
155-
// If we end up consuming both unglued tokens, this is not an issue. We'll
156-
// end up capturing the single 'glued' token.
156+
// If we end up consuming all the component tokens, this is not an issue,
157+
// because we'll end up capturing the single "glued" token.
157158
//
158-
// However, sometimes we may want to capture just the first 'unglued'
159+
// However, sometimes we may want to capture not all of the original
159160
// token. For example, capturing the `Vec<u8>` in `Option<Vec<u8>>`
160161
// requires us to unglue the trailing `>>` token. The `break_last_token`
161-
// field is used to track this token. It gets appended to the captured
162+
// field is used to track these tokens. They get appended to the captured
162163
// stream when we evaluate a `LazyAttrTokenStream`.
163-
break_last_token:bool,
164+
//
165+
// This value is always 0, 1, or 2. It can only reach 2 when splitting
166+
// `>>=` or `<<=`.
167+
break_last_token:u32,
164168
/// This field is used to keep track of how many left angle brackets we have seen. This is
165169
/// required in order to detect extra leading left angle brackets (`<` characters) and error
166170
/// appropriately.
@@ -453,7 +457,7 @@ impl<'a> Parser<'a> {
453457
expected_tokens:Vec::new(),
454458
token_cursor:TokenCursor{tree_cursor: stream.into_trees(),stack:Vec::new()},
455459
num_bump_calls:0,
456-
break_last_token:false,
460+
break_last_token:0,
457461
unmatched_angle_bracket_count:0,
458462
angle_bracket_nesting:0,
459463
last_unexpected_token_span:None,
@@ -773,7 +777,7 @@ impl<'a> Parser<'a> {
773777
self.bump();
774778
returntrue;
775779
}
776-
matchself.token.kind.break_two_token_op(){
780+
matchself.token.kind.break_two_token_op(1){
777781
Some((first, second))if first == expected => {
778782
let first_span = self.psess.source_map().start_point(self.token.span);
779783
let second_span = self.token.span.with_lo(first_span.hi());
@@ -783,8 +787,8 @@ impl<'a> Parser<'a> {
783787
//
784788
// If we consume any additional tokens, then this token
785789
// is not needed (we'll capture the entire 'glued' token),
786-
// and `bump` will set this field to `None`
787-
self.break_last_token= true;
790+
// and `bump` will set this field to 0.
791+
self.break_last_token+= 1;
788792
// Use the spacing of the glued token as the spacing of the
789793
// unglued second token.
790794
self.bump_with((Token::new(second, second_span),self.token_spacing));
@@ -1148,10 +1152,9 @@ impl<'a> Parser<'a> {
11481152
// than `.0`/`.1` access.
11491153
letmut next = self.token_cursor.inlined_next();
11501154
self.num_bump_calls += 1;
1151-
// We've retrieved an token from the underlying
1152-
// cursor, so we no longer need to worry about
1153-
// an unglued token. See `break_and_eat` for more details
1154-
self.break_last_token = false;
1155+
// We got a token from the underlying cursor and no longer need to
1156+
// worry about an unglued token. See `break_and_eat` for more details.
1157+
self.break_last_token = 0;
11551158
if next.0.span.is_dummy(){
11561159
// Tweak the location for better diagnostics, but keep syntactic context intact.
11571160
let fallback_span = self.token.span;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ check-pass
2+
3+
macro_rules! m {
4+
(static $name:ident: $t:ty = $e:expr) => {
5+
let $name: $t = $e;
6+
}
7+
}
8+
9+
fnmain(){
10+
m!{
11+
// Tricky: the trailing `>>=` token here is broken twice:
12+
// - into `>` and `>=`
13+
// - then the `>=` is broken into `>` and `=`
14+
static _x:Vec<Vec<u32>>= vec![]
15+
}
16+
}

0 commit comments

Comments
 (0)