Skip to content

Commit b637f6b

Browse files
author
Nick Hamann
committed
Fix the E0252 error message to use better names for things.
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.
1 parent 0250ff9 commit b637f6b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/librustc_resolve/resolve_imports.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use self::ImportDirectiveSubclass::*;
1212

1313
use DefModifiers;
1414
use Module;
15+
use ModuleKind;
1516
use Namespace::{self, TypeNS, ValueNS};
1617
use NameBindings;
1718
use NamespaceResult::{BoundResult, UnboundResult, UnknownResult};
@@ -899,7 +900,19 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
899900
match target {
900901
Some(ref target) if target.shadowable != Shadowable::Always => {
901902
let ns_word = match namespace {
902-
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+
},
903916
ValueNS => "value",
904917
};
905918
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)