2015-12-15 14:11:27 +13:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/")]
|
|
|
|
|
|
|
|
#![feature(custom_attribute)]
|
|
|
|
#![allow(unused_attributes)]
|
|
|
|
#![feature(range_contains)]
|
2017-08-23 09:57:05 +09:00
|
|
|
#![cfg_attr(unix, feature(libc))]
|
2018-08-09 04:08:45 -04:00
|
|
|
#![cfg_attr(not(stage0), feature(nll))]
|
2018-01-21 12:47:58 +01:00
|
|
|
#![feature(optin_builtin_traits)]
|
2016-06-21 18:08:13 -04:00
|
|
|
|
2018-02-27 10:33:02 -08:00
|
|
|
extern crate atty;
|
|
|
|
extern crate termcolor;
|
2017-08-23 09:57:05 +09:00
|
|
|
#[cfg(unix)]
|
2016-10-18 23:13:02 +05:30
|
|
|
extern crate libc;
|
2018-09-06 13:07:14 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2017-10-25 15:01:06 +02:00
|
|
|
extern crate rustc_data_structures;
|
2017-01-28 18:13:21 -05:00
|
|
|
extern crate serialize as rustc_serialize;
|
2016-06-21 18:08:13 -04:00
|
|
|
extern crate syntax_pos;
|
2017-12-10 23:35:53 -08:00
|
|
|
extern crate unicode_width;
|
2016-06-21 18:08:13 -04:00
|
|
|
|
|
|
|
pub use emitter::ColorConfig;
|
2015-12-15 14:11:27 +13:00
|
|
|
|
|
|
|
use self::Level::*;
|
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
use emitter::{Emitter, EmitterWriter};
|
2015-12-15 14:11:27 +13:00
|
|
|
|
2018-03-03 06:20:26 +01:00
|
|
|
use rustc_data_structures::sync::{self, Lrc, Lock, LockCell};
|
2017-10-25 15:01:06 +02:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
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};
|
2017-12-03 14:04:51 +01:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering::SeqCst;
|
2018-01-21 12:47:58 +01:00
|
|
|
use std::panic;
|
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;
|
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
|
|
|
|
2018-08-18 12:14:31 +02:00
|
|
|
use syntax_pos::{BytePos,
|
|
|
|
Loc,
|
|
|
|
FileLinesResult,
|
|
|
|
SourceFile,
|
|
|
|
FileName,
|
|
|
|
MultiSpan,
|
|
|
|
Span,
|
|
|
|
NO_EXPANSION};
|
2015-12-15 14:11:27 +13:00
|
|
|
|
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 {
|
2018-04-24 15:42:27 -07:00
|
|
|
MachineApplicable,
|
|
|
|
HasPlaceholders,
|
|
|
|
MaybeIncorrect,
|
|
|
|
Unspecified
|
|
|
|
}
|
|
|
|
|
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,
|
2017-07-16 11:43:24 -07:00
|
|
|
pub show_code_when_inline: bool,
|
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;
|
|
|
|
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-08-18 12:13:56 +02:00
|
|
|
fn ensure_source_file_source_present(&self, file_map: Lrc<SourceFile>) -> bool;
|
2018-01-08 20:17:23 +05:30
|
|
|
fn doctest_offset_line(&self, line: usize) -> usize;
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
2015-12-13 13:12:47 +01:00
|
|
|
impl CodeSuggestion {
|
2017-11-03 16:17:33 +01:00
|
|
|
/// Returns the assembled code suggestions and whether they should be shown with an underline.
|
2018-08-18 12:13:35 +02:00
|
|
|
pub fn splice_lines(&self, cm: &SourceMapperDyn)
|
2018-03-03 06:26:02 +01:00
|
|
|
-> Vec<(String, Vec<SubstitutionPart>)> {
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::{CharPos, Loc, Pos};
|
2015-12-13 13:12:47 +01:00
|
|
|
|
2016-10-18 23:13:02 +05:30
|
|
|
fn push_trailing(buf: &mut String,
|
2017-06-11 13:31:40 +02: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));
|
2015-12-13 13:12:47 +01:00
|
|
|
buf.push_str(match hi_opt {
|
|
|
|
Some(hi) => &line[lo..hi],
|
|
|
|
None => &line[lo..],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
let bounding_span = Span::new(lo, hi, NO_EXPANSION);
|
|
|
|
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) {
|
|
|
|
buf.push_str(&cur_line[..cur_lo.col.to_usize()]);
|
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
|
|
|
}
|
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();
|
|
|
|
}
|
2017-11-03 16:17:33 +01:00
|
|
|
(buf, substitution.parts)
|
|
|
|
}).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 {
|
2018-05-09 01:41:44 +02: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 {
|
2018-05-09 01:41:44 +02: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
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
/// A handler deals with errors; certain errors
|
|
|
|
/// (fatal, bug, unimpl) may cause immediate exit,
|
|
|
|
/// others log errors for later reporting.
|
|
|
|
pub struct Handler {
|
2017-11-20 18:03:20 +00:00
|
|
|
pub flags: HandlerFlags,
|
|
|
|
|
2017-12-03 14:04:51 +01:00
|
|
|
err_count: AtomicUsize,
|
2018-07-12 09:33:33 +02:00
|
|
|
emitter: Lock<Box<dyn Emitter + sync::Send>>,
|
2018-03-03 06:20:26 +01:00
|
|
|
continue_after_error: LockCell<bool>,
|
2018-07-21 16:09:10 +02:00
|
|
|
delayed_span_bugs: Lock<Vec<Diagnostic>>,
|
2017-10-25 15:01:06 +02:00
|
|
|
|
2018-01-25 11:40:33 -08: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.
|
2018-03-03 06:20:26 +01:00
|
|
|
taught_diagnostics: Lock<FxHashSet<DiagnosticId>>,
|
|
|
|
|
|
|
|
/// Used to suggest rustc --explain <error code>
|
|
|
|
emitted_diagnostic_codes: Lock<FxHashSet<DiagnosticId>>,
|
2017-10-25 15:01:06 +02: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.
|
2018-03-03 06:20:26 +01:00
|
|
|
emitted_diagnostics: Lock<FxHashSet<u128>>,
|
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));
|
|
|
|
|
2017-11-20 18:03:20 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
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`)
|
2017-11-20 18:03:20 +00:00
|
|
|
pub treat_err_as_bug: bool,
|
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,
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:53:44 +02:00
|
|
|
impl Drop for Handler {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.err_count() == 0 {
|
2018-07-21 16:09:10 +02:00
|
|
|
let mut bugs = self.delayed_span_bugs.borrow_mut();
|
2018-07-19 17:53:44 +02:00
|
|
|
let has_bugs = !bugs.is_empty();
|
|
|
|
for bug in bugs.drain(..) {
|
|
|
|
DiagnosticBuilder::new_diagnostic(self, bug).emit();
|
|
|
|
}
|
|
|
|
if has_bugs {
|
|
|
|
panic!("no errors encountered even though `delay_span_bug` issued");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 14:11:27 +13:00
|
|
|
impl Handler {
|
2015-12-31 18:47:14 +13:00
|
|
|
pub fn with_tty_emitter(color_config: ColorConfig,
|
|
|
|
can_emit_warnings: bool,
|
|
|
|
treat_err_as_bug: bool,
|
2018-08-18 12:13:35 +02:00
|
|
|
cm: Option<Lrc<SourceMapperDyn>>)
|
2015-12-31 18:47:14 +13:00
|
|
|
-> Handler {
|
2017-11-20 18:03:20 +00:00
|
|
|
Handler::with_tty_emitter_and_flags(
|
|
|
|
color_config,
|
|
|
|
cm,
|
|
|
|
HandlerFlags {
|
|
|
|
can_emit_warnings,
|
|
|
|
treat_err_as_bug,
|
|
|
|
.. Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_tty_emitter_and_flags(color_config: ColorConfig,
|
2018-08-18 12:13:35 +02:00
|
|
|
cm: Option<Lrc<SourceMapperDyn>>,
|
2017-11-20 18:03:20 +00:00
|
|
|
flags: HandlerFlags)
|
|
|
|
-> Handler {
|
2018-01-28 18:37:55 -08:00
|
|
|
let emitter = Box::new(EmitterWriter::stderr(color_config, cm, false, false));
|
2017-11-20 18:03:20 +00:00
|
|
|
Handler::with_emitter_and_flags(emitter, flags)
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_emitter(can_emit_warnings: bool,
|
|
|
|
treat_err_as_bug: bool,
|
2018-07-12 09:33:33 +02:00
|
|
|
e: Box<dyn Emitter + sync::Send>)
|
2016-10-18 23:13:02 +05:30
|
|
|
-> Handler {
|
2017-11-20 18:03:20 +00:00
|
|
|
Handler::with_emitter_and_flags(
|
|
|
|
e,
|
|
|
|
HandlerFlags {
|
|
|
|
can_emit_warnings,
|
|
|
|
treat_err_as_bug,
|
|
|
|
.. Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-12 09:33:33 +02:00
|
|
|
pub fn with_emitter_and_flags(e: Box<dyn Emitter + sync::Send>, flags: HandlerFlags) -> Handler
|
|
|
|
{
|
2015-12-15 14:11:27 +13:00
|
|
|
Handler {
|
2017-11-20 18:03:20 +00:00
|
|
|
flags,
|
2017-12-03 14:04:51 +01:00
|
|
|
err_count: AtomicUsize::new(0),
|
2018-03-03 06:20:26 +01:00
|
|
|
emitter: Lock::new(e),
|
|
|
|
continue_after_error: LockCell::new(true),
|
2018-07-21 16:09:10 +02:00
|
|
|
delayed_span_bugs: Lock::new(Vec::new()),
|
2018-03-03 06:20:26 +01:00
|
|
|
taught_diagnostics: Lock::new(FxHashSet()),
|
|
|
|
emitted_diagnostic_codes: Lock::new(FxHashSet()),
|
|
|
|
emitted_diagnostics: Lock::new(FxHashSet()),
|
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) {
|
|
|
|
self.continue_after_error.set(continue_after_error);
|
|
|
|
}
|
|
|
|
|
2018-01-06 13:33:20 +01:00
|
|
|
/// Resets the diagnostic error count as well as the cached emitted diagnostics.
|
|
|
|
///
|
|
|
|
/// NOTE: DO NOT call this function from rustc. It is only meant to be called from external
|
|
|
|
/// 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) {
|
2018-03-03 06:20:26 +01:00
|
|
|
*self.emitted_diagnostics.borrow_mut() = FxHashSet();
|
2017-12-03 14:04:51 +01:00
|
|
|
self.err_count.store(0, SeqCst);
|
2017-08-10 09:17:03 +09:00
|
|
|
}
|
|
|
|
|
2015-12-23 19:27:20 +13:00
|
|
|
pub fn struct_dummy<'a>(&'a self) -> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
DiagnosticBuilder::new(self, Level::Cancelled, "")
|
2015-12-23 19:27:20 +13:00
|
|
|
}
|
|
|
|
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn struct_span_warn<'a, S: Into<MultiSpan>>(&'a self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
|
2016-04-20 14:56:01 -04:00
|
|
|
result.set_span(sp);
|
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
|
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn struct_span_warn_with_code<'a, S: Into<MultiSpan>>(&'a self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
2017-10-27 08:21:22 +02:00
|
|
|
code: DiagnosticId)
|
2015-12-13 13:12:47 +01:00
|
|
|
-> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Warning, msg);
|
2016-04-20 14:56:01 -04:00
|
|
|
result.set_span(sp);
|
2017-10-27 08:21:22 +02:00
|
|
|
result.code(code);
|
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
|
|
|
|
}
|
2015-12-23 19:27:20 +13:00
|
|
|
pub fn struct_warn<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
|
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
|
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn struct_span_err<'a, S: Into<MultiSpan>>(&'a self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
|
2016-04-20 14:56:01 -04:00
|
|
|
result.set_span(sp);
|
2015-12-23 19:27:20 +13:00
|
|
|
result
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn struct_span_err_with_code<'a, S: Into<MultiSpan>>(&'a self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
2017-10-27 08:21:22 +02:00
|
|
|
code: DiagnosticId)
|
2015-12-13 13:12:47 +01:00
|
|
|
-> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
|
2016-04-20 14:56:01 -04:00
|
|
|
result.set_span(sp);
|
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
|
|
|
}
|
2017-05-29 18:46:29 +02:00
|
|
|
// FIXME: This method should be removed (every error should have an associated error code).
|
2015-12-23 19:27:20 +13:00
|
|
|
pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
DiagnosticBuilder::new(self, Level::Error, msg)
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2017-10-27 08:21:22 +02:00
|
|
|
pub fn struct_err_with_code<'a>(
|
|
|
|
&'a self,
|
|
|
|
msg: &str,
|
|
|
|
code: DiagnosticId,
|
|
|
|
) -> DiagnosticBuilder<'a> {
|
2017-05-29 18:46:29 +02:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
|
2017-10-27 08:21:22 +02:00
|
|
|
result.code(code);
|
2017-05-29 18:46:29 +02:00
|
|
|
result
|
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn struct_span_fatal<'a, S: Into<MultiSpan>>(&'a self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
|
2016-04-20 14:56:01 -04:00
|
|
|
result.set_span(sp);
|
2015-12-23 19:27:20 +13:00
|
|
|
result
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn struct_span_fatal_with_code<'a, S: Into<MultiSpan>>(&'a self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
2017-10-27 08:21:22 +02:00
|
|
|
code: DiagnosticId)
|
2015-12-13 13:12:47 +01:00
|
|
|
-> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Fatal, msg);
|
2016-04-20 14:56:01 -04:00
|
|
|
result.set_span(sp);
|
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
|
|
|
}
|
2015-12-23 19:27:20 +13:00
|
|
|
pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> {
|
2016-04-21 05:14:58 -04:00
|
|
|
DiagnosticBuilder::new(self, Level::Fatal, msg)
|
2015-12-23 19:27:20 +13:00
|
|
|
}
|
|
|
|
|
2016-07-21 20:12:04 +03:00
|
|
|
pub fn cancel(&self, err: &mut DiagnosticBuilder) {
|
2015-12-23 19:27:20 +13:00
|
|
|
err.cancel();
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
|
|
|
|
2016-04-21 05:14:58 -04:00
|
|
|
fn panic_if_treat_err_as_bug(&self) {
|
2017-11-20 18:03:20 +00:00
|
|
|
if self.flags.treat_err_as_bug {
|
2016-04-21 05:14:58 -04:00
|
|
|
panic!("encountered error with `-Z treat_err_as_bug");
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2016-04-21 05:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 23:13:02 +05:30
|
|
|
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> FatalError {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit(&sp.into(), msg, Fatal);
|
2017-05-19 01:20:48 +02:00
|
|
|
FatalError
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2016-10-18 23:13:02 +05:30
|
|
|
pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
2017-10-27 08:21:22 +02:00
|
|
|
code: DiagnosticId)
|
2016-04-21 05:14:58 -04:00
|
|
|
-> FatalError {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit_with_code(&sp.into(), msg, code, Fatal);
|
2017-05-19 01:20:48 +02:00
|
|
|
FatalError
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit(&sp.into(), msg, Error);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2016-05-27 19:05:22 -07:00
|
|
|
pub fn mut_span_err<'a, S: Into<MultiSpan>>(&'a self,
|
|
|
|
sp: S,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
|
|
|
let mut result = DiagnosticBuilder::new(self, Level::Error, msg);
|
|
|
|
result.set_span(sp);
|
|
|
|
result
|
|
|
|
}
|
2017-10-27 08:21:22 +02:00
|
|
|
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit_with_code(&sp.into(), msg, code, Error);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit(&sp.into(), msg, Warning);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2017-10-27 08:21:22 +02:00
|
|
|
pub fn span_warn_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit_with_code(&sp.into(), msg, code, Warning);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit(&sp.into(), msg, Bug);
|
2015-12-15 14:11:27 +13:00
|
|
|
panic!(ExplicitBug);
|
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
|
2017-11-20 18:03:20 +00:00
|
|
|
if self.flags.treat_err_as_bug {
|
2018-07-19 17:53:44 +02:00
|
|
|
// FIXME: don't abort here if report_delayed_bugs is off
|
2017-04-27 16:40:49 +03:00
|
|
|
self.span_bug(sp, msg);
|
|
|
|
}
|
2017-08-23 12:52:22 -07:00
|
|
|
let mut diagnostic = Diagnostic::new(Level::Bug, msg);
|
|
|
|
diagnostic.set_span(sp.into());
|
2018-07-19 17:53:44 +02:00
|
|
|
self.delay_as_bug(diagnostic);
|
|
|
|
}
|
|
|
|
fn delay_as_bug(&self, diagnostic: Diagnostic) {
|
|
|
|
if self.flags.report_delayed_bugs {
|
|
|
|
DiagnosticBuilder::new_diagnostic(self, diagnostic.clone()).emit();
|
|
|
|
}
|
2018-07-21 16:09:10 +02:00
|
|
|
self.delayed_span_bugs.borrow_mut().push(diagnostic);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn span_bug_no_panic<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
|
2016-04-20 14:56:01 -04:00
|
|
|
self.emit(&sp.into(), msg, Bug);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
|
2016-07-06 12:08:16 -04:00
|
|
|
self.emit(&sp.into(), msg, Note);
|
2015-12-18 16:15:53 +13:00
|
|
|
}
|
2017-05-05 21:49:59 -07:00
|
|
|
pub fn span_note_diag<'a>(&'a self,
|
|
|
|
sp: Span,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a> {
|
2017-04-24 16:27:07 -07:00
|
|
|
let mut db = DiagnosticBuilder::new(self, Note, msg);
|
|
|
|
db.set_span(sp);
|
2017-05-05 21:49:59 -07:00
|
|
|
db
|
2017-04-24 16:27:07 -07:00
|
|
|
}
|
2015-12-13 13:12:47 +01:00
|
|
|
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
|
2015-12-15 14:11:27 +13:00
|
|
|
self.span_bug(sp, &format!("unimplemented {}", msg));
|
|
|
|
}
|
2018-02-28 16:17:44 +01:00
|
|
|
pub fn failure(&self, msg: &str) {
|
|
|
|
DiagnosticBuilder::new(self, FailureNote, msg).emit()
|
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
pub fn fatal(&self, msg: &str) -> FatalError {
|
2017-11-20 18:03:20 +00:00
|
|
|
if self.flags.treat_err_as_bug {
|
2015-12-15 14:11:27 +13:00
|
|
|
self.bug(msg);
|
|
|
|
}
|
2018-02-28 16:17:44 +01:00
|
|
|
DiagnosticBuilder::new(self, Fatal, msg).emit();
|
2015-12-15 14:11:27 +13:00
|
|
|
FatalError
|
|
|
|
}
|
|
|
|
pub fn err(&self, msg: &str) {
|
2017-11-20 18:03:20 +00:00
|
|
|
if self.flags.treat_err_as_bug {
|
2015-12-15 14:11:27 +13:00
|
|
|
self.bug(msg);
|
|
|
|
}
|
2016-10-18 23:13:02 +05:30
|
|
|
let mut db = DiagnosticBuilder::new(self, Error, msg);
|
2016-07-06 12:08:16 -04:00
|
|
|
db.emit();
|
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
|
|
|
}
|
2015-12-18 16:15:53 +13:00
|
|
|
pub fn note_without_error(&self, msg: &str) {
|
2016-10-18 23:13:02 +05:30
|
|
|
let mut db = DiagnosticBuilder::new(self, Note, msg);
|
2016-07-06 12:08:16 -04:00
|
|
|
db.emit();
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
pub fn bug(&self, msg: &str) -> ! {
|
2016-10-18 23:13:02 +05:30
|
|
|
let mut db = DiagnosticBuilder::new(self, Bug, msg);
|
2016-07-06 12:08:16 -04:00
|
|
|
db.emit();
|
2015-12-15 14:11:27 +13:00
|
|
|
panic!(ExplicitBug);
|
|
|
|
}
|
|
|
|
pub fn unimpl(&self, msg: &str) -> ! {
|
|
|
|
self.bug(&format!("unimplemented {}", msg));
|
|
|
|
}
|
|
|
|
|
2017-08-19 03:09:55 +03:00
|
|
|
fn bump_err_count(&self) {
|
2017-07-31 15:54:16 +03:00
|
|
|
self.panic_if_treat_err_as_bug();
|
2017-12-03 14:04:51 +01:00
|
|
|
self.err_count.fetch_add(1, SeqCst);
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn err_count(&self) -> usize {
|
2017-12-03 14:04:51 +01:00
|
|
|
self.err_count.load(SeqCst)
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_errors(&self) -> bool {
|
2017-12-03 14:04:51 +01:00
|
|
|
self.err_count() > 0
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2018-03-15 10:09:20 +01:00
|
|
|
|
|
|
|
pub fn print_error_count(&self) {
|
|
|
|
let s = match self.err_count() {
|
|
|
|
0 => return,
|
|
|
|
1 => "aborting due to previous error".to_string(),
|
|
|
|
_ => format!("aborting due to {} previous errors", self.err_count())
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = self.fatal(&s);
|
2018-02-28 16:17:44 +01:00
|
|
|
|
|
|
|
let can_show_explain = self.emitter.borrow().should_show_explain();
|
2018-03-03 06:20:26 +01:00
|
|
|
let are_there_diagnostics = !self.emitted_diagnostic_codes.borrow().is_empty();
|
2018-02-28 16:17:44 +01:00
|
|
|
if can_show_explain && are_there_diagnostics {
|
|
|
|
let mut error_codes =
|
2018-03-03 06:20:26 +01:00
|
|
|
self.emitted_diagnostic_codes.borrow()
|
2018-03-16 22:35:26 +09:00
|
|
|
.iter()
|
|
|
|
.filter_map(|x| match *x {
|
2018-02-28 16:17:44 +01:00
|
|
|
DiagnosticId::Error(ref s) => Some(s.clone()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
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() };
|
|
|
|
self.failure(&format!("Some errors occurred: {}{}",
|
|
|
|
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
|
|
|
|
2018-03-15 10:09:20 +01:00
|
|
|
pub fn abort_if_errors(&self) {
|
|
|
|
if self.err_count() == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FatalError.raise();
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2016-10-18 23:13:02 +05:30
|
|
|
pub fn emit(&self, msp: &MultiSpan, msg: &str, lvl: Level) {
|
2017-11-20 18:03:20 +00:00
|
|
|
if lvl == Warning && !self.flags.can_emit_warnings {
|
2016-10-18 23:13:02 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-07-06 12:08:16 -04:00
|
|
|
let mut db = DiagnosticBuilder::new(self, lvl, msg);
|
|
|
|
db.set_span(msp.clone());
|
|
|
|
db.emit();
|
2016-10-18 23:13:02 +05:30
|
|
|
if !self.continue_after_error.get() {
|
|
|
|
self.abort_if_errors();
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 08:21:22 +02:00
|
|
|
pub fn emit_with_code(&self, msp: &MultiSpan, msg: &str, code: DiagnosticId, lvl: Level) {
|
2017-11-20 18:03:20 +00:00
|
|
|
if lvl == Warning && !self.flags.can_emit_warnings {
|
2016-10-18 23:13:02 +05:30
|
|
|
return;
|
|
|
|
}
|
2017-10-27 08:21:22 +02:00
|
|
|
let mut db = DiagnosticBuilder::new_with_code(self, lvl, Some(code), msg);
|
2016-07-06 12:08:16 -04:00
|
|
|
db.set_span(msp.clone());
|
|
|
|
db.emit();
|
2016-10-18 23:13:02 +05:30
|
|
|
if !self.continue_after_error.get() {
|
|
|
|
self.abort_if_errors();
|
|
|
|
}
|
2015-12-15 14:11:27 +13:00
|
|
|
}
|
2017-08-12 15:37:28 -07:00
|
|
|
|
2018-03-03 06:20:26 +01:00
|
|
|
/// `true` if we haven't taught a diagnostic with this code already.
|
|
|
|
/// The caller must then teach the user about such a diagnostic.
|
2018-01-25 11:40:33 -08:00
|
|
|
///
|
|
|
|
/// Used to suppress emitting the same error multiple times with extended explanation when
|
|
|
|
/// calling `-Zteach`.
|
2018-03-03 06:20:26 +01:00
|
|
|
pub fn must_teach(&self, code: &DiagnosticId) -> bool {
|
|
|
|
self.taught_diagnostics.borrow_mut().insert(code.clone())
|
2018-01-21 21:19:37 -08:00
|
|
|
}
|
|
|
|
|
2018-03-20 23:41:25 +01:00
|
|
|
pub fn force_print_db(&self, mut db: DiagnosticBuilder) {
|
|
|
|
self.emitter.borrow_mut().emit(&db);
|
|
|
|
db.cancel();
|
|
|
|
}
|
|
|
|
|
2017-08-12 15:37:28 -07:00
|
|
|
fn emit_db(&self, db: &DiagnosticBuilder) {
|
2017-10-25 15:01:06 +02:00
|
|
|
let diagnostic = &**db;
|
|
|
|
|
2018-03-15 10:03:36 +01:00
|
|
|
TRACK_DIAGNOSTICS.with(|track_diagnostics| {
|
|
|
|
track_diagnostics.get()(diagnostic);
|
|
|
|
});
|
2017-10-25 15:01:06 +02:00
|
|
|
|
2018-01-21 21:19:37 -08:00
|
|
|
if let Some(ref code) = diagnostic.code {
|
2018-03-03 06:20:26 +01:00
|
|
|
self.emitted_diagnostic_codes.borrow_mut().insert(code.clone());
|
2018-01-21 21:19:37 -08:00
|
|
|
}
|
|
|
|
|
2017-10-25 15:01:06 +02:00
|
|
|
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.borrow_mut().insert(diagnostic_hash) {
|
|
|
|
self.emitter.borrow_mut().emit(db);
|
2018-01-02 20:00:12 +01:00
|
|
|
if db.is_error() {
|
|
|
|
self.bump_err_count();
|
|
|
|
}
|
2017-08-12 15:37:28 -07: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,
|
2016-01-20 22:07:33 +13:00
|
|
|
// An error which while not immediately fatal, should stop the compiler
|
|
|
|
// progressing beyond the current phase.
|
|
|
|
PhaseFatal,
|
2015-12-15 14:11:27 +13:00
|
|
|
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 {
|
|
|
|
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 {
|
2018-02-27 10:33:02 -08:00
|
|
|
Bug | Fatal | PhaseFatal | Error => {
|
|
|
|
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",
|
2016-01-20 22:07:33 +13:00
|
|
|
Fatal | PhaseFatal | Error => "error",
|
2015-12-31 18:47:14 +13:00
|
|
|
Warning => "warning",
|
|
|
|
Note => "note",
|
|
|
|
Help => "help",
|
2018-02-28 16:17:44 +01:00
|
|
|
FailureNote => "",
|
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
|
|
|
}
|