2019-06-10 08:59:03 +00:00
|
|
|
//! Diagnostics creation and emission for `rustc`.
|
2019-06-09 10:04:40 +00:00
|
|
|
//!
|
|
|
|
//! This module contains the code for creating and emitting diagnostics.
|
|
|
|
|
2020-09-23 19:51:56 +00:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
2019-06-10 08:59:03 +00:00
|
|
|
#![feature(crate_visibility_modifier)]
|
2020-08-22 19:24:48 +00:00
|
|
|
#![feature(backtrace)]
|
Stabilize extended_key_value_attributes
# Stabilization report
## Summary
This stabilizes using macro expansion in key-value attributes, like so:
```rust
#[doc = include_str!("my_doc.md")]
struct S;
#[path = concat!(env!("OUT_DIR"), "/generated.rs")]
mod m;
```
See the changes to the reference for details on what macros are allowed;
see Petrochenkov's excellent blog post [on internals](https://internals.rust-lang.org/t/macro-expansion-points-in-attributes/11455)
for alternatives that were considered and rejected ("why accept no more
and no less?")
This has been available on nightly since 1.50 with no major issues.
## Notes
### Accepted syntax
The parser accepts arbitrary Rust expressions in this position, but any expression other than a macro invocation will ultimately lead to an error because it is not expected by the built-in expression forms (e.g., `#[doc]`). Note that decorators and the like may be able to observe other expression forms.
### Expansion ordering
Expansion of macro expressions in "inert" attributes occurs after decorators have executed, analogously to macro expressions appearing in the function body or other parts of decorator input.
There is currently no way for decorators to accept macros in key-value position if macro expansion must be performed before the decorator executes (if the macro can simply be copied into the output for later expansion, that can work).
## Test cases
- https://github.com/rust-lang/rust/blob/master/src/test/ui/attributes/key-value-expansion-on-mac.rs
- https://github.com/rust-lang/rust/blob/master/src/test/rustdoc/external-doc.rs
The feature has also been dogfooded extensively in the compiler and
standard library:
- https://github.com/rust-lang/rust/pull/83329
- https://github.com/rust-lang/rust/pull/83230
- https://github.com/rust-lang/rust/pull/82641
- https://github.com/rust-lang/rust/pull/80534
## Implementation history
- Initial proposal: https://github.com/rust-lang/rust/issues/55414#issuecomment-554005412
- Experiment to see how much code it would break: https://github.com/rust-lang/rust/pull/67121
- Preliminary work to restrict expansion that would conflict with this
feature: https://github.com/rust-lang/rust/pull/77271
- Initial implementation: https://github.com/rust-lang/rust/pull/78837
- Fix for an ICE: https://github.com/rust-lang/rust/pull/80563
## Unresolved Questions
~~https://github.com/rust-lang/rust/pull/83366#issuecomment-805180738 listed some concerns, but they have been resolved as of this final report.~~
## Additional Information
There are two workarounds that have a similar effect for `#[doc]`
attributes on nightly. One is to emulate this behavior by using a limited version of this feature that was stabilized for historical reasons:
```rust
macro_rules! forward_inner_docs {
($e:expr => $i:item) => {
#[doc = $e]
$i
};
}
forward_inner_docs!(include_str!("lib.rs") => struct S {});
```
This also works for other attributes (like `#[path = concat!(...)]`).
The other is to use `doc(include)`:
```rust
#![feature(external_doc)]
#[doc(include = "lib.rs")]
struct S {}
```
The first works, but is non-trivial for people to discover, and
difficult to read and maintain. The second is a strange special-case for
a particular use of the macro. This generalizes it to work for any use
case, not just including files.
I plan to remove `doc(include)` when this is stabilized. The
`forward_inner_docs` workaround will still compile without warnings, but
I expect it to be used less once it's no longer necessary.
2021-03-22 05:10:10 +00:00
|
|
|
#![cfg_attr(bootstrap, feature(extended_key_value_attributes))]
|
2021-02-13 19:52:25 +00:00
|
|
|
#![feature(format_args_capture)]
|
2021-03-08 23:32:41 +00:00
|
|
|
#![feature(iter_zip)]
|
2019-02-10 07:13:30 +00:00
|
|
|
#![feature(nll)]
|
2016-06-21 22:08:13 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc_macros;
|
|
|
|
|
2016-06-21 22:08:13 +00:00
|
|
|
pub use emitter::ColorConfig;
|
2015-12-15 01:11:27 +00:00
|
|
|
|
2020-08-14 06:05:01 +00:00
|
|
|
use tracing::debug;
|
2019-02-06 18:53:01 +00:00
|
|
|
use Level::*;
|
2015-12-15 01:11:27 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
use emitter::{is_case_difference, Emitter, EmitterWriter};
|
2019-04-17 17:26:38 +00:00
|
|
|
use registry::Registry;
|
2019-09-23 02:45:21 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
|
2017-10-25 13:01:06 +00:00
|
|
|
use rustc_data_structures::stable_hasher::StableHasher;
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_data_structures::sync::{self, Lock, Lrc};
|
2019-12-25 18:38:57 +00:00
|
|
|
use rustc_data_structures::AtomicRef;
|
2020-08-13 19:41:52 +00:00
|
|
|
use rustc_lint_defs::FutureBreakage;
|
|
|
|
pub use rustc_lint_defs::{pluralize, Applicability};
|
2020-05-25 23:21:25 +00:00
|
|
|
use rustc_serialize::json::Json;
|
|
|
|
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::source_map::SourceMap;
|
|
|
|
use rustc_span::{Loc, MultiSpan, Span};
|
2017-10-25 13:01:06 +00:00
|
|
|
|
2017-06-11 11:31:40 +00:00
|
|
|
use std::borrow::Cow;
|
2020-05-25 23:21:25 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2021-02-18 11:25:45 +00:00
|
|
|
use std::num::NonZeroUsize;
|
2018-01-21 11:47:58 +00:00
|
|
|
use std::panic;
|
2019-05-02 02:06:33 +00:00
|
|
|
use std::path::Path;
|
2019-12-22 22:42:04 +00:00
|
|
|
use std::{error, fmt};
|
2015-12-15 01:11:27 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
use termcolor::{Color, ColorSpec};
|
2018-02-27 18:33:02 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
pub mod annotate_snippet_emitter_writer;
|
2017-08-19 00:09:55 +00:00
|
|
|
mod diagnostic;
|
|
|
|
mod diagnostic_builder;
|
2015-12-15 01:11:27 +00:00
|
|
|
pub mod emitter;
|
2019-12-22 22:42:04 +00:00
|
|
|
pub mod json;
|
|
|
|
mod lock;
|
2016-06-21 22:08:13 +00:00
|
|
|
pub mod registry;
|
2019-12-22 22:42:04 +00:00
|
|
|
mod snippet;
|
2017-07-02 20:46:38 +00:00
|
|
|
mod styled_buffer;
|
2019-11-06 18:39:57 +00:00
|
|
|
pub use snippet::Style;
|
2016-06-21 22:08:13 +00:00
|
|
|
|
2019-10-11 11:06:36 +00:00
|
|
|
pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
|
|
|
|
|
|
|
|
// `PResult` is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
// (See also the comment on `DiagnosticBuilderInner`.)
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2019-11-11 19:18:35 +00:00
|
|
|
rustc_data_structures::static_assert_size!(PResult<'_, bool>, 16);
|
2019-10-11 11:06:36 +00:00
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Encodable, Decodable)]
|
2019-02-08 10:45:53 +00:00
|
|
|
pub enum SuggestionStyle {
|
|
|
|
/// Hide the suggested code when displaying this suggestion inline.
|
|
|
|
HideCodeInline,
|
2019-02-09 11:39:08 +00:00
|
|
|
/// Always hide the suggested code but display the message.
|
2019-02-08 10:45:53 +00:00
|
|
|
HideCodeAlways,
|
2019-02-09 11:39:08 +00:00
|
|
|
/// Do not display this suggestion in the cli output, it is only meant for tools.
|
|
|
|
CompletelyHidden,
|
2019-02-08 10:45:53 +00:00
|
|
|
/// Always show the suggested code.
|
|
|
|
/// This will *not* show the code if the suggestion is inline *and* the suggested code is
|
|
|
|
/// empty.
|
|
|
|
ShowCode,
|
2019-10-03 20:22:18 +00:00
|
|
|
/// Always show the suggested code independently.
|
|
|
|
ShowAlways,
|
2019-02-08 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SuggestionStyle {
|
|
|
|
fn hide_inline(&self) -> bool {
|
2020-10-27 01:02:48 +00:00
|
|
|
!matches!(*self, SuggestionStyle::ShowCode)
|
2019-02-08 10:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:21:25 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Default)]
|
|
|
|
pub struct ToolMetadata(pub Option<Json>);
|
|
|
|
|
|
|
|
impl ToolMetadata {
|
|
|
|
fn new(json: Json) -> Self {
|
|
|
|
ToolMetadata(Some(json))
|
|
|
|
}
|
2021-01-18 22:10:31 +00:00
|
|
|
|
|
|
|
fn is_set(&self) -> bool {
|
|
|
|
self.0.is_some()
|
|
|
|
}
|
2020-05-25 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for ToolMetadata {
|
|
|
|
fn hash<H: Hasher>(&self, _state: &mut H) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't really need to round-trip
|
|
|
|
impl<D: Decoder> Decodable<D> for ToolMetadata {
|
|
|
|
fn decode(_d: &mut D) -> Result<Self, D::Error> {
|
|
|
|
Ok(ToolMetadata(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Encoder> Encodable<S> for ToolMetadata {
|
|
|
|
fn encode(&self, e: &mut S) -> Result<(), S::Error> {
|
|
|
|
match &self.0 {
|
|
|
|
None => e.emit_unit(),
|
|
|
|
Some(json) => json.encode(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
|
2015-12-13 12:12:47 +00:00
|
|
|
pub struct CodeSuggestion {
|
2017-05-09 08:04:24 +00:00
|
|
|
/// Each substitute can have multiple variants due to multiple
|
|
|
|
/// applicable suggestions
|
|
|
|
///
|
|
|
|
/// `foo.bar` might be replaced with `a.b` or `x.y` by replacing
|
|
|
|
/// `foo` and `bar` on their own:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// vec![
|
2017-11-03 15:17:33 +00:00
|
|
|
/// Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
|
|
|
|
/// Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
|
2017-05-09 08:04:24 +00:00
|
|
|
/// ]
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// or by replacing the entire span:
|
|
|
|
///
|
|
|
|
/// ```
|
2017-11-03 15:17:33 +00:00
|
|
|
/// vec![
|
|
|
|
/// Substitution { parts: vec![(0..7, "a.b")] },
|
|
|
|
/// Substitution { parts: vec![(0..7, "x.y")] },
|
|
|
|
/// ]
|
2017-05-09 08:04:24 +00:00
|
|
|
/// ```
|
2017-11-03 15:17:33 +00:00
|
|
|
pub substitutions: Vec<Substitution>,
|
2017-03-24 16:31:41 +00:00
|
|
|
pub msg: String,
|
2019-02-08 10:45:53 +00:00
|
|
|
/// Visual representation of this suggestion.
|
|
|
|
pub style: SuggestionStyle,
|
2018-01-18 11:47:46 +00:00
|
|
|
/// Whether or not the suggestion is approximate
|
|
|
|
///
|
|
|
|
/// Sometimes we may show suggestions with placeholders,
|
|
|
|
/// which are useful for users but not useful for
|
|
|
|
/// tools like rustfix
|
2018-04-25 21:51:06 +00:00
|
|
|
pub applicability: Applicability,
|
2020-05-25 23:21:25 +00:00
|
|
|
/// Tool-specific metadata
|
|
|
|
pub tool_metadata: ToolMetadata,
|
2016-06-21 22:08:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
|
2017-05-11 13:26:22 +00:00
|
|
|
/// See the docs on `CodeSuggestion::substitutions`
|
|
|
|
pub struct Substitution {
|
2017-11-03 15:17:33 +00:00
|
|
|
pub parts: Vec<SubstitutionPart>,
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, Encodable, Decodable)]
|
2017-11-03 15:17:33 +00:00
|
|
|
pub struct SubstitutionPart {
|
2017-05-11 13:26:22 +00:00
|
|
|
pub span: Span,
|
2017-11-03 15:17:33 +00:00
|
|
|
pub snippet: String,
|
2017-05-11 13:26:22 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 12:12:47 +00:00
|
|
|
impl CodeSuggestion {
|
2019-10-14 04:48:39 +00:00
|
|
|
/// Returns the assembled code suggestions, whether they should be shown with an underline
|
|
|
|
/// and whether the substitution only differs in capitalization.
|
2020-02-22 14:07:05 +00:00
|
|
|
pub fn splice_lines(&self, sm: &SourceMap) -> Vec<(String, Vec<SubstitutionPart>, bool)> {
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::{CharPos, Pos};
|
2015-12-13 12:12:47 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn push_trailing(
|
|
|
|
buf: &mut String,
|
|
|
|
line_opt: Option<&Cow<'_, str>>,
|
|
|
|
lo: &Loc,
|
|
|
|
hi_opt: Option<&Loc>,
|
|
|
|
) {
|
2016-10-18 17:43:02 +00:00
|
|
|
let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
|
2015-12-13 12:12:47 +00:00
|
|
|
if let Some(line) = line_opt {
|
2017-02-25 13:05:30 +00:00
|
|
|
if let Some(lo) = line.char_indices().map(|(i, _)| i).nth(lo) {
|
|
|
|
let hi_opt = hi_opt.and_then(|hi| line.char_indices().map(|(i, _)| i).nth(hi));
|
2019-01-17 13:53:21 +00:00
|
|
|
match hi_opt {
|
|
|
|
Some(hi) if hi > lo => buf.push_str(&line[lo..hi]),
|
|
|
|
Some(_) => (),
|
|
|
|
None => buf.push_str(&line[lo..]),
|
|
|
|
}
|
2015-12-13 12:12:47 +00:00
|
|
|
}
|
2020-03-04 14:53:14 +00:00
|
|
|
if hi_opt.is_none() {
|
2015-12-13 12:12:47 +00:00
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 15:17:33 +00:00
|
|
|
assert!(!self.substitutions.is_empty());
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
self.substitutions
|
|
|
|
.iter()
|
2020-01-17 02:27:18 +00:00
|
|
|
.filter(|subst| {
|
|
|
|
// Suggestions coming from macros can have malformed spans. This is a heavy
|
|
|
|
// handed approach to avoid ICEs by ignoring the suggestion outright.
|
2020-02-22 14:07:05 +00:00
|
|
|
let invalid = subst.parts.iter().any(|item| sm.is_valid_span(item.span).is_err());
|
2020-01-17 02:27:18 +00:00
|
|
|
if invalid {
|
|
|
|
debug!("splice_lines: suggestion contains an invalid span: {:?}", subst);
|
|
|
|
}
|
|
|
|
!invalid
|
|
|
|
})
|
2019-12-22 22:42:04 +00:00
|
|
|
.cloned()
|
2020-01-25 02:03:09 +00:00
|
|
|
.filter_map(|mut substitution| {
|
2019-12-22 22:42:04 +00:00
|
|
|
// Assumption: all spans are in the same file, and all spans
|
|
|
|
// are disjoint. Sort in ascending order.
|
|
|
|
substitution.parts.sort_by_key(|part| part.span.lo());
|
|
|
|
|
|
|
|
// Find the bounding span.
|
2020-01-25 02:03:09 +00:00
|
|
|
let lo = substitution.parts.iter().map(|part| part.span.lo()).min()?;
|
|
|
|
let hi = substitution.parts.iter().map(|part| part.span.hi()).max()?;
|
2019-12-22 22:42:04 +00:00
|
|
|
let bounding_span = Span::with_root_ctxt(lo, hi);
|
2020-01-25 02:03:09 +00:00
|
|
|
// The different spans might belong to different contexts, if so ignore suggestion.
|
2020-02-22 14:07:05 +00:00
|
|
|
let lines = sm.span_to_lines(bounding_span).ok()?;
|
2020-03-13 21:01:35 +00:00
|
|
|
assert!(!lines.lines.is_empty() || bounding_span.is_dummy());
|
2019-12-22 22:42:04 +00:00
|
|
|
|
2020-03-05 15:31:11 +00:00
|
|
|
// We can't splice anything if the source is unavailable.
|
|
|
|
if !sm.ensure_source_file_source_present(lines.file.clone()) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
// To build up the result, we do this for each span:
|
|
|
|
// - push the line segment trailing the previous span
|
|
|
|
// (at the beginning a "phantom" span pointing at the start of the line)
|
|
|
|
// - push lines between the previous and current span (if any)
|
|
|
|
// - if the previous and current span are not on the same line
|
|
|
|
// push the line segment leading up to the current span
|
|
|
|
// - splice in the span substitution
|
|
|
|
//
|
|
|
|
// Finally push the trailing line segment of the last span
|
2020-02-22 14:07:05 +00:00
|
|
|
let sf = &lines.file;
|
|
|
|
let mut prev_hi = sm.lookup_char_pos(bounding_span.lo());
|
2019-12-22 22:42:04 +00:00
|
|
|
prev_hi.col = CharPos::from_usize(0);
|
2020-03-13 21:01:35 +00:00
|
|
|
let mut prev_line =
|
|
|
|
lines.lines.get(0).and_then(|line0| sf.get_line(line0.line_index));
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut buf = String::new();
|
|
|
|
|
|
|
|
for part in &substitution.parts {
|
2020-02-22 14:07:05 +00:00
|
|
|
let cur_lo = sm.lookup_char_pos(part.span.lo());
|
2019-12-22 22:42:04 +00:00
|
|
|
if prev_hi.line == cur_lo.line {
|
|
|
|
push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
|
|
|
|
} else {
|
|
|
|
push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
|
|
|
|
// push lines between the previous and current span (if any)
|
|
|
|
for idx in prev_hi.line..(cur_lo.line - 1) {
|
2020-02-22 14:07:05 +00:00
|
|
|
if let Some(line) = sf.get_line(idx) {
|
2019-12-22 22:42:04 +00:00
|
|
|
buf.push_str(line.as_ref());
|
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 14:07:05 +00:00
|
|
|
if let Some(cur_line) = sf.get_line(cur_lo.line - 1) {
|
2020-04-12 15:36:37 +00:00
|
|
|
let end = match cur_line.char_indices().nth(cur_lo.col.to_usize()) {
|
|
|
|
Some((i, _)) => i,
|
|
|
|
None => cur_line.len(),
|
|
|
|
};
|
2019-12-22 22:42:04 +00:00
|
|
|
buf.push_str(&cur_line[..end]);
|
2017-05-09 08:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
buf.push_str(&part.snippet);
|
2020-02-22 14:07:05 +00:00
|
|
|
prev_hi = sm.lookup_char_pos(part.span.hi());
|
|
|
|
prev_line = sf.get_line(prev_hi.line - 1);
|
2015-12-13 12:12:47 +00:00
|
|
|
}
|
2020-02-22 14:07:05 +00:00
|
|
|
let only_capitalization = is_case_difference(sm, &buf, bounding_span);
|
2019-12-22 22:42:04 +00:00
|
|
|
// if the replacement already ends with a newline, don't print the next line
|
|
|
|
if !buf.ends_with('\n') {
|
|
|
|
push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
|
|
|
|
}
|
|
|
|
// remove trailing newlines
|
|
|
|
while buf.ends_with('\n') {
|
|
|
|
buf.pop();
|
|
|
|
}
|
2020-01-25 02:03:09 +00:00
|
|
|
Some((buf, substitution.parts, only_capitalization))
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
|
|
|
.collect()
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 17:15:40 +00:00
|
|
|
pub use rustc_span::fatal_error::{FatalError, FatalErrorMarker};
|
2015-12-15 01:11:27 +00:00
|
|
|
|
|
|
|
/// Signifies that the compiler died with an explicit call to `.bug`
|
|
|
|
/// or `.span_bug` rather than a failed assertion, etc.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct ExplicitBug;
|
|
|
|
|
|
|
|
impl fmt::Display for ExplicitBug {
|
2019-02-06 18:53:01 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-12-15 01:11:27 +00:00
|
|
|
write!(f, "parser internal bug")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 04:01:48 +00:00
|
|
|
impl error::Error for ExplicitBug {}
|
2015-12-15 01:11:27 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
pub use diagnostic::{Diagnostic, DiagnosticId, DiagnosticStyledString, SubDiagnostic};
|
2016-10-11 16:26:32 +00:00
|
|
|
pub use diagnostic_builder::DiagnosticBuilder;
|
2021-05-05 20:52:58 +00:00
|
|
|
use std::backtrace::Backtrace;
|
2015-12-18 03:15:53 +00:00
|
|
|
|
2019-05-02 02:06:33 +00:00
|
|
|
/// A handler deals with errors and other compiler output.
|
|
|
|
/// Certain errors (fatal, bug, unimpl) may cause immediate exit,
|
|
|
|
/// others log errors for later reporting.
|
2015-12-15 01:11:27 +00:00
|
|
|
pub struct Handler {
|
2019-09-07 16:09:52 +00:00
|
|
|
flags: HandlerFlags,
|
|
|
|
inner: Lock<HandlerInner>,
|
|
|
|
}
|
2017-11-20 18:03:20 +00:00
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
/// This inner struct exists to keep it all behind a single lock;
|
|
|
|
/// this is done to prevent possible deadlocks in a multi-threaded compiler,
|
|
|
|
/// as well as inconsistent state observation.
|
2019-09-07 16:09:52 +00:00
|
|
|
struct HandlerInner {
|
|
|
|
flags: HandlerFlags,
|
2019-06-22 11:46:48 +00:00
|
|
|
/// The number of errors that have been emitted, including duplicates.
|
|
|
|
///
|
|
|
|
/// This is not necessarily the count that's reported to the user once
|
|
|
|
/// compilation ends.
|
2019-09-07 16:09:52 +00:00
|
|
|
err_count: usize,
|
2020-08-22 19:24:48 +00:00
|
|
|
warn_count: usize,
|
2019-09-07 16:09:52 +00:00
|
|
|
deduplicated_err_count: usize,
|
|
|
|
emitter: Box<dyn Emitter + sync::Send>,
|
|
|
|
delayed_span_bugs: Vec<Diagnostic>,
|
2021-05-05 20:52:58 +00:00
|
|
|
delayed_good_path_bugs: Vec<DelayedDiagnostic>,
|
2017-10-25 13:01:06 +00:00
|
|
|
|
2019-04-17 09:03:39 +00:00
|
|
|
/// This set contains the `DiagnosticId` of all emitted diagnostics to avoid
|
|
|
|
/// emitting the same diagnostic with extended help (`--teach`) twice, which
|
2021-04-19 12:57:08 +00:00
|
|
|
/// would be unnecessary repetition.
|
2019-09-07 16:09:52 +00:00
|
|
|
taught_diagnostics: FxHashSet<DiagnosticId>,
|
2018-03-03 05:20:26 +00:00
|
|
|
|
|
|
|
/// Used to suggest rustc --explain <error code>
|
2019-09-07 16:09:52 +00:00
|
|
|
emitted_diagnostic_codes: FxHashSet<DiagnosticId>,
|
2017-10-25 13:01:06 +00:00
|
|
|
|
2019-04-17 09:03:39 +00:00
|
|
|
/// This set contains a hash of every diagnostic that has been emitted by
|
|
|
|
/// this handler. These hashes is used to avoid emitting the same error
|
|
|
|
/// twice.
|
2019-09-07 16:09:52 +00:00
|
|
|
emitted_diagnostics: FxHashSet<u128>,
|
2019-09-23 02:45:21 +00:00
|
|
|
|
|
|
|
/// Stashed diagnostics emitted in one stage of the compiler that may be
|
|
|
|
/// stolen by other stages (e.g. to improve them and add more information).
|
|
|
|
/// The stashed diagnostics count towards the total error count.
|
|
|
|
/// When `.abort_if_errors()` is called, these are also emitted.
|
|
|
|
stashed_diagnostics: FxIndexMap<(Span, StashKey), Diagnostic>,
|
2020-03-11 15:30:09 +00:00
|
|
|
|
|
|
|
/// The warning count, used for a recap upon finishing
|
|
|
|
deduplicated_warn_count: usize,
|
2020-08-13 19:41:52 +00:00
|
|
|
|
|
|
|
future_breakage_diagnostics: Vec<Diagnostic>,
|
2019-09-23 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A key denoting where from a diagnostic was stashed.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
|
|
|
pub enum StashKey {
|
|
|
|
ItemNoType,
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 09:03:36 +00:00
|
|
|
fn default_track_diagnostic(_: &Diagnostic) {}
|
|
|
|
|
2019-12-25 18:38:57 +00:00
|
|
|
pub static TRACK_DIAGNOSTICS: AtomicRef<fn(&Diagnostic)> =
|
|
|
|
AtomicRef::new(&(default_track_diagnostic as fn(&_)));
|
2018-03-15 09:03:36 +00:00
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2017-11-20 18:03:20 +00:00
|
|
|
pub struct HandlerFlags {
|
2018-09-15 04:27:55 +00:00
|
|
|
/// If false, warning-level lints are suppressed.
|
|
|
|
/// (rustc: see `--allow warnings` and `--cap-lints`)
|
2017-11-20 18:03:20 +00:00
|
|
|
pub can_emit_warnings: bool,
|
2018-09-15 04:27:55 +00:00
|
|
|
/// If true, error-level diagnostics are upgraded to bug-level.
|
|
|
|
/// (rustc: see `-Z treat-err-as-bug`)
|
2021-02-18 11:25:45 +00:00
|
|
|
pub treat_err_as_bug: Option<NonZeroUsize>,
|
2018-09-15 04:27:55 +00:00
|
|
|
/// If true, immediately emit diagnostics that would otherwise be buffered.
|
|
|
|
/// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
|
|
|
|
pub dont_buffer_diagnostics: bool,
|
|
|
|
/// If true, immediately print bugs registered with `delay_span_bug`.
|
|
|
|
/// (rustc: see `-Z report-delayed-bugs`)
|
2018-07-19 15:53:44 +00:00
|
|
|
pub report_delayed_bugs: bool,
|
2019-12-15 15:12:30 +00:00
|
|
|
/// Show macro backtraces.
|
|
|
|
/// (rustc: see `-Z macro-backtrace`)
|
|
|
|
pub macro_backtrace: bool,
|
2019-12-29 19:10:47 +00:00
|
|
|
/// If true, identical diagnostics are reported only once.
|
|
|
|
pub deduplicate_diagnostics: bool,
|
2017-11-20 18:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
impl Drop for HandlerInner {
|
2018-07-19 15:53:44 +00:00
|
|
|
fn drop(&mut self) {
|
2019-09-23 02:45:21 +00:00
|
|
|
self.emit_stashed_diagnostics();
|
|
|
|
|
|
|
|
if !self.has_errors() {
|
2019-09-07 16:09:52 +00:00
|
|
|
let bugs = std::mem::replace(&mut self.delayed_span_bugs, Vec::new());
|
2020-08-22 19:24:48 +00:00
|
|
|
self.flush_delayed(bugs, "no errors encountered even though `delay_span_bug` issued");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.has_any_message() {
|
|
|
|
let bugs = std::mem::replace(&mut self.delayed_good_path_bugs, Vec::new());
|
|
|
|
self.flush_delayed(
|
2021-05-05 20:52:58 +00:00
|
|
|
bugs.into_iter().map(DelayedDiagnostic::decorate).collect(),
|
2020-08-22 19:24:48 +00:00
|
|
|
"no warnings or errors encountered even though `delayed_good_path_bugs` issued",
|
|
|
|
);
|
2018-07-19 15:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 01:11:27 +00:00
|
|
|
impl Handler {
|
2019-09-23 20:28:14 +00:00
|
|
|
pub fn with_tty_emitter(
|
|
|
|
color_config: ColorConfig,
|
|
|
|
can_emit_warnings: bool,
|
2021-02-18 11:25:45 +00:00
|
|
|
treat_err_as_bug: Option<NonZeroUsize>,
|
2020-02-22 14:07:05 +00:00
|
|
|
sm: Option<Lrc<SourceMap>>,
|
2019-09-23 20:28:14 +00:00
|
|
|
) -> Self {
|
|
|
|
Self::with_tty_emitter_and_flags(
|
2017-11-20 18:03:20 +00:00
|
|
|
color_config,
|
2020-02-22 14:07:05 +00:00
|
|
|
sm,
|
2019-12-22 22:42:04 +00:00
|
|
|
HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() },
|
2019-09-23 20:28:14 +00:00
|
|
|
)
|
2017-11-20 18:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
pub fn with_tty_emitter_and_flags(
|
|
|
|
color_config: ColorConfig,
|
2020-02-22 14:07:05 +00:00
|
|
|
sm: Option<Lrc<SourceMap>>,
|
2019-09-23 20:28:14 +00:00
|
|
|
flags: HandlerFlags,
|
|
|
|
) -> Self {
|
2019-09-07 13:57:11 +00:00
|
|
|
let emitter = Box::new(EmitterWriter::stderr(
|
2019-09-23 20:28:14 +00:00
|
|
|
color_config,
|
2020-02-22 14:07:05 +00:00
|
|
|
sm,
|
2019-09-23 20:28:14 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
None,
|
2019-12-15 15:12:30 +00:00
|
|
|
flags.macro_backtrace,
|
2019-09-23 20:28:14 +00:00
|
|
|
));
|
|
|
|
Self::with_emitter_and_flags(emitter, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_emitter(
|
|
|
|
can_emit_warnings: bool,
|
2021-02-18 11:25:45 +00:00
|
|
|
treat_err_as_bug: Option<NonZeroUsize>,
|
2019-09-23 20:28:14 +00:00
|
|
|
emitter: Box<dyn Emitter + sync::Send>,
|
|
|
|
) -> Self {
|
2017-11-20 18:03:20 +00:00
|
|
|
Handler::with_emitter_and_flags(
|
2019-09-23 20:28:14 +00:00
|
|
|
emitter,
|
2019-12-22 22:42:04 +00:00
|
|
|
HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() },
|
2019-09-23 20:28:14 +00:00
|
|
|
)
|
2017-11-20 18:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
pub fn with_emitter_and_flags(
|
|
|
|
emitter: Box<dyn Emitter + sync::Send>,
|
2019-12-22 22:42:04 +00:00
|
|
|
flags: HandlerFlags,
|
2019-09-23 20:28:14 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2017-11-20 18:03:20 +00:00
|
|
|
flags,
|
2019-09-07 16:09:52 +00:00
|
|
|
inner: Lock::new(HandlerInner {
|
|
|
|
flags,
|
|
|
|
err_count: 0,
|
2020-08-22 19:24:48 +00:00
|
|
|
warn_count: 0,
|
2019-09-07 16:09:52 +00:00
|
|
|
deduplicated_err_count: 0,
|
2020-03-11 15:30:09 +00:00
|
|
|
deduplicated_warn_count: 0,
|
2019-09-23 20:28:14 +00:00
|
|
|
emitter,
|
2019-09-07 16:09:52 +00:00
|
|
|
delayed_span_bugs: Vec::new(),
|
2020-08-22 19:24:48 +00:00
|
|
|
delayed_good_path_bugs: Vec::new(),
|
2019-09-07 16:09:52 +00:00
|
|
|
taught_diagnostics: Default::default(),
|
|
|
|
emitted_diagnostic_codes: Default::default(),
|
|
|
|
emitted_diagnostics: Default::default(),
|
2019-09-23 02:45:21 +00:00
|
|
|
stashed_diagnostics: Default::default(),
|
2020-08-13 19:41:52 +00:00
|
|
|
future_breakage_diagnostics: Vec::new(),
|
2019-09-07 16:09:52 +00:00
|
|
|
}),
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
// This is here to not allow mutation of flags;
|
2020-03-29 13:24:45 +00:00
|
|
|
// as of this writing it's only used in tests in librustc_middle.
|
2019-09-07 16:09:52 +00:00
|
|
|
pub fn can_emit_warnings(&self) -> bool {
|
|
|
|
self.flags.can_emit_warnings
|
2016-03-25 17:17:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 12:33:20 +00:00
|
|
|
/// Resets the diagnostic error count as well as the cached emitted diagnostics.
|
|
|
|
///
|
2019-02-08 13:53:55 +00:00
|
|
|
/// NOTE: *do not* call this function from rustc. It is only meant to be called from external
|
2018-01-06 12:33:20 +00:00
|
|
|
/// tools that want to reuse a `Parser` cleaning the previously emitted diagnostics as well as
|
|
|
|
/// the overall count of emitted error diagnostics.
|
2017-08-10 00:17:03 +00:00
|
|
|
pub fn reset_err_count(&self) {
|
2019-09-07 16:09:52 +00:00
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
inner.err_count = 0;
|
2020-08-22 19:24:48 +00:00
|
|
|
inner.warn_count = 0;
|
2019-10-02 01:13:02 +00:00
|
|
|
inner.deduplicated_err_count = 0;
|
2020-03-11 15:30:09 +00:00
|
|
|
inner.deduplicated_warn_count = 0;
|
2019-10-02 01:13:02 +00:00
|
|
|
|
|
|
|
// actually free the underlying memory (which `clear` would not do)
|
|
|
|
inner.delayed_span_bugs = Default::default();
|
2020-08-22 19:24:48 +00:00
|
|
|
inner.delayed_good_path_bugs = Default::default();
|
2019-10-02 01:13:02 +00:00
|
|
|
inner.taught_diagnostics = Default::default();
|
|
|
|
inner.emitted_diagnostic_codes = Default::default();
|
|
|
|
inner.emitted_diagnostics = Default::default();
|
|
|
|
inner.stashed_diagnostics = Default::default();
|
2019-09-23 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stash a given diagnostic with the given `Span` and `StashKey` as the key for later stealing.
|
|
|
|
pub fn stash_diagnostic(&self, span: Span, key: StashKey, diag: Diagnostic) {
|
2019-09-23 17:29:02 +00:00
|
|
|
let mut inner = self.inner.borrow_mut();
|
2020-03-01 23:07:23 +00:00
|
|
|
// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
|
|
|
|
// if/when we have a more robust macro-friendly replacement for `(span, key)` as a key.
|
|
|
|
// See the PR for a discussion.
|
|
|
|
inner.stashed_diagnostics.insert((span, key), diag);
|
2019-09-23 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Steal a previously stashed diagnostic with the given `Span` and `StashKey` as the key.
|
|
|
|
pub fn steal_diagnostic(&self, span: Span, key: StashKey) -> Option<DiagnosticBuilder<'_>> {
|
|
|
|
self.inner
|
|
|
|
.borrow_mut()
|
|
|
|
.stashed_diagnostics
|
|
|
|
.remove(&(span, key))
|
|
|
|
.map(|diag| DiagnosticBuilder::new_diagnostic(self, diag))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit all stashed diagnostics.
|
|
|
|
pub fn emit_stashed_diagnostics(&self) {
|
|
|
|
self.inner.borrow_mut().emit_stashed_diagnostics();
|
2017-08-10 00:17:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
/// Construct a dummy builder with `Level::Cancelled`.
|
|
|
|
///
|
|
|
|
/// Using this will neither report anything to the user (e.g. a warning),
|
|
|
|
/// nor will compilation cancel as a result.
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn struct_dummy(&self) -> DiagnosticBuilder<'_> {
|
2016-04-21 09:14:58 +00:00
|
|
|
DiagnosticBuilder::new(self, Level::Cancelled, "")
|
2015-12-23 06:27:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
|
|
|
|
pub fn struct_span_warn(&self, span: impl Into<MultiSpan>, msg: &str) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_warn(msg);
|
|
|
|
result.set_span(span);
|
2015-12-18 03:15:53 +00:00
|
|
|
result
|
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
/// Construct a builder at the `Allow` level at the given `span` and with the `msg`.
|
|
|
|
pub fn struct_span_allow(
|
|
|
|
&self,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_allow(msg);
|
|
|
|
result.set_span(span);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
|
|
|
|
/// Also include a code.
|
|
|
|
pub fn struct_span_warn_with_code(
|
|
|
|
&self,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
|
|
|
) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_span_warn(span, msg);
|
2017-10-27 06:21:22 +00:00
|
|
|
result.code(code);
|
2015-12-18 03:15:53 +00:00
|
|
|
result
|
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
/// Construct a builder at the `Warning` level with the `msg`.
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
2016-04-21 09:14:58 +00:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
|
2017-11-20 18:03:20 +00:00
|
|
|
if !self.flags.can_emit_warnings {
|
2015-12-18 03:15:53 +00:00
|
|
|
result.cancel();
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
/// Construct a builder at the `Allow` level with the `msg`.
|
|
|
|
pub fn struct_allow(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
|
|
|
DiagnosticBuilder::new(self, Level::Allow, msg)
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
/// Construct a builder at the `Error` level at the given `span` and with the `msg`.
|
|
|
|
pub fn struct_span_err(&self, span: impl Into<MultiSpan>, msg: &str) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_err(msg);
|
|
|
|
result.set_span(span);
|
2015-12-23 06:27:20 +00:00
|
|
|
result
|
2015-12-18 03:15:53 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
/// Construct a builder at the `Error` level at the given `span`, with the `msg`, and `code`.
|
|
|
|
pub fn struct_span_err_with_code(
|
|
|
|
&self,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
|
|
|
) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_span_err(span, msg);
|
2017-10-27 06:21:22 +00:00
|
|
|
result.code(code);
|
2015-12-23 06:27:20 +00:00
|
|
|
result
|
2015-12-18 03:15:53 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
/// Construct a builder at the `Error` level with the `msg`.
|
2017-05-29 16:46:29 +00:00
|
|
|
// FIXME: This method should be removed (every error should have an associated error code).
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn struct_err(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
2016-04-21 09:14:58 +00:00
|
|
|
DiagnosticBuilder::new(self, Level::Error, msg)
|
2015-12-18 03:15:53 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
/// Construct a builder at the `Error` level with the `msg` and the `code`.
|
|
|
|
pub fn struct_err_with_code(&self, msg: &str, code: DiagnosticId) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_err(msg);
|
2017-10-27 06:21:22 +00:00
|
|
|
result.code(code);
|
2017-05-29 16:46:29 +00:00
|
|
|
result
|
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
/// Construct a builder at the `Fatal` level at the given `span` and with the `msg`.
|
|
|
|
pub fn struct_span_fatal(
|
|
|
|
&self,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_fatal(msg);
|
|
|
|
result.set_span(span);
|
2015-12-23 06:27:20 +00:00
|
|
|
result
|
2015-12-18 03:15:53 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
/// Construct a builder at the `Fatal` level at the given `span`, with the `msg`, and `code`.
|
|
|
|
pub fn struct_span_fatal_with_code(
|
|
|
|
&self,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
|
|
|
) -> DiagnosticBuilder<'_> {
|
|
|
|
let mut result = self.struct_span_fatal(span, msg);
|
2017-10-27 06:21:22 +00:00
|
|
|
result.code(code);
|
2015-12-23 06:27:20 +00:00
|
|
|
result
|
2015-12-18 03:15:53 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
/// Construct a builder at the `Error` level with the `msg`.
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn struct_fatal(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
2016-04-21 09:14:58 +00:00
|
|
|
DiagnosticBuilder::new(self, Level::Fatal, msg)
|
2015-12-23 06:27:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 23:14:25 +00:00
|
|
|
/// Construct a builder at the `Help` level with the `msg`.
|
|
|
|
pub fn struct_help(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
|
|
|
DiagnosticBuilder::new(self, Level::Help, msg)
|
|
|
|
}
|
|
|
|
|
2020-06-09 13:37:59 +00:00
|
|
|
/// Construct a builder at the `Note` level with the `msg`.
|
|
|
|
pub fn struct_note_without_error(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
|
|
|
DiagnosticBuilder::new(self, Level::Note, msg)
|
|
|
|
}
|
|
|
|
|
2021-03-28 02:45:01 +00:00
|
|
|
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: &str) -> ! {
|
2019-09-23 20:28:14 +00:00
|
|
|
self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
|
2021-03-28 02:45:01 +00:00
|
|
|
FatalError.raise()
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_fatal_with_code(
|
|
|
|
&self,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
2021-03-28 02:45:01 +00:00
|
|
|
) -> ! {
|
2019-09-23 20:28:14 +00:00
|
|
|
self.emit_diag_at_span(Diagnostic::new_with_code(Fatal, Some(code), msg), span);
|
2021-03-28 02:45:01 +00:00
|
|
|
FatalError.raise()
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_err(&self, span: impl Into<MultiSpan>, msg: &str) {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Error, msg), span);
|
2016-05-28 02:05:22 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_err_with_code(&self, span: impl Into<MultiSpan>, msg: &str, code: DiagnosticId) {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new_with_code(Error, Some(code), msg), span);
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: &str) {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Warning, msg), span);
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_warn_with_code(&self, span: impl Into<MultiSpan>, msg: &str, code: DiagnosticId) {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new_with_code(Warning, Some(code), msg), span);
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: &str) -> ! {
|
|
|
|
self.inner.borrow_mut().span_bug(span, msg)
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2020-05-26 17:49:11 +00:00
|
|
|
#[track_caller]
|
2019-09-23 20:28:14 +00:00
|
|
|
pub fn delay_span_bug(&self, span: impl Into<MultiSpan>, msg: &str) {
|
|
|
|
self.inner.borrow_mut().delay_span_bug(span, msg)
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2020-08-22 19:24:48 +00:00
|
|
|
pub fn delay_good_path_bug(&self, msg: &str) {
|
|
|
|
self.inner.borrow_mut().delay_good_path_bug(msg)
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
pub fn span_bug_no_panic(&self, span: impl Into<MultiSpan>, msg: &str) {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Bug, msg), span);
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_note_without_error(&self, span: impl Into<MultiSpan>, msg: &str) {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Note, msg), span);
|
2015-12-18 03:15:53 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
|
|
|
pub fn span_note_diag(&self, span: Span, msg: &str) -> DiagnosticBuilder<'_> {
|
2017-04-24 23:27:07 +00:00
|
|
|
let mut db = DiagnosticBuilder::new(self, Note, msg);
|
2019-09-23 20:28:14 +00:00
|
|
|
db.set_span(span);
|
2017-05-06 04:49:59 +00:00
|
|
|
db
|
2017-04-24 23:27:07 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2021-03-28 02:58:25 +00:00
|
|
|
// NOTE: intentionally doesn't raise an error so rustc_codegen_ssa only reports fatal errors in the main thread
|
2015-12-15 01:11:27 +00:00
|
|
|
pub fn fatal(&self, msg: &str) -> FatalError {
|
2019-09-07 16:09:52 +00:00
|
|
|
self.inner.borrow_mut().fatal(msg)
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2015-12-15 01:11:27 +00:00
|
|
|
pub fn err(&self, msg: &str) {
|
2019-09-07 16:09:52 +00:00
|
|
|
self.inner.borrow_mut().err(msg);
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2015-12-15 01:11:27 +00:00
|
|
|
pub fn warn(&self, msg: &str) {
|
2016-10-18 17:43:02 +00:00
|
|
|
let mut db = DiagnosticBuilder::new(self, Warning, msg);
|
2016-07-06 16:08:16 +00:00
|
|
|
db.emit();
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2015-12-18 03:15:53 +00:00
|
|
|
pub fn note_without_error(&self, msg: &str) {
|
2019-09-23 20:28:14 +00:00
|
|
|
DiagnosticBuilder::new(self, Note, msg).emit();
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
|
2015-12-15 01:11:27 +00:00
|
|
|
pub fn bug(&self, msg: &str) -> ! {
|
2019-09-07 16:09:52 +00:00
|
|
|
self.inner.borrow_mut().bug(msg)
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2015-12-15 01:11:27 +00:00
|
|
|
pub fn err_count(&self) -> usize {
|
2019-09-23 02:45:21 +00:00
|
|
|
self.inner.borrow().err_count()
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_errors(&self) -> bool {
|
2019-09-23 02:45:21 +00:00
|
|
|
self.inner.borrow().has_errors()
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-10-16 11:13:13 +00:00
|
|
|
pub fn has_errors_or_delayed_span_bugs(&self) -> bool {
|
|
|
|
self.inner.borrow().has_errors_or_delayed_span_bugs()
|
|
|
|
}
|
2018-03-15 09:09:20 +00:00
|
|
|
|
2019-04-17 17:26:38 +00:00
|
|
|
pub fn print_error_count(&self, registry: &Registry) {
|
2019-09-07 16:09:52 +00:00
|
|
|
self.inner.borrow_mut().print_error_count(registry)
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
pub fn take_future_breakage_diagnostics(&self) -> Vec<Diagnostic> {
|
|
|
|
std::mem::take(&mut self.inner.borrow_mut().future_breakage_diagnostics)
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
pub fn abort_if_errors(&self) {
|
2019-09-23 02:45:21 +00:00
|
|
|
self.inner.borrow_mut().abort_if_errors()
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:29:02 +00:00
|
|
|
/// `true` if we haven't taught a diagnostic with this code already.
|
|
|
|
/// The caller must then teach the user about such a diagnostic.
|
|
|
|
///
|
|
|
|
/// Used to suppress emitting the same error multiple times with extended explanation when
|
|
|
|
/// calling `-Zteach`.
|
2019-09-07 16:09:52 +00:00
|
|
|
pub fn must_teach(&self, code: &DiagnosticId) -> bool {
|
|
|
|
self.inner.borrow_mut().must_teach(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn force_print_diagnostic(&self, db: Diagnostic) {
|
|
|
|
self.inner.borrow_mut().force_print_diagnostic(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn emit_diagnostic(&self, diagnostic: &Diagnostic) {
|
|
|
|
self.inner.borrow_mut().emit_diagnostic(diagnostic)
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
fn emit_diag_at_span(&self, mut diag: Diagnostic, sp: impl Into<MultiSpan>) {
|
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
inner.emit_diagnostic(diag.set_span(sp));
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
pub fn emit_artifact_notification(&self, path: &Path, artifact_type: &str) {
|
|
|
|
self.inner.borrow_mut().emit_artifact_notification(path, artifact_type)
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
pub fn emit_future_breakage_report(&self, diags: Vec<(FutureBreakage, Diagnostic)>) {
|
|
|
|
self.inner.borrow_mut().emitter.emit_future_breakage_report(diags)
|
|
|
|
}
|
|
|
|
|
2020-08-09 23:57:35 +00:00
|
|
|
pub fn emit_unused_externs(&self, lint_level: &str, unused_externs: &[&str]) {
|
|
|
|
self.inner.borrow_mut().emit_unused_externs(lint_level, unused_externs)
|
2020-06-30 16:58:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
pub fn delay_as_bug(&self, diagnostic: Diagnostic) {
|
|
|
|
self.inner.borrow_mut().delay_as_bug(diagnostic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HandlerInner {
|
|
|
|
fn must_teach(&mut self, code: &DiagnosticId) -> bool {
|
|
|
|
self.taught_diagnostics.insert(code.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn force_print_diagnostic(&mut self, db: Diagnostic) {
|
|
|
|
self.emitter.emit_diagnostic(&db);
|
|
|
|
}
|
|
|
|
|
2019-09-23 02:45:21 +00:00
|
|
|
/// Emit all stashed diagnostics.
|
|
|
|
fn emit_stashed_diagnostics(&mut self) {
|
|
|
|
let diags = self.stashed_diagnostics.drain(..).map(|x| x.1).collect::<Vec<_>>();
|
|
|
|
diags.iter().for_each(|diag| self.emit_diagnostic(diag));
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) {
|
|
|
|
if diagnostic.cancelled() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
if diagnostic.has_future_breakage() {
|
|
|
|
self.future_breakage_diagnostics.push(diagnostic.clone());
|
|
|
|
}
|
|
|
|
|
2021-06-04 12:37:20 +00:00
|
|
|
if diagnostic.level == Warning
|
|
|
|
&& !self.flags.can_emit_warnings
|
|
|
|
&& !diagnostic.is_force_warn()
|
|
|
|
{
|
2020-08-13 19:41:52 +00:00
|
|
|
if diagnostic.has_future_breakage() {
|
|
|
|
(*TRACK_DIAGNOSTICS)(diagnostic);
|
|
|
|
}
|
2019-09-07 16:09:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-25 18:38:57 +00:00
|
|
|
(*TRACK_DIAGNOSTICS)(diagnostic);
|
2019-09-07 16:09:52 +00:00
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
if diagnostic.level == Allow {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
if let Some(ref code) = diagnostic.code {
|
|
|
|
self.emitted_diagnostic_codes.insert(code.clone());
|
|
|
|
}
|
|
|
|
|
2019-12-29 19:10:47 +00:00
|
|
|
let already_emitted = |this: &mut Self| {
|
2019-09-07 16:09:52 +00:00
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
diagnostic.hash(&mut hasher);
|
2019-12-29 19:10:47 +00:00
|
|
|
let diagnostic_hash = hasher.finish();
|
|
|
|
!this.emitted_diagnostics.insert(diagnostic_hash)
|
2019-09-07 16:09:52 +00:00
|
|
|
};
|
|
|
|
|
2020-01-03 14:03:11 +00:00
|
|
|
// Only emit the diagnostic if we've been asked to deduplicate and
|
|
|
|
// haven't already emitted an equivalent diagnostic.
|
2019-12-29 19:10:47 +00:00
|
|
|
if !(self.flags.deduplicate_diagnostics && already_emitted(self)) {
|
2019-09-07 16:09:52 +00:00
|
|
|
self.emitter.emit_diagnostic(diagnostic);
|
|
|
|
if diagnostic.is_error() {
|
|
|
|
self.deduplicated_err_count += 1;
|
2020-03-11 15:30:09 +00:00
|
|
|
} else if diagnostic.level == Warning {
|
|
|
|
self.deduplicated_warn_count += 1;
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if diagnostic.is_error() {
|
|
|
|
self.bump_err_count();
|
2020-08-22 19:24:48 +00:00
|
|
|
} else {
|
|
|
|
self.bump_warn_count();
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
|
|
|
|
self.emitter.emit_artifact_notification(path, artifact_type);
|
2020-06-30 16:58:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 23:57:35 +00:00
|
|
|
fn emit_unused_externs(&mut self, lint_level: &str, unused_externs: &[&str]) {
|
|
|
|
self.emitter.emit_unused_externs(lint_level, unused_externs);
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn treat_err_as_bug(&self) -> bool {
|
2021-02-18 11:25:45 +00:00
|
|
|
self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() >= c.get())
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_error_count(&mut self, registry: &Registry) {
|
2019-09-23 02:45:21 +00:00
|
|
|
self.emit_stashed_diagnostics();
|
|
|
|
|
2020-03-11 15:30:09 +00:00
|
|
|
let warnings = match self.deduplicated_warn_count {
|
|
|
|
0 => String::new(),
|
|
|
|
1 => "1 warning emitted".to_string(),
|
|
|
|
count => format!("{} warnings emitted", count),
|
|
|
|
};
|
|
|
|
let errors = match self.deduplicated_err_count {
|
|
|
|
0 => String::new(),
|
2018-03-15 09:09:20 +00:00
|
|
|
1 => "aborting due to previous error".to_string(),
|
2019-12-22 22:42:04 +00:00
|
|
|
count => format!("aborting due to {} previous errors", count),
|
2018-03-15 09:09:20 +00:00
|
|
|
};
|
2019-03-07 16:09:41 +00:00
|
|
|
if self.treat_err_as_bug() {
|
2019-03-07 09:54:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 09:09:20 +00:00
|
|
|
|
2020-03-11 15:30:09 +00:00
|
|
|
match (errors.len(), warnings.len()) {
|
|
|
|
(0, 0) => return,
|
2021-06-04 12:37:20 +00:00
|
|
|
(0, _) => self.emitter.emit_diagnostic(&Diagnostic::new(Level::Warning, &warnings)),
|
2020-03-11 15:30:09 +00:00
|
|
|
(_, 0) => {
|
|
|
|
let _ = self.fatal(&errors);
|
|
|
|
}
|
|
|
|
(_, _) => {
|
|
|
|
let _ = self.fatal(&format!("{}; {}", &errors, &warnings));
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 15:17:44 +00:00
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
let can_show_explain = self.emitter.should_show_explain();
|
|
|
|
let are_there_diagnostics = !self.emitted_diagnostic_codes.is_empty();
|
2018-02-28 15:17:44 +00:00
|
|
|
if can_show_explain && are_there_diagnostics {
|
2019-04-17 17:26:38 +00:00
|
|
|
let mut error_codes = self
|
|
|
|
.emitted_diagnostic_codes
|
|
|
|
.iter()
|
|
|
|
.filter_map(|x| match &x {
|
2020-02-28 22:32:09 +00:00
|
|
|
DiagnosticId::Error(s) => {
|
|
|
|
if let Ok(Some(_explanation)) = registry.try_find_description(s) {
|
|
|
|
Some(s.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-04-17 17:26:38 +00:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2018-02-28 15:17:44 +00:00
|
|
|
if !error_codes.is_empty() {
|
|
|
|
error_codes.sort();
|
|
|
|
if error_codes.len() > 1 {
|
|
|
|
let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
|
2019-12-22 22:42:04 +00:00
|
|
|
self.failure(&format!(
|
|
|
|
"Some errors have detailed explanations: {}{}",
|
|
|
|
error_codes[..limit].join(", "),
|
|
|
|
if error_codes.len() > 9 { "..." } else { "." }
|
|
|
|
));
|
|
|
|
self.failure(&format!(
|
|
|
|
"For more information about an error, try \
|
2020-01-09 10:56:38 +00:00
|
|
|
`rustc --explain {}`.",
|
2019-12-22 22:42:04 +00:00
|
|
|
&error_codes[0]
|
|
|
|
));
|
2018-02-28 15:17:44 +00:00
|
|
|
} else {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.failure(&format!(
|
|
|
|
"For more information about this error, try \
|
2020-01-09 10:56:38 +00:00
|
|
|
`rustc --explain {}`.",
|
2019-12-22 22:42:04 +00:00
|
|
|
&error_codes[0]
|
|
|
|
));
|
2018-02-28 15:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 09:09:20 +00:00
|
|
|
}
|
2015-12-15 01:11:27 +00:00
|
|
|
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2019-09-23 02:45:21 +00:00
|
|
|
fn err_count(&self) -> usize {
|
|
|
|
self.err_count + self.stashed_diagnostics.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_errors(&self) -> bool {
|
|
|
|
self.err_count() > 0
|
|
|
|
}
|
2019-10-16 11:13:13 +00:00
|
|
|
fn has_errors_or_delayed_span_bugs(&self) -> bool {
|
|
|
|
self.has_errors() || !self.delayed_span_bugs.is_empty()
|
|
|
|
}
|
2020-08-22 19:24:48 +00:00
|
|
|
fn has_any_message(&self) -> bool {
|
|
|
|
self.err_count() > 0 || self.warn_count > 0
|
|
|
|
}
|
2019-09-23 02:45:21 +00:00
|
|
|
|
|
|
|
fn abort_if_errors(&mut self) {
|
|
|
|
self.emit_stashed_diagnostics();
|
|
|
|
|
|
|
|
if self.has_errors() {
|
2019-09-07 15:21:17 +00:00
|
|
|
FatalError.raise();
|
2016-10-18 17:43:02 +00:00
|
|
|
}
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2017-08-12 22:37:28 +00:00
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
fn span_bug(&mut self, sp: impl Into<MultiSpan>, msg: &str) -> ! {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Bug, msg), sp);
|
2021-02-01 23:17:51 +00:00
|
|
|
panic::panic_any(ExplicitBug);
|
2019-09-23 17:29:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 20:28:14 +00:00
|
|
|
fn emit_diag_at_span(&mut self, mut diag: Diagnostic, sp: impl Into<MultiSpan>) {
|
|
|
|
self.emit_diagnostic(diag.set_span(sp));
|
2018-01-22 05:19:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:49:11 +00:00
|
|
|
#[track_caller]
|
2019-09-23 20:28:14 +00:00
|
|
|
fn delay_span_bug(&mut self, sp: impl Into<MultiSpan>, msg: &str) {
|
2020-04-24 04:03:20 +00:00
|
|
|
// This is technically `self.treat_err_as_bug()` but `delay_span_bug` is called before
|
|
|
|
// incrementing `err_count` by one, so we need to +1 the comparing.
|
|
|
|
// FIXME: Would be nice to increment err_count in a more coherent way.
|
2021-02-18 11:25:45 +00:00
|
|
|
if self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() + 1 >= c.get()) {
|
2019-09-07 16:09:52 +00:00
|
|
|
// FIXME: don't abort here if report_delayed_bugs is off
|
|
|
|
self.span_bug(sp, msg);
|
|
|
|
}
|
|
|
|
let mut diagnostic = Diagnostic::new(Level::Bug, msg);
|
|
|
|
diagnostic.set_span(sp.into());
|
2020-05-26 17:49:11 +00:00
|
|
|
diagnostic.note(&format!("delayed at {}", std::panic::Location::caller()));
|
2019-09-07 16:09:52 +00:00
|
|
|
self.delay_as_bug(diagnostic)
|
2018-03-20 22:41:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:24:48 +00:00
|
|
|
fn delay_good_path_bug(&mut self, msg: &str) {
|
2021-05-05 20:52:58 +00:00
|
|
|
let diagnostic = Diagnostic::new(Level::Bug, msg);
|
2020-08-22 19:24:48 +00:00
|
|
|
if self.flags.report_delayed_bugs {
|
|
|
|
self.emit_diagnostic(&diagnostic);
|
|
|
|
}
|
2021-05-05 20:52:58 +00:00
|
|
|
let backtrace = std::backtrace::Backtrace::force_capture();
|
|
|
|
self.delayed_good_path_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
|
2020-08-22 19:24:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn failure(&mut self, msg: &str) {
|
|
|
|
self.emit_diagnostic(&Diagnostic::new(FailureNote, msg));
|
|
|
|
}
|
2017-10-25 13:01:06 +00:00
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn fatal(&mut self, msg: &str) -> FatalError {
|
2019-09-23 20:28:14 +00:00
|
|
|
self.emit_error(Fatal, msg);
|
2019-09-07 16:09:52 +00:00
|
|
|
FatalError
|
|
|
|
}
|
2017-10-25 13:01:06 +00:00
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn err(&mut self, msg: &str) {
|
2019-09-23 20:28:14 +00:00
|
|
|
self.emit_error(Error, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit an error; level should be `Error` or `Fatal`.
|
2019-12-22 22:42:04 +00:00
|
|
|
fn emit_error(&mut self, level: Level, msg: &str) {
|
2019-09-07 16:09:52 +00:00
|
|
|
if self.treat_err_as_bug() {
|
|
|
|
self.bug(msg);
|
2018-01-22 05:19:37 +00:00
|
|
|
}
|
2019-09-23 20:28:14 +00:00
|
|
|
self.emit_diagnostic(&Diagnostic::new(level, msg));
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
2018-01-22 05:19:37 +00:00
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn bug(&mut self, msg: &str) -> ! {
|
|
|
|
self.emit_diagnostic(&Diagnostic::new(Bug, msg));
|
2021-02-01 23:17:51 +00:00
|
|
|
panic::panic_any(ExplicitBug);
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
2017-10-25 13:01:06 +00:00
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn delay_as_bug(&mut self, diagnostic: Diagnostic) {
|
|
|
|
if self.flags.report_delayed_bugs {
|
|
|
|
self.emit_diagnostic(&diagnostic);
|
2019-06-22 11:46:48 +00:00
|
|
|
}
|
2019-09-07 16:09:52 +00:00
|
|
|
self.delayed_span_bugs.push(diagnostic);
|
2017-08-12 22:37:28 +00:00
|
|
|
}
|
2015-12-15 01:11:27 +00:00
|
|
|
|
2020-08-22 19:24:48 +00:00
|
|
|
fn flush_delayed(&mut self, bugs: Vec<Diagnostic>, explanation: &str) {
|
|
|
|
let has_bugs = !bugs.is_empty();
|
|
|
|
for bug in bugs {
|
|
|
|
self.emit_diagnostic(&bug);
|
|
|
|
}
|
|
|
|
if has_bugs {
|
|
|
|
panic!("{}", explanation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn bump_err_count(&mut self) {
|
|
|
|
self.err_count += 1;
|
|
|
|
self.panic_if_treat_err_as_bug();
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:24:48 +00:00
|
|
|
fn bump_warn_count(&mut self) {
|
|
|
|
self.warn_count += 1;
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:09:52 +00:00
|
|
|
fn panic_if_treat_err_as_bug(&self) {
|
|
|
|
if self.treat_err_as_bug() {
|
2021-02-18 11:25:45 +00:00
|
|
|
match (self.err_count(), self.flags.treat_err_as_bug.map(|c| c.get()).unwrap_or(0)) {
|
2020-09-18 05:57:01 +00:00
|
|
|
(1, 1) => panic!("aborting due to `-Z treat-err-as-bug=1`"),
|
|
|
|
(0, _) | (1, _) => {}
|
|
|
|
(count, as_bug) => panic!(
|
2019-12-22 22:42:04 +00:00
|
|
|
"aborting after {} errors due to `-Z treat-err-as-bug={}`",
|
|
|
|
count, as_bug,
|
|
|
|
),
|
2020-09-18 05:57:01 +00:00
|
|
|
}
|
2019-09-07 16:09:52 +00:00
|
|
|
}
|
2019-04-14 22:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 01:11:27 +00:00
|
|
|
|
2021-05-05 20:52:58 +00:00
|
|
|
struct DelayedDiagnostic {
|
|
|
|
inner: Diagnostic,
|
|
|
|
note: Backtrace,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DelayedDiagnostic {
|
|
|
|
fn with_backtrace(diagnostic: Diagnostic, backtrace: Backtrace) -> Self {
|
|
|
|
DelayedDiagnostic { inner: diagnostic, note: backtrace }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decorate(mut self) -> Diagnostic {
|
|
|
|
self.inner.note(&format!("delayed at {}", self.note));
|
|
|
|
self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Copy, PartialEq, Clone, Hash, Debug, Encodable, Decodable)]
|
2015-12-15 01:11:27 +00:00
|
|
|
pub enum Level {
|
|
|
|
Bug,
|
|
|
|
Fatal,
|
|
|
|
Error,
|
|
|
|
Warning,
|
|
|
|
Note,
|
|
|
|
Help,
|
2015-12-23 06:27:20 +00:00
|
|
|
Cancelled,
|
2018-02-28 15:17:44 +00:00
|
|
|
FailureNote,
|
2020-08-13 19:41:52 +00:00
|
|
|
Allow,
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Level {
|
2019-02-06 18:53:01 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-12-31 05:47:14 +00:00
|
|
|
self.to_str().fmt(f)
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Level {
|
2018-02-27 18:33:02 +00:00
|
|
|
fn color(self) -> ColorSpec {
|
|
|
|
let mut spec = ColorSpec::new();
|
2015-12-15 01:11:27 +00:00
|
|
|
match self {
|
2019-08-14 19:22:46 +00:00
|
|
|
Bug | Fatal | Error => {
|
2019-12-22 22:42:04 +00:00
|
|
|
spec.set_fg(Some(Color::Red)).set_intense(true);
|
2018-02-27 18:33:02 +00:00
|
|
|
}
|
2016-08-31 22:19:43 +00:00
|
|
|
Warning => {
|
2019-12-22 22:42:04 +00:00
|
|
|
spec.set_fg(Some(Color::Yellow)).set_intense(cfg!(windows));
|
2018-02-27 18:33:02 +00:00
|
|
|
}
|
|
|
|
Note => {
|
2019-12-22 22:42:04 +00:00
|
|
|
spec.set_fg(Some(Color::Green)).set_intense(true);
|
2018-02-27 18:33:02 +00:00
|
|
|
}
|
|
|
|
Help => {
|
2019-12-22 22:42:04 +00:00
|
|
|
spec.set_fg(Some(Color::Cyan)).set_intense(true);
|
2016-10-18 17:43:02 +00:00
|
|
|
}
|
2018-02-28 15:17:44 +00:00
|
|
|
FailureNote => {}
|
2020-08-13 19:41:52 +00:00
|
|
|
Allow | Cancelled => unreachable!(),
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2018-02-28 15:17:44 +00:00
|
|
|
spec
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2015-12-31 05:47:14 +00:00
|
|
|
|
2016-06-21 22:08:13 +00:00
|
|
|
pub fn to_str(self) -> &'static str {
|
2015-12-31 05:47:14 +00:00
|
|
|
match self {
|
|
|
|
Bug => "error: internal compiler error",
|
2019-08-14 19:22:46 +00:00
|
|
|
Fatal | Error => "error",
|
2015-12-31 05:47:14 +00:00
|
|
|
Warning => "warning",
|
|
|
|
Note => "note",
|
|
|
|
Help => "help",
|
2019-09-13 13:48:47 +00:00
|
|
|
FailureNote => "failure-note",
|
2015-12-31 05:47:14 +00:00
|
|
|
Cancelled => panic!("Shouldn't call on cancelled error"),
|
2020-08-13 19:41:52 +00:00
|
|
|
Allow => panic!("Shouldn't call on allowed error"),
|
2015-12-31 05:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 15:17:44 +00:00
|
|
|
|
|
|
|
pub fn is_failure_note(&self) -> bool {
|
2020-10-27 01:02:48 +00:00
|
|
|
matches!(*self, FailureNote)
|
2018-02-28 15:17:44 +00:00
|
|
|
}
|
2015-12-15 01:11:27 +00:00
|
|
|
}
|
2019-09-08 09:27:03 +00:00
|
|
|
|
2020-08-13 19:41:52 +00:00
|
|
|
pub fn add_elided_lifetime_in_path_suggestion(
|
|
|
|
source_map: &SourceMap,
|
|
|
|
db: &mut DiagnosticBuilder<'_>,
|
|
|
|
n: usize,
|
|
|
|
path_span: Span,
|
|
|
|
incl_angl_brckt: bool,
|
|
|
|
insertion_span: Span,
|
|
|
|
anon_lts: String,
|
|
|
|
) {
|
|
|
|
let (replace_span, suggestion) = if incl_angl_brckt {
|
|
|
|
(insertion_span, anon_lts)
|
|
|
|
} else {
|
|
|
|
// When possible, prefer a suggestion that replaces the whole
|
|
|
|
// `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, `
|
|
|
|
// at a point (which makes for an ugly/confusing label)
|
|
|
|
if let Ok(snippet) = source_map.span_to_snippet(path_span) {
|
|
|
|
// But our spans can get out of whack due to macros; if the place we think
|
|
|
|
// we want to insert `'_` isn't even within the path expression's span, we
|
|
|
|
// should bail out of making any suggestion rather than panicking on a
|
|
|
|
// subtract-with-overflow or string-slice-out-out-bounds (!)
|
|
|
|
// FIXME: can we do better?
|
|
|
|
if insertion_span.lo().0 < path_span.lo().0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let insertion_index = (insertion_span.lo().0 - path_span.lo().0) as usize;
|
|
|
|
if insertion_index > snippet.len() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let (before, after) = snippet.split_at(insertion_index);
|
|
|
|
(path_span, format!("{}{}{}", before, anon_lts, after))
|
|
|
|
} else {
|
|
|
|
(insertion_span, anon_lts)
|
|
|
|
}
|
2019-09-08 17:31:43 +00:00
|
|
|
};
|
2020-08-13 19:41:52 +00:00
|
|
|
db.span_suggestion(
|
|
|
|
replace_span,
|
|
|
|
&format!("indicate the anonymous lifetime{}", pluralize!(n)),
|
|
|
|
suggestion,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2019-09-08 09:27:03 +00:00
|
|
|
}
|
2019-11-24 21:59:56 +00:00
|
|
|
|
|
|
|
// Useful type to use with `Result<>` indicate that an error has already
|
|
|
|
// been reported to the user, so no need to continue checking.
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq)]
|
2019-11-24 21:59:56 +00:00
|
|
|
pub struct ErrorReported;
|
|
|
|
|
|
|
|
rustc_data_structures::impl_stable_hash_via_hash!(ErrorReported);
|