@@ -10,7 +10,10 @@ use rustc_errors::PResult;
1010use rustc_session:: parse:: ParseSess ;
1111use rustc_span:: { sym, Span , DUMMY_SP } ;
1212
13- use super :: { Capturing , FlatToken , ForceCollect , Parser , ReplaceRange , TokenCursor } ;
13+ use super :: {
14+ Capturing , FlatToken , ForceCollect , NodeRange , NodeReplacement , Parser , ParserRange ,
15+ TokenCursor ,
16+ } ;
1417
1518/// A wrapper type to ensure that the parser handles outer attributes correctly.
1619/// When we parse outer attributes, we need to ensure that we capture tokens
@@ -28,8 +31,8 @@ use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCurso
2831#[ derive( Debug , Clone ) ]
2932pub struct AttrWrapper {
3033 attrs : AttrVec ,
31- // The start of the outer attributes in the token cursor .
32- // This allows us to create a `ReplaceRange ` for the entire attribute
34+ // The start of the outer attributes in the parser's token stream .
35+ // This lets us create a `NodeReplacement ` for the entire attribute
3336 // target, including outer attributes.
3437 start_pos : u32 ,
3538}
@@ -88,7 +91,7 @@ struct LazyAttrTokenStreamImpl {
8891 cursor_snapshot : TokenCursor ,
8992 num_calls : u32 ,
9093 break_last_token : bool ,
91- replace_ranges : Box < [ ReplaceRange ] > ,
94+ node_replacements : Box < [ NodeReplacement ] > ,
9295}
9396
9497impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
@@ -103,21 +106,24 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
103106 . chain ( iter:: repeat_with ( || FlatToken :: Token ( cursor_snapshot. next ( ) ) ) )
104107 . take ( self . num_calls as usize ) ;
105108
106- if self . replace_ranges . is_empty ( ) {
109+ if self . node_replacements . is_empty ( ) {
107110 make_attr_token_stream ( tokens, self . break_last_token )
108111 } else {
109112 let mut tokens: Vec < _ > = tokens. collect ( ) ;
110- let mut replace_ranges = self . replace_ranges . to_vec ( ) ;
111- replace_ranges . sort_by_key ( |( range, _) | range. start ) ;
113+ let mut node_replacements = self . node_replacements . to_vec ( ) ;
114+ node_replacements . sort_by_key ( |( range, _) | range. 0 . start ) ;
112115
113116 #[ cfg( debug_assertions) ]
114- for [ ( range, tokens) , ( next_range, next_tokens) ] in replace_ranges. array_windows ( ) {
117+ for [ ( node_range, tokens) , ( next_node_range, next_tokens) ] in
118+ node_replacements. array_windows ( )
119+ {
115120 assert ! (
116- range. end <= next_range. start || range. end >= next_range. end,
117- "Replace ranges should either be disjoint or nested: ({:?}, {:?}) ({:?}, {:?})" ,
118- range,
121+ node_range. 0 . end <= next_node_range. 0 . start
122+ || node_range. 0 . end >= next_node_range. 0 . end,
123+ "Node ranges should be disjoint or nested: ({:?}, {:?}) ({:?}, {:?})" ,
124+ node_range,
119125 tokens,
120- next_range ,
126+ next_node_range ,
121127 next_tokens,
122128 ) ;
123129 }
@@ -135,20 +141,23 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
135141 // start position, we ensure that any (outer) replace range which
136142 // encloses another (inner) replace range will fully overwrite the
137143 // inner range's replacement.
138- for ( range, target) in replace_ranges. into_iter ( ) . rev ( ) {
139- assert ! ( !range. is_empty( ) , "Cannot replace an empty range: {range:?}" ) ;
144+ for ( node_range, target) in node_replacements. into_iter ( ) . rev ( ) {
145+ assert ! (
146+ !node_range. 0 . is_empty( ) ,
147+ "Cannot replace an empty node range: {:?}" ,
148+ node_range. 0
149+ ) ;
140150
141151 // Replace the tokens in range with zero or one `FlatToken::AttrsTarget`s, plus
142152 // enough `FlatToken::Empty`s to fill up the rest of the range. This keeps the
143153 // total length of `tokens` constant throughout the replacement process, allowing
144- // us to use all of the `ReplaceRanges` entries without adjusting indices.
154+ // us to do all replacements without adjusting indices.
145155 let target_len = target. is_some ( ) as usize ;
146156 tokens. splice (
147- ( range. start as usize ) ..( range. end as usize ) ,
148- target
149- . into_iter ( )
150- . map ( |target| FlatToken :: AttrsTarget ( target) )
151- . chain ( iter:: repeat ( FlatToken :: Empty ) . take ( range. len ( ) - target_len) ) ,
157+ ( node_range. 0 . start as usize ) ..( node_range. 0 . end as usize ) ,
158+ target. into_iter ( ) . map ( |target| FlatToken :: AttrsTarget ( target) ) . chain (
159+ iter:: repeat ( FlatToken :: Empty ) . take ( node_range. 0 . len ( ) - target_len) ,
160+ ) ,
152161 ) ;
153162 }
154163 make_attr_token_stream ( tokens. into_iter ( ) , self . break_last_token )
@@ -215,7 +224,7 @@ impl<'a> Parser<'a> {
215224 let cursor_snapshot = self . token_cursor . clone ( ) ;
216225 let start_pos = self . num_bump_calls ;
217226 let has_outer_attrs = !attrs. attrs . is_empty ( ) ;
218- let replace_ranges_start = self . capture_state . replace_ranges . len ( ) ;
227+ let parser_replacements_start = self . capture_state . parser_replacements . len ( ) ;
219228
220229 // We set and restore `Capturing::Yes` on either side of the call to
221230 // `f`, so we can distinguish the outermost call to
@@ -270,7 +279,7 @@ impl<'a> Parser<'a> {
270279 return Ok ( ret) ;
271280 }
272281
273- let replace_ranges_end = self . capture_state . replace_ranges . len ( ) ;
282+ let parser_replacements_end = self . capture_state . parser_replacements . len ( ) ;
274283
275284 assert ! (
276285 !( self . break_last_token && capture_trailing) ,
@@ -287,15 +296,16 @@ impl<'a> Parser<'a> {
287296
288297 let num_calls = end_pos - start_pos;
289298
290- // Take the captured ranges for any inner attributes that we parsed in
291- // `Parser::parse_inner_attributes`, and pair them in a `ReplaceRange`
292- // with `None`, which means the relevant tokens will be removed. (More
293- // details below.)
294- let mut inner_attr_replace_ranges = Vec :: new ( ) ;
299+ // Take the captured `ParserRange`s for any inner attributes that we parsed in
300+ // `Parser::parse_inner_attributes`, and pair them in a `ParserReplacement` with `None`,
301+ // which means the relevant tokens will be removed. (More details below.)
302+ let mut inner_attr_parser_replacements = Vec :: new ( ) ;
295303 for attr in ret. attrs ( ) {
296304 if attr. style == ast:: AttrStyle :: Inner {
297- if let Some ( attr_range) = self . capture_state . inner_attr_ranges . remove ( & attr. id ) {
298- inner_attr_replace_ranges. push ( ( attr_range, None ) ) ;
305+ if let Some ( inner_attr_parser_range) =
306+ self . capture_state . inner_attr_parser_ranges . remove ( & attr. id )
307+ {
308+ inner_attr_parser_replacements. push ( ( inner_attr_parser_range, None ) ) ;
299309 } else {
300310 self . dcx ( ) . span_delayed_bug ( attr. span , "Missing token range for attribute" ) ;
301311 }
@@ -304,37 +314,41 @@ impl<'a> Parser<'a> {
304314
305315 // This is hot enough for `deep-vector` that checking the conditions for an empty iterator
306316 // is measurably faster than actually executing the iterator.
307- let replace_ranges: Box < [ ReplaceRange ] > =
308- if replace_ranges_start == replace_ranges_end && inner_attr_replace_ranges. is_empty ( ) {
309- Box :: new ( [ ] )
310- } else {
311- // Grab any replace ranges that occur *inside* the current AST node. We will
312- // perform the actual replacement only when we convert the `LazyAttrTokenStream` to
313- // an `AttrTokenStream`.
314- self . capture_state . replace_ranges [ replace_ranges_start..replace_ranges_end]
315- . iter ( )
316- . cloned ( )
317- . chain ( inner_attr_replace_ranges. iter ( ) . cloned ( ) )
318- . map ( |( range, data) | ( ( range. start - start_pos) ..( range. end - start_pos) , data) )
319- . collect ( )
320- } ;
317+ let node_replacements: Box < [ _ ] > = if parser_replacements_start == parser_replacements_end
318+ && inner_attr_parser_replacements. is_empty ( )
319+ {
320+ Box :: new ( [ ] )
321+ } else {
322+ // Grab any replace ranges that occur *inside* the current AST node. Convert them
323+ // from `ParserRange` form to `NodeRange` form. We will perform the actual
324+ // replacement only when we convert the `LazyAttrTokenStream` to an
325+ // `AttrTokenStream`.
326+ self . capture_state . parser_replacements
327+ [ parser_replacements_start..parser_replacements_end]
328+ . iter ( )
329+ . cloned ( )
330+ . chain ( inner_attr_parser_replacements. iter ( ) . cloned ( ) )
331+ . map ( |( parser_range, data) | ( NodeRange :: new ( parser_range, start_pos) , data) )
332+ . collect ( )
333+ } ;
321334
322335 // What is the status here when parsing the example code at the top of this method?
323336 //
324337 // When parsing `g`:
325338 // - `start_pos..end_pos` is `12..33` (`fn g { ... }`, excluding the outer attr).
326- // - `inner_attr_replace_ranges ` has one entry (`5..15`, when counting from `fn `), to
339+ // - `inner_attr_parser_replacements ` has one entry (`ParserRange(17..27) `), to
327340 // delete the inner attr's tokens.
328- // - This entry is put into the lazy tokens for `g`, i.e. deleting the inner attr from
329- // those tokens (if they get evaluated).
341+ // - This entry is converted to `NodeRange(5..15)` (relative to the `fn`) and put into
342+ // the lazy tokens for `g`, i.e. deleting the inner attr from those tokens (if they get
343+ // evaluated).
330344 // - Those lazy tokens are also put into an `AttrsTarget` that is appended to `self`'s
331345 // replace ranges at the bottom of this function, for processing when parsing `m`.
332- // - `replace_ranges_start..replace_ranges_end ` is empty.
346+ // - `parser_replacements_start..parser_replacements_end ` is empty.
333347 //
334348 // When parsing `m`:
335349 // - `start_pos..end_pos` is `0..34` (`mod m`, excluding the `#[cfg_eval]` attribute).
336- // - `inner_attr_replace_ranges ` is empty.
337- // - `replace_range_start..replace_ranges_end ` has one entry.
350+ // - `inner_attr_parser_replacements ` is empty.
351+ // - `parser_replacements_start..parser_replacements_end ` has one entry.
338352 // - One `AttrsTarget` (added below when parsing `g`) to replace all of `g` (`3..33`,
339353 // including its outer attribute), with:
340354 // - `attrs`: includes the outer and the inner attr.
@@ -345,7 +359,7 @@ impl<'a> Parser<'a> {
345359 num_calls,
346360 cursor_snapshot,
347361 break_last_token : self . break_last_token ,
348- replace_ranges ,
362+ node_replacements ,
349363 } ) ;
350364
351365 // If we support tokens and don't already have them, store the newly captured tokens.
@@ -366,7 +380,7 @@ impl<'a> Parser<'a> {
366380 // What is the status here when parsing the example code at the top of this method?
367381 //
368382 // When parsing `g`, we add one entry:
369- // - The `start_pos..end_pos` (` 3..33`) entry has a new `AttrsTarget` with:
383+ // - The pushed entry (`ParserRange( 3..33)`) has a new `AttrsTarget` with:
370384 // - `attrs`: includes the outer and the inner attr.
371385 // - `tokens`: lazy tokens for `g` (with its inner attr deleted).
372386 //
@@ -377,12 +391,14 @@ impl<'a> Parser<'a> {
377391 // cfg-expand this AST node.
378392 let start_pos = if has_outer_attrs { attrs. start_pos } else { start_pos } ;
379393 let target = AttrsTarget { attrs : ret. attrs ( ) . iter ( ) . cloned ( ) . collect ( ) , tokens } ;
380- self . capture_state . replace_ranges . push ( ( start_pos..end_pos, Some ( target) ) ) ;
394+ self . capture_state
395+ . parser_replacements
396+ . push ( ( ParserRange ( start_pos..end_pos) , Some ( target) ) ) ;
381397 } else if matches ! ( self . capture_state. capturing, Capturing :: No ) {
382398 // Only clear the ranges once we've finished capturing entirely, i.e. we've finished
383399 // the outermost call to this method.
384- self . capture_state . replace_ranges . clear ( ) ;
385- self . capture_state . inner_attr_ranges . clear ( ) ;
400+ self . capture_state . parser_replacements . clear ( ) ;
401+ self . capture_state . inner_attr_parser_ranges . clear ( ) ;
386402 }
387403 Ok ( ret)
388404 }
0 commit comments