rust/compiler
Aaron Hill f916b0474a
Implement span quoting for proc-macros
This PR implements span quoting, allowing proc-macros to produce spans
pointing *into their own crate*. This is used by the unstable
`proc_macro::quote!` macro, allowing us to get error messages like this:

```
error[E0412]: cannot find type `MissingType` in this scope
  --> $DIR/auxiliary/span-from-proc-macro.rs:37:20
   |
LL | pub fn error_from_attribute(_args: TokenStream, _input: TokenStream) -> TokenStream {
   | ----------------------------------------------------------------------------------- in this expansion of procedural macro `#[error_from_attribute]`
...
LL |             field: MissingType
   |                    ^^^^^^^^^^^ not found in this scope
   |
  ::: $DIR/span-from-proc-macro.rs:8:1
   |
LL | #[error_from_attribute]
   | ----------------------- in this macro invocation
```

Here, `MissingType` occurs inside the implementation of the proc-macro
`#[error_from_attribute]`. Previosuly, this would always result in a
span pointing at `#[error_from_attribute]`

This will make many proc-macro-related error message much more useful -
when a proc-macro generates code containing an error, users will get an
error message pointing directly at that code (within the macro
definition), instead of always getting a span pointing at the macro
invocation site.

This is implemented as follows:
* When a proc-macro crate is being *compiled*, it causes the `quote!`
  macro to get run. This saves all of the sapns in the input to `quote!`
  into the metadata of *the proc-macro-crate* (which we are currently
  compiling). The `quote!` macro then expands to a call to
  `proc_macro::Span::recover_proc_macro_span(id)`, where `id` is an
opaque identifier for the span in the crate metadata.
* When the same proc-macro crate is *run* (e.g. it is loaded from disk
  and invoked by some consumer crate), the call to
`proc_macro::Span::recover_proc_macro_span` causes us to load the span
from the proc-macro crate's metadata. The proc-macro then produces a
`TokenStream` containing a `Span` pointing into the proc-macro crate
itself.

The recursive nature of 'quote!' can be difficult to understand at
first. The file `src/test/ui/proc-macro/quote-debug.stdout` shows
the output of the `quote!` macro, which should make this eaier to
understand.

This PR also supports custom quoting spans in custom quote macros (e.g.
the `quote` crate). All span quoting goes through the
`proc_macro::quote_span` method, which can be called by a custom quote
macro to perform span quoting. An example of this usage is provided in
`src/test/ui/proc-macro/auxiliary/custom-quote.rs`

Custom quoting currently has a few limitations:

In order to quote a span, we need to generate a call to
`proc_macro::Span::recover_proc_macro_span`. However, proc-macros
support renaming the `proc_macro` crate, so we can't simply hardcode
this path. Previously, the `quote_span` method used the path
`crate::Span` - however, this only works when it is called by the
builtin `quote!` macro in the same crate. To support being called from
arbitrary crates, we need access to the name of the `proc_macro` crate
to generate a path. This PR adds an additional argument to `quote_span`
to specify the name of the `proc_macro` crate. Howver, this feels kind
of hacky, and we may want to change this before stabilizing anything
quote-related.

