1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
12 html_favicon_url
= "https://doc.rust-lang.org/favicon.ico",
13 html_root_url
= "https://doc.rust-lang.org/nightly/")]
16 #![feature(custom_attribute)]
17 #![allow(unused_attributes)]
18 #![feature(range_contains)]
19 #![cfg_attr(unix, feature(libc))]
20 #![feature(conservative_impl_trait)]
21 #![feature(i128_type)]
26 extern crate rustc_data_structures
;
27 extern crate serialize
as rustc_serialize
;
28 extern crate syntax_pos
;
30 pub use emitter
::ColorConfig
;
34 use emitter
::{Emitter, EmitterWriter}
;
36 use rustc_data_structures
::fx
::FxHashSet
;
37 use rustc_data_structures
::stable_hasher
::StableHasher
;
40 use std
::cell
::{RefCell, Cell}
;
43 use std
::{error, fmt}
;
46 mod diagnostic_builder
;
53 use syntax_pos
::{BytePos, Loc, FileLinesResult, FileMap, FileName, MultiSpan, Span, NO_EXPANSION}
;
55 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
56 pub struct CodeSuggestion
{
57 /// Each substitute can have multiple variants due to multiple
58 /// applicable suggestions
60 /// `foo.bar` might be replaced with `a.b` or `x.y` by replacing
61 /// `foo` and `bar` on their own:
65 /// Substitution { parts: vec![(0..3, "a"), (4..7, "b")] },
66 /// Substitution { parts: vec![(0..3, "x"), (4..7, "y")] },
70 /// or by replacing the entire span:
74 /// Substitution { parts: vec![(0..7, "a.b")] },
75 /// Substitution { parts: vec![(0..7, "x.y")] },
78 pub substitutions
: Vec
<Substitution
>,
80 pub show_code_when_inline
: bool
,
83 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
84 /// See the docs on `CodeSuggestion::substitutions`
85 pub struct Substitution
{
86 pub parts
: Vec
<SubstitutionPart
>,
89 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
90 pub struct SubstitutionPart
{
95 pub trait CodeMapper
{
96 fn lookup_char_pos(&self, pos
: BytePos
) -> Loc
;
97 fn span_to_lines(&self, sp
: Span
) -> FileLinesResult
;
98 fn span_to_string(&self, sp
: Span
) -> String
;
99 fn span_to_filename(&self, sp
: Span
) -> FileName
;
100 fn merge_spans(&self, sp_lhs
: Span
, sp_rhs
: Span
) -> Option
<Span
>;
101 fn call_span_if_macro(&self, sp
: Span
) -> Span
;
102 fn ensure_filemap_source_present(&self, file_map
: Rc
<FileMap
>) -> bool
;
105 impl CodeSuggestion
{
106 /// Returns the assembled code suggestions and whether they should be shown with an underline.
107 pub fn splice_lines(&self, cm
: &CodeMapper
) -> Vec
<(String
, Vec
<SubstitutionPart
>)> {
108 use syntax_pos
::{CharPos, Loc, Pos}
;
110 fn push_trailing(buf
: &mut String
,
111 line_opt
: Option
<&Cow
<str>>,
113 hi_opt
: Option
<&Loc
>) {
114 let (lo
, hi_opt
) = (lo
.col
.to_usize(), hi_opt
.map(|hi
| hi
.col
.to_usize()));
115 if let Some(line
) = line_opt
{
116 if let Some(lo
) = line
.char_indices().map(|(i
, _
)| i
).nth(lo
) {
117 let hi_opt
= hi_opt
.and_then(|hi
| line
.char_indices().map(|(i
, _
)| i
).nth(hi
));
118 buf
.push_str(match hi_opt
{
119 Some(hi
) => &line
[lo
..hi
],
123 if let None
= hi_opt
{
129 assert
!(!self.substitutions
.is_empty());
131 self.substitutions
.iter().cloned().map(|mut substitution
| {
132 // Assumption: all spans are in the same file, and all spans
133 // are disjoint. Sort in ascending order.
134 substitution
.parts
.sort_by_key(|part
| part
.span
.lo());
136 // Find the bounding span.
137 let lo
= substitution
.parts
.iter().map(|part
| part
.span
.lo()).min().unwrap();
138 let hi
= substitution
.parts
.iter().map(|part
| part
.span
.hi()).min().unwrap();
139 let bounding_span
= Span
::new(lo
, hi
, NO_EXPANSION
);
140 let lines
= cm
.span_to_lines(bounding_span
).unwrap();
141 assert
!(!lines
.lines
.is_empty());
143 // To build up the result, we do this for each span:
144 // - push the line segment trailing the previous span
145 // (at the beginning a "phantom" span pointing at the start of the line)
146 // - push lines between the previous and current span (if any)
147 // - if the previous and current span are not on the same line
148 // push the line segment leading up to the current span
149 // - splice in the span substitution
151 // Finally push the trailing line segment of the last span
152 let fm
= &lines
.file
;
153 let mut prev_hi
= cm
.lookup_char_pos(bounding_span
.lo());
154 prev_hi
.col
= CharPos
::from_usize(0);
156 let mut prev_line
= fm
.get_line(lines
.lines
[0].line_index
);
157 let mut buf
= String
::new();
159 for part
in &substitution
.parts
{
160 let cur_lo
= cm
.lookup_char_pos(part
.span
.lo());
161 if prev_hi
.line
== cur_lo
.line
{
162 push_trailing(&mut buf
, prev_line
.as_ref(), &prev_hi
, Some(&cur_lo
));
164 push_trailing(&mut buf
, prev_line
.as_ref(), &prev_hi
, None
);
165 // push lines between the previous and current span (if any)
166 for idx
in prev_hi
.line
..(cur_lo
.line
- 1) {
167 if let Some(line
) = fm
.get_line(idx
) {
168 buf
.push_str(line
.as_ref());
172 if let Some(cur_line
) = fm
.get_line(cur_lo
.line
- 1) {
173 buf
.push_str(&cur_line
[..cur_lo
.col
.to_usize()]);
176 buf
.push_str(&part
.snippet
);
177 prev_hi
= cm
.lookup_char_pos(part
.span
.hi());
178 prev_line
= fm
.get_line(prev_hi
.line
- 1);
180 // if the replacement already ends with a newline, don't print the next line
181 if !buf
.ends_with('
\n'
) {
182 push_trailing(&mut buf
, prev_line
.as_ref(), &prev_hi
, None
);
184 // remove trailing newlines
185 while buf
.ends_with('
\n'
) {
188 (buf
, substitution
.parts
)
193 /// Used as a return value to signify a fatal error occurred. (It is also
194 /// used as the argument to panic at the moment, but that will eventually
196 #[derive(Copy, Clone, Debug)]
198 pub struct FatalError
;
200 impl fmt
::Display
for FatalError
{
201 fn fmt(&self, f
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
202 write
!(f
, "parser fatal error")
206 impl error
::Error
for FatalError
{
207 fn description(&self) -> &str {
208 "The parser has encountered a fatal error"
212 /// Signifies that the compiler died with an explicit call to `.bug`
213 /// or `.span_bug` rather than a failed assertion, etc.
214 #[derive(Copy, Clone, Debug)]
215 pub struct ExplicitBug
;
217 impl fmt
::Display
for ExplicitBug
{
218 fn fmt(&self, f
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
219 write
!(f
, "parser internal bug")
223 impl error
::Error
for ExplicitBug
{
224 fn description(&self) -> &str {
225 "The parser has encountered an internal bug"
229 pub use diagnostic
::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId}
;
230 pub use diagnostic_builder
::DiagnosticBuilder
;
232 /// A handler deals with errors; certain errors
233 /// (fatal, bug, unimpl) may cause immediate exit,
234 /// others log errors for later reporting.
236 err_count
: Cell
<usize>,
237 emitter
: RefCell
<Box
<Emitter
>>,
238 pub can_emit_warnings
: bool
,
239 treat_err_as_bug
: bool
,
240 continue_after_error
: Cell
<bool
>,
241 delayed_span_bug
: RefCell
<Option
<Diagnostic
>>,
242 tracked_diagnostics
: RefCell
<Option
<Vec
<Diagnostic
>>>,
244 // This set contains a hash of every diagnostic that has been emitted by
245 // this handler. These hashes is used to avoid emitting the same error
247 emitted_diagnostics
: RefCell
<FxHashSet
<u128
>>,
251 pub fn with_tty_emitter(color_config
: ColorConfig
,
252 can_emit_warnings
: bool
,
253 treat_err_as_bug
: bool
,
254 cm
: Option
<Rc
<CodeMapper
>>)
256 let emitter
= Box
::new(EmitterWriter
::stderr(color_config
, cm
, false));
257 Handler
::with_emitter(can_emit_warnings
, treat_err_as_bug
, emitter
)
260 pub fn with_emitter(can_emit_warnings
: bool
,
261 treat_err_as_bug
: bool
,
265 err_count
: Cell
::new(0),
266 emitter
: RefCell
::new(e
),
269 continue_after_error
: Cell
::new(true),
270 delayed_span_bug
: RefCell
::new(None
),
271 tracked_diagnostics
: RefCell
::new(None
),
272 emitted_diagnostics
: RefCell
::new(FxHashSet()),
276 pub fn set_continue_after_error(&self, continue_after_error
: bool
) {
277 self.continue_after_error
.set(continue_after_error
);
280 // NOTE: DO NOT call this function from rustc, as it relies on `err_count` being non-zero
281 // if an error happened to avoid ICEs. This function should only be called from tools.
282 pub fn reset_err_count(&self) {
283 self.err_count
.set(0);
286 pub fn struct_dummy
<'a
>(&'a
self) -> DiagnosticBuilder
<'a
> {
287 DiagnosticBuilder
::new(self, Level
::Cancelled
, "")
290 pub fn struct_span_warn
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
293 -> DiagnosticBuilder
<'a
> {
294 let mut result
= DiagnosticBuilder
::new(self, Level
::Warning
, msg
);
296 if !self.can_emit_warnings
{
301 pub fn struct_span_warn_with_code
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
305 -> DiagnosticBuilder
<'a
> {
306 let mut result
= DiagnosticBuilder
::new(self, Level
::Warning
, msg
);
309 if !self.can_emit_warnings
{
314 pub fn struct_warn
<'a
>(&'a
self, msg
: &str) -> DiagnosticBuilder
<'a
> {
315 let mut result
= DiagnosticBuilder
::new(self, Level
::Warning
, msg
);
316 if !self.can_emit_warnings
{
321 pub fn struct_span_err
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
324 -> DiagnosticBuilder
<'a
> {
325 let mut result
= DiagnosticBuilder
::new(self, Level
::Error
, msg
);
329 pub fn struct_span_err_with_code
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
333 -> DiagnosticBuilder
<'a
> {
334 let mut result
= DiagnosticBuilder
::new(self, Level
::Error
, msg
);
339 // FIXME: This method should be removed (every error should have an associated error code).
340 pub fn struct_err
<'a
>(&'a
self, msg
: &str) -> DiagnosticBuilder
<'a
> {
341 DiagnosticBuilder
::new(self, Level
::Error
, msg
)
343 pub fn struct_err_with_code
<'a
>(
347 ) -> DiagnosticBuilder
<'a
> {
348 let mut result
= DiagnosticBuilder
::new(self, Level
::Error
, msg
);
352 pub fn struct_span_fatal
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
355 -> DiagnosticBuilder
<'a
> {
356 let mut result
= DiagnosticBuilder
::new(self, Level
::Fatal
, msg
);
360 pub fn struct_span_fatal_with_code
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
364 -> DiagnosticBuilder
<'a
> {
365 let mut result
= DiagnosticBuilder
::new(self, Level
::Fatal
, msg
);
370 pub fn struct_fatal
<'a
>(&'a
self, msg
: &str) -> DiagnosticBuilder
<'a
> {
371 DiagnosticBuilder
::new(self, Level
::Fatal
, msg
)
374 pub fn cancel(&self, err
: &mut DiagnosticBuilder
) {
378 fn panic_if_treat_err_as_bug(&self) {
379 if self.treat_err_as_bug
{
380 panic
!("encountered error with `-Z treat_err_as_bug");
384 pub fn span_fatal
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) -> FatalError
{
385 self.emit(&sp
.into(), msg
, Fatal
);
388 pub fn span_fatal_with_code
<S
: Into
<MultiSpan
>>(&self,
393 self.emit_with_code(&sp
.into(), msg
, code
, Fatal
);
396 pub fn span_err
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) {
397 self.emit(&sp
.into(), msg
, Error
);
399 pub fn mut_span_err
<'a
, S
: Into
<MultiSpan
>>(&'a
self,
402 -> DiagnosticBuilder
<'a
> {
403 let mut result
= DiagnosticBuilder
::new(self, Level
::Error
, msg
);
407 pub fn span_err_with_code
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str, code
: DiagnosticId
) {
408 self.emit_with_code(&sp
.into(), msg
, code
, Error
);
410 pub fn span_warn
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) {
411 self.emit(&sp
.into(), msg
, Warning
);
413 pub fn span_warn_with_code
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str, code
: DiagnosticId
) {
414 self.emit_with_code(&sp
.into(), msg
, code
, Warning
);
416 pub fn span_bug
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) -> ! {
417 self.emit(&sp
.into(), msg
, Bug
);
420 pub fn delay_span_bug
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) {
421 if self.treat_err_as_bug
{
422 self.span_bug(sp
, msg
);
424 let mut diagnostic
= Diagnostic
::new(Level
::Bug
, msg
);
425 diagnostic
.set_span(sp
.into());
426 *self.delayed_span_bug
.borrow_mut() = Some(diagnostic
);
428 pub fn span_bug_no_panic
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) {
429 self.emit(&sp
.into(), msg
, Bug
);
431 pub fn span_note_without_error
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) {
432 self.emit(&sp
.into(), msg
, Note
);
434 pub fn span_note_diag
<'a
>(&'a
self,
437 -> DiagnosticBuilder
<'a
> {
438 let mut db
= DiagnosticBuilder
::new(self, Note
, msg
);
442 pub fn span_unimpl
<S
: Into
<MultiSpan
>>(&self, sp
: S
, msg
: &str) -> ! {
443 self.span_bug(sp
, &format
!("unimplemented {}", msg
));
445 pub fn fatal(&self, msg
: &str) -> FatalError
{
446 if self.treat_err_as_bug
{
449 let mut db
= DiagnosticBuilder
::new(self, Fatal
, msg
);
453 pub fn err(&self, msg
: &str) {
454 if self.treat_err_as_bug
{
457 let mut db
= DiagnosticBuilder
::new(self, Error
, msg
);
460 pub fn warn(&self, msg
: &str) {
461 let mut db
= DiagnosticBuilder
::new(self, Warning
, msg
);
464 pub fn note_without_error(&self, msg
: &str) {
465 let mut db
= DiagnosticBuilder
::new(self, Note
, msg
);
468 pub fn bug(&self, msg
: &str) -> ! {
469 let mut db
= DiagnosticBuilder
::new(self, Bug
, msg
);
473 pub fn unimpl(&self, msg
: &str) -> ! {
474 self.bug(&format
!("unimplemented {}", msg
));
477 fn bump_err_count(&self) {
478 self.panic_if_treat_err_as_bug();
479 self.err_count
.set(self.err_count
.get() + 1);
482 pub fn err_count(&self) -> usize {
486 pub fn has_errors(&self) -> bool
{
487 self.err_count
.get() > 0
489 pub fn abort_if_errors(&self) {
491 match self.err_count
.get() {
493 if let Some(bug
) = self.delayed_span_bug
.borrow_mut().take() {
494 DiagnosticBuilder
::new_diagnostic(self, bug
).emit();
498 1 => s
= "aborting due to previous error".to_string(),
500 s
= format
!("aborting due to {} previous errors", self.err_count
.get());
504 panic
!(self.fatal(&s
));
506 pub fn emit(&self, msp
: &MultiSpan
, msg
: &str, lvl
: Level
) {
507 if lvl
== Warning
&& !self.can_emit_warnings
{
510 let mut db
= DiagnosticBuilder
::new(self, lvl
, msg
);
511 db
.set_span(msp
.clone());
513 if !self.continue_after_error
.get() {
514 self.abort_if_errors();
517 pub fn emit_with_code(&self, msp
: &MultiSpan
, msg
: &str, code
: DiagnosticId
, lvl
: Level
) {
518 if lvl
== Warning
&& !self.can_emit_warnings
{
521 let mut db
= DiagnosticBuilder
::new_with_code(self, lvl
, Some(code
), msg
);
522 db
.set_span(msp
.clone());
524 if !self.continue_after_error
.get() {
525 self.abort_if_errors();
529 pub fn track_diagnostics
<F
, R
>(&self, f
: F
) -> (R
, Vec
<Diagnostic
>)
530 where F
: FnOnce() -> R
532 let prev
= mem
::replace(&mut *self.tracked_diagnostics
.borrow_mut(),
535 let diagnostics
= mem
::replace(&mut *self.tracked_diagnostics
.borrow_mut(), prev
)
540 fn emit_db(&self, db
: &DiagnosticBuilder
) {
541 let diagnostic
= &**db
;
543 if let Some(ref mut list
) = *self.tracked_diagnostics
.borrow_mut() {
544 list
.push(diagnostic
.clone());
547 let diagnostic_hash
= {
549 let mut hasher
= StableHasher
::new();
550 diagnostic
.hash(&mut hasher
);
554 // Only emit the diagnostic if we haven't already emitted an equivalent
556 if self.emitted_diagnostics
.borrow_mut().insert(diagnostic_hash
) {
557 self.emitter
.borrow_mut().emit(db
);
563 #[derive(Copy, PartialEq, Clone, Hash, Debug, RustcEncodable, RustcDecodable)]
567 // An error which while not immediately fatal, should stop the compiler
568 // progressing beyond the current phase.
577 impl fmt
::Display
for Level
{
578 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
584 fn color(self) -> term
::color
::Color
{
586 Bug
| Fatal
| PhaseFatal
| Error
=> term
::color
::BRIGHT_RED
,
589 term
::color
::BRIGHT_YELLOW
594 Note
=> term
::color
::BRIGHT_GREEN
,
595 Help
=> term
::color
::BRIGHT_CYAN
,
596 Cancelled
=> unreachable
!(),
600 pub fn to_str(self) -> &'
static str {
602 Bug
=> "error: internal compiler error",
603 Fatal
| PhaseFatal
| Error
=> "error",
604 Warning
=> "warning",
607 Cancelled
=> panic
!("Shouldn't call on cancelled error"),