-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Refactor suggestion diagnostic API to allow for multiple suggestions #41876
Conversation
d2f94e1
to
f859f25
Compare
The failure is spurious |
seems fine to me, I haven't done a detailed review, but the API looks good |
src/librustc_errors/emitter.rs
Outdated
// to be consistent. We could try to figure out if we can | ||
// make one (or the first one) inline, but that would give | ||
// undue importance to a semi-random suggestion | ||
for sugg in &db.suggestions { |
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 duplicates the code somewhat. Consider doing this instead:
if let Some((sugg, rest)) = db.suggestions.split_first() {
if rest.is_empty() && sugg.substitutes.len() == 1 && ... {
show `sugg` as label
} else {
for sugg in &db.suggestions {
show `sugg` proper
}
}
}
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 did not know about split_first
. That's a great method.
src/librustc_errors/lib.rs
Outdated
/// ``` | ||
/// vec![(0..7, vec!["a.b", "x.y"])] | ||
/// ``` | ||
pub substitutes: Vec<(Span, Vec<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 makes code fairly ugly. Can we just get a struct with named fields for this?
src/librustc_errors/emitter.rs
Outdated
@@ -1054,38 +1073,44 @@ impl EmitterWriter { | |||
-> io::Result<()> { | |||
use std::borrow::Borrow; | |||
|
|||
let primary_span = suggestion.msp.primary_span().unwrap(); | |||
let primary_span = suggestion.substitutes[0].0; |
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.
Should be abstracted into a method IMO.
// remove trailing newline | ||
buf.pop(); | ||
buf | ||
for buf in &mut bufs { |
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 code is weird, mostly due to how push_trailing
is used in the loop.
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.
Not sure what you mean. The code is exactly the same as it was before, but produces multiple suggestions instead of one. The only change i made other than the multiple suggestion part is to not add the trailing line if the replacement already produced a newline character and is thus finished.
src/librustc_resolve/lib.rs
Outdated
@@ -2584,7 +2598,7 @@ impl<'a> Resolver<'a> { | |||
module = Some(self.graph_root); | |||
continue | |||
} else if i == 0 && ns == TypeNS && ident.name == "$crate" { | |||
module = Some(self.resolve_crate_var(ident.ctxt)); | |||
module = Some(self.resolve_crate_var(ident.ctxt, DUMMY_SP)); |
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.
Is DUMMY_SP here intended?
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 for external crates. I'm not sure if external crates have spans
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 could use the span of the use location? Any span is better than DUMMY_SP.
"".to_owned() | ||
} | ||
)); | ||
err.span_suggestions(span, &msg, path_strings); |
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.
Why not just call err.span_suggestion
multiple times in the loop above?
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.
There's a difference. The API is probably badly named. it would be span_suggestion_with_multiple_substitutions
.
I could automate it by checking if the message and span are the same as an existing suggestion and then merge them. This would get rid of the span_suggestions
entirely.
src/libsyntax/json.rs
Outdated
RenderSpan::Suggestion(ref suggestion) => | ||
DiagnosticSpan::from_suggestion(suggestion, je), | ||
// regular diagnostics don't produce this anymore | ||
// will be removed in a later commit |
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.
Tag the comment with FIXME please.
`use std::io::Result;` | ||
`use std::thread::Result;` | ||
help: possible better candidates are found in other modules, you can import them into scope | ||
| use std::fmt::Result; |
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.
Are these multiple suggestions or multiple substitutions? If suggestions, how does the one suggestion with multiple substitutions look like? What about multiple suggestions with one substitution each? Multiple suggestions with multiple substitutions?
If they do not look any differently, perhaps we could get rid of the multiple substitutions concept and always use multiple suggestions instead?
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.
multiple substitutions.
multiple suggestions show up with their own help
each (just as they did before).
multiple suggestions with multiple substitutions show up as a help
each + their substitutions below.
r=me with the dummy_sp somehow removed. |
It was a little more involved to get the span to that function, but it's there now. |
33dcc19
to
3f2bbe3
Compare
@bors r+ |
📌 Commit 3f2bbe3 has been approved by |
…agisa Refactor suggestion diagnostic API to allow for multiple suggestions r? @jonathandturner cc @nrc @petrochenkov
Refactor internal suggestion API ~~The only functional change is that whitespace, which is suggested to be added, also gets `^^^^` under it. An example is shown in the tests (the only test that changed).~~ Continuation of #41876 r? @nagisa the changes are probably best viewed [without whitespace](https://github.com/rust-lang/rust/pull/45741/files?w=1)
r? @jonathandturner
cc @nrc @petrochenkov