diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2018-07-14 23:50:08 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2018-08-06 20:26:21 -0700 |
| commit | 4862eee8b762257cf28bddc41d9bb709d1fb9359 (patch) | |
| tree | bbac725e00dc03816a310717106e09add7a2bde7 /src/libsyntax | |
| parent | 73c78734bae8f2947a4bfdeabebeeb84ccf0b0e1 (diff) | |
| download | rust-4862eee8b762257cf28bddc41d9bb709d1fb9359.tar.gz rust-4862eee8b762257cf28bddc41d9bb709d1fb9359.zip | |
Suggest comma when writing `println!("{}" a);`
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/tt/macro_rules.rs | 27 | ||||
| -rw-r--r-- | src/libsyntax/tokenstream.rs | 25 |
2 files changed, 51 insertions, 1 deletions
diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index c9ec2c7d1e8..e7e94614ac8 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -174,7 +174,32 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt, } let best_fail_msg = parse_failure_msg(best_fail_tok.expect("ran no matchers")); - cx.span_err(best_fail_spot.substitute_dummy(sp), &best_fail_msg); + let mut err = cx.struct_span_err(best_fail_spot.substitute_dummy(sp), &best_fail_msg); + + // Check whether there's a missing comma in this macro call, like `println!("{}" a);` + if let Some((arg, comma_span)) = arg.add_comma() { + for lhs in lhses { // try each arm's matchers + let lhs_tt = match *lhs { + quoted::TokenTree::Delimited(_, ref delim) => &delim.tts[..], + _ => cx.span_bug(sp, "malformed macro lhs") + }; + match TokenTree::parse(cx, lhs_tt, arg.clone()) { + Success(_) => { + if comma_span == DUMMY_SP { + err.note("you might be missing a comma"); + } else { + err.span_suggestion_short( + comma_span, + "missing comma here", + ",".to_string(), + ); + } + } + _ => {} + } + } + } + err.emit(); cx.trace_macros_diag(); DummyResult::any(sp) } diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 1a4236b280b..ef914e8de53 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -182,6 +182,31 @@ pub struct TokenStream { kind: TokenStreamKind, } +impl TokenStream { + /// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream` + /// separating the two arguments with a comma for diagnostic suggestions. + pub(crate) fn add_comma(&self) -> Option<(TokenStream, Span)> { + // Used ot suggest if a user writes `println!("{}" a);` + if let TokenStreamKind::Stream(ref slice) = self.kind { + if slice.len() == 2 { + let comma_span = match slice[0] { + TokenStream { kind: TokenStreamKind::Tree(TokenTree::Token(sp, _)) } | + TokenStream { kind: TokenStreamKind::Tree(TokenTree::Delimited(sp, _)) } => { + sp.shrink_to_hi() + } + _ => DUMMY_SP, + }; + let comma = TokenStream { + kind: TokenStreamKind::Tree(TokenTree::Token(comma_span, token::Comma)), + }; + let slice = RcSlice::new(vec![slice[0].clone(), comma, slice[1].clone()]); + return Some((TokenStream { kind: TokenStreamKind::Stream(slice) }, comma_span)); + } + } + None + } +} + #[derive(Clone, Debug)] enum TokenStreamKind { Empty, |
