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

Deduplicate identifier printing a bit #69387

Merged
merged 1 commit into from
Feb 26, 2020
Merged

Conversation

petrochenkov
Copy link
Contributor

#67010 introduced a couple more subtly different ways to print an identifier.
This PR attempts to restore the order.

The most basic identifier printing interface is Formatter-based now, so Strings are not allocated unless required.

r? @Mark-Simulacrum

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 22, 2020
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

r=me with comments resolved, or I can review again if you have feedback you feel I should take a look at

src/librustc_span/symbol.rs Show resolved Hide resolved
src/librustc_span/symbol.rs Show resolved Hide resolved
@@ -2,7 +2,7 @@ mod a {}

macro_rules! m {
() => {
use a::$crate; //~ ERROR unresolved import `a::$crate`
use a::$crate; //~ ERROR unresolved import `a::crate`
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit surprised to see this, though I didn't try to trace the calls to the new infrastructure. I think it's a correct change though strange (to me) that how we print paths differs from how we print the errors below, I guess. But ultimately seems good -- it's a more correct error AFAICT.

Out of interest, in a cross-crate use, does the expansion break down and expand to a::::crate_name here?

Copy link
Contributor Author

@petrochenkov petrochenkov Feb 23, 2020

Choose a reason for hiding this comment

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

In one case it happens to be printed through ident (ast::Ident) and in another through ident.name (ast::Name aka Symbol).

This is all annoying.
Previously format!("{}", ident) and format!("{}", ident.name) were interchangeable, but in #67010 @estebank wanted to print identifiers in error messages as raw if they are likely raw, so they are now different.

Now @olegnn in #68963 wants to print Symbols in the same way, with rawness guessing.
This, however needs to be opt-in because Symbol is not generally an identifier, raw or not, it's simply an interned string.

There are seems to be three cases for identifier printing with different requirements:

  • Token printing. It is important for this one to be precise, no rawness guessing, no $crate conversions. This is how proc macros know what tokens are passed to them.
    This is also used for error messages in lexer/parser where we are dealing with tokens (through fn token_to_string). This is an excellent candidate for a Display impl for Token, which doesn't exist yet.
  • AST printing. We need to print larger pieces of (possibly expanded) AST for both people, and for proc macros (due to issues). Both people and proc macros are then trying to parse and further compile that pretty-printed code again, so this printing has to recover rawness (which is not stored in AST, but can be guessed very well) and convert $crates, which simply don't parse otherwise.
  • Printing individual identifiers in error messages. That's the use case that @estebank had in mind when implementing Accurately portray raw identifiers in error messages #67010.
    Apparently the rawness should be guessed as well in this case. Should the $crates be converted though? I have no idea.
    If the logic is that the printed error message should look as similar as possible to the source code, then it shouldn't.
    I agree that this use case is better served by a Display impl (the most ergonomic way) than by an explicit function, because error reporting code is more often written by people than pretty-printing code which is already written once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess I'll change the PR to account for the "print ast::Ident in a way that is as similar as possible to its originally written token in source code" use case, which is important for error messages.

Copy link
Contributor Author

@petrochenkov petrochenkov Feb 23, 2020

Choose a reason for hiding this comment

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

And also move token_to_string to Display for Token while I'm here.
Nonterminal printing needs to pretty-print AST, so it won't fly.

@bors
Copy link
Contributor

bors commented Feb 23, 2020

☔ The latest upstream changes (presumably #69334) made this pull request unmergeable. Please resolve the merge conflicts.

@petrochenkov
Copy link
Contributor Author

Updated with more comments and #69387 (comment) addressed.

@Mark-Simulacrum
Copy link
Member

@bors r+

Looks good, thanks for the clarifications!

@bors
Copy link
Contributor

bors commented Feb 24, 2020

📌 Commit e355a33 has been approved by Mark-Simulacrum

@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-review Status: Awaiting review from the assignee but also interested parties. labels Feb 24, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 25, 2020
…crum

Deduplicate identifier printing a bit

rust-lang#67010 introduced a couple more subtly different ways to print an identifier.
This PR attempts to restore the order.

The most basic identifier printing interface is `Formatter`-based now, so `String`s are not allocated unless required.

r? @Mark-Simulacrum
bors added a commit that referenced this pull request Feb 26, 2020
Rollup of 7 pull requests

Successful merges:

 - #67637 (Add primitive module to libcore)
 - #69387 (Deduplicate identifier printing a bit)
 - #69412 (Mark attributes consumed by `check_mod_attrs` as normal)
 - #69423 (syntax: Remove `Nt(Impl,Trait,Foreign)Item`)
 - #69429 (remove redundant clones and import)
 - #69457 (Clean up e0370 e0371)
 - #69468 ([master] Backport release notes of 1.41.1)

Failed merges:

r? @ghost
@bors bors merged commit 41bf200 into rust-lang:master Feb 26, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 27, 2020
…acrum

rustc_span: Add `Symbol::to_ident_string` for use in diagnostic messages

Covers the same error reporting use case (rust-lang#69387 (comment)) as the `Display` impl for `Ident`.
cc rust-lang#69053

Follow-up to rust-lang#69387.
r? @Mark-Simulacrum
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 28, 2020
…acrum

rustc_span: Add `Symbol::to_ident_string` for use in diagnostic messages

Covers the same error reporting use case (rust-lang#69387 (comment)) as the `Display` impl for `Ident`.
cc rust-lang#69053

Follow-up to rust-lang#69387.
r? @Mark-Simulacrum
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.

5 participants