Skip to content

Commit f582fa3

Browse files
committed
item_like_imports: Allow multiple glob imports of the same item.
1 parent 245a0c5 commit f582fa3

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/librustc_resolve/resolve_imports.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,17 @@ impl<'a> Resolver<'a> {
317317
where T: ToNameBinding<'a>
318318
{
319319
let binding = self.arenas.alloc_name_binding(binding.to_name_binding());
320-
self.update_resolution(module, name, ns, |_, resolution| {
320+
self.update_resolution(module, name, ns, |this, resolution| {
321321
if let Some(old_binding) = resolution.binding {
322322
if binding.is_glob_import() {
323-
resolution.duplicate_globs.push(binding);
323+
if !this.new_import_semantics || !old_binding.is_glob_import() {
324+
resolution.duplicate_globs.push(binding);
325+
} else if binding.def() != old_binding.def() {
326+
resolution.duplicate_globs.push(binding);
327+
} else if !old_binding.vis.is_at_least(binding.vis, this) {
328+
// We are glob-importing the same item but with greater visibility.
329+
resolution.binding = Some(binding);
330+
}
324331
} else if old_binding.is_glob_import() {
325332
resolution.duplicate_globs.push(old_binding);
326333
resolution.binding = Some(binding);
@@ -344,14 +351,17 @@ impl<'a> Resolver<'a> {
344351
// during which the resolution might end up getting re-defined via a glob cycle.
345352
let (binding, t) = {
346353
let mut resolution = &mut *self.resolution(module, name, ns).borrow_mut();
347-
let was_known = resolution.binding().is_some();
354+
let old_binding = resolution.binding();
348355

349356
let t = f(self, resolution);
350357

351-
if was_known { return t; }
352358
match resolution.binding() {
353-
Some(binding) => (binding, t),
359+
_ if !self.new_import_semantics && old_binding.is_some() => return t,
354360
None => return t,
361+
Some(binding) => match old_binding {
362+
Some(old_binding) if old_binding as *const _ == binding as *const _ => return t,
363+
_ => (binding, t),
364+
}
355365
}
356366
};
357367

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2016 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+
#![feature(item_like_imports)]
12+
13+
mod a {
14+
pub fn foo() {}
15+
}
16+
17+
mod b {
18+
pub fn foo() {}
19+
}
20+
21+
mod c {
22+
pub use a::foo;
23+
}
24+
25+
mod d {
26+
use a::foo; //~ NOTE previous import
27+
use a::foo; //~ ERROR `foo` has already been imported
28+
//~| NOTE already imported
29+
}
30+
31+
mod e {
32+
pub use a::*;
33+
pub use c::*; // ok
34+
}
35+
36+
fn main() {
37+
e::foo();
38+
}

0 commit comments

Comments
 (0)