-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Use MultiSpan for unused imports #31452
Conversation
Yes, that I think that would make sense. (I've experimented with a similar approach a few days ago here, though I don't really understand the lint system too well) One question I have here is how important is it that reported spans are lexically continuous? A MultiSpan does not have to be limited to a single line (you'd probably be forced to change This isn't currently an issue due to the |
I think this is starting on the wrong foot - a multispan should be used where a single error affects multiple spans in the source code. Each unused import is a separate error, so should be reported separately. To make this concrete, if I fix one of the unused imports, the others are still valid. For a multispan to make sense, I think fixing a single error should make the whole thing go away. |
Yeah that makes sense. I still think using a multispan for unused imports would be nice (making the message more compact), but I guess there are not too many other error messages where using multispans would make sense remotely. Anyhow, this seems more like a discussion whether #16132 is still valid or should be closed as wont fix. Maybe we should move the discussion there? |
OK, looking again, I see a bit more what the original motivation was here. I think it is fine to group the lints for a single import into a single error, and then use the multispan for each item in the import. In which case, we should add the lint for the ast node for the import (which maybe is what we do today anyway?). This feels like it should be possible and maybe even easier than what you have at the moment - just need to group the lints by AST node id. |
e2510b7
to
521dc17
Compare
I've pushed an updated version of the commit. It now uses the AST node id of the import (previously it used the ids of the path items). At the moment the grouping is done in the unused import lint. This means What do you think? |
521dc17
to
2d32c14
Compare
The failing tests are due to not updating the compile-fail tests which check for unused imports. |
Could you give an example of what the error output looks like with this PR please? |
let mut err = match (level, span) { | ||
(Warn, Some(sp)) => sess.struct_span_warn(sp, &msg[..]), | ||
let span = span.map(|s| s.into()); | ||
let mut err = match (level, span.clone()) { |
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.
can we take spans by reference to avoid the new clones?
I think this approach looks good. I don't think there is too much advantage to grouping spans later. Do you think it could be significant? |
Yes I do not think that grouping spans later would be an advantage either, because I think the right grouping will depend on the particular lints. For the following program
this output is produced:
|
☔ The latest upstream changes (presumably #32496) made this pull request unmergeable. Please resolve the merge conflicts. |
Closing due to inactivity, but feel free to resubmit with a rebase! |
cc @nikomatsakis use of |
Should be easy enough to port this over to the new system. This use case was definitely something we had in mind. |
This is an initial PR that uses
MultiSpan
introduced by @mitaa to make the unused imports lint less nosiy (#16132).However there are still a few open questions about how the lint system should use MultiSpans. At the moment each lint added by
add_lint
is tied to a single AST node. If we want to use a multispan to bundle a few spans together for a lint , we would probably want to calladd_lint
with a MultiSpan. But when we bundle multipe lint messages together, there also are multiple AST ids.The unused imports lint should illustrate the problem quite well. Consider the exmaple from the issue. Ideally we would use a multispan to highlight all unused imports in a single line. This could be done by grouping the lint spans and passing a MultiSpan to
add_lint
. But as the lint system works at the moment, lints are tied to a single AST node they originated from. What I do in the initial version of the PR is just picking the AST id of the first node, but that's not good.It also breaks down when you use
#[allow()]
attributes on imports, likeIt seems like we would have to have use the allow information when grouping spans, but this is currently done when the lint is emitted, not when it's added. Pushing the grouping to the stage where the lints are emitted would have quite an impact on the code, because we would have first collect all instances of a lint that should be emitted, and then group them if it makes sense.
Any ideas/preferences? I think @nrc handled the PR that added MultiSpan, so I hope r? @nrc makes sense.