@@ -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}
@@ -53,10 +56,9 @@ impl AttrWrapper {
5356
5457 /// Prepend `self.attrs` to `attrs`.
5558 // FIXME: require passing an NT to prevent misuse of this method
56- pub ( crate ) fn prepend_to_nt_inner ( self , attrs : & mut AttrVec ) {
57- let mut self_attrs = self . attrs ;
58- mem:: swap ( attrs, & mut self_attrs) ;
59- attrs. extend ( self_attrs) ;
59+ pub ( crate ) fn prepend_to_nt_inner ( mut self , attrs : & mut AttrVec ) {
60+ mem:: swap ( attrs, & mut self . attrs ) ;
61+ attrs. extend ( self . attrs ) ;
6062 }
6163
6264 pub fn is_empty ( & self ) -> bool {
@@ -89,7 +91,7 @@ struct LazyAttrTokenStreamImpl {
8991 cursor_snapshot : TokenCursor ,
9092 num_calls : u32 ,
9193 break_last_token : bool ,
92- replace_ranges : Box < [ ReplaceRange ] > ,
94+ node_replacements : Box < [ NodeReplacement ] > ,
9395}
9496
9597impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
@@ -104,21 +106,24 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
104106 . chain ( iter:: repeat_with ( || FlatToken :: Token ( cursor_snapshot. next ( ) ) ) )
105107 . take ( self . num_calls as usize ) ;
106108
107- if self . replace_ranges . is_empty ( ) {
109+ if self . node_replacements . is_empty ( ) {
108110 make_attr_token_stream ( tokens, self . break_last_token )
109111 } else {
110112 let mut tokens: Vec < _ > = tokens. collect ( ) ;
111- let mut replace_ranges = self . replace_ranges . to_vec ( ) ;
112- 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 ) ;
113115
114116 #[ cfg( debug_assertions) ]
115- 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+ {
116120 assert ! (
117- range. end <= next_range. start || range. end >= next_range. end,
118- "Replace ranges should either be disjoint or nested: ({:?}, {:?}) ({:?}, {:?})" ,
119- 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,
120125 tokens,
121- next_range ,
126+ next_node_range ,
122127 next_tokens,
123128 ) ;
124129 }
@@ -136,20 +141,23 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
136141 // start position, we ensure that any (outer) replace range which
137142 // encloses another (inner) replace range will fully overwrite the
138143 // inner range's replacement.
139- for ( range, target) in replace_ranges. into_iter ( ) . rev ( ) {
140- 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+ ) ;
141150
142151 // Replace the tokens in range with zero or one `FlatToken::AttrsTarget`s, plus
143152 // enough `FlatToken::Empty`s to fill up the rest of the range. This keeps the
144153 // total length of `tokens` constant throughout the replacement process, allowing
145- // us to use all of the `ReplaceRanges` entries without adjusting indices.
154+ // us to do all replacements without adjusting indices.
146155 let target_len = target. is_some ( ) as usize ;
147156 tokens. splice (
148- ( range. start as usize ) ..( range. end as usize ) ,
149- target
150- . into_iter ( )
151- . map ( |target| FlatToken :: AttrsTarget ( target) )
152- . 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+ ) ,
153161 ) ;
154162 }
155163 make_attr_token_stream ( tokens. into_iter ( ) , self . break_last_token )
@@ -216,7 +224,7 @@ impl<'a> Parser<'a> {
216224 let cursor_snapshot = self . token_cursor . clone ( ) ;
217225 let start_pos = self . num_bump_calls ;
218226 let has_outer_attrs = !attrs. attrs . is_empty ( ) ;
219- let replace_ranges_start = self . capture_state . replace_ranges . len ( ) ;
227+ let parser_replacements_start = self . capture_state . parser_replacements . len ( ) ;
220228
221229 // We set and restore `Capturing::Yes` on either side of the call to
222230 // `f`, so we can distinguish the outermost call to
@@ -271,7 +279,7 @@ impl<'a> Parser<'a> {
271279 return Ok ( ret) ;
272280 }
273281
274- let replace_ranges_end = self . capture_state . replace_ranges . len ( ) ;
282+ let parser_replacements_end = self . capture_state . parser_replacements . len ( ) ;
275283
276284 assert ! (
277285 !( self . break_last_token && capture_trailing) ,
@@ -288,15 +296,16 @@ impl<'a> Parser<'a> {
288296
289297 let num_calls = end_pos - start_pos;
290298
291- // Take the captured ranges for any inner attributes that we parsed in
292- // `Parser::parse_inner_attributes`, and pair them in a `ReplaceRange`
293- // with `None`, which means the relevant tokens will be removed. (More
294- // details below.)
295- 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 ( ) ;
296303 for attr in ret. attrs ( ) {
297304 if attr. style == ast:: AttrStyle :: Inner {
298- if let Some ( attr_range) = self . capture_state . inner_attr_ranges . remove ( & attr. id ) {
299- 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 ) ) ;
300309 } else {
301310 self . dcx ( ) . span_delayed_bug ( attr. span , "Missing token range for attribute" ) ;
302311 }
@@ -305,37 +314,41 @@ impl<'a> Parser<'a> {
305314
306315 // This is hot enough for `deep-vector` that checking the conditions for an empty iterator
307316 // is measurably faster than actually executing the iterator.
308- let replace_ranges: Box < [ ReplaceRange ] > =
309- if replace_ranges_start == replace_ranges_end && inner_attr_replace_ranges. is_empty ( ) {
310- Box :: new ( [ ] )
311- } else {
312- // Grab any replace ranges that occur *inside* the current AST node. We will
313- // perform the actual replacement only when we convert the `LazyAttrTokenStream` to
314- // an `AttrTokenStream`.
315- self . capture_state . replace_ranges [ replace_ranges_start..replace_ranges_end]
316- . iter ( )
317- . cloned ( )
318- . chain ( inner_attr_replace_ranges. iter ( ) . cloned ( ) )
319- . map ( |( range, data) | ( ( range. start - start_pos) ..( range. end - start_pos) , data) )
320- . collect ( )
321- } ;
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+ } ;
322334
323335 // What is the status here when parsing the example code at the top of this method?
324336 //
325337 // When parsing `g`:
326338 // - `start_pos..end_pos` is `12..33` (`fn g { ... }`, excluding the outer attr).
327- // - `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
328340 // delete the inner attr's tokens.
329- // - This entry is put into the lazy tokens for `g`, i.e. deleting the inner attr from
330- // 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).
331344 // - Those lazy tokens are also put into an `AttrsTarget` that is appended to `self`'s
332345 // replace ranges at the bottom of this function, for processing when parsing `m`.
333- // - `replace_ranges_start..replace_ranges_end ` is empty.
346+ // - `parser_replacements_start..parser_replacements_end ` is empty.
334347 //
335348 // When parsing `m`:
336349 // - `start_pos..end_pos` is `0..34` (`mod m`, excluding the `#[cfg_eval]` attribute).
337- // - `inner_attr_replace_ranges ` is empty.
338- // - `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.
339352 // - One `AttrsTarget` (added below when parsing `g`) to replace all of `g` (`3..33`,
340353 // including its outer attribute), with:
341354 // - `attrs`: includes the outer and the inner attr.
@@ -346,7 +359,7 @@ impl<'a> Parser<'a> {
346359 num_calls,
347360 cursor_snapshot,
348361 break_last_token : self . break_last_token ,
349- replace_ranges ,
362+ node_replacements ,
350363 } ) ;
351364
352365 // If we support tokens and don't already have them, store the newly captured tokens.
@@ -367,7 +380,7 @@ impl<'a> Parser<'a> {
367380 // What is the status here when parsing the example code at the top of this method?
368381 //
369382 // When parsing `g`, we add one entry:
370- // - 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:
371384 // - `attrs`: includes the outer and the inner attr.
372385 // - `tokens`: lazy tokens for `g` (with its inner attr deleted).
373386 //
@@ -378,12 +391,14 @@ impl<'a> Parser<'a> {
378391 // cfg-expand this AST node.
379392 let start_pos = if has_outer_attrs { attrs. start_pos } else { start_pos } ;
380393 let target = AttrsTarget { attrs : ret. attrs ( ) . iter ( ) . cloned ( ) . collect ( ) , tokens } ;
381- 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) ) ) ;
382397 } else if matches ! ( self . capture_state. capturing, Capturing :: No ) {
383398 // Only clear the ranges once we've finished capturing entirely, i.e. we've finished
384399 // the outermost call to this method.
385- self . capture_state . replace_ranges . clear ( ) ;
386- self . capture_state . inner_attr_ranges . clear ( ) ;
400+ self . capture_state . parser_replacements . clear ( ) ;
401+ self . capture_state . inner_attr_parser_ranges . clear ( ) ;
387402 }
388403 Ok ( ret)
389404 }
0 commit comments