Skip to content

Commit fe6484d

Browse files
lhtmarijnh
authored andcommitted
rustc: Fix warn on unused import bug
rustc generates incorrect warning for cascaded import declarations like: use std; import std::io; import io::println; fn main() { println("hello"); } warning: unused import io A followup of issue #889
1 parent bfc8135 commit fe6484d

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/comp/middle/resolve.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ type env =
107107
mod_map: hashmap<ast::node_id, @indexed_mod>,
108108
ext_map: hashmap<def_id, [ident]>,
109109
ext_cache: ext_hash,
110-
mutable used_imports: option::t<[ast::node_id]>,
110+
used_imports: {mutable track: bool,
111+
mutable data: [ast::node_id]},
111112
mutable reported: [{ident: str, sc: scope}],
112113
mutable currently_resolving: node_id,
113114
sess: session};
@@ -129,15 +130,14 @@ fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) ->
129130
mod_map: new_int_hash::<@indexed_mod>(),
130131
ext_map: new_def_hash::<[ident]>(),
131132
ext_cache: new_ext_hash(),
132-
mutable used_imports: none,
133+
used_imports: {mutable track: false, mutable data: []},
133134
mutable reported: [],
134135
mutable currently_resolving: -1,
135136
sess: sess};
136137
map_crate(e, crate);
137138
resolve_imports(*e);
138139
check_for_collisions(e, *crate);
139140
check_bad_exports(e);
140-
e.used_imports = some([]);
141141
resolve_names(e, crate);
142142
check_unused_imports(e);
143143
ret {def_map: e.def_map, ext_map: e.ext_map};
@@ -238,6 +238,7 @@ fn map_crate(e: @env, c: @ast::crate) {
238238
}
239239

240240
fn resolve_imports(e: env) {
241+
e.used_imports.track = true;
241242
e.imports.values {|v|
242243
alt v {
243244
todo(node_id, name, path, span, scopes) {
@@ -246,15 +247,15 @@ fn resolve_imports(e: env) {
246247
resolved(_, _, _, _, _) { }
247248
}
248249
};
250+
e.used_imports.track = false;
249251
e.sess.abort_if_errors();
250252
}
251253

252254
fn check_unused_imports(e: @env) {
253-
let used = option::get(e.used_imports);
254255
e.imports.items {|k, v|
255256
alt v {
256257
resolved(val, ty, md, name, sp) {
257-
if !vec::member(k, used) {
258+
if !vec::member(k, e.used_imports.data) {
258259
e.sess.span_warn(sp, "unused import " + name);
259260
}
260261
}
@@ -264,6 +265,7 @@ fn check_unused_imports(e: @env) {
264265
}
265266

266267
fn resolve_names(e: @env, c: @ast::crate) {
268+
e.used_imports.track = true;
267269
let v =
268270
@{visit_native_item: visit_native_item_with_scope,
269271
visit_item: visit_item_with_scope,
@@ -277,6 +279,7 @@ fn resolve_names(e: @env, c: @ast::crate) {
277279
visit_fn: bind visit_fn_with_scope(e, _, _, _, _, _, _, _)
278280
with *visit::default_visitor()};
279281
visit::visit_crate(*c, cons(scope_crate, @nil), visit::mk_vt(v));
282+
e.used_imports.track = false;
280283
e.sess.abort_if_errors();
281284

282285
fn walk_expr(e: @env, exp: @ast::expr, sc: scopes, v: vt<scopes>) {
@@ -950,12 +953,8 @@ fn lookup_import(e: env, defid: def_id, ns: namespace) -> option::t<def> {
950953
ret none;
951954
}
952955
resolved(val, typ, md, _, _) {
953-
alt e.used_imports {
954-
none. { }
955-
some(lst_) {
956-
let lst = lst_ + [defid.node];
957-
e.used_imports = option::some(lst);
958-
}
956+
if e.used_imports.track {
957+
e.used_imports.data += [defid.node];
959958
}
960959
ret alt ns { ns_value. { val } ns_type. { typ } ns_module. { md } };
961960
}

0 commit comments

Comments
 (0)