Skip to content

Commit 1400cb0

Browse files
committed
Fix use placement for suggestions near main.
1 parent 5f10d31 commit 1400cb0

11 files changed

+167
-20
lines changed

compiler/rustc_resolve/src/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,15 @@ impl UsePlacementFinder {
330330
if self.span.map_or(true, |span| item.span < span)
331331
&& !item.span.from_expansion()
332332
{
333+
self.span = Some(item.span.shrink_to_lo());
333334
// don't insert between attributes and an item
334-
if item.attrs.is_empty() {
335-
self.span = Some(item.span.shrink_to_lo());
336-
} else {
337-
// find the first attribute on the item
338-
for attr in &item.attrs {
339-
if self.span.map_or(true, |span| attr.span < span) {
340-
self.span = Some(attr.span.shrink_to_lo());
341-
}
335+
// find the first attribute on the item
336+
// FIXME: This is broken for active attributes.
337+
for attr in &item.attrs {
338+
if !attr.span.is_dummy()
339+
&& self.span.map_or(true, |span| attr.span < span)
340+
{
341+
self.span = Some(attr.span.shrink_to_lo());
342342
}
343343
}
344344
}

compiler/rustc_typeck/src/check/method/suggest.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1557,16 +1557,16 @@ impl intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> {
15571557
_ => {
15581558
if self.span.map_or(true, |span| item.span < span) {
15591559
if !item.span.from_expansion() {
1560+
self.span = Some(item.span.shrink_to_lo());
15601561
// Don't insert between attributes and an item.
15611562
let attrs = self.tcx.hir().attrs(item.hir_id());
1562-
if attrs.is_empty() {
1563-
self.span = Some(item.span.shrink_to_lo());
1564-
} else {
1565-
// Find the first attribute on the item.
1566-
for attr in attrs {
1567-
if self.span.map_or(true, |span| attr.span < span) {
1568-
self.span = Some(attr.span.shrink_to_lo());
1569-
}
1563+
// Find the first attribute on the item.
1564+
// FIXME: This is broken for active attributes.
1565+
for attr in attrs {
1566+
if !attr.span.is_dummy()
1567+
&& self.span.map_or(true, |span| attr.span < span)
1568+
{
1569+
self.span = Some(attr.span.shrink_to_lo());
15701570
}
15711571
}
15721572
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// run-rustfix
2+
#![allow(dead_code)]
3+
4+
use m::A;
5+
6+
use std::collections::HashMap;
7+
8+
macro_rules! y {
9+
() => {}
10+
}
11+
12+
mod m {
13+
pub const A: i32 = 0;
14+
}
15+
16+
mod foo {
17+
// FIXME: UsePlacementFinder is broken because active attributes are
18+
// removed, and thus the `derive` attribute here is not in the AST.
19+
// An inert attribute should work, though.
20+
// #[derive(Debug)]
21+
use std::path::Path;
22+
23+
#[allow(warnings)]
24+
pub struct Foo;
25+
26+
// test whether the use suggestion isn't
27+
// placed into the expansion of `#[derive(Debug)]
28+
type Bar = Path; //~ ERROR cannot find
29+
}
30+
31+
fn main() {
32+
y!();
33+
let _ = A; //~ ERROR cannot find
34+
foo();
35+
}
36+
37+
fn foo() {
38+
type Dict<K, V> = HashMap<K, V>; //~ ERROR cannot find
39+
}

src/test/ui/resolve/use_suggestion_placement.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// run-rustfix
2+
#![allow(dead_code)]
3+
14
macro_rules! y {
25
() => {}
36
}
@@ -7,7 +10,11 @@ mod m {
710
}
811

912
mod foo {
10-
#[derive(Debug)]
13+
// FIXME: UsePlacementFinder is broken because active attributes are
14+
// removed, and thus the `derive` attribute here is not in the AST.
15+
// An inert attribute should work, though.
16+
// #[derive(Debug)]
17+
#[allow(warnings)]
1118
pub struct Foo;
1219

1320
// test whether the use suggestion isn't

src/test/ui/resolve/use_suggestion_placement.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0412]: cannot find type `Path` in this scope
2-
--> $DIR/use_suggestion_placement.rs:15:16
2+
--> $DIR/use_suggestion_placement.rs:22:16
33
|
44
LL | type Bar = Path;
55
| ^^^^ not found in this scope
@@ -10,7 +10,7 @@ LL | use std::path::Path;
1010
|
1111

1212
error[E0425]: cannot find value `A` in this scope
13-
--> $DIR/use_suggestion_placement.rs:20:13
13+
--> $DIR/use_suggestion_placement.rs:27:13
1414
|
1515
LL | let _ = A;
1616
| ^ not found in this scope
@@ -21,7 +21,7 @@ LL | use m::A;
2121
|
2222

2323
error[E0412]: cannot find type `HashMap` in this scope
24-
--> $DIR/use_suggestion_placement.rs:25:23
24+
--> $DIR/use_suggestion_placement.rs:32:23
2525
|
2626
LL | type Dict<K, V> = HashMap<K, V>;
2727
| ^^^^^^^ not found in this scope
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile-flags: --test
2+
// run-rustfix
3+
// Checks that the `use` suggestion appears *below* this inner attribute.
4+
// There was an issue where the test synthetic #[allow(dead)] attribute on
5+
// main which has a dummy span caused the suggestion to be placed at the top
6+
// of the file.
7+
#![allow(unused)]
8+
9+
use std::fmt::Debug;
10+
11+
fn main() {}
12+
13+
fn foobar<T: Debug>(x: T) {} //~ ERROR expected trait, found derive macro
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: --test
2+
// run-rustfix
3+
// Checks that the `use` suggestion appears *below* this inner attribute.
4+
// There was an issue where the test synthetic #[allow(dead)] attribute on
5+
// main which has a dummy span caused the suggestion to be placed at the top
6+
// of the file.
7+
#![allow(unused)]
8+
9+
fn main() {}
10+
11+
fn foobar<T: Debug>(x: T) {} //~ ERROR expected trait, found derive macro
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0404]: expected trait, found derive macro `Debug`
2+
--> $DIR/use-placement-resolve.rs:11:14
3+
|
4+
LL | fn foobar<T: Debug>(x: T) {}
5+
| ^^^^^ not a trait
6+
|
7+
help: consider importing this trait instead
8+
|
9+
LL | use std::fmt::Debug;
10+
|
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0404`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// compile-flags: --test
2+
// run-rustfix
3+
// Checks that the `use` suggestion appears *below* this inner attribute.
4+
// There was an issue where the test synthetic #[allow(dead)] attribute on
5+
// main which has a dummy span caused the suggestion to be placed at the top
6+
// of the file.
7+
#![allow(unused)]
8+
9+
use m::Foo;
10+
11+
fn main() {
12+
let s = m::S;
13+
s.abc(); //~ ERROR no method named `abc`
14+
}
15+
16+
mod m {
17+
pub trait Foo {
18+
fn abc(&self) {}
19+
}
20+
pub struct S;
21+
impl Foo for S{}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: --test
2+
// run-rustfix
3+
// Checks that the `use` suggestion appears *below* this inner attribute.
4+
// There was an issue where the test synthetic #[allow(dead)] attribute on
5+
// main which has a dummy span caused the suggestion to be placed at the top
6+
// of the file.
7+
#![allow(unused)]
8+
9+
fn main() {
10+
let s = m::S;
11+
s.abc(); //~ ERROR no method named `abc`
12+
}
13+
14+
mod m {
15+
pub trait Foo {
16+
fn abc(&self) {}
17+
}
18+
pub struct S;
19+
impl Foo for S{}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0599]: no method named `abc` found for struct `S` in the current scope
2+
--> $DIR/use-placement-typeck.rs:11:7
3+
|
4+
LL | s.abc();
5+
| ^^^ method not found in `S`
6+
...
7+
LL | fn abc(&self) {}
8+
| --- the method is available for `S` here
9+
LL | }
10+
LL | pub struct S;
11+
| ------------- method `abc` not found for this
12+
|
13+
= help: items from traits can only be used if the trait is in scope
14+
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
15+
|
16+
LL | use m::Foo;
17+
|
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)