rust/compiler/rustc_expand/src
Aaron Hill a93c4f05de
Implement token-based handling of attributes during expansion
This PR modifies the macro expansion infrastructure to handle attributes
in a fully token-based manner. As a result:

* Derives macros no longer lose spans when their input is modified
  by eager cfg-expansion. This is accomplished by performing eager
  cfg-expansion on the token stream that we pass to the derive
  proc-macro
* Inner attributes now preserve spans in all cases, including when we
  have multiple inner attributes in a row.

This is accomplished through the following changes:

* New structs `AttrAnnotatedTokenStream` and `AttrAnnotatedTokenTree` are introduced.
  These are very similar to a normal `TokenTree`, but they also track
  the position of attributes and attribute targets within the stream.
  They are built when we collect tokens during parsing.
  An `AttrAnnotatedTokenStream` is converted to a regular `TokenStream` when
  we invoke a macro.
* Token capturing and `LazyTokenStream` are modified to work with
  `AttrAnnotatedTokenStream`. A new `ReplaceRange` type is introduced, which
  is created during the parsing of a nested AST node to make the 'outer'
  AST node aware of the attributes and attribute target stored deeper in the token stream.
* When we need to perform eager cfg-expansion (either due to `#[derive]` or `#[cfg_eval]`),
we tokenize and reparse our target, capturing additional information about the locations of
`#[cfg]` and `#[cfg_attr]` attributes at any depth within the target.
This is a performance optimization, allowing us to perform less work
in the typical case where captured tokens never have eager cfg-expansion run.
2021-04-11 01:31:36 -04:00
..
mbe address comments 2021-04-02 08:08:02 +08:00
mut_visit
parse
tokenstream Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
base.rs Implement token-based handling of attributes during expansion 2021-04-11 01:31:36 -04:00
build.rs Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
config.rs Implement token-based handling of attributes during expansion 2021-04-11 01:31:36 -04:00
expand.rs Implement token-based handling of attributes during expansion 2021-04-11 01:31:36 -04:00
lib.rs Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
mbe.rs
module.rs expand: Resolve and expand inner attributes on out-of-line modules 2021-03-14 18:10:29 +03:00
placeholders.rs ast/hir: Rename field-related structures 2021-03-16 11:41:24 +03:00
proc_macro_server.rs Extend proc_macro_back_compat lint to js-sys 2021-03-19 14:40:20 -04:00
proc_macro.rs Implement token-based handling of attributes during expansion 2021-04-11 01:31:36 -04:00
tests.rs