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

json byte position doesn't start at top of file #35164

Closed
sophiajt opened this issue Aug 1, 2016 · 5 comments
Closed

json byte position doesn't start at top of file #35164

sophiajt opened this issue Aug 1, 2016 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@sophiajt
Copy link
Contributor

sophiajt commented Aug 1, 2016

From the IRC conversation:

1:17 AM jntrnr: re json compiler output, i totally forgot to ask about byte_{start,end}! it's either not mapping to the byte indices of the source file (is it maybe counting them after after macro expansion or something?) or i'm stupid ;) do you know what's up this that? it'd make rustfix (and editor plugin i imagine) a bit easier to implement if we could use byte indices
4:38 AM killercup: you have to look at how they're created
4:38 AM src/libsyntax/json.rs: byte_start: span.lo.0,
4:39 AM see, that's wrong
4:39 AM that's the byte start within the crate
4:39 AM without the CodeMap you can't know where the file starts
4:39 AM it should subtract the file start
4:40 AM assuming the start and end are in the same file

The fix should be to alter the json output so that lo/hi of the span's byte position start at the top of the file rather than at a codemap offset.

@Mark-Simulacrum
Copy link
Member

As far as I can tell, this hasn't happened. @jonathandturner This feels like it may be an E-easy bug. Could you provide some more instructions, perhaps?

@sophiajt
Copy link
Contributor Author

@Mark-Simulacrum - it may be, but tbh I totally forgot what I was thinking when I wrote this 😅 It's been a while since I worked on any error message code

@Mark-Simulacrum
Copy link
Member

Based on the IRC log, I think the problem is that these lines need to be something more than they are, going through some sort of CodeMap or maybe CodeMapper? I'm not familiar with this code, though, so I don't know what exactly needs to happen here.

Would you perhaps know who we could cc for some instructions here?

@sophiajt
Copy link
Contributor Author

