Closed
Description
In #40851, owing to the the fact that suggestions have been moved inline, no doubt is a good idea. But in the case where the span of the suggestion and the span of the main comment coincide, it currently causes the suggestion to appear before the main comment, which seems suboptimal. We should reorder suggestions so they appear below other labels.
Here is the example,
fn main() {
let v = vec![String::from("oh no")];
let e = v[0];
}
error[E0507]: cannot move out of indexed content
--> ex1.rs:4:13
|
4 | let e = v[0];
| ^^^^
| |
| help: consider using a reference instead `&v[0]`
| cannot move out of indexed content
error: aborting due to previous error
In the move_error.rs
let initializer =
e.init.as_ref().expect("should have an initializer to get an error");
if let Ok(snippet) = bccx.tcx.sess.codemap().span_to_snippet(initializer.span) {
err.span_suggestion(initializer.span,
"consider using a reference instead",format!("&{}", snippet));
}
}
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsCategory: An issue proposing an enhancement or a PR with one.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
nikomatsakis commentedon May 2, 2017
cc @oli-obk
nikomatsakis commentedon May 2, 2017
This seems like it could make a good mentorship issue. @estebank or @oli-obk, do you want to write up instructions?
oli-obk commentedon May 2, 2017
This is somewhat related to #41695
Here we are already duplicating the error in the main message and again in the note. (See https://github.com/rust-lang/rust/blob/master/src/librustc_borrowck/borrowck/gather_loans/move_error.rs#L138-L144 for the creation of the error and note)
In this case I'd just remove the duplication of the message and only show the suggestion inline. But I don't know the rules around this duplication stuff, although I personally dislike it if the message is duplicated.
Back to the topic:
I think the issue is that https://github.com/rust-lang/rust/blob/master/src/librustc_errors/emitter.rs#L322-L344 sorts the labels according to their start/end column position.
So if we have three labels AA, AB, BB where AA and AB have the same span, we might get AA, AB, BB after sorting, then reverse it to BB, AB, AA, but we wanted to get BB, AA, AB (everything with the same span should keep its original order)
Right now emitter does: sort notes, reverse notes.
So we probably need to: reverse notes, sort notes, reverse notes.
nikomatsakis commentedon May 2, 2017
@oli-obk yes, I agree that this is strongly related to #41695, though I guess that we can have the problem even if the messages aren't the same. It seems like suggestions ought to come last -- I wonder if the general rule should be that you add labels in order of precedence anyhow (i.e., given two labels with same span, we'd prefer the one added first?).
oli-obk commentedon May 17, 2017
This can be tagged E-mentor and E-easy. Instructions are given in my previous comment. Feel free to ask me any questions about this here or on irc.
imanti commentedon May 23, 2017
I'd like to take this one if it's not yet assigned.
oli-obk commentedon May 23, 2017
@imanti go ahead! It's all yours
imanti commentedon May 25, 2017
@oli-obk the problem with the first example is how the list of annotations is being sorted: since both annotation have the exact same values in all fields except the message string, it will always find the suggestion to be bigger. As you pointed out, in these lines https://github.com/rust-lang/rust/blob/master/src/librustc_errors/emitter.rs#L322-L344 the list is being sorted and then reversed. So no matter how's the order of the list before those two lines, it'll always end up with the suggestion first.
Consider this snippet (it has the real values for the annotations):
I'm not an expert at all in rust code, but I can't think in a way of generically order these messages without knowing which one is a suggestion and which one not. So let me know your ideas.
oli-obk commentedon May 25, 2017
The sorting could be done with sort_by_key to not sort by the message string, which is a useless criterion for sorting anyway
imanti commentedon May 25, 2017
nice!! I'll try it tonigth at home and let you know.
imanti commentedon May 26, 2017
@oli-obk sorting by key works as expected:
I created branch here with the changes (just two lines).
Do you think it is worth finding other examples where span ordering is wrong before doing the pull request?
16 remaining items