mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-21 17:52:12 +03:00
item_like_imports: Allow multiple glob imports of the same item.
This commit is contained in:
@@ -317,10 +317,17 @@ pub fn try_define<T>(&mut self, module: Module<'a>, name: Name, ns: Namespace, b
|
||||
where T: ToNameBinding<'a>
|
||||
{
|
||||
let binding = self.arenas.alloc_name_binding(binding.to_name_binding());
|
||||
self.update_resolution(module, name, ns, |_, resolution| {
|
||||
self.update_resolution(module, name, ns, |this, resolution| {
|
||||
if let Some(old_binding) = resolution.binding {
|
||||
if binding.is_glob_import() {
|
||||
resolution.duplicate_globs.push(binding);
|
||||
if !this.new_import_semantics || !old_binding.is_glob_import() {
|
||||
resolution.duplicate_globs.push(binding);
|
||||
} else if binding.def() != old_binding.def() {
|
||||
resolution.duplicate_globs.push(binding);
|
||||
} else if !old_binding.vis.is_at_least(binding.vis, this) {
|
||||
// We are glob-importing the same item but with greater visibility.
|
||||
resolution.binding = Some(binding);
|
||||
}
|
||||
} else if old_binding.is_glob_import() {
|
||||
resolution.duplicate_globs.push(old_binding);
|
||||
resolution.binding = Some(binding);
|
||||
@@ -344,14 +351,17 @@ fn update_resolution<T, F>(&mut self, module: Module<'a>, name: Name, ns: Namesp
|
||||
// during which the resolution might end up getting re-defined via a glob cycle.
|
||||
let (binding, t) = {
|
||||
let mut resolution = &mut *self.resolution(module, name, ns).borrow_mut();
|
||||
let was_known = resolution.binding().is_some();
|
||||
let old_binding = resolution.binding();
|
||||
|
||||
let t = f(self, resolution);
|
||||
|
||||
if was_known { return t; }
|
||||
match resolution.binding() {
|
||||
Some(binding) => (binding, t),
|
||||
_ if !self.new_import_semantics && old_binding.is_some() => return t,
|
||||
None => return t,
|
||||
Some(binding) => match old_binding {
|
||||
Some(old_binding) if old_binding as *const _ == binding as *const _ => return t,
|
||||
_ => (binding, t),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(item_like_imports)]
|
||||
|
||||
mod a {
|
||||
pub fn foo() {}
|
||||
}
|
||||
|
||||
mod b {
|
||||
pub fn foo() {}
|
||||
}
|
||||
|
||||
mod c {
|
||||
pub use a::foo;
|
||||
}
|
||||
|
||||
mod d {
|
||||
use a::foo; //~ NOTE previous import
|
||||
use a::foo; //~ ERROR `foo` has already been imported
|
||||
//~| NOTE already imported
|
||||
}
|
||||
|
||||
mod e {
|
||||
pub use a::*;
|
||||
pub use c::*; // ok
|
||||
}
|
||||
|
||||
fn main() {
|
||||
e::foo();
|
||||
}
|
||||
Reference in New Issue
Block a user