-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unstable Literal::subspan(). #56120
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2018 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> { | ||
if let Some(TokenTree::Literal(lit)) = input.into_iter().next() { | ||
let mut spans = vec![]; | ||
let string = lit.to_string(); | ||
for hi in string.matches("hi") { | ||
let index = hi.as_ptr() as usize - string.as_ptr() as usize; | ||
let subspan = lit.subspan(index..(index + hi.len())).unwrap(); | ||
spans.push(subspan); | ||
} | ||
|
||
if !spans.is_empty() { | ||
Err(Span::call_site().error("found 'hi's").span_note(spans, "here")) | ||
} else { | ||
Ok(()) | ||
} | ||
} else { | ||
Err(Span::call_site().error("invalid input: expected string literal")) | ||
} | ||
} | ||
|
||
#[proc_macro] | ||
pub fn subspan(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,37 @@ | ||
// Copyright 2018 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:subspan.rs | ||
// ignore-stage1 | ||
|
||
extern crate subspan; | ||
|
||
use subspan::subspan; | ||
|
||
// This one emits no error. | ||
subspan!(""); | ||
|
||
// Exactly one 'hi'. | ||
subspan!("hi"); //~ ERROR found 'hi's | ||
|
||
// Now two, back to back. | ||
subspan!("hihi"); //~ ERROR found 'hi's | ||
|
||
// Now three, back to back. | ||
subspan!("hihihi"); //~ ERROR found 'hi's | ||
|
||
// Now several, with spacing. | ||
subspan!("why I hide? hi!"); //~ ERROR found 'hi's | ||
subspan!("hey, hi, hidy, hidy, hi hi"); //~ ERROR found 'hi's | ||
subspan!("this is a hi, and this is another hi"); //~ ERROR found 'hi's | ||
subspan!("how are you this evening"); //~ ERROR found 'hi's | ||
subspan!("this is highly eradic"); //~ ERROR found 'hi's | ||
|
||
fn main() { } |
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,98 @@ | ||
error: found 'hi's | ||
--> $DIR/subspan.rs:22:1 | ||
| | ||
LL | subspan!("hi"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:22:11 | ||
| | ||
LL | subspan!("hi"); //~ ERROR found 'hi's | ||
| ^^ | ||
|
||
error: found 'hi's | ||
--> $DIR/subspan.rs:25:1 | ||
| | ||
LL | subspan!("hihi"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:25:11 | ||
| | ||
LL | subspan!("hihi"); //~ ERROR found 'hi's | ||
| ^^^^ | ||
|
||
error: found 'hi's | ||
--> $DIR/subspan.rs:28:1 | ||
| | ||
LL | subspan!("hihihi"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:28:11 | ||
| | ||
LL | subspan!("hihihi"); //~ ERROR found 'hi's | ||
| ^^^^^^ | ||
|
||
error: found 'hi's | ||
--> $DIR/subspan.rs:31:1 | ||
| | ||
LL | subspan!("why I hide? hi!"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:31:17 | ||
| | ||
LL | subspan!("why I hide? hi!"); //~ ERROR found 'hi's | ||
| ^^ ^^ | ||
|
||
error: found 'hi's | ||
--> $DIR/subspan.rs:32:1 | ||
| | ||
LL | subspan!("hey, hi, hidy, hidy, hi hi"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:32:16 | ||
| | ||
LL | subspan!("hey, hi, hidy, hidy, hi hi"); //~ ERROR found 'hi's | ||
| ^^ ^^ ^^ ^^ ^^ | ||
|
||
error: found 'hi's | ||
--> $DIR/subspan.rs:33:1 | ||
| | ||
LL | subspan!("this is a hi, and this is another hi"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:33:12 | ||
| | ||
LL | subspan!("this is a hi, and this is another hi"); //~ ERROR found 'hi's | ||
| ^^ ^^ ^^ ^^ | ||
|
||
error: found 'hi's | ||
--> $DIR/subspan.rs:34:1 | ||
| | ||
LL | subspan!("how are you this evening"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:34:24 | ||
| | ||
LL | subspan!("how are you this evening"); //~ ERROR found 'hi's | ||
| ^^ | ||
|
||
error: found 'hi's | ||
--> $DIR/subspan.rs:35:1 | ||
| | ||
LL | subspan!("this is highly eradic"); //~ ERROR found 'hi's | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: here | ||
--> $DIR/subspan.rs:35:12 | ||
| | ||
LL | subspan!("this is highly eradic"); //~ ERROR found 'hi's | ||
| ^^ ^^ | ||
|
||
error: aborting due to 8 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is correct.
Literal::to_string
should show the exact source contents, if the literal isn't synthetic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct. Try:
Edit: This may be different if the
Literal
comes from aTokenStream
parsed from source. Nevertheless, this method needs to be correct regardless of where theLiteral
comes from.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's synthetic then the span doesn't matter, I was talking about
Literal
s from the source. But also, we shouldn't be usingescape_unicode
like that.