-
Notifications
You must be signed in to change notification settings - Fork 60
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
[codespan-reporting] Don't require FileId when instantiating Label #229
Comments
This is also (somewhat) related to #200, Looking at the existing/current Label structure: pub struct Label<FileId> {
pub style: LabelStyle,
pub file_id: FileId,
pub range: Range<usize>,
pub message: String,
} It seems like there are 2 options for achieving the proposed behavior:
It is worth noting that your parser could perform the second case already with a struct like: pub struct AnonLabel {
pub style: LabelStyle,
pub range: Range<usize>,
pub message: String,
}
impl AnonLabel {
pub fn with_file<FileId>(self,file:FileId) -> Label<FileId> {
// or maybe this should just construct directly rather than new().with_message()
Label::new(self.style, file, self.range).with_message(self.message)
}
} So, even if codespan-reporting doesn't change the Edit: It seems like you can also get the first option without modifying codespan-reporting as well, |
Yeah I've already gotten around this by using my own |
Sorry for the lack of updates on this, it's been a while! This is definitely a bit annoying. It would be helpful to know what any proposal would look like in the multi-file use-case. Like, when you have labels from different files in the same diagnostic. |
Possibly |
I mean, we have |
I'm now wondering if we could investigate a richer 'diagnostic tree'. Perhaps something like: Diagnostic::error()
FileLabel::primary(file_id, range)
FileLabel::secondary(file_id, range)
FileLabel::secondary(file_id, range)
FileDiagnostic::error(file_id)
Label::primary(range)
Label::secondary(range)
FileLabel::secondary(file_id, range) I'm unsure how this would actually work in practice though. |
I think the simplest solution is to use impl Label<()>{
fn with_file<NewFileId>(self, file_id: NewFileId)->Label<NewFileId>{
Label {
file_id,
..self
}
}
}
impl Diagnostic<()>{
fn with_file<NewFileId>(self, file_id: NewFileId)->Diagnostic<NewFileId>{
// ...
}
} |
Problem
This is just a small pain point I've been having with
Label
: you are required to pass in theFileId
when creating aLabel
, which means that any error reporting logic must always be aware of what file it's operating on. This has led to some unfortunate coupling whereFileId
has to be threaded throughout any logic that might report a diagnostic.For example, I have a
Parser
struct that can be constructed with a string reference:But actually the
Parser
needs theFileId
to createLabel
instances, so it looks more like:Not a huge deal, but it's more annoying.
Parser
will only ever report diagnostics for a single file. It also adds friction for writing tests with modules likeParser
. I have to instantiate thatfilesystem
for the tests instead of just passing in that&str
. I wantParser
and other modules like it to be independent of my filesystem abstraction, but thisFileId
requirement makes that difficult.Proposal
It would be nice if
FileId
was not required when instantiatingLabel
and instead you could add it with another chained method likewith_file
This could be paired with an API for
Diagnostic
that applies aFileId
to allLabel
instances that don't have one, which would be useful forParser
.The text was updated successfully, but these errors were encountered: