-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
reduce the amount of panics in {TokenStream, Literal}::from_str calls
#147859
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
Open
cyrgani
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
cyrgani:nonfatal-tokenstream-parse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashMap; | |
| use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan, PResult}; | ||
| use rustc_parse::lexer::{StripTokens, nfc_normalize}; | ||
| use rustc_parse::parser::Parser; | ||
| use rustc_parse::{exp, new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal}; | ||
| use rustc_parse::{exp, new_parser_from_source_str, source_str_to_stream}; | ||
| use rustc_proc_macro::bridge::{ | ||
| DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server, | ||
| }; | ||
|
|
@@ -483,35 +483,42 @@ impl server::FreeFunctions for Rustc<'_, '_> { | |
| self.psess().file_depinfo.borrow_mut().insert(Symbol::intern(path)); | ||
| } | ||
|
|
||
| fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span, Self::Symbol>, ()> { | ||
| fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span, Self::Symbol>, String> { | ||
| const ERROR_MSG: &str = "cannot parse string into literal"; | ||
|
|
||
| let name = FileName::proc_macro_source_code(s); | ||
|
|
||
| let mut parser = unwrap_or_emit_fatal(new_parser_from_source_str( | ||
| self.psess(), | ||
| name, | ||
| s.to_owned(), | ||
| StripTokens::Nothing, | ||
| )); | ||
| let mut parser = | ||
| new_parser_from_source_str(self.psess(), name, s.to_owned(), StripTokens::Nothing) | ||
| .map_err(|diags| { | ||
| let mut messages = diags.into_iter().map(Diag::cancel_into_message).flatten(); | ||
| if let Some(msg) = messages.next() { | ||
| messages.for_each(drop); | ||
| format!("{ERROR_MSG}: {msg}") | ||
| } else { | ||
| ERROR_MSG.to_string() | ||
| } | ||
| })?; | ||
|
|
||
| let first_span = parser.token.span.data(); | ||
| let minus_present = parser.eat(exp!(Minus)); | ||
|
|
||
| let lit_span = parser.token.span.data(); | ||
| let token::Literal(mut lit) = parser.token.kind else { | ||
| return Err(()); | ||
| return Err("not a literal".to_string()); | ||
| }; | ||
|
|
||
| // Check no comment or whitespace surrounding the (possibly negative) | ||
| // literal, or more tokens after it. | ||
| if (lit_span.hi.0 - first_span.lo.0) as usize != s.len() { | ||
| return Err(()); | ||
| return Err("comment or whitespace around literal".to_string()); | ||
| } | ||
|
|
||
| if minus_present { | ||
| // If minus is present, check no comment or whitespace in between it | ||
| // and the literal token. | ||
| if first_span.hi.0 != lit_span.lo.0 { | ||
| return Err(()); | ||
| return Err("comment or whitespace after minus".to_string()); | ||
| } | ||
|
|
||
| // Check literal is a kind we allow to be negated in a proc macro token. | ||
|
|
@@ -525,7 +532,9 @@ impl server::FreeFunctions for Rustc<'_, '_> { | |
| | token::LitKind::ByteStrRaw(_) | ||
| | token::LitKind::CStr | ||
| | token::LitKind::CStrRaw(_) | ||
| | token::LitKind::Err(_) => return Err(()), | ||
| | token::LitKind::Err(_) => { | ||
| return Err("non-numeric literal may not be negated".to_string()); | ||
| } | ||
| token::LitKind::Integer | token::LitKind::Float => {} | ||
| } | ||
|
|
||
|
|
@@ -562,13 +571,24 @@ impl server::TokenStream for Rustc<'_, '_> { | |
| stream.is_empty() | ||
| } | ||
|
|
||
| fn from_str(&mut self, src: &str) -> Self::TokenStream { | ||
| unwrap_or_emit_fatal(source_str_to_stream( | ||
| fn from_str(&mut self, src: &str) -> Result<Self::TokenStream, String> { | ||
| const ERROR_MSG: &str = "cannot parse string into token stream"; | ||
|
|
||
| source_str_to_stream( | ||
| self.psess(), | ||
| FileName::proc_macro_source_code(src), | ||
| src.to_string(), | ||
| Some(self.call_site), | ||
| )) | ||
| ) | ||
| .map_err(|diags| { | ||
| let mut messages = diags.into_iter().map(Diag::cancel_into_message).flatten(); | ||
| if let Some(msg) = messages.next() { | ||
| messages.for_each(drop); | ||
| format!("{ERROR_MSG}: {msg}") | ||
| } else { | ||
| ERROR_MSG.to_string() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here |
||
| } | ||
| }) | ||
| } | ||
|
|
||
| fn to_string(&mut self, stream: &Self::TokenStream) -> String { | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,13 +1,9 @@ | ||
| //@ proc-macro: invalid-punct-ident.rs | ||
| //@ needs-unwind proc macro panics to report errors | ||
| //@ check-pass | ||
|
|
||
| #[macro_use] | ||
| extern crate invalid_punct_ident; | ||
|
|
||
| lexer_failure!(); | ||
| //~^ ERROR proc macro panicked | ||
| //~| ERROR unexpected closing delimiter: `)` | ||
cyrgani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fn main() { | ||
| let _recovery_witness: () = 0; //~ ERROR mismatched types | ||
| } | ||
| fn main() {} | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Is this branch reachable?
If it is reachable, I'd like to see a test for it
If it is not reachable, I think an ICE makes more sense here