diff options
| author | bors <bors@rust-lang.org> | 2014-07-10 23:26:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-07-10 23:26:39 +0000 |
| commit | 0e80dbe59ea986ea53cc3caabffd40b2eaee4dc6 (patch) | |
| tree | 3044cbdb7fe356d52a07c9048bc431aac3de5c4a /src/libsyntax/diagnostics/plugin.rs | |
| parent | a672456c40d28f051ecbdb2caf5bf6733371d494 (diff) | |
| parent | 9b9cce2316119a2ffdc9556d410e464b7542d64d (diff) | |
| download | rust-0e80dbe59ea986ea53cc3caabffd40b2eaee4dc6.tar.gz rust-0e80dbe59ea986ea53cc3caabffd40b2eaee4dc6.zip | |
auto merge of #15336 : jakub-/rust/diagnostics, r=brson
This is a continuation of @brson's work from https://github.com/rust-lang/rust/pull/12144.
This implements the minimal scaffolding that allows mapping diagnostic messages to alpha-numeric codes, which could improve the searchability of errors. In addition, there's a new compiler option, `--explain {code}` which takes an error code and prints out a somewhat detailed explanation of the error. Example:
```rust
fn f(x: Option<bool>) {
match x {
Some(true) | Some(false) => (),
None => (),
Some(true) => ()
}
}
```
```shell
[~/rust]$ ./build/x86_64-apple-darwin/stage2/bin/rustc ./diagnostics.rs --crate-type dylib
diagnostics.rs:5:3: 5:13 error: unreachable pattern [E0001] (pass `--explain E0001` to see a detailed explanation)
diagnostics.rs:5 Some(true) => ()
^~~~~~~~~~
error: aborting due to previous error
[~/rust]$ ./build/x86_64-apple-darwin/stage2/bin/rustc --explain E0001
This error suggests that the expression arm corresponding to the noted pattern
will never be reached as for all possible values of the expression being matched,
one of the preceeding patterns will match.
This means that perhaps some of the preceeding patterns are too general, this
one is too specific or the ordering is incorrect.
```
I've refrained from migrating many errors to actually use the new macros as it can be done in an incremental fashion but if we're happy with the approach, it'd be good to do all of them sooner rather than later.
Originally, I was going to make libdiagnostics a separate crate but that's posing some interesting challenges with semi-circular dependencies. In particular, librustc would have a plugin-phase dependency on libdiagnostics, which itself depends on librustc. Per my conversation with @alexcrichton, it seems like the snapshotting process would also have to change. So for now the relevant modules from libdiagnostics are included using `#[path = ...] mod`.
Diffstat (limited to 'src/libsyntax/diagnostics/plugin.rs')
| -rw-r--r-- | src/libsyntax/diagnostics/plugin.rs | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs new file mode 100644 index 00000000000..6582d2e44c8 --- /dev/null +++ b/src/libsyntax/diagnostics/plugin.rs @@ -0,0 +1,132 @@ +// Copyright 2014 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. + +use std::cell::RefCell; +use std::collections::HashMap; +use std::gc::Gc; +use ast; +use ast::{Ident, Name, TokenTree}; +use codemap::Span; +use ext::base::{ExtCtxt, MacExpr, MacItem, MacResult}; +use ext::build::AstBuilder; +use parse::token; + +local_data_key!(registered_diagnostics: RefCell<HashMap<Name, Option<Name>>>) +local_data_key!(used_diagnostics: RefCell<HashMap<Name, Span>>) + +fn with_registered_diagnostics<T>(f: |&mut HashMap<Name, Option<Name>>| -> T) -> T { + match registered_diagnostics.get() { + Some(cell) => f(cell.borrow_mut().deref_mut()), + None => { + let mut map = HashMap::new(); + let value = f(&mut map); + registered_diagnostics.replace(Some(RefCell::new(map))); + value + } + } +} + +fn with_used_diagnostics<T>(f: |&mut HashMap<Name, Span>| -> T) -> T { + match used_diagnostics.get() { + Some(cell) => f(cell.borrow_mut().deref_mut()), + None => { + let mut map = HashMap::new(); + let value = f(&mut map); + used_diagnostics.replace(Some(RefCell::new(map))); + value + } + } +} + +pub fn expand_diagnostic_used(ecx: &mut ExtCtxt, span: Span, + token_tree: &[TokenTree]) -> Box<MacResult> { + let code = match token_tree { + [ast::TTTok(_, token::IDENT(code, _))] => code, + _ => unreachable!() + }; + with_registered_diagnostics(|diagnostics| { + if !diagnostics.contains_key(&code.name) { + ecx.span_err(span, format!( + "unknown diagnostic code {}", token::get_ident(code).get() + ).as_slice()); + } + () + }); + with_used_diagnostics(|diagnostics| { + match diagnostics.swap(code.name, span) { + Some(previous_span) => { + ecx.span_warn(span, format!( + "diagnostic code {} already used", token::get_ident(code).get() + ).as_slice()); + ecx.span_note(previous_span, "previous invocation"); + }, + None => () + } + () + }); + MacExpr::new(quote_expr!(ecx, ())) +} + +pub fn expand_register_diagnostic(ecx: &mut ExtCtxt, span: Span, + token_tree: &[TokenTree]) -> Box<MacResult> { + let (code, description) = match token_tree { + [ast::TTTok(_, token::IDENT(ref code, _))] => { + (code, None) + }, + [ast::TTTok(_, token::IDENT(ref code, _)), + ast::TTTok(_, token::COMMA), + ast::TTTok(_, token::LIT_STR_RAW(description, _))] => { + (code, Some(description)) + } + _ => unreachable!() + }; + with_registered_diagnostics(|diagnostics| { + if !diagnostics.insert(code.name, description) { + ecx.span_err(span, format!( + "diagnostic code {} already registered", token::get_ident(*code).get() + ).as_slice()); + } + }); + let sym = Ident::new(token::gensym(( + "__register_diagnostic_".to_string() + token::get_ident(*code).get() + ).as_slice())); + MacItem::new(quote_item!(ecx, mod $sym {}).unwrap()) +} + +pub fn expand_build_diagnostic_array(ecx: &mut ExtCtxt, span: Span, + token_tree: &[TokenTree]) -> Box<MacResult> { + let name = match token_tree { + [ast::TTTok(_, token::IDENT(ref name, _))] => name, + _ => unreachable!() + }; + + let (count, expr) = with_used_diagnostics(|diagnostics_in_use| { + with_registered_diagnostics(|diagnostics| { + let descriptions: Vec<Gc<ast::Expr>> = diagnostics + .iter().filter_map(|(code, description)| { + if !diagnostics_in_use.contains_key(code) { + ecx.span_warn(span, format!( + "diagnostic code {} never used", token::get_name(*code).get() + ).as_slice()); + } + description.map(|description| { + ecx.expr_tuple(span, vec![ + ecx.expr_str(span, token::get_name(*code)), + ecx.expr_str(span, token::get_name(description)) + ]) + }) + }).collect(); + (descriptions.len(), ecx.expr_vec(span, descriptions)) + }) + }); + MacItem::new(quote_item!(ecx, + pub static $name: [(&'static str, &'static str), ..$count] = $expr; + ).unwrap()) +} |
