Skip to content

Commit a243e88

Browse files
committed
auto merge of #19522 : mukilan/rust/import-conflicts-item, r=cmr
Fixes #19498
2 parents 1e835cc + 4b75a5d commit a243e88

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/librustc/middle/resolve.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -2654,10 +2654,34 @@ impl<'a> Resolver<'a> {
26542654

26552655
}
26562656
Some(_) => {
2657-
// The import is unresolved. Bail out.
2658-
debug!("(resolving single import) unresolved import; \
2659-
bailing out");
2660-
return Indeterminate;
2657+
// If containing_module is the same module whose import we are resolving
2658+
// and there it has an unresolved import with the same name as `source`,
2659+
// then the user is actually trying to import an item that is declared
2660+
// in the same scope
2661+
//
2662+
// e.g
2663+
// use self::submodule;
2664+
// pub mod submodule;
2665+
//
2666+
// In this case we continue as if we resolved the import and let the
2667+
// check_for_conflicts_between_imports_and_items call below handle
2668+
// the conflict
2669+
match (module_.def_id.get(), containing_module.def_id.get()) {
2670+
(Some(id1), Some(id2)) if id1 == id2 => {
2671+
if value_result.is_unknown() {
2672+
value_result = UnboundResult;
2673+
}
2674+
if type_result.is_unknown() {
2675+
type_result = UnboundResult;
2676+
}
2677+
}
2678+
_ => {
2679+
// The import is unresolved. Bail out.
2680+
debug!("(resolving single import) unresolved import; \
2681+
bailing out");
2682+
return Indeterminate;
2683+
}
2684+
}
26612685
}
26622686
}
26632687
}
@@ -3018,7 +3042,7 @@ impl<'a> Resolver<'a> {
30183042
fn check_for_conflicts_between_imports_and_items(&mut self,
30193043
module: &Module,
30203044
import_resolution:
3021-
&mut ImportResolution,
3045+
&ImportResolution,
30223046
import_span: Span,
30233047
name: Name) {
30243048
if self.session.features.borrow().import_shadowing {
@@ -3031,8 +3055,9 @@ impl<'a> Resolver<'a> {
30313055
.contains_key(&name) {
30323056
match import_resolution.type_target {
30333057
Some(ref target) if !target.shadowable => {
3034-
let msg = format!("import `{}` conflicts with imported \
3035-
crate in this module",
3058+
let msg = format!("import `{0}` conflicts with imported \
3059+
crate in this module \
3060+
(maybe you meant `use {0}::*`?)",
30363061
token::get_name(name).get());
30373062
self.session.span_err(import_span, msg.as_slice());
30383063
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 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 self::A; //~ ERROR import `A` conflicts with existing submodule
12+
use self::B; //~ ERROR import `B` conflicts with existing submodule
13+
mod A {}
14+
pub mod B {}
15+
16+
mod C {
17+
use C::D; //~ ERROR import `D` conflicts with existing submodule
18+
mod D {}
19+
}
20+
21+
fn main() {}

src/test/compile-fail/unresolved-extern-mod-suggestion.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
extern crate core;
12-
use core; //~ ERROR unresolved import (maybe you meant `core::*`?)
12+
use core;
13+
//~^ ERROR import `core` conflicts with imported crate in this module
1314

1415
fn main() {}

0 commit comments

Comments
 (0)