Looks like the compiler team and @killercup sitting down together and figuring it out (if they haven't already)

killercup added a commit to rust-lang/rustfix that referenced this issue May 15, 2017
killercup added a commit to rust-lang/rustfix that referenced this issue May 15, 2017
@killercup
Copy link
Member

Not sure if anything changed since last year, but I just wrote this small example program that still crashes when the message is in another file: https://github.com/killercup/rustfix/blob/do-byte-indices-work/examples/do-byte-indices-work.rs

You can play around with it by adding unused warnings in various places.

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 22, 2017
zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 21, 2017
The `hi` and `lo` offsets in a span are relative to a `CodeMap`, but this
doesn't seem to be terribly useful for tool consumers who don't have the
codemap, but might want the byte offset within an actual file?

Resolves rust-lang#35164.
zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 21, 2017
bors added a commit that referenced this issue Jul 22, 2017
…of_file, r=nrc

make JSON error byte position start at top of file

The `hi` and `lo` offsets in a span are relative to a `CodeMap`, but this
doesn't seem to be terribly useful for tool consumers who don't have the
codemap, but might want the byte offset within an actual file?

I couldn't get @killercup's [example](#35164 (comment)) to run, perhaps due to the limitations of the merely-stage-1 compiler that I built (error was `libproc_macro-456500c7095d8fbe.so: cannot open shared object file: No such file or directory`)??—but a dummy project confirms that the byte offsets have successfully been changed to be file-relative—

**Before:**

```
$ cargo run --message-format json
   Compiling byte_json v0.1.0 (file:///home/ubuntu/byte_json)
{"message":{"children":[{"children":[],"code":null,"level":"note","message":"#[warn(dead_code)] on by default","rendered":null,"spans":[]}],"code":null,"level":"warning","message":"function is never used: `rah`","rendered":null,"spans":[{"byte_end":100,"byte_start":67,"column_end":2,"column_start":1,"expansion":null,"file_name":"src/foo.rs","is_primary":true,"label":null,"line_end":5,"line_start":3,"suggested_replacement":null,"text":[{"highlight_end":11,"highlight_start":1,"text":"fn rah() {"},{"highlight_end":21,"highlight_start":1,"text":"    println!(\"rah!\")"},{"highlight_end":2,"highlight_start":1,"text":"}"}]}]},"package_id":"byte_json 0.1.0 (path+file:///home/ubuntu/byte_json)","reason":"compiler-message","target":{"crate_types":["bin"],"kind":["bin"],"name":"byte_json","src_path":"/home/ubuntu/byte_json/src/main.rs"}}
{"message":{"children":[{"children":[],"code":null,"level":"note","message":"#[warn(dead_code)] on by default","rendered":null,"spans":[]}],"code":null,"level":"warning","message":"function is never used: `alas`","rendered":null,"spans":[{"byte_end":137,"byte_start":102,"column_end":2,"column_start":1,"expansion":null,"file_name":"src/bar.rs","is_primary":true,"label":null,"line_end":3,"line_start":1,"suggested_replacement":null,"text":[{"highlight_end":12,"highlight_start":1,"text":"fn alas() {"},{"highlight_end":22,"highlight_start":1,"text":"    println!(\"alas\");"},{"highlight_end":2,"highlight_start":1,"text":"}"}]}]},"package_id":"byte_json 0.1.0 (path+file:///home/ubuntu/byte_json)","reason":"compiler-message","target":{"crate_types":["bin"],"kind":["bin"],"name":"byte_json","src_path":"/home/ubuntu/byte_json/src/main.rs"}}
{"features":[],"filenames":["/home/ubuntu/byte_json/target/debug/byte_json"],"fresh":false,"package_id":"byte_json 0.1.0 (path+file:///home/ubuntu/byte_json)","profile":{"debug_assertions":true,"debuginfo":2,"opt_level":"0","overflow_checks":true,"test":false},"reason":"compiler-artifact","target":{"crate_types":["bin"],"kind":["bin"],"name":"byte_json","src_path":"/home/ubuntu/byte_json/src/main.rs"}}
    Finished dev [unoptimized + debuginfo] target(s) in 0.36 secs
     Running `target/debug/byte_json`
Hello, world!
```

**After:**

```
$ RUSTC=../rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc cargo run --message-format json
   Compiling byte_json v0.1.0 (file:///home/ubuntu/byte_json)
{"message":{"children":[{"children":[],"code":null,"level":"note","message":"#[warn(dead_code)] on by default","rendered":null,"spans":[]}],"code":null,"level":"warning","message":"function is never used: `rah`","rendered":null,"spans":[{"byte_end":35,"byte_start":2,"column_end":2,"column_start":1,"expansion":null,"file_name":"src/foo.rs","is_primary":true,"label":null,"line_end":5,"line_start":3,"suggested_replacement":null,"text":[{"highlight_end":11,"highlight_start":1,"text":"fn rah() {"},{"highlight_end":21,"highlight_start":1,"text":"    println!(\"rah!\")"},{"highlight_end":2,"highlight_start":1,"text":"}"}]}]},"package_id":"byte_json 0.1.0 (path+file:///home/ubuntu/byte_json)","reason":"compiler-message","target":{"crate_types":["bin"],"kind":["bin"],"name":"byte_json","src_path":"/home/ubuntu/byte_json/src/main.rs"}}
{"message":{"children":[{"children":[],"code":null,"level":"note","message":"#[warn(dead_code)] on by default","rendered":null,"spans":[]}],"code":null,"level":"warning","message":"function is never used: `alas`","rendered":null,"spans":[{"byte_end":35,"byte_start":0,"column_end":2,"column_start":1,"expansion":null,"file_name":"src/bar.rs","is_primary":true,"label":null,"line_end":3,"line_start":1,"suggested_replacement":null,"text":[{"highlight_end":12,"highlight_start":1,"text":"fn alas() {"},{"highlight_end":22,"highlight_start":1,"text":"    println!(\"alas\");"},{"highlight_end":2,"highlight_start":1,"text":"}"}]}]},"package_id":"byte_json 0.1.0 (path+file:///home/ubuntu/byte_json)","reason":"compiler-message","target":{"crate_types":["bin"],"kind":["bin"],"name":"byte_json","src_path":"/home/ubuntu/byte_json/src/main.rs"}}
{"features":[],"filenames":["/home/ubuntu/byte_json/target/debug/byte_json"],"fresh":false,"package_id":"byte_json 0.1.0 (path+file:///home/ubuntu/byte_json)","profile":{"debug_assertions":true,"debuginfo":2,"opt_level":"0","overflow_checks":true,"test":false},"reason":"compiler-artifact","target":{"crate_types":["bin"],"kind":["bin"],"name":"byte_json","src_path":"/home/ubuntu/byte_json/src/main.rs"}}
    Finished dev [unoptimized + debuginfo] target(s) in 2.59 secs
     Running `target/debug/byte_json`
Hello, world!
```

Resolves #35164.

r? @jonathandturner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

3 participants