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

Fix mispositioned error indicators #44386

Merged
merged 1 commit into from
Sep 13, 2017
Merged

Fix mispositioned error indicators #44386

merged 1 commit into from
Sep 13, 2017

Conversation

est31
Copy link
Member

@est31 est31 commented Sep 7, 2017

Fixes #38384

Most of the Rust community uses 4 spaces for indentation,
but there are also tab users of Rust (including myself!).

This patch fixes a bug in error printing which mispositions
error indicators when near code with tabs.

The code attempted to fix the issue by replacing spaces
with tabs, but it sadly wasn't enough, as sometimes
you may not print spaces but _ or ^ instead.

This patch employs the reverse strategy: it replaces each
tab with a space, so that the number of _ and ^ and spaces
in error indicators below the code snippet line up
perfectly.

In a study 1 preceeding this patch, we could see that
this strategy is also chosen by gcc version 6.3.0.

Its not perfect, as the output is not beautiful, but its
the easiest to implement. If anyone wants to improve on
this, be my guest! This patch is meant as improvement of
the status quo, not as perfect end status. It fixes the
actual issue of mispositioning error indicators.

@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

Fixes rust-lang#38384

Most of the Rust community uses 4 spaces for indentation,
but there are also tab users of Rust (including myself!).

This patch fixes a bug in error printing which mispositions
error indicators when near code with tabs.

The code attempted to fix the issue by replacing spaces
with tabs, but it sadly wasn't enough, as sometimes
you may not print spaces but _ or ^ instead.

This patch employs the reverse strategy: it replaces each
tab with a space, so that the number of _ and ^ and spaces
in error indicators below the code snippet line up
perfectly.

In a study [1] preceeding this patch, we could see that
this strategy is also chosen by gcc version 6.3.0.

Its not perfect, as the output is not beautiful, but its
the easiest to implement. If anyone wants to improve on
this, be my guest! This patch is meant as improvement of
the status quo, not as perfect end status. It fixes the
actual issue of mispositioning error indicators.

[1]: rust-lang#38384 (comment)
@estebank
Copy link
Contributor

estebank commented Sep 7, 2017

Given that this is not an ideal solution, I feel that we should keep the current output for any situation where there are no multiline spans being displayed, and this output otherwise. This could be problematic when we're displaying two diagnostics, one with a multiline span and one without, as they would look different. This could be alleviated somewhat by modifying replace_tabs slightly to add the tabs as StyledString { text: " ", style: Style::BackgroundHighlight }, where Style::BackgroundHighlight would be white text on red background.

@alexcrichton alexcrichton added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 7, 2017
@est31
Copy link
Member Author

est31 commented Sep 8, 2017

I feel that we should keep the current output for any situation where there are no multiline spans being displayed, and this output otherwise

I'm not sure what you mean with "multiline" spans but the bug already appears in this example.

I just want the output to stop being buggy. It might look different from your text editor, so my solution is not perfect, as I'm admitting, but I didn't mean this PR as reaching perfection.

Displaying tabs with a red background would make them look like errors themselves, which they are not (its completely fine to use tabs).

@pnkfelix
Copy link
Member

pnkfelix commented Sep 13, 2017

I think I would have preferred replacing each tab with four space characters rather than a single space character (and it seems like that was @est31 's original plan here), but I am also a fan of keeping implementations simple.

So I'll r+ this now, and independently investigate whether we can further extend this code to do a replacement with four space characters rather than just one.


Speaking of simple, this new code is much simpler to understand than the previous algorithm; that's a plus.

  • (The previous algorithm was also O(n²) time if I am not mistaken... though n here is #lines of message output, which tends to be relatively small) Update; I was mistaken; in my skimming I thought it was repeatedly searching for tabs starting from every row, but I guess it was hard coded to always use the 4th row (and the 4th row alone).

@pnkfelix
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Sep 13, 2017

📌 Commit d582810 has been approved by pnkfelix

@bors
Copy link
Contributor

bors commented Sep 13, 2017

⌛ Testing commit d582810 with merge 824952f...

bors added a commit that referenced this pull request Sep 13, 2017
Fix mispositioned error indicators

Fixes #38384

Most of the Rust community uses 4 spaces for indentation,
but there are also tab users of Rust (including myself!).

This patch fixes a bug in error printing which mispositions
error indicators when near code with tabs.

The code attempted to fix the issue by replacing spaces
with tabs, but it sadly wasn't enough, as sometimes
you may not print spaces but _ or ^ instead.

This patch employs the reverse strategy: it replaces each
tab with a space, so that the number of _ and ^ and spaces
in error indicators below the code snippet line up
perfectly.

In a study [1] preceeding this patch, we could see that
this strategy is also chosen by gcc version 6.3.0.

Its not perfect, as the output is not beautiful, but its
the easiest to implement. If anyone wants to improve on
this, be my guest! This patch is meant as improvement of
the status quo, not as perfect end status. It fixes the
actual issue of mispositioning error indicators.

[1]: #38384 (comment)
@pnkfelix pnkfelix 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 Sep 13, 2017
@bors
Copy link
Contributor

bors commented Sep 13, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: pnkfelix
Pushing 824952f to master...

@bors bors merged commit d582810 into rust-lang:master Sep 13, 2017
bors added a commit that referenced this pull request Dec 6, 2017
Display `\t` in diagnostics code as four spaces

Follow up to #44386 using the unicode variable width machinery from #45711 to replace tabs in the source code when displaying a diagnostic error with four spaces (instead of only one), while properly accounting for this when calculating underlines.

Partly addresses #44618.
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.

Mispositioned error-indicator at compile-time
6 participants