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

Hint correct extern constant syntax #43720

Merged
merged 3 commits into from Aug 10, 2017
Merged

Conversation

kornelski
Copy link
Contributor

@kornelski kornelski commented Aug 7, 2017

Error message for extern "C" { const …} is terse, and the right syntax is hard to guess given unfortunate difference between meaning of static in C and Rust.

I've added a hint for the right syntax.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

return Err(self.span_fatal(self.span, "extern items cannot be `const`"));
let mut err = self.span_fatal(self.span, "extern items cannot be `const`");
err.help("use `static` instead");
return Err(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

This can use better recovery - you can report a non-fatal error and then return the same successful result as self.check_keyword(keywords::Static) does.
You'll have to move self.expect_keyword(keywords::Static) from parse_item_foreign_static though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be better, but I just wanted to improve the error message. I don't feel the need to rework the error handling here.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could also use span_suggestion instead of help to replace the const with static

@oli-obk
Copy link
Contributor

oli-obk commented Aug 8, 2017

I think you should also add an ui test showing the new error message in action. Then again, ths change seems simple enough.

@arielb1 arielb1 assigned petrochenkov and unassigned nikomatsakis Aug 8, 2017
@arielb1 arielb1 added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 8, 2017
@arielb1
Copy link
Contributor

arielb1 commented Aug 8, 2017

r? @petrochenkov

@kornelski
Copy link
Contributor Author

kornelski commented Aug 8, 2017

I don't know if I need to change any tests, or how. I've ran

python x.py test src/rustc --stage 1

it melted my CPU for 15 minutes and showed "running 0 tests", so I'm not sure if that worked.

I've tried span_suggestion(), but it formats the message with "hint: {}: static", and I'm unable to come up with a sentence that sounds natural this way.

This is a too minor thing, and Rust compilation takes way too long for me to touch this any further.

@estebank
Copy link
Contributor

estebank commented Aug 8, 2017

@pornel you'll have to add a new test to an appropriate directory under src/test/ui. Once you create the .rs test file you can run ./x.py test src/test/ui --stage 1 to generate the outputs and use update-references.sh to copy the output of the compilation of your new .rs file to an .stderr file. If you rerun the tests, they all should pass now. Add the new file and verify the contents of the new files to make sure it is your expected output.

@kornelski
Copy link
Contributor Author

@estebank thank you for the instructions. The ui tests pass, even though I haven't changed them. I guess the help text is ignored in tests?

@estebank
Copy link
Contributor

estebank commented Aug 8, 2017

@pornel none of the existing tests is hitting your new code. You should create a new test that will trigger the new output.

@petrochenkov
Copy link
Contributor

@pornel
petrochenkov@26fa755 shows how to do recovery and correctly populate the expected token set (feel free to reuse it for this PR).

@kornelski
Copy link
Contributor Author

@petrochenkov Thank you for improving this.

if self.token.is_keyword(keywords::Const) {
self.diagnostic()
.struct_span_err(self.span, "extern items cannot be `const`")
.span_label(self.span, "use `static` instead").emit();
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be .span_suggestion(self.span, "instead try using", "static".to_owned()), which would allow IDEs and tools to automatically replace it.

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Aug 10, 2017

📌 Commit cabc9be has been approved by petrochenkov

@bors
Copy link
Contributor

bors commented Aug 10, 2017

⌛ Testing commit cabc9be with merge b617960...

bors added a commit that referenced this pull request Aug 10, 2017
Hint correct extern constant syntax

Error message for `extern "C" { const …}` is terse, and the right syntax is hard to guess given unfortunate difference between meaning of `static` in C and Rust.

I've added a hint for the right syntax.
@bors
Copy link
Contributor

bors commented Aug 10, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: petrochenkov
Pushing b617960 to master...

@bors bors merged commit cabc9be into rust-lang:master Aug 10, 2017
@kornelski kornelski deleted the staticconst branch August 10, 2017 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants