Skip to content
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

Correct suggestion for println #51614

Merged
merged 4 commits into from
Jul 11, 2018
Merged

Correct suggestion for println #51614

merged 4 commits into from
Jul 11, 2018

Conversation

csmoe
Copy link
Member

@csmoe csmoe commented Jun 18, 2018

Closes #51585
r? @estebank

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 18, 2018
@@ -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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this suggestion as-is could be a little confusing: selecting the entire expression and suggesting it be replaced with println!("{}", 3 + 4) would give more helpful context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to avoid having to play with the spans can be to make the help message long enough that it no longer is suggested in place, instead showing the new code on its own:

help: you might be missing a string literal to format with
   |
LL |     println!("{}", 3 + 4); //~ ERROR expected a literal
   |              ^^^^^^^^^^^

err.span_suggestion(
e.span,
"consider changing this to",
format!("\"{{}}\", {}", pprust::expr_to_string(&e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use span to snippet here and only use expression to string only if that call fails? We want to preserve the code as is, like for example whitespace.

"consider changing this to",
format!("\"{{}}\", {}", pprust::expr_to_string(&e))
);
err.emit();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a note explaining that a format string is always needed?

@pietroalbini pietroalbini added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 25, 2018
@TimNN
Copy link
Contributor

TimNN commented Jul 3, 2018

Ping form triage, @csmoe: It looks like there are some comments on your PR, do you think you'll be able to address those?

@csmoe
Copy link
Member Author

csmoe commented Jul 3, 2018

@TimNN testing locally, more commits later.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 4, 2018
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:07:19] 
[00:07:19] error: Could not compile `syntax_ext`.
[00:07:19] 
[00:07:19] Caused by:
[00:07:19]   process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc --crate-name syntax_ext libsyntax_ext/lib.rs --color always --error-format json --crate-type dylib --emit=dep-info,link -C prefer-dynamic -C opt-level=2 -C metadata=152376803e8694f9 -C extra-filename=-152376803e8694f9 --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/release/deps --extern fmt_macros=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libfmt_macros-6ad9c4f0e3eb0853.so --extern proc_macro=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libproc_macro-c5970c5befc4b045.so --extern rustc_data_structures=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_data_structures-25582ce13d3618ea.so --extern rustc_errors=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_errors-e036f8f5b9204e52.so --extern rustc_target=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_target-066098b54e835a1f.so --extern syntax=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libsyntax-a1b02e0d020520ac.so --extern syntax_pos=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libsyntax_pos-f36f6cb494d373be.so` (exit code: 101)
[00:13:02] error: build failed
[00:13:02] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "x86_64-unknown-linux-gnu" "-j" "4" "--release" "--locked" "--color" "always" "--features" " jemalloc" "--manifest-path" "/checkout/src/rustc/Cargo.toml" "--message-format" "json"
[00:13:02] expected success, got: exit code: 101
[00:13:02] expected success, got: exit code: 101
[00:13:02] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1114:9
[00:13:02] travis_fold:end:stage0-rustc

[00:13:02] travis_time:end:stage0-rustc:start=1530709661509315972,finish=1530710156097007040,duration=494587691068


[00:13:02] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap build
[00:13:02] Build completed unsuccessfully in 0:08:26
[00:13:02] Makefile:28: recipe for target 'all' failed
[00:13:02] make: *** [all] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:02212070
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
travis_time:end:018d5c1f:start=1530710156837729300,finish=1530710156843856072,duration=6126772
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:02b33e7f
$ head -30 ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
head: cannot open ‘./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers’ for reading: No such file or directory
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:003d4a87
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@rust-highfive

This comment has been minimized.

@TimNN
Copy link
Contributor

TimNN commented Jul 10, 2018

Ping from triage, @estebank ! This PR requires your review.

@@ -53,7 +53,12 @@ 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_help(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

if let Some(snippet) = cx.codemap().span_to_snippet(span) { 
    err.span_suggestion(
        e.span,
        "you might be missing a string literal to format with",
        format!("\"{{}}\", {}", snippet);
    );
}

Which will give you the following output:


error: expected a literal
  --> $DIR/bad_hello.rs:12:14
   |
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
   |              ^^^^^^^^^^^

@estebank estebank added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 10, 2018
@rust-highfive

This comment has been minimized.

@estebank
Copy link
Contributor

Oh, sorry, it is a Result, not an Option :-/ (I think it changed "recently")

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 11, 2018

📌 Commit 790c09e has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 11, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jul 11, 2018
bors added a commit that referenced this pull request Jul 11, 2018
Rollup of 14 pull requests

Successful merges:

 - #51614 (Correct suggestion for println)
 - #51952 ( hygiene: Decouple transparencies from expansion IDs)
 - #52193 (step_by: leave time of item skip unspecified)
 - #52207 (improve error message shown for unsafe operations)
 - #52223 (Deny bare trait objects in in src/liballoc)
 - #52224 (Deny bare trait objects in in src/libsyntax)
 - #52239 (Remove sync::Once::call_once 'static bound)
 - #52247 (Deny bare trait objects in in src/librustc)
 - #52248 (Deny bare trait objects in in src/librustc_allocator)
 - #52252 (Deny bare trait objects in in src/librustc_codegen_llvm)
 - #52253 (Deny bare trait objects in in src/librustc_data_structures)
 - #52254 (Deny bare trait objects in in src/librustc_metadata)
 - #52261 (Deny bare trait objects in in src/libpanic_unwind)
 - #52265 (Deny bare trait objects in in src/librustc_codegen_utils)

Failed merges:

r? @ghost
@bors bors merged commit 790c09e into rust-lang:master Jul 11, 2018
@csmoe csmoe deleted the lit_sugg branch July 12, 2018 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants