Skip to content

Commit feac9f1

Browse files
committed
Auto merge of #24818 - tbelaire:double-import, r=nrc
This isn't quite right, but it's interesting.
2 parents f2e1a1b + db9d018 commit feac9f1

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/librustc_resolve/resolve_imports.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
618618
namespace_name,
619619
name_bindings.def_for_namespace(namespace));
620620
self.check_for_conflicting_import(
621-
&import_resolution.target_for_namespace(namespace),
621+
&import_resolution,
622622
directive.span,
623623
target,
624624
namespace);
@@ -755,7 +755,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
755755
// Continue.
756756
}
757757
Some(ref value_target) => {
758-
self.check_for_conflicting_import(&dest_import_resolution.value_target,
758+
self.check_for_conflicting_import(&dest_import_resolution,
759759
import_directive.span,
760760
*ident,
761761
ValueNS);
@@ -767,7 +767,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
767767
// Continue.
768768
}
769769
Some(ref type_target) => {
770-
self.check_for_conflicting_import(&dest_import_resolution.type_target,
770+
self.check_for_conflicting_import(&dest_import_resolution,
771771
import_directive.span,
772772
*ident,
773773
TypeNS);
@@ -887,24 +887,31 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
887887

888888
/// Checks that imported names and items don't have the same name.
889889
fn check_for_conflicting_import(&mut self,
890-
target: &Option<Target>,
890+
import_resolution: &ImportResolution,
891891
import_span: Span,
892892
name: Name,
893893
namespace: Namespace) {
894+
let target = import_resolution.target_for_namespace(namespace);
894895
debug!("check_for_conflicting_import: {}; target exists: {}",
895896
&token::get_name(name),
896897
target.is_some());
897898

898-
match *target {
899+
match target {
899900
Some(ref target) if target.shadowable != Shadowable::Always => {
900-
let msg = format!("a {} named `{}` has already been imported \
901-
in this module",
902-
match namespace {
903-
TypeNS => "type",
904-
ValueNS => "value",
905-
},
901+
let ns_word = match namespace {
902+
TypeNS => "type",
903+
ValueNS => "value",
904+
};
905+
span_err!(self.resolver.session, import_span, E0252,
906+
"a {} named `{}` has already been imported \
907+
in this module", ns_word,
906908
&token::get_name(name));
907-
span_err!(self.resolver.session, import_span, E0252, "{}", &msg[..]);
909+
let use_id = import_resolution.id(namespace);
910+
let item = self.resolver.ast_map.expect_item(use_id);
911+
// item is syntax::ast::Item;
912+
span_note!(self.resolver.session, item.span,
913+
"previous import of `{}` here",
914+
token::get_name(name));
908915
}
909916
Some(_) | None => {}
910917
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(no_std)]
11+
#![no_std]
12+
13+
// This tests that conflicting imports shows both `use` lines
14+
// when reporting the error.
15+
16+
mod sub1 {
17+
fn foo() {} // implementation 1
18+
}
19+
20+
mod sub2 {
21+
fn foo() {} // implementation 2
22+
}
23+
24+
use sub1::foo; //~ NOTE previous import of `foo` here
25+
use sub2::foo; //~ ERROR a value named `foo` has already been imported in this module [E0252]
26+
27+
fn main() {}

0 commit comments

Comments
 (0)