-
Notifications
You must be signed in to change notification settings - Fork 13.3k
make panictry!
private to libsyntax
#56897
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ use self::State::*; | |
|
||
use rustc_data_structures::thin_vec::ThinVec; | ||
|
||
use errors::DiagnosticBuilder; | ||
use syntax::ast; | ||
use syntax::ext::base; | ||
use syntax::ext::base::*; | ||
|
@@ -51,6 +52,34 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, | |
feature_gate::EXPLAIN_ASM); | ||
} | ||
|
||
let mut inline_asm = match parse_inline_asm(cx, sp, tts) { | ||
Ok(Some(inline_asm)) => inline_asm, | ||
Ok(None) => return DummyResult::expr(sp), | ||
Err(mut err) => { | ||
err.emit(); | ||
return DummyResult::expr(sp); | ||
} | ||
}; | ||
|
||
// If there are no outputs, the inline assembly is executed just for its side effects, | ||
// so ensure that it is volatile | ||
if inline_asm.outputs.is_empty() { | ||
inline_asm.volatile = true; | ||
} | ||
|
||
MacEager::expr(P(ast::Expr { | ||
id: ast::DUMMY_NODE_ID, | ||
node: ast::ExprKind::InlineAsm(P(inline_asm)), | ||
span: sp, | ||
attrs: ThinVec::new(), | ||
})) | ||
} | ||
|
||
fn parse_inline_asm<'a>( | ||
cx: &mut ExtCtxt<'a>, | ||
sp: Span, | ||
tts: &[tokenstream::TokenTree], | ||
) -> Result<Option<ast::InlineAsm>, DiagnosticBuilder<'a>> { | ||
// Split the tts before the first colon, to avoid `asm!("x": y)` being | ||
// parsed as `asm!(z)` with `z = "x": y` which is type ascription. | ||
let first_colon = tts.iter() | ||
|
@@ -80,22 +109,33 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, | |
if asm_str_style.is_some() { | ||
// If we already have a string with instructions, | ||
// ending up in Asm state again is an error. | ||
span_err!(cx, sp, E0660, "malformed inline assembly"); | ||
return DummyResult::expr(sp); | ||
return Err(struct_span_err!( | ||
cx.parse_sess.span_diagnostic, | ||
sp, | ||
E0660, | ||
"malformed inline assembly" | ||
)); | ||
} | ||
// Nested parser, stop before the first colon (see above). | ||
let mut p2 = cx.new_parser_from_tts(&tts[..first_colon]); | ||
let (s, style) = match expr_to_string(cx, | ||
panictry!(p2.parse_expr()), | ||
"inline assembly must be a string literal") { | ||
Some((s, st)) => (s, st), | ||
// let compilation continue | ||
None => return DummyResult::expr(sp), | ||
}; | ||
|
||
if p2.token == token::Eof { | ||
let mut err = | ||
cx.struct_span_err(sp, "macro requires a string literal as an argument"); | ||
err.span_label(sp, "string literal required"); | ||
return Err(err); | ||
} | ||
|
||
let expr = p2.parse_expr()?; | ||
let (s, style) = | ||
match expr_to_string(cx, expr, "inline assembly must be a string literal") { | ||
Some((s, st)) => (s, st), | ||
None => return Ok(None), | ||
}; | ||
|
||
// This is most likely malformed. | ||
if p2.token != token::Eof { | ||
let mut extra_tts = panictry!(p2.parse_all_token_trees()); | ||
let mut extra_tts = p2.parse_all_token_trees()?; | ||
extra_tts.extend(tts[first_colon..].iter().cloned()); | ||
p = parse::stream_to_parser(cx.parse_sess, extra_tts.into_iter().collect()); | ||
} | ||
|
@@ -105,18 +145,17 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, | |
} | ||
Outputs => { | ||
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep { | ||
|
||
if !outputs.is_empty() { | ||
p.eat(&token::Comma); | ||
} | ||
|
||
let (constraint, _str_style) = panictry!(p.parse_str()); | ||
let (constraint, _) = p.parse_str()?; | ||
|
||
let span = p.prev_span; | ||
|
||
panictry!(p.expect(&token::OpenDelim(token::Paren))); | ||
let out = panictry!(p.parse_expr()); | ||
panictry!(p.expect(&token::CloseDelim(token::Paren))); | ||
p.expect(&token::OpenDelim(token::Paren))?; | ||
let expr = p.parse_expr()?; | ||
p.expect(&token::CloseDelim(token::Paren))?; | ||
|
||
// Expands a read+write operand into two operands. | ||
// | ||
|
@@ -143,20 +182,19 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, | |
let is_indirect = constraint_str.contains("*"); | ||
outputs.push(ast::InlineAsmOutput { | ||
constraint: output.unwrap_or(constraint), | ||
expr: out, | ||
expr, | ||
is_rw, | ||
is_indirect, | ||
}); | ||
} | ||
} | ||
Inputs => { | ||
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep { | ||
|
||
if !inputs.is_empty() { | ||
p.eat(&token::Comma); | ||
} | ||
|
||
let (constraint, _str_style) = panictry!(p.parse_str()); | ||
let (constraint, _) = p.parse_str()?; | ||
|
||
if constraint.as_str().starts_with("=") { | ||
span_err!(cx, p.prev_span, E0662, | ||
|
@@ -166,21 +204,20 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, | |
"input operand constraint contains '+'"); | ||
} | ||
|
||
panictry!(p.expect(&token::OpenDelim(token::Paren))); | ||
let input = panictry!(p.parse_expr()); | ||
panictry!(p.expect(&token::CloseDelim(token::Paren))); | ||
p.expect(&token::OpenDelim(token::Paren))?; | ||
let input = p.parse_expr()?; | ||
p.expect(&token::CloseDelim(token::Paren))?; | ||
|
||
inputs.push((constraint, input)); | ||
} | ||
} | ||
Clobbers => { | ||
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep { | ||
|
||
if !clobs.is_empty() { | ||
p.eat(&token::Comma); | ||
} | ||
|
||
let (s, _str_style) = panictry!(p.parse_str()); | ||
let (s, _) = p.parse_str()?; | ||
|
||
if OPTIONS.iter().any(|&opt| s == opt) { | ||
cx.span_warn(p.prev_span, "expected a clobber, found an option"); | ||
|
@@ -193,7 +230,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, | |
} | ||
} | ||
Options => { | ||
let (option, _str_style) = panictry!(p.parse_str()); | ||
let (option, _) = p.parse_str()?; | ||
|
||
if option == "volatile" { | ||
// Indicates that the inline assembly has side effects | ||
|
@@ -234,26 +271,15 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, | |
} | ||
} | ||
|
||
// If there are no outputs, the inline assembly is executed just for its side effects, | ||
// so ensure that it is volatile | ||
if outputs.is_empty() { | ||
volatile = true; | ||
} | ||
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. where did this logic (about 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. It's in the main expansion function now. 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. OK. |
||
|
||
MacEager::expr(P(ast::Expr { | ||
id: ast::DUMMY_NODE_ID, | ||
node: ast::ExprKind::InlineAsm(P(ast::InlineAsm { | ||
asm, | ||
asm_str_style: asm_str_style.unwrap(), | ||
outputs, | ||
inputs, | ||
clobbers: clobs, | ||
volatile, | ||
alignstack, | ||
dialect, | ||
ctxt: cx.backtrace(), | ||
})), | ||
span: sp, | ||
attrs: ThinVec::new(), | ||
Ok(Some(ast::InlineAsm { | ||
asm, | ||
asm_str_style: asm_str_style.unwrap(), | ||
outputs, | ||
inputs, | ||
clobbers: clobs, | ||
volatile, | ||
alignstack, | ||
dialect, | ||
ctxt: cx.backtrace(), | ||
})) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
use syntax::ast::*; | ||
use errors::DiagnosticBuilder; | ||
use syntax::ast::{self, *}; | ||
use syntax::source_map::Spanned; | ||
use syntax::ext::base::*; | ||
use syntax::ext::build::AstBuilder; | ||
use syntax::parse::token; | ||
use syntax::print::pprust; | ||
use syntax::ptr::P; | ||
use syntax::symbol::Symbol; | ||
use syntax::tokenstream::{TokenStream, TokenTree}; | ||
use syntax_pos::{Span, DUMMY_SP}; | ||
|
@@ -13,33 +15,18 @@ pub fn expand_assert<'cx>( | |
sp: Span, | ||
tts: &[TokenTree], | ||
) -> Box<dyn MacResult + 'cx> { | ||
let mut parser = cx.new_parser_from_tts(tts); | ||
|
||
if parser.token == token::Eof { | ||
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. I guess we have some similar code duplication here already, though. |
||
cx.struct_span_err(sp, "macro requires a boolean expression as an argument") | ||
.span_label(sp, "boolean expression required") | ||
.emit(); | ||
return DummyResult::expr(sp); | ||
} | ||
|
||
let cond_expr = panictry!(parser.parse_expr()); | ||
let custom_msg_args = if parser.eat(&token::Comma) { | ||
let ts = parser.parse_tokens(); | ||
if !ts.is_empty() { | ||
Some(ts) | ||
} else { | ||
None | ||
let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) { | ||
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. Perhaps we should use This could be
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. We could, but doesn't this require migrating to the new edition? Also, I personally prefer the separation of the parsing logic and the expansion logic. 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. Seems fine. We can leave it like this. I agree separating the parsing/expansion logic makes sense. |
||
Ok(assert) => assert, | ||
Err(mut err) => { | ||
err.emit(); | ||
return DummyResult::expr(sp); | ||
} | ||
} else { | ||
None | ||
}; | ||
|
||
let sp = sp.apply_mark(cx.current_expansion.mark); | ||
let panic_call = Mac_ { | ||
path: Path::from_ident(Ident::new(Symbol::intern("panic"), sp)), | ||
tts: if let Some(ts) = custom_msg_args { | ||
ts.into() | ||
} else { | ||
tts: custom_message.unwrap_or_else(|| { | ||
TokenStream::from(TokenTree::Token( | ||
DUMMY_SP, | ||
token::Literal( | ||
|
@@ -49,8 +36,8 @@ pub fn expand_assert<'cx>( | |
))), | ||
None, | ||
), | ||
)).into() | ||
}, | ||
)) | ||
}).into(), | ||
delim: MacDelimiter::Parenthesis, | ||
}; | ||
let if_expr = cx.expr_if( | ||
|
@@ -67,3 +54,36 @@ pub fn expand_assert<'cx>( | |
); | ||
MacEager::expr(if_expr) | ||
} | ||
|
||
struct Assert { | ||
cond_expr: P<ast::Expr>, | ||
custom_message: Option<TokenStream>, | ||
} | ||
|
||
fn parse_assert<'a>( | ||
cx: &mut ExtCtxt<'a>, | ||
sp: Span, | ||
tts: &[TokenTree] | ||
) -> Result<Assert, DiagnosticBuilder<'a>> { | ||
let mut parser = cx.new_parser_from_tts(tts); | ||
|
||
if parser.token == token::Eof { | ||
let mut err = cx.struct_span_err(sp, "macro requires a boolean expression as an argument"); | ||
err.span_label(sp, "boolean expression required"); | ||
return Err(err); | ||
} | ||
|
||
Ok(Assert { | ||
cond_expr: parser.parse_expr()?, | ||
custom_message: if parser.eat(&token::Comma) { | ||
let ts = parser.parse_tokens(); | ||
if !ts.is_empty() { | ||
Some(ts) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
}, | ||
}) | ||
} |
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.
why are we handling EOF specially here compared to how we did it before? Is this to preserve the error message?
(if so, it seems a bit unfortunate that we have to duplicate the error here)
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.
There isn't a span associated with the EOF error, so the diagnostics are very poor unless we specifically check for it. See #55547 for an example..
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.
OK, seems like a problem, but an orthogonal one.