swift-syntax icon indicating copy to clipboard operation
swift-syntax copied to clipboard

Diagnostic formatter doesn't account for grapheme length in its column annotations

Open DougGregor opened this issue 2 years ago • 4 comments

Description

Given this code:

for (i = 🐮; i != 👩‍👩‍👦‍👦; i += 1) { x x }

The diagnostic formatter produces this:

1 │ for (i = 🐮; i != 👩‍👩‍👦‍👦; i += 1) { x x }
  ∣ │      │                                                  ╰─ error: consecutive statements on a line must be separated by ';'
  ∣ │      ╰─ error: expected ')' to end tuple pattern
  ∣ ╰─ error: C-style for statement has been removed in Swift 3
2 │ 

Note that the consecutive statements on a line error is shown at the wrong column, because we're counting columns in terms of UTF-8 bytes rather than in terms of grapheme cluster. We'll need to update the column calculation to operate in terms of "column width per grapheme cluster" so we get the right locations for the annotations.

Steps to Reproduce

Paste that code into a file for.swift, and run swift-parser-cli print-diags for.swift.

DougGregor avatar Mar 01 '23 18:03 DougGregor

rdar://106093487

ahoppen avatar Mar 01 '23 18:03 ahoppen

https://github.com/apple/swift-syntax/pull/1365

bnbarham avatar Mar 06 '23 19:03 bnbarham

Ah whoops, that's where Doug noticed the issue, not where it's fixed :)

bnbarham avatar Mar 06 '23 19:03 bnbarham

I’ve got the same problem with my backtracing code, as you might imagine, and I’d already added code to deal with the common cases. What we really need is the East Asian Width tables (which AFAIK aren’t in our Unicode data at present), and I think my code probably doesn’t cope with some of the more complex emoji cases.

Even then, I have a horrible suspicion that some terminals defer to the width information in the font, and in that case we just have to accept we’ll get it wrong in some cases.

al45tair avatar Mar 12 '23 08:03 al45tair

I believe this was fixed by #2512

Matejkob avatar Apr 05 '24 11:04 Matejkob

Yes, indeed.

ahoppen avatar Apr 05 '24 17:04 ahoppen