Skip to content

Incorrect suggestion location for import with outer attribute at top of file #55335

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

Closed
nrc opened this issue Oct 25, 2018 · 3 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nrc
Copy link
Member

nrc commented Oct 25, 2018

E.g.,

#[derive(Clone, Debug)] // this is line 1 in this file
pub struct Service;

pub struct Response<T> where T: Bar { pub item: T }

gives

error[E0405]: cannot find trait `Bar` in this scope                                                                                       
 --> src/main.rs:4:33                                                                                                                     
  |                                                                                                                                       
4 | pub struct Response<T> where T: Bar { pub item: T }                                                                                   
  |                                 ^^^ not found in this scope                                                                           
help: possible candidate is found in another module, you can import it into scope                                                         
  |                                                                                                                                       
2 | use crate::foo::Bar; 

Note that line 2 is between the derive and the the struct.

Moved from rust-lang/rls#1085

@nrc nrc added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 25, 2018
@estebank
Copy link
Contributor

estebank commented Oct 25, 2018

This is the code that selects the suggestion span:

struct UsePlacementFinder {
target_module: NodeId,
span: Option<Span>,
found_use: bool,
}
impl UsePlacementFinder {
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, bool) {
let mut finder = UsePlacementFinder {
target_module,
span: None,
found_use: false,
};
visit::walk_crate(&mut finder, krate);
(finder.span, finder.found_use)
}
}
impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
fn visit_mod(
&mut self,
module: &'tcx ast::Mod,
_: Span,
_: &[ast::Attribute],
node_id: NodeId,
) {
if self.span.is_some() {
return;
}
if node_id != self.target_module {
visit::walk_mod(self, module);
return;
}
// find a use statement
for item in &module.items {
match item.node {
ItemKind::Use(..) => {
// don't suggest placing a use before the prelude
// import or other generated ones
if item.span.ctxt().outer().expn_info().is_none() {
self.span = Some(item.span.shrink_to_lo());
self.found_use = true;
return;
}
},
// don't place use before extern crate
ItemKind::ExternCrate(_) => {}
// but place them before the first other item
_ => if self.span.map_or(true, |span| item.span < span ) {
if item.span.ctxt().outer().expn_info().is_none() {
// don't insert between attributes and an item
if item.attrs.is_empty() {
self.span = Some(item.span.shrink_to_lo());
} else {
// find the first attribute on the item
for attr in &item.attrs {
if self.span.map_or(true, |span| attr.span < span) {
self.span = Some(attr.span.shrink_to_lo());
}
}
}
}
},
}
}
}
}

@euclio
Copy link
Contributor

euclio commented Mar 5, 2019

This is caused by #45216. The suggestion is correct if the derive attribute is replaced by #[allow(warnings)], etc.

@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

Current output:

error[E0405]: cannot find trait `Bar` in this scope
 --> src/lib.rs:7:33
  |
7 | pub struct Response<T> where T: Bar { pub item: T }
  |                                 ^^^ not found in this scope
  |
help: consider importing this trait
  |
1 + use crate::foo::Bar;
  |

@estebank estebank closed this as completed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants