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

Handle missing sources #160

Open
Nadrieril opened this issue Nov 21, 2024 · 2 comments
Open

Handle missing sources #160

Nadrieril opened this issue Nov 21, 2024 · 2 comments

Comments

@Nadrieril
Copy link
Member

Sometimes I don't have access to the source text for one reason or another, but I'd still like to display a message with a file name and line/col numbers, e.g

error: Trait aliases are not supported
 --> /rustc/library/core/src/ptr/metadata.rs:81:1

(that's how rustc renders that today).

As far as I can tell this isn't supported with the current API. The closest I managed is to use a snippet with empty source and add the line/col numbers by hand in the origin.

let origin = format!("{}:{}:{}", file.name, span.beg.line, span.beg.col + 1);
let snippet = Snippet::source("").origin(&origin);
let message = level.title(&msg).snippet(snippet);

I get (note the two empty lines starting with |, and the lack of indentation of the --> arrow):

error: Trait aliases are not supported
--> /rustc/library/core/src/ptr/metadata.rs:81:1
 |
 |

Trying to add an annotation with a span obviously panics because the span is not in range of the empty string.

Would it be possible to have a Snippet::no_source() builder that supports annotations with spans and renders them like the above?

@epage
Copy link
Contributor

epage commented Nov 21, 2024

I'm curious how you have access to line and column information without the source.

There might be some overlap with #38.

We are working on an API redesign to support Suggestions and it might offer us an opportunity to offer Origin sections that is just file, optional line, optional column without having to have two modes for Snippet where much of the API gets ignored in the second mode.

@Nadrieril
Copy link
Member Author

Nadrieril commented Nov 21, 2024

I'm curious how you have access to line and column information without the source.

It so happens that rustc's SourceFile remembers the mapping from byte to line even when it doesn't remember the whole source text. (I'm not working on rustc mind you, this is for the purposes of a custom rustc driver).

We are working on an API redesign to support Suggestions and it might offer us an opportunity to offer Origin sections that is just file, optional line, optional column without having to have two modes for Snippet where much of the API gets ignored in the second mode.

That sounds good. To be clear the thing that annoys me most in my case is the two extra | lines and the inconsistently indented -->; adding the line number to the origin myself is easy enough. So maybe this doesn't require a whole new mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants