diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2018-07-11 12:38:32 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-11 12:38:32 -0600 |
| commit | d2a8a2b34a26b73dfd9e0179d67cff7c8f8ef07d (patch) | |
| tree | fb9d70714923cbcc766b106e20be55bd20da15bf /src | |
| parent | d573fe17786b9c2f5a766498d411d54eee5fa19f (diff) | |
| parent | 790c09e8495d74e31073da0a88f1eacdb4a77dee (diff) | |
| download | rust-d2a8a2b34a26b73dfd9e0179d67cff7c8f8ef07d.tar.gz rust-d2a8a2b34a26b73dfd9e0179d67cff7c8f8ef07d.zip | |
Rollup merge of #51614 - csmoe:lit_sugg, r=estebank
Correct suggestion for println Closes https://github.com/rust-lang/rust/issues/51585 r? @estebank
Diffstat (limited to 'src')
| -rw-r--r-- | src/libsyntax_ext/concat.rs | 63 | ||||
| -rw-r--r-- | src/test/ui/macros/bad_hello.stderr | 4 |
2 files changed, 38 insertions, 29 deletions
diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index 6c085528a66..1c6f0089503 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -12,15 +12,16 @@ use syntax::ast; use syntax::ext::base; use syntax::ext::build::AstBuilder; use syntax::symbol::Symbol; -use syntax_pos; use syntax::tokenstream; +use syntax_pos; use std::string::String; -pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, - sp: syntax_pos::Span, - tts: &[tokenstream::TokenTree]) - -> Box<base::MacResult + 'static> { +pub fn expand_syntax_ext( + cx: &mut base::ExtCtxt, + sp: syntax_pos::Span, + tts: &[tokenstream::TokenTree], +) -> Box<base::MacResult + 'static> { let es = match base::get_exprs_from_tts(cx, sp, tts) { Some(e) => e, None => return base::DummyResult::expr(sp), @@ -28,32 +29,36 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, let mut accumulator = String::new(); for e in es { match e.node { - ast::ExprKind::Lit(ref lit) => { - match lit.node { - ast::LitKind::Str(ref s, _) | - ast::LitKind::Float(ref s, _) | - ast::LitKind::FloatUnsuffixed(ref s) => { - accumulator.push_str(&s.as_str()); - } - ast::LitKind::Char(c) => { - accumulator.push(c); - } - ast::LitKind::Int(i, ast::LitIntType::Unsigned(_)) | - ast::LitKind::Int(i, ast::LitIntType::Signed(_)) | - ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => { - accumulator.push_str(&format!("{}", i)); - } - ast::LitKind::Bool(b) => { - accumulator.push_str(&format!("{}", b)); - } - ast::LitKind::Byte(..) | - ast::LitKind::ByteStr(..) => { - cx.span_err(e.span, "cannot concatenate a byte string literal"); - } + ast::ExprKind::Lit(ref lit) => match lit.node { + ast::LitKind::Str(ref s, _) + | ast::LitKind::Float(ref s, _) + | ast::LitKind::FloatUnsuffixed(ref s) => { + accumulator.push_str(&s.as_str()); } - } + ast::LitKind::Char(c) => { + accumulator.push(c); + } + ast::LitKind::Int(i, ast::LitIntType::Unsigned(_)) + | ast::LitKind::Int(i, ast::LitIntType::Signed(_)) + | ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => { + accumulator.push_str(&format!("{}", i)); + } + ast::LitKind::Bool(b) => { + accumulator.push_str(&format!("{}", b)); + } + ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => { + cx.span_err(e.span, "cannot concatenate a byte string literal"); + } + }, _ => { - cx.span_err(e.span, "expected a literal"); + let mut err = cx.struct_span_err(e.span, "expected a literal"); + let snippet = cx.codemap().span_to_snippet(e.span).unwrap(); + err.span_suggestion( + e.span, + "you might be missing a string literal to format with", + format!("\"{{}}\", {}", snippet), + ); + err.emit(); } } } diff --git a/src/test/ui/macros/bad_hello.stderr b/src/test/ui/macros/bad_hello.stderr index 578ff4ab9d4..0bfb060f844 100644 --- a/src/test/ui/macros/bad_hello.stderr +++ b/src/test/ui/macros/bad_hello.stderr @@ -3,6 +3,10 @@ error: expected a literal | LL | println!(3 + 4); //~ ERROR expected a literal | ^^^^^ +help: you might be missing a string literal to format with + | +LL | println!("{}", 3 + 4); //~ ERROR expected a literal + | ^^^^^^^^^^^ error: aborting due to previous error |
