2019-10-09 01:23:10 +00:00
|
|
|
//! Contains `ParseSess` which holds state living beyond what one `Parser` might.
|
|
|
|
//! It also serves as an input to the parser itself.
|
|
|
|
|
2021-09-29 00:39:30 +00:00
|
|
|
use crate::config::CheckCfg;
|
2020-01-05 08:40:16 +00:00
|
|
|
use crate::lint::{BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId};
|
2022-04-25 20:53:09 +00:00
|
|
|
use crate::SessionDiagnostic;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::node_id::NodeId;
|
2019-11-29 20:45:26 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2020-11-12 21:35:46 +00:00
|
|
|
use rustc_data_structures::sync::{Lock, Lrc};
|
2020-01-02 10:41:57 +00:00
|
|
|
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_errors::{
|
2022-03-26 07:27:43 +00:00
|
|
|
error_code, fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder,
|
2022-04-25 20:53:09 +00:00
|
|
|
DiagnosticMessage, ErrorGuaranteed, MultiSpan,
|
2022-03-24 02:03:04 +00:00
|
|
|
};
|
2020-01-02 10:41:57 +00:00
|
|
|
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::edition::Edition;
|
|
|
|
use rustc_span::hygiene::ExpnId;
|
|
|
|
use rustc_span::source_map::{FilePathMapping, SourceMap};
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_span::{Span, Symbol};
|
2019-10-09 01:23:10 +00:00
|
|
|
|
|
|
|
use std::str;
|
|
|
|
|
2019-11-29 21:09:00 +00:00
|
|
|
/// The set of keys (and, optionally, values) that define the compilation
|
|
|
|
/// environment of the crate, used to drive conditional compilation.
|
|
|
|
pub type CrateConfig = FxHashSet<(Symbol, Option<Symbol>)>;
|
2021-09-29 00:39:30 +00:00
|
|
|
pub type CrateCheckConfig = CheckCfg<Symbol>;
|
2019-11-29 20:45:26 +00:00
|
|
|
|
2019-10-09 01:23:10 +00:00
|
|
|
/// Collected spans during parsing for places where a certain feature was
|
|
|
|
/// used and should be feature gated accordingly in `check_crate`.
|
|
|
|
#[derive(Default)]
|
2019-10-30 16:34:00 +00:00
|
|
|
pub struct GatedSpans {
|
|
|
|
pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>,
|
2019-10-30 15:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GatedSpans {
|
|
|
|
/// Feature gate the given `span` under the given `feature`
|
|
|
|
/// which is same `Symbol` used in `active.rs`.
|
|
|
|
pub fn gate(&self, feature: Symbol, span: Span) {
|
|
|
|
self.spans.borrow_mut().entry(feature).or_default().push(span);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ungate the last span under the given `feature`.
|
|
|
|
/// Panics if the given `span` wasn't the last one.
|
|
|
|
///
|
|
|
|
/// Using this is discouraged unless you have a really good reason to.
|
|
|
|
pub fn ungate_last(&self, feature: Symbol, span: Span) {
|
|
|
|
let removed_span = self.spans.borrow_mut().entry(feature).or_default().pop().unwrap();
|
|
|
|
debug_assert_eq!(span, removed_span);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the provided `feature` gate ungated currently?
|
|
|
|
///
|
|
|
|
/// Using this is discouraged unless you have a really good reason to.
|
|
|
|
pub fn is_ungated(&self, feature: Symbol) -> bool {
|
|
|
|
self.spans.borrow().get(&feature).map_or(true, |spans| spans.is_empty())
|
|
|
|
}
|
2019-10-30 16:34:00 +00:00
|
|
|
|
|
|
|
/// Prepend the given set of `spans` onto the set in `self`.
|
|
|
|
pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) {
|
|
|
|
let mut inner = self.spans.borrow_mut();
|
|
|
|
for (gate, mut gate_spans) in inner.drain() {
|
|
|
|
spans.entry(gate).or_default().append(&mut gate_spans);
|
|
|
|
}
|
|
|
|
*inner = spans;
|
|
|
|
}
|
2019-10-09 01:23:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:38:31 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct SymbolGallery {
|
2020-08-02 15:20:00 +00:00
|
|
|
/// All symbols occurred and their first occurrence span.
|
2020-08-05 07:29:13 +00:00
|
|
|
pub symbols: Lock<FxHashMap<Symbol, Span>>,
|
2020-04-25 01:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SymbolGallery {
|
|
|
|
/// Insert a symbol and its span into symbol gallery.
|
2022-03-16 12:12:30 +00:00
|
|
|
/// If the symbol has occurred before, ignore the new occurrence.
|
2020-04-25 01:38:31 +00:00
|
|
|
pub fn insert(&self, symbol: Symbol, span: Span) {
|
|
|
|
self.symbols.lock().entry(symbol).or_insert(span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 10:55:00 +00:00
|
|
|
/// Construct a diagnostic for a language feature error due to the given `span`.
|
|
|
|
/// The `feature`'s `Symbol` is the one you used in `active.rs` and `rustc_span::symbols`.
|
2020-01-02 10:41:57 +00:00
|
|
|
pub fn feature_err<'a>(
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
feature: Symbol,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
explain: &str,
|
2022-01-23 18:34:26 +00:00
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
2020-01-02 10:41:57 +00:00
|
|
|
feature_err_issue(sess, feature, span, GateIssue::Language, explain)
|
|
|
|
}
|
|
|
|
|
2020-01-02 10:55:00 +00:00
|
|
|
/// Construct a diagnostic for a feature gate error.
|
|
|
|
///
|
|
|
|
/// This variant allows you to control whether it is a library or language feature.
|
|
|
|
/// Almost always, you want to use this for a language feature. If so, prefer `feature_err`.
|
2020-01-02 10:41:57 +00:00
|
|
|
pub fn feature_err_issue<'a>(
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
feature: Symbol,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
issue: GateIssue,
|
|
|
|
explain: &str,
|
2022-01-23 18:34:26 +00:00
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
2020-01-08 17:27:06 +00:00
|
|
|
let mut err = sess.span_diagnostic.struct_span_err_with_code(span, explain, error_code!(E0658));
|
2022-02-28 20:29:06 +00:00
|
|
|
add_feature_diagnostics_for_issue(&mut err, sess, feature, issue);
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the diagnostics for a feature to an existing error.
|
|
|
|
pub fn add_feature_diagnostics<'a>(err: &mut Diagnostic, sess: &'a ParseSess, feature: Symbol) {
|
|
|
|
add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language);
|
|
|
|
}
|
2020-01-02 10:41:57 +00:00
|
|
|
|
2022-02-28 20:29:06 +00:00
|
|
|
/// Adds the diagnostics for a feature to an existing error.
|
|
|
|
///
|
|
|
|
/// This variant allows you to control whether it is a library or language feature.
|
|
|
|
/// Almost always, you want to use this for a language feature. If so, prefer
|
|
|
|
/// `add_feature_diagnostics`.
|
|
|
|
pub fn add_feature_diagnostics_for_issue<'a>(
|
|
|
|
err: &mut Diagnostic,
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
feature: Symbol,
|
|
|
|
issue: GateIssue,
|
|
|
|
) {
|
2020-01-02 10:41:57 +00:00
|
|
|
if let Some(n) = find_feature_issue(feature, issue) {
|
|
|
|
err.note(&format!(
|
2022-03-25 05:11:05 +00:00
|
|
|
"see issue #{n} <https://github.com/rust-lang/rust/issues/{n}> for more information"
|
2020-01-02 10:41:57 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
|
|
|
|
if sess.unstable_features.is_nightly_build() {
|
2022-03-25 05:11:05 +00:00
|
|
|
err.help(&format!("add `#![feature({feature})]` to the crate attributes to enable"));
|
2020-01-02 10:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:23:10 +00:00
|
|
|
/// Info about a parsing session.
|
|
|
|
pub struct ParseSess {
|
|
|
|
pub span_diagnostic: Handler,
|
2019-10-15 20:48:13 +00:00
|
|
|
pub unstable_features: UnstableFeatures,
|
2019-10-09 01:23:10 +00:00
|
|
|
pub config: CrateConfig,
|
2021-09-29 00:39:30 +00:00
|
|
|
pub check_config: CrateCheckConfig,
|
2019-10-09 01:23:10 +00:00
|
|
|
pub edition: Edition,
|
2021-08-29 08:34:23 +00:00
|
|
|
/// Places where raw identifiers were used. This is used to avoid complaining about idents
|
|
|
|
/// clashing with keywords in new editions.
|
2019-10-09 01:23:10 +00:00
|
|
|
pub raw_identifier_spans: Lock<Vec<Span>>,
|
2021-08-29 08:34:23 +00:00
|
|
|
/// Places where identifiers that contain invalid Unicode codepoints but that look like they
|
2021-09-10 07:30:58 +00:00
|
|
|
/// should be. Useful to avoid bad tokenization when encountering emoji. We group them to
|
2021-08-29 08:34:23 +00:00
|
|
|
/// provide a single error per unique incorrect identifier.
|
|
|
|
pub bad_unicode_identifiers: Lock<FxHashMap<Symbol, Vec<Span>>>,
|
2019-10-09 01:23:10 +00:00
|
|
|
source_map: Lrc<SourceMap>,
|
|
|
|
pub buffered_lints: Lock<Vec<BufferedEarlyLint>>,
|
|
|
|
/// Contains the spans of block expressions that could have been incomplete based on the
|
|
|
|
/// operation token that followed it, but that the parser cannot identify without further
|
|
|
|
/// analysis.
|
|
|
|
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
|
2019-10-30 16:34:00 +00:00
|
|
|
pub gated_spans: GatedSpans,
|
2020-04-25 01:38:31 +00:00
|
|
|
pub symbol_gallery: SymbolGallery,
|
2019-10-29 00:44:20 +00:00
|
|
|
/// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors.
|
|
|
|
pub reached_eof: Lock<bool>,
|
2020-05-03 17:47:51 +00:00
|
|
|
/// Environment variables accessed during the build and their values when they exist.
|
|
|
|
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
|
2021-04-09 14:35:40 +00:00
|
|
|
/// File paths accessed during the build.
|
|
|
|
pub file_depinfo: Lock<FxHashSet<Symbol>>,
|
2020-07-09 20:49:55 +00:00
|
|
|
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
|
|
|
|
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
|
2021-01-28 08:24:55 +00:00
|
|
|
/// Whether cfg(version) should treat the current release as incomplete
|
|
|
|
pub assume_incomplete_release: bool,
|
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`.
2020-08-02 23:52:16 +00:00
|
|
|
/// Spans passed to `proc_macro::quote_span`. Each span has a numerical
|
|
|
|
/// identifier represented by its position in the vector.
|
|
|
|
pub proc_macro_quoted_spans: Lock<Vec<Span>>,
|
2019-10-09 01:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseSess {
|
2021-03-16 05:50:34 +00:00
|
|
|
/// Used for testing.
|
2019-11-20 02:38:31 +00:00
|
|
|
pub fn new(file_path_mapping: FilePathMapping) -> Self {
|
2022-04-12 08:34:40 +00:00
|
|
|
let fallback_bundle = fallback_fluent_bundle(rustc_errors::DEFAULT_LOCALE_RESOURCES, false);
|
2020-02-22 14:07:05 +00:00
|
|
|
let sm = Lrc::new(SourceMap::new(file_path_mapping));
|
2022-03-26 07:27:43 +00:00
|
|
|
let handler = Handler::with_tty_emitter(
|
|
|
|
ColorConfig::Auto,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
Some(sm.clone()),
|
2022-03-28 08:36:20 +00:00
|
|
|
None,
|
2022-03-26 07:27:43 +00:00
|
|
|
fallback_bundle,
|
|
|
|
);
|
2020-02-22 14:07:05 +00:00
|
|
|
ParseSess::with_span_handler(handler, sm)
|
2019-10-09 01:23:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 08:26:10 +00:00
|
|
|
pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
|
2019-10-09 01:23:10 +00:00
|
|
|
Self {
|
|
|
|
span_diagnostic: handler,
|
2020-10-10 18:27:52 +00:00
|
|
|
unstable_features: UnstableFeatures::from_environment(None),
|
2019-10-09 01:23:10 +00:00
|
|
|
config: FxHashSet::default(),
|
2021-09-29 00:39:30 +00:00
|
|
|
check_config: CrateCheckConfig::default(),
|
2019-10-09 01:23:10 +00:00
|
|
|
edition: ExpnId::root().expn_data().edition,
|
|
|
|
raw_identifier_spans: Lock::new(Vec::new()),
|
2021-08-29 08:34:23 +00:00
|
|
|
bad_unicode_identifiers: Lock::new(Default::default()),
|
2019-10-09 01:23:10 +00:00
|
|
|
source_map,
|
|
|
|
buffered_lints: Lock::new(vec![]),
|
|
|
|
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
|
|
|
|
gated_spans: GatedSpans::default(),
|
2020-04-25 01:38:31 +00:00
|
|
|
symbol_gallery: SymbolGallery::default(),
|
2019-10-29 00:44:20 +00:00
|
|
|
reached_eof: Lock::new(false),
|
2020-05-03 17:47:51 +00:00
|
|
|
env_depinfo: Default::default(),
|
2021-04-09 14:35:40 +00:00
|
|
|
file_depinfo: Default::default(),
|
2020-07-09 20:49:55 +00:00
|
|
|
type_ascription_path_suggestions: Default::default(),
|
2021-01-28 08:24:55 +00:00
|
|
|
assume_incomplete_release: false,
|
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`.
2020-08-02 23:52:16 +00:00
|
|
|
proc_macro_quoted_spans: Default::default(),
|
2019-10-09 01:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 17:15:45 +00:00
|
|
|
pub fn with_silent_emitter(fatal_note: Option<String>) -> Self {
|
2022-04-12 08:34:40 +00:00
|
|
|
let fallback_bundle = fallback_fluent_bundle(rustc_errors::DEFAULT_LOCALE_RESOURCES, false);
|
2020-02-22 14:07:05 +00:00
|
|
|
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
2022-03-26 07:27:43 +00:00
|
|
|
let fatal_handler =
|
2022-03-28 08:36:20 +00:00
|
|
|
Handler::with_tty_emitter(ColorConfig::Auto, false, None, None, None, fallback_bundle);
|
2021-10-02 17:15:45 +00:00
|
|
|
let handler = Handler::with_emitter(
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
Box::new(SilentEmitter { fatal_handler, fatal_note }),
|
|
|
|
);
|
2020-02-22 14:07:05 +00:00
|
|
|
ParseSess::with_span_handler(handler, sm)
|
2019-11-03 17:04:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:23:10 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn source_map(&self) -> &SourceMap {
|
|
|
|
&self.source_map
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:34:17 +00:00
|
|
|
pub fn clone_source_map(&self) -> Lrc<SourceMap> {
|
|
|
|
self.source_map.clone()
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:23:10 +00:00
|
|
|
pub fn buffer_lint(
|
|
|
|
&self,
|
2020-01-05 08:40:16 +00:00
|
|
|
lint: &'static Lint,
|
2019-10-09 01:23:10 +00:00
|
|
|
span: impl Into<MultiSpan>,
|
2020-01-05 08:40:16 +00:00
|
|
|
node_id: NodeId,
|
2019-10-09 01:23:10 +00:00
|
|
|
msg: &str,
|
|
|
|
) {
|
|
|
|
self.buffered_lints.with_lock(|buffered_lints| {
|
|
|
|
buffered_lints.push(BufferedEarlyLint {
|
|
|
|
span: span.into(),
|
2020-01-05 08:40:16 +00:00
|
|
|
node_id,
|
2019-10-09 01:23:10 +00:00
|
|
|
msg: msg.into(),
|
2020-01-05 08:40:16 +00:00
|
|
|
lint_id: LintId::of(lint),
|
|
|
|
diagnostic: BuiltinLintDiagnostics::Normal,
|
2019-10-09 01:23:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-22 00:01:48 +00:00
|
|
|
pub fn buffer_lint_with_diagnostic(
|
|
|
|
&self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
node_id: NodeId,
|
|
|
|
msg: &str,
|
|
|
|
diagnostic: BuiltinLintDiagnostics,
|
|
|
|
) {
|
|
|
|
self.buffered_lints.with_lock(|buffered_lints| {
|
|
|
|
buffered_lints.push(BufferedEarlyLint {
|
|
|
|
span: span.into(),
|
|
|
|
node_id,
|
|
|
|
msg: msg.into(),
|
|
|
|
lint_id: LintId::of(lint),
|
|
|
|
diagnostic,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:23:10 +00:00
|
|
|
/// Extend an error with a suggestion to wrap an expression with parentheses to allow the
|
|
|
|
/// parser to continue parsing the following operation as part of the same expression.
|
2022-01-23 20:41:46 +00:00
|
|
|
pub fn expr_parentheses_needed(&self, err: &mut Diagnostic, span: Span) {
|
2021-06-28 18:22:47 +00:00
|
|
|
err.multipart_suggestion(
|
|
|
|
"parentheses are required to parse this as an expression",
|
|
|
|
vec![(span.shrink_to_lo(), "(".to_string()), (span.shrink_to_hi(), ")".to_string())],
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2019-10-09 01:23:10 +00:00
|
|
|
}
|
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`.
2020-08-02 23:52:16 +00:00
|
|
|
|
|
|
|
pub fn save_proc_macro_span(&self, span: Span) -> usize {
|
|
|
|
let mut spans = self.proc_macro_quoted_spans.lock();
|
|
|
|
spans.push(span);
|
|
|
|
return spans.len() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn proc_macro_quoted_spans(&self) -> Vec<Span> {
|
|
|
|
self.proc_macro_quoted_spans.lock().clone()
|
|
|
|
}
|
2022-04-25 20:53:09 +00:00
|
|
|
|
2022-05-06 02:44:41 +00:00
|
|
|
pub fn create_err<'a>(
|
|
|
|
&'a self,
|
|
|
|
err: impl SessionDiagnostic<'a>,
|
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
|
|
|
err.into_diagnostic(self)
|
|
|
|
}
|
|
|
|
|
2022-04-25 20:53:09 +00:00
|
|
|
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
|
2022-05-06 02:44:41 +00:00
|
|
|
self.create_err(err).emit()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_warning<'a>(
|
|
|
|
&'a self,
|
|
|
|
warning: impl SessionDiagnostic<'a, ()>,
|
|
|
|
) -> DiagnosticBuilder<'a, ()> {
|
|
|
|
warning.into_diagnostic(self)
|
2022-04-25 20:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) {
|
2022-05-06 02:44:41 +00:00
|
|
|
self.create_warning(warning).emit()
|
2022-04-25 20:53:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-04-25 20:53:09 +00:00
|
|
|
pub fn struct_err(
|
|
|
|
&self,
|
|
|
|
msg: impl Into<DiagnosticMessage>,
|
|
|
|
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
|
|
|
|
self.span_diagnostic.struct_err(msg)
|
|
|
|
}
|
2022-04-25 21:49:53 +00:00
|
|
|
|
2022-07-01 13:48:23 +00:00
|
|
|
#[rustc_lint_diagnostics]
|
2022-04-25 21:49:53 +00:00
|
|
|
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
|
|
|
|
self.span_diagnostic.struct_warn(msg)
|
|
|
|
}
|
2019-10-09 01:23:10 +00:00
|
|
|
}
|