From b79a83b4e41858205e5cf057a9f05d299d66859a Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 17 Jun 2018 10:52:02 +0800 Subject: [PATCH 1/4] Suggestion for print --- src/libsyntax_ext/concat.rs | 9 ++++++++- src/test/ui/macros/bad_hello.stderr | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index 6c085528a6632..9ebd2e4be71cb 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -14,6 +14,7 @@ use syntax::ext::build::AstBuilder; use syntax::symbol::Symbol; use syntax_pos; use syntax::tokenstream; +use syntax::print::pprust; use std::string::String; @@ -53,7 +54,13 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, } } _ => { - cx.span_err(e.span, "expected a literal"); + let mut err = cx.struct_span_err(e.span, "expected a literal"); + err.span_suggestion( + e.span, + "consider changing this to", + format!("\"{{}}\", {}", pprust::expr_to_string(&e)) + ); + err.emit(); } } } diff --git a/src/test/ui/macros/bad_hello.stderr b/src/test/ui/macros/bad_hello.stderr index 578ff4ab9d4ad..23486e3cd1c22 100644 --- a/src/test/ui/macros/bad_hello.stderr +++ b/src/test/ui/macros/bad_hello.stderr @@ -2,7 +2,7 @@ error: expected a literal --> $DIR/bad_hello.rs:12:14 | LL | println!(3 + 4); //~ ERROR expected a literal - | ^^^^^ + | ^^^^^ help: consider changing this to: `"{}", 3 + 4` error: aborting due to previous error From c999b253b7587cd647b06582f10d55cbaf65e635 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Wed, 4 Jul 2018 09:01:11 +0800 Subject: [PATCH 2/4] add span note --- src/libsyntax_ext/concat.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index 9ebd2e4be71cb..1f79c1db9950b 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -55,10 +55,17 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, } _ => { let mut err = cx.struct_span_err(e.span, "expected a literal"); + let msg = cx.codemap().span_to_snippet(e.span).unwrap_or_else( + |_| pprust::expr_to_string(&e) + ); err.span_suggestion( e.span, "consider changing this to", - format!("\"{{}}\", {}", pprust::expr_to_string(&e)) + format!("\"{{}}\", {}", msg) + ); + err.span_note( + e.span, + "you might be missing a string literal to format with", ); err.emit(); } From 88f475c8083f668218f93afa88eab4410769cdcd Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Wed, 4 Jul 2018 11:48:56 +0800 Subject: [PATCH 3/4] suggests with whole macro call --- src/libsyntax_ext/concat.rs | 11 +---------- src/test/ui/macros/bad_hello.stderr | 8 +++++++- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index 1f79c1db9950b..2325bcbd6578a 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -14,7 +14,6 @@ use syntax::ext::build::AstBuilder; use syntax::symbol::Symbol; use syntax_pos; use syntax::tokenstream; -use syntax::print::pprust; use std::string::String; @@ -55,15 +54,7 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, } _ => { let mut err = cx.struct_span_err(e.span, "expected a literal"); - let msg = cx.codemap().span_to_snippet(e.span).unwrap_or_else( - |_| pprust::expr_to_string(&e) - ); - err.span_suggestion( - e.span, - "consider changing this to", - format!("\"{{}}\", {}", msg) - ); - err.span_note( + err.span_help( e.span, "you might be missing a string literal to format with", ); diff --git a/src/test/ui/macros/bad_hello.stderr b/src/test/ui/macros/bad_hello.stderr index 23486e3cd1c22..34bc589b5c54b 100644 --- a/src/test/ui/macros/bad_hello.stderr +++ b/src/test/ui/macros/bad_hello.stderr @@ -2,7 +2,13 @@ error: expected a literal --> $DIR/bad_hello.rs:12:14 | LL | println!(3 + 4); //~ ERROR expected a literal - | ^^^^^ help: consider changing this to: `"{}", 3 + 4` + | ^^^^^ + | +help: you might be missing a string literal to format with + --> $DIR/bad_hello.rs:12:14 + | +LL | println!(3 + 4); //~ ERROR expected a literal + | ^^^^^ error: aborting due to previous error From 790c09e8495d74e31073da0a88f1eacdb4a77dee Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Wed, 11 Jul 2018 08:50:21 +0800 Subject: [PATCH 4/4] suggest on new snippet --- src/libsyntax_ext/concat.rs | 58 ++++++++++++++--------------- src/test/ui/macros/bad_hello.stderr | 6 +-- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index 2325bcbd6578a..1c6f0089503e2 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 { +pub fn expand_syntax_ext( + cx: &mut base::ExtCtxt, + sp: syntax_pos::Span, + tts: &[tokenstream::TokenTree], +) -> Box { let es = match base::get_exprs_from_tts(cx, sp, tts) { Some(e) => e, None => return base::DummyResult::expr(sp), @@ -28,35 +29,34 @@ 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"); + } + }, _ => { let mut err = cx.struct_span_err(e.span, "expected a literal"); - err.span_help( + 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 34bc589b5c54b..0bfb060f84416 100644 --- a/src/test/ui/macros/bad_hello.stderr +++ b/src/test/ui/macros/bad_hello.stderr @@ -3,12 +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 - --> $DIR/bad_hello.rs:12:14 | -LL | println!(3 + 4); //~ ERROR expected a literal - | ^^^^^ +LL | println!("{}", 3 + 4); //~ ERROR expected a literal + | ^^^^^^^^^^^ error: aborting due to previous error