Skip to content

Commit 8f4f0be

Browse files
committed
auto merge of #7038 : alexcrichton/rust/issue-6935, r=catamorphism
Tracks the `type_target` and `value_target` source imports separately to correctly warn about unused imports.
2 parents 6e5e97f + b6cccb3 commit 8f4f0be

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

src/librustc/middle/resolve.rs

+33-12
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,31 @@ pub struct ImportResolution {
366366
/// The privacy of this `use` directive (whether it's `use` or
367367
/// `pub use`.
368368
privacy: Privacy,
369-
id: node_id,
370369

371370
// The number of outstanding references to this name. When this reaches
372371
// zero, outside modules can count on the targets being correct. Before
373372
// then, all bets are off; future imports could override this name.
374-
375373
outstanding_references: uint,
376374

377375
/// The value that this `use` directive names, if there is one.
378376
value_target: Option<Target>,
377+
/// The source node of the `use` directive leading to the value target
378+
/// being non-none
379+
value_id: node_id,
380+
379381
/// The type that this `use` directive names, if there is one.
380382
type_target: Option<Target>,
383+
/// The source node of the `use` directive leading to the type target
384+
/// being non-none
385+
type_id: node_id,
381386
}
382387

383388
pub fn ImportResolution(privacy: Privacy,
384389
id: node_id) -> ImportResolution {
385390
ImportResolution {
386391
privacy: privacy,
387-
id: id,
392+
type_id: id,
393+
value_id: id,
388394
outstanding_references: 0,
389395
value_target: None,
390396
type_target: None,
@@ -399,6 +405,13 @@ impl ImportResolution {
399405
ValueNS => return copy self.value_target
400406
}
401407
}
408+
409+
fn id(&self, namespace: Namespace) -> node_id {
410+
match namespace {
411+
TypeNS => self.type_id,
412+
ValueNS => self.value_id,
413+
}
414+
}
402415
}
403416

404417
/// The link from a module up to its nearest parent node.
@@ -1920,7 +1933,8 @@ impl Resolver {
19201933

19211934
// the source of this name is different now
19221935
resolution.privacy = privacy;
1923-
resolution.id = id;
1936+
resolution.type_id = id;
1937+
resolution.value_id = id;
19241938
}
19251939
None => {
19261940
debug!("(building import directive) creating new");
@@ -2118,7 +2132,7 @@ impl Resolver {
21182132
containing_module,
21192133
target,
21202134
source,
2121-
import_directive.span);
2135+
import_directive);
21222136
}
21232137
GlobImport => {
21242138
let privacy = import_directive.privacy;
@@ -2181,7 +2195,7 @@ impl Resolver {
21812195
containing_module: @mut Module,
21822196
target: ident,
21832197
source: ident,
2184-
span: span)
2198+
directive: &ImportDirective)
21852199
-> ResolveResult<()> {
21862200
debug!("(resolving single import) resolving `%s` = `%s::%s` from \
21872201
`%s`",
@@ -2270,9 +2284,10 @@ impl Resolver {
22702284
return UnboundResult;
22712285
}
22722286
Some(target) => {
2273-
this.used_imports.insert(import_resolution.id);
2287+
let id = import_resolution.id(namespace);
2288+
this.used_imports.insert(id);
22742289
return BoundResult(target.target_module,
2275-
target.bindings);
2290+
target.bindings);
22762291
}
22772292
}
22782293
}
@@ -2323,8 +2338,10 @@ impl Resolver {
23232338

23242339
match value_result {
23252340
BoundResult(target_module, name_bindings) => {
2341+
debug!("(resolving single import) found value target");
23262342
import_resolution.value_target =
23272343
Some(Target(target_module, name_bindings));
2344+
import_resolution.value_id = directive.id;
23282345
}
23292346
UnboundResult => { /* Continue. */ }
23302347
UnknownResult => {
@@ -2333,8 +2350,10 @@ impl Resolver {
23332350
}
23342351
match type_result {
23352352
BoundResult(target_module, name_bindings) => {
2353+
debug!("(resolving single import) found type target");
23362354
import_resolution.type_target =
23372355
Some(Target(target_module, name_bindings));
2356+
import_resolution.type_id = directive.id;
23382357
}
23392358
UnboundResult => { /* Continue. */ }
23402359
UnknownResult => {
@@ -2383,6 +2402,7 @@ impl Resolver {
23832402
}
23842403
}
23852404

2405+
let span = directive.span;
23862406
if resolve_fail {
23872407
self.session.span_err(span, fmt!("unresolved import: there is no `%s` in `%s`",
23882408
*self.session.str_of(source),
@@ -2774,7 +2794,7 @@ impl Resolver {
27742794
Some(target) => {
27752795
debug!("(resolving item in lexical scope) using \
27762796
import resolution");
2777-
self.used_imports.insert(import_resolution.id);
2797+
self.used_imports.insert(import_resolution.id(namespace));
27782798
return Success(copy target);
27792799
}
27802800
}
@@ -3043,7 +3063,7 @@ impl Resolver {
30433063
import_resolution.privacy == Public => {
30443064
debug!("(resolving name in module) resolved to \
30453065
import");
3046-
self.used_imports.insert(import_resolution.id);
3066+
self.used_imports.insert(import_resolution.id(namespace));
30473067
return Success(copy target);
30483068
}
30493069
Some(_) => {
@@ -4525,7 +4545,8 @@ impl Resolver {
45254545
namespace)) {
45264546
(Some(def), Some(Public)) => {
45274547
// Found it.
4528-
self.used_imports.insert(import_resolution.id);
4548+
let id = import_resolution.id(namespace);
4549+
self.used_imports.insert(id);
45294550
return ImportNameDefinition(def);
45304551
}
45314552
(Some(_), _) | (None, _) => {
@@ -5140,7 +5161,7 @@ impl Resolver {
51405161
&mut found_traits,
51415162
trait_def_id, name);
51425163
self.used_imports.insert(
5143-
import_resolution.id);
5164+
import_resolution.type_id);
51445165
}
51455166
}
51465167
_ => {

src/test/compile-fail/lint-unused-import-tricky-names.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@
1111
#[deny(unused_imports)];
1212

1313
// Regression test for issue #6633
14+
mod issue6633 {
15+
use self::foo::name::name; //~ ERROR: unused import
16+
use self::foo::name;
1417

15-
use foo::name::name; //~ ERROR: unused import
16-
use foo::name;
17-
18-
pub mod foo {
19-
pub mod name {
20-
pub type a = int;
18+
pub mod foo {
2119
pub mod name {
22-
pub type a = float;
20+
pub type a = int;
21+
pub mod name {
22+
pub type a = float;
23+
}
2324
}
2425
}
26+
27+
fn bar() -> name::a { 1 }
2528
}
2629

27-
fn bar() -> name::a { 1 }
30+
// Regression test for issue #6935
31+
mod issue6935 {
32+
use self::a::foo::a::foo;
33+
use self::a::foo; //~ ERROR: unused import
34+
35+
pub mod a {
36+
pub mod foo {
37+
pub mod a {
38+
pub fn foo() {}
39+
}
40+
}
41+
}
42+
43+
fn bar() { foo(); }
44+
}
2845

2946
fn main(){}
47+

0 commit comments

Comments
 (0)