-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Report lint names in json diagnostics #45484
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
src/librustc_errors/emitter.rs
Outdated
@@ -893,7 +893,8 @@ impl EmitterWriter { | |||
} else { | |||
buffer.append(0, &level.to_string(), Style::Level(level.clone())); | |||
match code { | |||
&Some(ref code) => { | |||
// only render error codes, not lint codes | |||
&Some(ref code) if code.starts_with("E") && code.len() == 5 => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to check for the regex E[0-9]+
- I could imagine someone using a string "Exlint" and getting confused about the weird rendering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't do that. they are lowercase by definition (autogenerated from the global-name in the lint declaration macro)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't do that. they are lowercase by definition (autogenerated from the global-name in the lint declaration macro)
Wait, I'm confused -- what are you saying here? The error codes are lowercase? You're already checking for E
, so that can't be it.
Still, this kinda' feels like a hack to me. Why not add another field to the diagnostic, like lint_name
or something? It seems pretty odd that our final JSON winds up with "explanation":""
, which has nothing to do with lints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant lint names are always lowercase, that's how we can distinguish them.
That said, I can replace the code
field (currently a String
) with an
enum DiagnosticId {
Error(String),
Lint(String),
}
It seems pretty odd that our final JSON winds up with "explanation":"", which has nothing to do with lints.
The explanation being empty is an implementation detail. It was nontrivial to add and I didn't want to mess up the PR with unrelated changes. The issue is that the array with the error explanations is filled in before the lints are loaded into the session and then frozen by creating the error emitter. So once the lints are loaded (and thus their explanations are available), we can't place them into the emitter.
It just requires some refactoring to get done.
I found it odder to have two Option
fields which are mutually exclusive than a temporarily empty explanation string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will make rustfix very happy!
src/librustc_errors/emitter.rs
Outdated
@@ -893,7 +893,8 @@ impl EmitterWriter { | |||
} else { | |||
buffer.append(0, &level.to_string(), Style::Level(level.clone())); | |||
match code { | |||
&Some(ref code) => { | |||
// only render error codes, not lint codes | |||
&Some(ref code) if code.starts_with("E") && code.len() == 5 => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kinda why I suggested to not use the code field for the lint name
cc @estebank -- JSON format discussion |
db21fe9
to
181054a
Compare
I'm slightly worried about people getting confused with @bors r+ |
📌 Commit 181054a has been approved by |
@bors r- I thought @nikomatsakis had r'd me, it was a cc instead. |
☔ The latest upstream changes (presumably #45489) made this pull request unmergeable. Please resolve the merge conflicts. |
@@ -81,7 +87,7 @@ impl Diagnostic { | |||
Diagnostic::new_with_code(level, None, message) | |||
} | |||
|
|||
pub fn new_with_code(level: Level, code: Option<String>, message: &str) -> Self { | |||
pub fn new_with_code(level: Level, code: Option<DiagnosticId>, message: &str) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to guarantee that lints cannot have diagnostic codes? That doesn't seem like an obviously good thing... although I guess that the lint name can serve as a diagnostic code, which is kind of nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diagnostic codes are for errors. Everything else is silenceable in some way and thus a lint. Clippy has a lint list like the error code list of rustc. I don't think having two different identifiers for a lint would be useful. At least I can't think of any use
@oli-obk I'm curious, what's the case for using the We don't really have a strong back-compat story around JSON, so it seems like we'd be pretty committed to this format if we decide to do it. I'd like the reasoning and longer term plan to be a bit more "well documented" here. |
Having two different fields feels like both of them could be non-null. But that would only make sense if lints additionally had an error code. I don't think we'd be able to do that, since clippy and rustc would have to agree on error codes. Lint names on the other hand are less likely to collide, and lints already have an identifier: the lint name. I'll add some documentation. |
I've come around; I think re-using the same code field makes sense. The way I look at it, diagnostic codes have two purposes:
Both make sense for lints, but then it seems that lint names can serve both purposes perfectly well. So I think I agree with you that we don't need to have two fields here. (To be honest, I am not 100% convinced that diagnostic codes serve that much value. If we reworked |
@oli-obk needs rebase but r=me or r=estebank |
rebased |
@bors r+ |
📌 Commit 6ae440e has been approved by |
Report lint names in json diagnostics This allows tools like `rustfix` to have whitelists for what to automatically apply and what not.
☀️ Test successful - status-appveyor, status-travis |
This allows tools like
rustfix
to have whitelists for what to automatically apply and what not.