Additionally, using `quote_span` currently requires enabling the
`proc_macro_internals` feature. The builtin `quote!` macro
has an `#[allow_internal_unstable]` attribute, but this won't work for
custom quote implementations. This will likely require some additional
tricks to apply `allow_internal_unstable` to the span of
`proc_macro::Span::recover_proc_macro_span`.
2021-05-12 00:51:31 -04:00
..
rustc use jemallocator in rustc/rustdoc 2021-04-04 14:10:26 -07:00
rustc_apfloat Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_arena Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_ast Auto merge of #83386 - mark-i-m:stabilize-pat2015, r=nikomatsakis 2021-04-28 20:35:17 +00:00
rustc_ast_lowering improve diagnosts for GATs 2021-05-11 14:09:46 +02:00
rustc_ast_passes Implement RFC 2951: Native link modifiers 2021-05-05 16:04:25 -07:00
rustc_ast_pretty parser: Remove support for inner attributes on non-block expressions 2021-05-03 13:33:53 +03:00
rustc_attr Allow specifying alignment for functions 2021-04-05 17:36:51 -04:00
rustc_builtin_macros Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_codegen_cranelift Implement RFC 2951: Native link modifiers 2021-05-05 16:04:25 -07:00
rustc_codegen_llvm Support -C passes in NewPM 2021-05-08 10:58:08 +02:00
rustc_codegen_ssa Adjust target search algorithm for rustlib path 2021-05-10 19:15:19 +03:00
rustc_data_structures Only compute Obligation cache_key once in register_obligation_at 2021-05-04 11:57:53 -07:00
rustc_driver Rollup merge of #83895 - eggyal:issue-83883, r=jyn514 2021-04-06 17:42:31 +02:00
rustc_error_codes Auto merge of #83213 - rylev:update-lints-to-errors, r=nikomatsakis 2021-05-04 08:09:23 +00:00
rustc_errors Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_expand Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_feature remove const_fn feature gate 2021-05-09 14:29:31 +02:00
rustc_fs_util
rustc_graphviz Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_hir Add type to differentiate between fake and real DefId's 2021-05-04 19:34:12 +02:00
rustc_hir_pretty Handle pretty printing of else if let clauses 2021-04-23 13:29:18 -04:00
rustc_incremental Add [TRACKED_NO_CRATE_HASH] and [SUBSTRUCT] directives 2021-04-27 16:46:33 +00:00
rustc_index Change bitwise operator to more easily keep data in vector registers 2021-05-04 11:55:18 -04:00
rustc_infer remove unused variant 2021-05-06 14:59:53 +02:00
rustc_interface Adjust target search algorithm for rustlib path 2021-05-10 19:15:19 +03:00
rustc_lexer Fix outdated crate names in compiler docs 2021-04-08 11:12:14 -05:00
rustc_lint Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_lint_defs Auto merge of #83213 - rylev:update-lints-to-errors, r=nikomatsakis 2021-05-04 08:09:23 +00:00
rustc_llvm Support -C passes in NewPM 2021-05-08 10:58:08 +02:00
rustc_macros Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_metadata Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_middle Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_mir Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_mir_build Auto merge of #84310 - RalfJung:const-fn-feature-flags, r=oli-obk 2021-04-24 23:16:03 +00:00
rustc_parse Auto merge of #85104 - hi-rustin:rustin-patch-typo, r=jonas-schievink 2021-05-10 07:15:23 +00:00
rustc_parse_format Rollup merge of #83348 - osa1:issue83344, r=jackh726 2021-03-28 01:33:13 +09:00
rustc_passes Rollup merge of #85018 - hi-rustin:rustin-patch-84637, r=estebank 2021-05-12 07:18:00 +09:00
rustc_plugin_impl Remove hir::Item::attrs. 2021-03-09 19:27:50 +01:00
rustc_privacy fix few typos 2021-04-19 15:57:08 +03:00
rustc_query_impl Apply --cfg parallel_compiler when documenting 2021-05-01 00:25:11 -04:00
rustc_query_system Show nicer error when an 'unstable fingerprints' error occurs 2021-05-10 17:43:51 -04:00
rustc_resolve Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_save_analysis Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_serialize Auto merge of #83465 - michaelwoerister:safe-read_raw_bytes, r=cjgillot 2021-03-26 01:28:59 +00:00
rustc_session Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_span Implement span quoting for proc-macros 2021-05-12 00:51:31 -04:00
rustc_symbol_mangling Rollup merge of #80525 - devsnek:wasm64, r=nagisa 2021-04-05 00:24:23 +02:00
rustc_target Adjust target search algorithm for rustlib path 2021-05-10 19:15:19 +03:00
rustc_trait_selection Rollup merge of #84987 - lcnr:nits, r=Mark-Simulacrum 2021-05-07 00:38:45 +02:00
rustc_traits Auto merge of #83207 - oli-obk:valtree2, r=lcnr 2021-04-02 10:28:12 +00:00
rustc_ty_utils Implement changes suggested by tmiasko and davidtwco 2021-05-10 17:47:50 +02:00
rustc_type_ir Make TypeFoldable::is_global() false when fresh tys/consts are present 2021-05-01 16:58:33 -04:00
rustc_typeck Rollup merge of #85018 - hi-rustin:rustin-patch-84637, r=estebank 2021-05-12 07:18:00 +09:00