2019-06-10 10:59:03 +02:00
|
|
|
//! Diagnostics creation and emission for `rustc`.
|
2019-06-09 12:04:40 +02:00
|
|
|
//!
|
|
|
|
//! This module contains the code for creating and emitting diagnostics.
|
|
|
|
|
2019-02-05 14:37:15 +01:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2016-06-21 18:08:13 -04:00
|
|
|
|
2019-06-10 10:59:03 +02:00
|
|
|
#![feature(crate_visibility_modifier)]
|
2017-08-23 09:57:05 +09:00
|
|
|
#![cfg_attr(unix, feature(libc))]
|
2019-02-10 16:13:30 +09:00
|
|
|
#![feature(nll)]
|
2018-01-21 12:47:58 +01:00
|
|
|
#![feature(optin_builtin_traits)]
|
2016-06-21 18:08:13 -04:00
|
|
|
|
|
|
|
pub use emitter::ColorConfig;
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2019-02-07 03:53:01 +09:00
|
|
|
use Level::*;
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
use emitter::{Emitter, EmitterWriter};
|
2019-04-17 13:26:38 -04:00
|
|
|
use registry::Registry;
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
use rustc_data_structures::sync::{self, Lrc, Lock};
|
2019-09-23 04:45:21 +02:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
|
2017-10-25 15:01:06 +02:00
|
|
|
use rustc_data_structures::stable_hasher::StableHasher;
|
|
|
|
|
2017-06-11 13:31:40 +02:00
|
|
|
use std::borrow::Cow;
|
2018-03-03 06:20:26 +01:00
|
|
|
use std::cell::Cell;
|
2017-08-12 15:37:28 -07:00
|
|
|
use std::{error, fmt};
|
2018-01-21 12:47:58 +01:00
|
|
|
use std::panic;
|
2019-05-02 05:06:33 +03:00
|
|
|
use std::path::Path;
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2018-02-27 10:33:02 -08:00
|
|
|
use termcolor::{ColorSpec, Color};
|
|
|
|
|
2017-08-19 03:09:55 +03:00
|
|
|
mod diagnostic;
|
|
|
|
mod diagnostic_builder;
|
2015-12-15 14:11:27 +13:00
|
|
|
pub mod emitter;
|
2019-06-05 21:13:56 +02:00
|
|
|
pub mod annotate_snippet_emitter_writer;
|
2017-07-02 13:46:38 -07:00
|
|
|
mod snippet;
|
2016-06-21 18:08:13 -04:00
|
|
|
pub mod registry;
|
2017-07-02 13:46:38 -07:00
|
|
|
mod styled_buffer;
|
2016-08-25 13:28:35 -07:00
|
|
|
mod lock;
|
2016-06-21 18:08:13 -04:00
|
|
|
|
2019-10-13 21:48:39 -07:00
|
|
|
use syntax_pos::{
|
|
|
|
BytePos,
|
|
|
|
FileLinesResult,
|
|
|
|
FileName,
|
|
|
|
Loc,
|
|
|
|
MultiSpan,
|
|
|
|
SourceFile,
|
|
|
|
Span,
|
|
|
|
SpanSnippetError,
|
|
|
|
};
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2019-01-25 14:56:27 -05:00
|
|
|
/// Indicates the confidence in the correctness of a suggestion.
|
|
|
|
///
|
|
|
|
/// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion
|
|
|
|
/// to determine whether it should be automatically applied or if the user should be consulted
|
|
|
|
/// before applying the suggestion.
|
2018-04-24 15:42:27 -07:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
2018-04-25 14:51:06 -07:00
|
|
|
pub enum Applicability {
|
2019-01-25 14:56:27 -05:00
|
|
|
/// The suggestion is definitely what the user intended. This suggestion should be
|
|
|
|
/// automatically applied.
|
2018-04-24 15:42:27 -07:00
|
|
|
MachineApplicable,
|
2019-01-25 14:56:27 -05:00
|
|
|
|
|
|
|
/// The suggestion may be what the user intended, but it is uncertain. The suggestion should
|
|
|
|
/// result in valid Rust code if it is applied.
|
2018-04-24 15:42:27 -07:00
|
|
|
MaybeIncorrect,
|
2019-01-25 14:56:27 -05:00
|
|
|
|
|
|
|
/// The suggestion contains placeholders like `(...)` or `{ /* fields */ }`. The suggestion
|
|
|
|
/// cannot be applied automatically because it will not result in valid Rust code. The user
|
|
|
|
/// will need to fill in the placeholders.
|
|
|
|
HasPlaceholders,
|
|
|
|
|
|
|
|
/// The applicability of the suggestion is unknown.
|
|
|
|
Unspecified,
|
2018-04-24 15:42:27 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 02:45:53 -08:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
pub enum SuggestionStyle {
|
|
|
|
/// Hide the suggested code when displaying this suggestion inline.
|
|
|
|
HideCodeInline,
|
2019-02-09 03:39:08 -08:00
|
|
|
/// Always hide the suggested code but display the message.
|
2019-02-08 02:45:53 -08:00
|
|
|
HideCodeAlways,
|
2019-02-09 03:39:08 -08:00
|
|
|
/// Do not display this suggestion in the cli output, it is only meant for tools.
|
|
|
|
CompletelyHidden,
|
2019-02-08 02:45:53 -08: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 13:22:18 -07:00
|
|
|
/// Always show the suggested code independently.
|
|
|
|
ShowAlways,
|
2019-02-08 02:45:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SuggestionStyle {
|
|
|
|
fn hide_inline(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
SuggestionStyle::ShowCode => false,
|
2019-02-09 03:39:08 -08:00
|
|
|
_ => true,
|
2019-02-08 02:45:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 15:01:06 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
2015-12-13 13:12:47 +01:00
|
|
|
pub struct CodeSuggestion {
|
2017-05-09 10:04:24 +02: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 16:17:33 +01:00
|
|
|
/// Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
|
|
|
|
/// Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
|
2017-05-09 10:04:24 +02:00
|
|
|
/// ]
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// or by replacing the entire span:
|
|
|
|
///
|
|
|
|
/// ```
|
2017-11-03 16:17:33 +01:00
|
|
|
/// vec![
|
|
|
|
/// Substitution { parts: vec![(0..7, "a.b")] },
|
|
|
|
/// Substitution { parts: vec![(0..7, "x.y")] },
|
|
|
|
/// ]
|
2017-05-09 10:04:24 +02:00
|
|
|
/// ```
|
2017-11-03 16:17:33 +01:00
|
|
|
pub substitutions: Vec<Substitution>,
|
2017-03-24 17:31:41 +01:00
|
|
|
pub msg: String,
|
2019-02-08 02:45:53 -08:00
|
|
|
/// Visual representation of this suggestion.
|
|
|
|
pub style: SuggestionStyle,
|
2018-01-18 17:17:46 +05:30
|
|
|
/// 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 14:51:06 -07:00
|
|
|
pub applicability: Applicability,
|
2016-06-21 18:08:13 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 15:01:06 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
2017-05-11 15:26:22 +02:00
|
|
|
/// See the docs on `CodeSuggestion::substitutions`
|
|
|
|
pub struct Substitution {
|
2017-11-03 16:17:33 +01:00
|
|
|
pub parts: Vec<SubstitutionPart>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct SubstitutionPart {
|
2017-05-11 15:26:22 +02:00
|
|
|
pub span: Span,
|
2017-11-03 16:17:33 +01:00
|
|
|
pub snippet: String,
|
2017-05-11 15:26:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 12:13:35 +02:00
|
|
|
pub type SourceMapperDyn = dyn SourceMapper + sync::Send + sync::Sync;
|
2018-03-03 06:26:02 +01:00
|
|
|
|
2018-08-18 12:13:35 +02:00
|
|
|
pub trait SourceMapper {
|
2016-06-21 18:08:13 -04:00
|
|
|
fn lookup_char_pos(&self, pos: BytePos) -> Loc;
|
|
|
|
fn span_to_lines(&self, sp: Span) -> FileLinesResult;
|
|
|
|
fn span_to_string(&self, sp: Span) -> String;
|
2019-10-13 21:48:39 -07:00
|
|
|
fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError>;
|
2016-06-21 18:08:13 -04:00
|
|
|
fn span_to_filename(&self, sp: Span) -> FileName;
|
2016-09-19 12:31:56 -07:00
|
|
|
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
|
2017-05-31 23:48:19 -07:00
|
|
|
fn call_span_if_macro(&self, sp: Span) -> Span;
|
2018-10-29 21:26:13 +01:00
|
|
|
fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool;
|
2018-12-04 15:18:03 -05:00
|
|
|
fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize;
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
2015-12-13 13:12:47 +01:00
|
|
|
impl CodeSuggestion {
|
2019-10-13 21:48:39 -07:00
|
|
|
/// Returns the assembled code suggestions, whether they should be shown with an underline
|
|
|
|
/// and whether the substitution only differs in capitalization.
|
|
|
|
pub fn splice_lines(
|
|
|
|
&self,
|
|
|
|
cm: &SourceMapperDyn,
|
|
|
|
) -> Vec<(String, Vec<SubstitutionPart>, bool)> {
|
2019-03-10 12:52:30 +01:00
|
|
|
use syntax_pos::{CharPos, Pos};
|
2015-12-13 13:12:47 +01:00
|
|
|
|
2016-10-18 23:13:02 +05:30
|
|
|
fn push_trailing(buf: &mut String,
|
2019-02-07 03:53:01 +09:00
|
|
|
line_opt: Option<&Cow<'_, str>>,
|
2016-10-18 23:13:02 +05:30
|
|
|
lo: &Loc,
|
|
|
|
hi_opt: Option<&Loc>) {
|
|
|
|
let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
|
2015-12-13 13:12:47 +01:00
|
|
|
if let Some(line) = line_opt {
|
2017-02-25 22:05:30 +09: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 13:12:47 +01:00
|
|
|
}
|
|
|
|
if let None = hi_opt {
|
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 16:17:33 +01:00
|
|
|
assert!(!self.substitutions.is_empty());
|
|
|
|
|
|
|
|
self.substitutions.iter().cloned().map(|mut substitution| {
|
|
|
|
// 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.
|
|
|
|
let lo = substitution.parts.iter().map(|part| part.span.lo()).min().unwrap();
|
|
|
|
let hi = substitution.parts.iter().map(|part| part.span.hi()).min().unwrap();
|
2019-08-11 01:44:55 +03:00
|
|
|
let bounding_span = Span::with_root_ctxt(lo, hi);
|
2017-11-03 16:17:33 +01:00
|
|
|
let lines = cm.span_to_lines(bounding_span).unwrap();
|
|
|
|
assert!(!lines.lines.is_empty());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
let fm = &lines.file;
|
|
|
|
let mut prev_hi = cm.lookup_char_pos(bounding_span.lo());
|
|
|
|
prev_hi.col = CharPos::from_usize(0);
|
|
|
|
|
|
|
|
let mut prev_line = fm.get_line(lines.lines[0].line_index);
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
|
|
|
for part in &substitution.parts {
|
|
|
|
let cur_lo = cm.lookup_char_pos(part.span.lo());
|
2017-05-09 10:04:24 +02:00
|
|
|
if prev_hi.line == cur_lo.line {
|
2017-11-03 16:17:33 +01:00
|
|
|
push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
|
2017-05-09 10:04:24 +02:00
|
|
|
} else {
|
2017-11-03 16:17:33 +01:00
|
|
|
push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
|
2017-05-09 10:04:24 +02:00
|
|
|
// push lines between the previous and current span (if any)
|
|
|
|
for idx in prev_hi.line..(cur_lo.line - 1) {
|
|
|
|
if let Some(line) = fm.get_line(idx) {
|
2017-06-11 13:31:40 +02:00
|
|
|
buf.push_str(line.as_ref());
|
2017-05-09 10:04:24 +02:00
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(cur_line) = fm.get_line(cur_lo.line - 1) {
|
2019-07-25 15:59:38 -07:00
|
|
|
let end = std::cmp::min(cur_line.len(), cur_lo.col.to_usize());
|
|
|
|
buf.push_str(&cur_line[..end]);
|
2015-12-13 13:12:47 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 16:17:33 +01:00
|
|
|
buf.push_str(&part.snippet);
|
|
|
|
prev_hi = cm.lookup_char_pos(part.span.hi());
|
|
|
|
prev_line = fm.get_line(prev_hi.line - 1);
|
2015-12-13 13:12:47 +01:00
|
|
|
}
|
2019-10-13 21:48:39 -07:00
|
|
|
let only_capitalization = buf.clone().to_lowercase()
|
|
|
|
== cm.span_to_snippet(bounding_span).unwrap().to_lowercase();
|
2017-05-10 13:19:29 +02:00
|
|
|
// if the replacement already ends with a newline, don't print the next line
|
|
|
|
if !buf.ends_with('\n') {
|
2017-11-03 16:17:33 +01:00
|
|
|
push_trailing(&mut buf, prev_line.as_ref(), &prev_hi, None);
|
2017-05-10 13:19:29 +02:00
|
|
|
}
|
2017-08-18 12:46:28 +02:00
|
|
|
// remove trailing newlines
|
|
|
|
while buf.ends_with('\n') {
|
|
|
|
buf.pop();
|
|
|
|
}
|
2019-10-13 21:48:39 -07:00
|
|
|
(buf, substitution.parts, only_capitalization)
|
|
|
|
|
2017-11-03 16:17:33 +01:00
|
|
|
}).collect()
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used as a return value to signify a fatal error occurred. (It is also
|
|
|
|
/// used as the argument to panic at the moment, but that will eventually
|
|
|
|
/// not be true.)
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
#[must_use]
|
|
|
|
pub struct FatalError;
|
|
|
|
|
2018-01-21 12:47:58 +01:00
|
|
|
pub struct FatalErrorMarker;
|
|
|
|
|
|
|
|
// Don't implement Send on FatalError. This makes it impossible to panic!(FatalError).
|
|
|
|
// We don't want to invoke the panic handler and print a backtrace for fatal errors.
|
|
|
|
impl !Send for FatalError {}
|
|
|
|
|
|
|
|
impl FatalError {
|
|
|
|
pub fn raise(self) -> ! {
|
|
|
|
panic::resume_unwind(Box::new(FatalErrorMarker))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
impl fmt::Display for FatalError {
|
2019-02-07 03:53:01 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-12-15 14:11:27 +13:00
|
|
|
write!(f, "parser fatal error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for FatalError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"The parser has encountered a fatal error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-07 03:53:01 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-12-15 14:11:27 +13:00
|
|
|
write!(f, "parser internal bug")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for ExplicitBug {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"The parser has encountered an internal bug"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 08:21:22 +02:00
|
|
|
pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
|
2016-10-11 12:26:32 -04:00
|
|
|
pub use diagnostic_builder::DiagnosticBuilder;
|
2015-12-18 16:15:53 +13:00
|
|
|
|
2019-05-02 05:06:33 +03: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 14:11:27 +13:00
|
|
|
pub struct Handler {
|
2019-09-07 12:09:52 -04:00
|
|
|
flags: HandlerFlags,
|
|
|
|
inner: Lock<HandlerInner>,
|
|
|
|
}
|
2017-11-20 18:03:20 +00:00
|
|
|
|
2019-09-23 22:28:14 +02: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 12:09:52 -04:00
|
|
|
struct HandlerInner {
|
|
|
|
flags: HandlerFlags,
|
2019-06-22 12:46:48 +01: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 12:09:52 -04:00
|
|
|
err_count: usize,
|
|
|
|
deduplicated_err_count: usize,
|
|
|
|
emitter: Box<dyn Emitter + sync::Send>,
|
|
|
|
continue_after_error: bool,
|
|
|
|
delayed_span_bugs: Vec<Diagnostic>,
|
2017-10-25 15:01:06 +02:00
|
|
|
|
2019-04-17 12:03:39 +03:00
|
|
|
/// This set contains the `DiagnosticId` of all emitted diagnostics to avoid
|
|
|
|
/// emitting the same diagnostic with extended help (`--teach`) twice, which
|
|
|
|
/// would be uneccessary repetition.
|
2019-09-07 12:09:52 -04:00
|
|
|
taught_diagnostics: FxHashSet<DiagnosticId>,
|
2018-03-03 06:20:26 +01:00
|
|
|
|
|
|
|
/// Used to suggest rustc --explain <error code>
|
2019-09-07 12:09:52 -04:00
|
|
|
emitted_diagnostic_codes: FxHashSet<DiagnosticId>,
|
2017-10-25 15:01:06 +02:00
|
|
|
|
2019-04-17 12:03:39 +03: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 12:09:52 -04:00
|
|
|
emitted_diagnostics: FxHashSet<u128>,
|
2019-09-23 04:45:21 +02: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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
2018-03-15 10:03:36 +01:00
|
|
|
fn default_track_diagnostic(_: &Diagnostic) {}
|
|
|
|
|
|
|
|
thread_local!(pub static TRACK_DIAGNOSTICS: Cell<fn(&Diagnostic)> =
|
|
|
|
Cell::new(default_track_diagnostic));
|
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
#[derive(Copy, Clone, Default)]
|
2017-11-20 18:03:20 +00:00
|
|
|
pub struct HandlerFlags {
|
2018-09-15 06:27:55 +02: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 06:27:55 +02:00
|
|
|
/// If true, error-level diagnostics are upgraded to bug-level.
|
|
|
|
/// (rustc: see `-Z treat-err-as-bug`)
|
2019-03-06 19:49:39 -08:00
|
|
|
pub treat_err_as_bug: Option<usize>,
|
2018-09-15 06:27:55 +02: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 17:53:44 +02:00
|
|
|
pub report_delayed_bugs: bool,
|
2018-09-15 06:27:55 +02:00
|
|
|
/// show macro backtraces even for non-local macros.
|
|
|
|
/// (rustc: see `-Z external-macro-backtrace`)
|
2017-11-20 18:03:20 +00:00
|
|
|
pub external_macro_backtrace: bool,
|
|
|
|
}
|
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
impl Drop for HandlerInner {
|
2018-07-19 17:53:44 +02:00
|
|
|
fn drop(&mut self) {
|
2019-09-23 04:45:21 +02:00
|
|
|
self.emit_stashed_diagnostics();
|
|
|
|
|
|
|
|
if !self.has_errors() {
|
2019-09-07 12:09:52 -04:00
|
|
|
let bugs = std::mem::replace(&mut self.delayed_span_bugs, Vec::new());
|
2018-07-19 17:53:44 +02:00
|
|
|
let has_bugs = !bugs.is_empty();
|
2019-09-07 12:09:52 -04:00
|
|
|
for bug in bugs {
|
|
|
|
self.emit_diagnostic(&bug);
|
2018-07-19 17:53:44 +02:00
|
|
|
}
|
|
|
|
if has_bugs {
|
|
|
|
panic!("no errors encountered even though `delay_span_bug` issued");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
impl Handler {
|
2019-09-23 22:28:14 +02:00
|
|
|
pub fn with_tty_emitter(
|
|
|
|
color_config: ColorConfig,
|
|
|
|
can_emit_warnings: bool,
|
|
|
|
treat_err_as_bug: Option<usize>,
|
|
|
|
cm: Option<Lrc<SourceMapperDyn>>,
|
|
|
|
) -> Self {
|
|
|
|
Self::with_tty_emitter_and_flags(
|
2017-11-20 18:03:20 +00:00
|
|
|
color_config,
|
|
|
|
cm,
|
|
|
|
HandlerFlags {
|
|
|
|
can_emit_warnings,
|
|
|
|
treat_err_as_bug,
|
|
|
|
.. Default::default()
|
2019-09-23 22:28:14 +02:00
|
|
|
},
|
|
|
|
)
|
2017-11-20 18:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:28:14 +02:00
|
|
|
pub fn with_tty_emitter_and_flags(
|
|
|
|
color_config: ColorConfig,
|
|
|
|
cm: Option<Lrc<SourceMapperDyn>>,
|
|
|
|
flags: HandlerFlags,
|
|
|
|
) -> Self {
|
2019-09-07 09:57:11 -04:00
|
|
|
let emitter = Box::new(EmitterWriter::stderr(
|
2019-09-23 22:28:14 +02:00
|
|
|
color_config,
|
|
|
|
cm,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
flags.external_macro_backtrace,
|
|
|
|
));
|
|
|
|
Self::with_emitter_and_flags(emitter, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_emitter(
|
|
|
|
can_emit_warnings: bool,
|
|
|
|
treat_err_as_bug: Option<usize>,
|
|
|
|
emitter: Box<dyn Emitter + sync::Send>,
|
|
|
|
) -> Self {
|
2017-11-20 18:03:20 +00:00
|
|
|
Handler::with_emitter_and_flags(
|
2019-09-23 22:28:14 +02:00
|
|
|
emitter,
|
2017-11-20 18:03:20 +00:00
|
|
|
HandlerFlags {
|
|
|
|
can_emit_warnings,
|
|
|
|
treat_err_as_bug,
|
|
|
|
.. Default::default()
|
2019-09-23 22:28:14 +02:00
|
|
|
},
|
|
|
|
)
|
2017-11-20 18:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:28:14 +02:00
|
|
|
pub fn with_emitter_and_flags(
|
|
|
|
emitter: Box<dyn Emitter + sync::Send>,
|
|
|
|
flags: HandlerFlags
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2017-11-20 18:03:20 +00:00
|
|
|
flags,
|
2019-09-07 12:09:52 -04:00
|
|
|
inner: Lock::new(HandlerInner {
|
|
|
|
flags,
|
|
|
|
err_count: 0,
|
|
|
|
deduplicated_err_count: 0,
|
2019-09-23 22:28:14 +02:00
|
|
|
emitter,
|
2019-09-07 12:09:52 -04:00
|
|
|
continue_after_error: true,
|
|
|
|
delayed_span_bugs: Vec::new(),
|
|
|
|
taught_diagnostics: Default::default(),
|
|
|
|
emitted_diagnostic_codes: Default::default(),
|
|
|
|
emitted_diagnostics: Default::default(),
|
2019-09-23 04:45:21 +02:00
|
|
|
stashed_diagnostics: Default::default(),
|
2019-09-07 12:09:52 -04:00
|
|
|
}),
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 18:17:04 +01:00
|
|
|
pub fn set_continue_after_error(&self, continue_after_error: bool) {
|
2019-09-07 12:09:52 -04:00
|
|
|
self.inner.borrow_mut().continue_after_error = continue_after_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is here to not allow mutation of flags;
|
|
|
|
// as of this writing it's only used in tests in librustc.
|
|
|
|
pub fn can_emit_warnings(&self) -> bool {
|
|
|
|
self.flags.can_emit_warnings
|
2016-03-25 18:17:04 +01:00
|
|
|
}
|
|
|
|
|
2018-01-06 13:33:20 +01:00
|
|
|
/// Resets the diagnostic error count as well as the cached emitted diagnostics.
|
|
|
|
///
|
2019-02-08 14:53:55 +01:00
|
|
|
/// NOTE: *do not* call this function from rustc. It is only meant to be called from external
|
2018-01-06 13:33:20 +01: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 09:17:03 +09:00
|
|
|
pub fn reset_err_count(&self) {
|
2019-09-07 12:09:52 -04:00
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
inner.err_count = 0;
|
2019-10-02 04:13:02 +03:00
|
|
|
inner.deduplicated_err_count = 0;
|
|
|
|
|
|
|
|
// actually free the underlying memory (which `clear` would not do)
|
|
|
|
inner.delayed_span_bugs = Default::default();
|
|
|
|
inner.taught_diagnostics = Default::default();
|
|
|
|
inner.emitted_diagnostic_codes = Default::default();
|
|
|
|
inner.emitted_diagnostics = Default::default();
|
|
|
|
inner.stashed_diagnostics = Default::default();
|
2019-09-23 04:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stash a given diagnostic with the given `Span` and `StashKey` as the key for later stealing.
|
|
|
|
/// If the diagnostic with this `(span, key)` already exists, this will result in an ICE.
|
|
|
|
pub fn stash_diagnostic(&self, span: Span, key: StashKey, diag: Diagnostic) {
|
2019-09-23 19:29:02 +02:00
|
|
|
let mut inner = self.inner.borrow_mut();
|
|
|
|
if let Some(mut old_diag) = inner.stashed_diagnostics.insert((span, key), diag) {
|
2019-09-23 04:45:21 +02:00
|
|
|
// We are removing a previously stashed diagnostic which should not happen.
|
2019-09-23 19:29:02 +02:00
|
|
|
old_diag.level = Bug;
|
|
|
|
old_diag.note(&format!(
|
|
|
|
"{}:{}: already existing stashed diagnostic with (span = {:?}, key = {:?})",
|
|
|
|
file!(), line!(), span, key
|
|
|
|
));
|
2019-09-23 22:28:14 +02:00
|
|
|
inner.emit_diag_at_span(old_diag, span);
|
|
|
|
panic!(ExplicitBug);
|
2019-09-23 04:45:21 +02: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 09:17:03 +09:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:28:14 +02: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 20:27:44 +02:00
|
|
|
pub fn struct_dummy(&self) -> DiagnosticBuilder<'_> {
|
2016-04-21 05:14:58 -04:00
|
|
|
DiagnosticBuilder::new(self, Level::Cancelled, "")
|
2015-12-23 19:27:20 +13:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:28:14 +02: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 16:15:53 +13:00
|
|
|
result
|
|
|
|
}
|
2019-09-23 22:28:14 +02: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 08:21:22 +02:00
|
|
|
result.code(code);
|
2015-12-18 16:15:53 +13:00
|
|
|
result
|
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
|
|
|
/// Construct a builder at the `Warning` level with the `msg`.
|
2019-06-21 20:27:44 +02:00
|
|
|
pub fn struct_warn(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
2016-04-21 05:14:58 -04: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 16:15:53 +13:00
|
|
|
result.cancel();
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2019-09-23 22:28:14 +02: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 19:27:20 +13:00
|
|
|
result
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 08:21:22 +02:00
|
|
|
result.code(code);
|
2015-12-23 19:27:20 +13:00
|
|
|
result
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
|
|
|
/// Construct a builder at the `Error` level with the `msg`.
|
2017-05-29 18:46:29 +02:00
|
|
|
// FIXME: This method should be removed (every error should have an associated error code).
|
2019-06-21 20:27:44 +02:00
|
|
|
pub fn struct_err(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
2016-04-21 05:14:58 -04:00
|
|
|
DiagnosticBuilder::new(self, Level::Error, msg)
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 08:21:22 +02:00
|
|
|
result.code(code);
|
2017-05-29 18:46:29 +02:00
|
|
|
result
|
|
|
|
}
|
2019-09-23 22:28:14 +02: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 19:27:20 +13:00
|
|
|
result
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 08:21:22 +02:00
|
|
|
result.code(code);
|
2015-12-23 19:27:20 +13:00
|
|
|
result
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
|
|
|
/// Construct a builder at the `Error` level with the `msg`.
|
2019-06-21 20:27:44 +02:00
|
|
|
pub fn struct_fatal(&self, msg: &str) -> DiagnosticBuilder<'_> {
|
2016-04-21 05:14:58 -04:00
|
|
|
DiagnosticBuilder::new(self, Level::Fatal, msg)
|
2015-12-23 19:27:20 +13:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:28:14 +02:00
|
|
|
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: &str) -> FatalError {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
|
2017-05-19 01:20:48 +02:00
|
|
|
FatalError
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
|
|
|
pub fn span_fatal_with_code(
|
|
|
|
&self,
|
|
|
|
span: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
|
|
|
) -> FatalError {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new_with_code(Fatal, Some(code), msg), span);
|
2017-05-19 01:20:48 +02:00
|
|
|
FatalError
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
|
|
|
pub fn span_err(&self, span: impl Into<MultiSpan>, msg: &str) {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Error, msg), span);
|
2016-05-27 19:05:22 -07:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
|
|
|
pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: &str) -> ! {
|
|
|
|
self.inner.borrow_mut().span_bug(span, msg)
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02: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 16:15:53 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
|
|
|
pub fn span_note_diag(&self, span: Span, msg: &str) -> DiagnosticBuilder<'_> {
|
2017-04-24 16:27:07 -07:00
|
|
|
let mut db = DiagnosticBuilder::new(self, Note, msg);
|
2019-09-23 22:28:14 +02:00
|
|
|
db.set_span(span);
|
2017-05-05 21:49:59 -07:00
|
|
|
db
|
2017-04-24 16:27:07 -07:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
2018-02-28 16:17:44 +01:00
|
|
|
pub fn failure(&self, msg: &str) {
|
2019-09-07 12:09:52 -04:00
|
|
|
self.inner.borrow_mut().failure(msg);
|
2018-02-28 16:17:44 +01:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
pub fn fatal(&self, msg: &str) -> FatalError {
|
2019-09-07 12:09:52 -04:00
|
|
|
self.inner.borrow_mut().fatal(msg)
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
pub fn err(&self, msg: &str) {
|
2019-09-07 12:09:52 -04:00
|
|
|
self.inner.borrow_mut().err(msg);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
pub fn warn(&self, msg: &str) {
|
2016-10-18 23:13:02 +05:30
|
|
|
let mut db = DiagnosticBuilder::new(self, Warning, msg);
|
2016-07-06 12:08:16 -04:00
|
|
|
db.emit();
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
2015-12-18 16:15:53 +13:00
|
|
|
pub fn note_without_error(&self, msg: &str) {
|
2019-09-23 22:28:14 +02:00
|
|
|
DiagnosticBuilder::new(self, Note, msg).emit();
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
pub fn bug(&self, msg: &str) -> ! {
|
2019-09-07 12:09:52 -04:00
|
|
|
self.inner.borrow_mut().bug(msg)
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn err_count(&self) -> usize {
|
2019-09-23 04:45:21 +02:00
|
|
|
self.inner.borrow().err_count()
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_errors(&self) -> bool {
|
2019-09-23 04:45:21 +02:00
|
|
|
self.inner.borrow().has_errors()
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2018-03-15 10:09:20 +01:00
|
|
|
|
2019-04-17 13:26:38 -04:00
|
|
|
pub fn print_error_count(&self, registry: &Registry) {
|
2019-09-07 12:09:52 -04:00
|
|
|
self.inner.borrow_mut().print_error_count(registry)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn abort_if_errors(&self) {
|
2019-09-23 04:45:21 +02:00
|
|
|
self.inner.borrow_mut().abort_if_errors()
|
2019-09-07 12:09:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn abort_if_errors_and_should_abort(&self) {
|
2019-09-23 04:45:21 +02:00
|
|
|
self.inner.borrow_mut().abort_if_errors_and_should_abort()
|
2019-09-07 12:09:52 -04:00
|
|
|
}
|
|
|
|
|
2019-09-23 19:29:02 +02: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 12:09:52 -04: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 22:28:14 +02: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));
|
|
|
|
inner.abort_if_errors_and_should_abort();
|
|
|
|
}
|
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
pub fn emit_artifact_notification(&self, path: &Path, artifact_type: &str) {
|
|
|
|
self.inner.borrow_mut().emit_artifact_notification(path, artifact_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 04:45:21 +02: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 12:09:52 -04:00
|
|
|
fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) {
|
|
|
|
if diagnostic.cancelled() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if diagnostic.level == Warning && !self.flags.can_emit_warnings {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACK_DIAGNOSTICS.with(|track_diagnostics| {
|
|
|
|
track_diagnostics.get()(diagnostic);
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(ref code) = diagnostic.code {
|
|
|
|
self.emitted_diagnostic_codes.insert(code.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let diagnostic_hash = {
|
|
|
|
use std::hash::Hash;
|
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
diagnostic.hash(&mut hasher);
|
|
|
|
hasher.finish()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only emit the diagnostic if we haven't already emitted an equivalent
|
|
|
|
// one:
|
|
|
|
if self.emitted_diagnostics.insert(diagnostic_hash) {
|
|
|
|
self.emitter.emit_diagnostic(diagnostic);
|
|
|
|
if diagnostic.is_error() {
|
|
|
|
self.deduplicated_err_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if diagnostic.is_error() {
|
|
|
|
self.bump_err_count();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {
|
|
|
|
self.emitter.emit_artifact_notification(path, artifact_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn treat_err_as_bug(&self) -> bool {
|
2019-09-23 04:45:21 +02:00
|
|
|
self.flags.treat_err_as_bug.map(|c| self.err_count() >= c).unwrap_or(false)
|
2019-09-07 12:09:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_error_count(&mut self, registry: &Registry) {
|
2019-09-23 04:45:21 +02:00
|
|
|
self.emit_stashed_diagnostics();
|
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
let s = match self.deduplicated_err_count {
|
2018-03-15 10:09:20 +01:00
|
|
|
0 => return,
|
|
|
|
1 => "aborting due to previous error".to_string(),
|
2019-06-22 12:46:48 +01:00
|
|
|
count => format!("aborting due to {} previous errors", count)
|
2018-03-15 10:09:20 +01:00
|
|
|
};
|
2019-03-07 08:09:41 -08:00
|
|
|
if self.treat_err_as_bug() {
|
2019-03-07 01:54:53 -08:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 10:09:20 +01:00
|
|
|
|
2019-03-07 01:54:53 -08:00
|
|
|
let _ = self.fatal(&s);
|
2018-02-28 16:17:44 +01:00
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
let can_show_explain = self.emitter.should_show_explain();
|
|
|
|
let are_there_diagnostics = !self.emitted_diagnostic_codes.is_empty();
|
2018-02-28 16:17:44 +01:00
|
|
|
if can_show_explain && are_there_diagnostics {
|
2019-04-17 13:26:38 -04:00
|
|
|
let mut error_codes = self
|
|
|
|
.emitted_diagnostic_codes
|
|
|
|
.iter()
|
|
|
|
.filter_map(|x| match &x {
|
|
|
|
DiagnosticId::Error(s) if registry.find_description(s).is_some() => {
|
|
|
|
Some(s.clone())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2018-02-28 16:17:44 +01: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-04-17 13:26:38 -04:00
|
|
|
self.failure(&format!("Some errors have detailed explanations: {}{}",
|
2018-02-28 16:17:44 +01:00
|
|
|
error_codes[..limit].join(", "),
|
|
|
|
if error_codes.len() > 9 { "..." } else { "." }));
|
|
|
|
self.failure(&format!("For more information about an error, try \
|
|
|
|
`rustc --explain {}`.",
|
|
|
|
&error_codes[0]));
|
|
|
|
} else {
|
|
|
|
self.failure(&format!("For more information about this error, try \
|
|
|
|
`rustc --explain {}`.",
|
|
|
|
&error_codes[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 10:09:20 +01:00
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2019-09-23 04:45:21 +02:00
|
|
|
fn err_count(&self) -> usize {
|
|
|
|
self.err_count + self.stashed_diagnostics.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_errors(&self) -> bool {
|
|
|
|
self.err_count() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn abort_if_errors_and_should_abort(&mut self) {
|
|
|
|
self.emit_stashed_diagnostics();
|
|
|
|
|
|
|
|
if self.has_errors() && !self.continue_after_error {
|
2019-06-22 12:44:03 +01:00
|
|
|
FatalError.raise();
|
2018-03-15 10:09:20 +01:00
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-07 11:21:17 -04:00
|
|
|
|
2019-09-23 04:45:21 +02:00
|
|
|
fn abort_if_errors(&mut self) {
|
|
|
|
self.emit_stashed_diagnostics();
|
|
|
|
|
|
|
|
if self.has_errors() {
|
2019-09-07 11:21:17 -04:00
|
|
|
FatalError.raise();
|
2016-10-18 23:13:02 +05:30
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2017-08-12 15:37:28 -07:00
|
|
|
|
2019-09-23 22:28:14 +02:00
|
|
|
fn span_bug(&mut self, sp: impl Into<MultiSpan>, msg: &str) -> ! {
|
|
|
|
self.emit_diag_at_span(Diagnostic::new(Bug, msg), sp);
|
|
|
|
panic!(ExplicitBug);
|
2019-09-23 19:29:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:28:14 +02:00
|
|
|
fn emit_diag_at_span(&mut self, mut diag: Diagnostic, sp: impl Into<MultiSpan>) {
|
|
|
|
self.emit_diagnostic(diag.set_span(sp));
|
2019-09-07 12:09:52 -04:00
|
|
|
self.abort_if_errors_and_should_abort();
|
2018-01-21 21:19:37 -08:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:28:14 +02:00
|
|
|
fn delay_span_bug(&mut self, sp: impl Into<MultiSpan>, msg: &str) {
|
2019-09-07 12:09:52 -04:00
|
|
|
if self.treat_err_as_bug() {
|
|
|
|
// 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());
|
|
|
|
self.delay_as_bug(diagnostic)
|
2018-03-20 23:41:25 +01:00
|
|
|
}
|
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
fn failure(&mut self, msg: &str) {
|
|
|
|
self.emit_diagnostic(&Diagnostic::new(FailureNote, msg));
|
|
|
|
}
|
2017-10-25 15:01:06 +02:00
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
fn fatal(&mut self, msg: &str) -> FatalError {
|
2019-09-23 22:28:14 +02:00
|
|
|
self.emit_error(Fatal, msg);
|
2019-09-07 12:09:52 -04:00
|
|
|
FatalError
|
|
|
|
}
|
2017-10-25 15:01:06 +02:00
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
fn err(&mut self, msg: &str) {
|
2019-09-23 22:28:14 +02:00
|
|
|
self.emit_error(Error, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit an error; level should be `Error` or `Fatal`.
|
|
|
|
fn emit_error(&mut self, level: Level, msg: &str,) {
|
2019-09-07 12:09:52 -04:00
|
|
|
if self.treat_err_as_bug() {
|
|
|
|
self.bug(msg);
|
2018-01-21 21:19:37 -08:00
|
|
|
}
|
2019-09-23 22:28:14 +02:00
|
|
|
self.emit_diagnostic(&Diagnostic::new(level, msg));
|
2019-09-07 12:09:52 -04:00
|
|
|
}
|
2018-01-21 21:19:37 -08:00
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
fn bug(&mut self, msg: &str) -> ! {
|
|
|
|
self.emit_diagnostic(&Diagnostic::new(Bug, msg));
|
|
|
|
panic!(ExplicitBug);
|
|
|
|
}
|
2017-10-25 15:01:06 +02:00
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
fn delay_as_bug(&mut self, diagnostic: Diagnostic) {
|
|
|
|
if self.flags.report_delayed_bugs {
|
|
|
|
self.emit_diagnostic(&diagnostic);
|
2019-06-22 12:46:48 +01:00
|
|
|
}
|
2019-09-07 12:09:52 -04:00
|
|
|
self.delayed_span_bugs.push(diagnostic);
|
2017-08-12 15:37:28 -07:00
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2019-09-07 12:09:52 -04:00
|
|
|
fn bump_err_count(&mut self) {
|
|
|
|
self.err_count += 1;
|
|
|
|
self.panic_if_treat_err_as_bug();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn panic_if_treat_err_as_bug(&self) {
|
|
|
|
if self.treat_err_as_bug() {
|
2019-09-23 04:45:21 +02:00
|
|
|
let s = match (self.err_count(), self.flags.treat_err_as_bug.unwrap_or(0)) {
|
2019-09-07 12:09:52 -04:00
|
|
|
(0, _) => return,
|
|
|
|
(1, 1) => "aborting due to `-Z treat-err-as-bug=1`".to_string(),
|
|
|
|
(1, _) => return,
|
|
|
|
(count, as_bug) => {
|
|
|
|
format!(
|
|
|
|
"aborting after {} errors due to `-Z treat-err-as-bug={}`",
|
|
|
|
count,
|
|
|
|
as_bug,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
panic!(s);
|
|
|
|
}
|
2019-04-15 08:26:08 +10:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2017-10-25 15:01:06 +02:00
|
|
|
#[derive(Copy, PartialEq, Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
|
2015-12-15 14:11:27 +13:00
|
|
|
pub enum Level {
|
|
|
|
Bug,
|
|
|
|
Fatal,
|
|
|
|
Error,
|
|
|
|
Warning,
|
|
|
|
Note,
|
|
|
|
Help,
|
2015-12-23 19:27:20 +13:00
|
|
|
Cancelled,
|
2018-02-28 16:17:44 +01:00
|
|
|
FailureNote,
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Level {
|
2019-02-07 03:53:01 +09:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-12-31 18:47:14 +13:00
|
|
|
self.to_str().fmt(f)
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Level {
|
2018-02-27 10:33:02 -08:00
|
|
|
fn color(self) -> ColorSpec {
|
|
|
|
let mut spec = ColorSpec::new();
|
2015-12-15 14:11:27 +13:00
|
|
|
match self {
|
2019-08-14 22:22:46 +03:00
|
|
|
Bug | Fatal | Error => {
|
2018-02-27 10:33:02 -08:00
|
|
|
spec.set_fg(Some(Color::Red))
|
|
|
|
.set_intense(true);
|
|
|
|
}
|
2016-08-31 15:19:43 -07:00
|
|
|
Warning => {
|
2018-02-27 10:33:02 -08:00
|
|
|
spec.set_fg(Some(Color::Yellow))
|
|
|
|
.set_intense(cfg!(windows));
|
|
|
|
}
|
|
|
|
Note => {
|
|
|
|
spec.set_fg(Some(Color::Green))
|
|
|
|
.set_intense(true);
|
|
|
|
}
|
|
|
|
Help => {
|
|
|
|
spec.set_fg(Some(Color::Cyan))
|
|
|
|
.set_intense(true);
|
2016-10-18 23:13:02 +05:30
|
|
|
}
|
2018-02-28 16:17:44 +01:00
|
|
|
FailureNote => {}
|
2015-12-23 19:27:20 +13:00
|
|
|
Cancelled => unreachable!(),
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2018-02-28 16:17:44 +01:00
|
|
|
spec
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2015-12-31 18:47:14 +13:00
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
pub fn to_str(self) -> &'static str {
|
2015-12-31 18:47:14 +13:00
|
|
|
match self {
|
|
|
|
Bug => "error: internal compiler error",
|
2019-08-14 22:22:46 +03:00
|
|
|
Fatal | Error => "error",
|
2015-12-31 18:47:14 +13:00
|
|
|
Warning => "warning",
|
|
|
|
Note => "note",
|
|
|
|
Help => "help",
|
2019-09-13 06:48:47 -07:00
|
|
|
FailureNote => "failure-note",
|
2015-12-31 18:47:14 +13:00
|
|
|
Cancelled => panic!("Shouldn't call on cancelled error"),
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 16:17:44 +01:00
|
|
|
|
|
|
|
pub fn is_failure_note(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
FailureNote => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2019-09-08 14:57:03 +05:30
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pluralise {
|
2019-09-08 23:01:43 +05:30
|
|
|
($x:expr) => {
|
|
|
|
if $x != 1 { "s" } else { "" }
|
|
|
|
};
|
2019-09-08 14:57:03 +05:30
|
|
|
}
|