Skip to content

Commit f451812

Browse files
committed
Auto merge of #26385 - nham:fix_25396, r=alexcrichton
Currently in the E0252 message, traits and modules are all called types (as in "a type named `Foo` has already been imported", even when `Foo` was a trait or module). This commit changes that to additionally detect when the import in question is a trait or module and report it accordingly. Fixes #25396.
2 parents 7a13b93 + b637f6b commit f451812

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/librustc_resolve/resolve_imports.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,19 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
900900
match target {
901901
Some(ref target) if target.shadowable != Shadowable::Always => {
902902
let ns_word = match namespace {
903-
TypeNS => "type",
903+
TypeNS => {
904+
if let Some(ref ty_def) = *target.bindings.type_def.borrow() {
905+
match ty_def.module_def {
906+
Some(ref module)
907+
if module.kind.get() == ModuleKind::NormalModuleKind =>
908+
"module",
909+
Some(ref module)
910+
if module.kind.get() == ModuleKind::TraitModuleKind =>
911+
"trait",
912+
_ => "type",
913+
}
914+
} else { "type" }
915+
},
904916
ValueNS => "value",
905917
};
906918
span_err!(self.resolver.session, import_span, E0252,

src/test/compile-fail/issue-25396.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
11+
use foo::baz;
12+
use bar::baz; //~ ERROR a module named `baz` has already been imported
13+
14+
use foo::Quux;
15+
use bar::Quux; //~ ERROR a trait named `Quux` has already been imported
16+
17+
use foo::blah;
18+
use bar::blah; //~ ERROR a type named `blah` has already been imported
19+
20+
use foo::WOMP;
21+
use bar::WOMP; //~ ERROR a value named `WOMP` has already been imported
22+
23+
fn main() {}
24+
25+
mod foo {
26+
pub mod baz {}
27+
pub trait Quux { }
28+
pub type blah = (f64, u32);
29+
pub const WOMP: u8 = 5;
30+
}
31+
32+
mod bar {
33+
pub mod baz {}
34+
pub type Quux = i32;
35+
struct blah { x: i8 }
36+
pub const WOMP: i8 = -5;
37+
}

0 commit comments

Comments
 (0)