-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #52896 - SergioBenitez:master, r=alexcrichton
Add inspection and setter methods to proc_macro::Diagnostic. A few useful methods for `proc_macro::Diagnostic`. r? @alexcrichton
- Loading branch information
Showing
5 changed files
with
279 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2017 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. | ||
|
||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
#![feature(proc_macro_diagnostic, proc_macro_span)] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::{TokenStream, TokenTree, Span, Diagnostic}; | ||
|
||
fn parse(input: TokenStream) -> Result<(), Diagnostic> { | ||
let mut hi_spans = vec![]; | ||
for tree in input { | ||
if let TokenTree::Ident(ref ident) = tree { | ||
if ident.to_string() == "hi" { | ||
hi_spans.push(ident.span()); | ||
} | ||
} | ||
} | ||
|
||
if !hi_spans.is_empty() { | ||
return Err(Span::def_site() | ||
.error("hello to you, too!") | ||
.span_note(hi_spans, "found these 'hi's")); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[proc_macro] | ||
pub fn hello(input: TokenStream) -> TokenStream { | ||
if let Err(diag) = parse(input) { | ||
diag.emit(); | ||
} | ||
|
||
TokenStream::new() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2016 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. | ||
|
||
// aux-build:multispan.rs | ||
// ignore-stage1 | ||
|
||
#![feature(proc_macro_non_items)] | ||
|
||
extern crate multispan; | ||
|
||
use multispan::hello; | ||
|
||
fn main() { | ||
// This one emits no error. | ||
hello!(); | ||
|
||
// Exactly one 'hi'. | ||
hello!(hi); //~ ERROR hello to you, too! | ||
|
||
// Now two, back to back. | ||
hello!(hi hi); //~ ERROR hello to you, too! | ||
|
||
// Now three, back to back. | ||
hello!(hi hi hi); //~ ERROR hello to you, too! | ||
|
||
// Now several, with spacing. | ||
hello!(hi hey hi yo hi beep beep hi hi); //~ ERROR hello to you, too! | ||
hello!(hi there, hi how are you? hi... hi.); //~ ERROR hello to you, too! | ||
hello!(whoah. hi di hi di ho); //~ ERROR hello to you, too! | ||
hello!(hi good hi and good bye); //~ ERROR hello to you, too! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
error: hello to you, too! | ||
--> $DIR/multispan.rs:25:5 | ||
| | ||
LL | hello!(hi); //~ ERROR hello to you, too! | ||
| ^^^^^^^^^^^ | ||
| | ||
note: found these 'hi's | ||
--> $DIR/multispan.rs:25:12 | ||
| | ||
LL | hello!(hi); //~ ERROR hello to you, too! | ||
| ^^ | ||
|
||
error: hello to you, too! | ||
--> $DIR/multispan.rs:28:5 | ||
| | ||
LL | hello!(hi hi); //~ ERROR hello to you, too! | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
note: found these 'hi's | ||
--> $DIR/multispan.rs:28:12 | ||
| | ||
LL | hello!(hi hi); //~ ERROR hello to you, too! | ||
| ^^ ^^ | ||
|
||
error: hello to you, too! | ||
--> $DIR/multispan.rs:31:5 | ||
| | ||
LL | hello!(hi hi hi); //~ ERROR hello to you, too! | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
note: found these 'hi's | ||
--> $DIR/multispan.rs:31:12 | ||
| | ||
LL | hello!(hi hi hi); //~ ERROR hello to you, too! | ||
| ^^ ^^ ^^ | ||
|
||
error: hello to you, too! | ||
--> $DIR/multispan.rs:34:5 | ||
| | ||
LL | hello!(hi hey hi yo hi beep beep hi hi); //~ ERROR hello to you, too! | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: found these 'hi's | ||
--> $DIR/multispan.rs:34:12 | ||
| | ||
LL | hello!(hi hey hi yo hi beep beep hi hi); //~ ERROR hello to you, too! | ||
| ^^ ^^ ^^ ^^ ^^ | ||
|
||
error: hello to you, too! | ||
--> $DIR/multispan.rs:35:5 | ||
| | ||
LL | hello!(hi there, hi how are you? hi... hi.); //~ ERROR hello to you, too! | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: found these 'hi's | ||
--> $DIR/multispan.rs:35:12 | ||
| | ||
LL | hello!(hi there, hi how are you? hi... hi.); //~ ERROR hello to you, too! | ||
| ^^ ^^ ^^ ^^ | ||
|
||
error: hello to you, too! | ||
--> $DIR/multispan.rs:36:5 | ||
| | ||
LL | hello!(whoah. hi di hi di ho); //~ ERROR hello to you, too! | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: found these 'hi's | ||
--> $DIR/multispan.rs:36:19 | ||
| | ||
LL | hello!(whoah. hi di hi di ho); //~ ERROR hello to you, too! | ||
| ^^ ^^ | ||
|
||
error: hello to you, too! | ||
--> $DIR/multispan.rs:37:5 | ||
| | ||
LL | hello!(hi good hi and good bye); //~ ERROR hello to you, too! | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: found these 'hi's | ||
--> $DIR/multispan.rs:37:12 | ||
| | ||
LL | hello!(hi good hi and good bye); //~ ERROR hello to you, too! | ||
| ^^ ^^ | ||
|
||
error: aborting due to 7 previous errors | ||
|