Skip to content

Commit b052e23

Browse files
committed
Fix import restrictions
I am not happy with the way that we track importability for external crates
1 parent 24d9db9 commit b052e23

9 files changed

+245
-57
lines changed

src/librustc/middle/resolve.rs

+84-50
Large diffs are not rendered by default.

src/test/auxiliary/namespaced_enums.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ pub enum Foo {
1515
C { a: int },
1616
}
1717

18+
impl Foo {
19+
pub fn foo() {}
20+
pub fn bar(&self) {}
21+
}
22+

src/test/auxiliary/use_from_trait_xc.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
pub use self::sub::Bar;
11+
pub use self::sub::{Bar, Baz};
1212

1313
pub trait Trait {
1414
fn foo();
@@ -26,4 +26,10 @@ mod sub {
2626
impl Bar {
2727
pub fn new() {}
2828
}
29+
30+
pub enum Baz {}
31+
32+
impl Baz {
33+
pub fn new() {}
34+
}
2935
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 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+
// aux-build:namespaced_enums.rs
12+
#![feature(struct_variant, globs)]
13+
14+
extern crate namespaced_enums;
15+
16+
mod m {
17+
pub use namespaced_enums::Foo::*;
18+
}
19+
20+
pub fn main() {
21+
use namespaced_enums::Foo::*;
22+
23+
foo(); //~ ERROR unresolved name `foo`
24+
m::foo(); //~ ERROR unresolved name `m::foo`
25+
bar(); //~ ERROR unresolved name `bar`
26+
m::bar(); //~ ERROR unresolved name `m::bar`
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2014 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+
#![feature(struct_variant, globs)]
11+
12+
mod m2 {
13+
pub enum Foo {
14+
A,
15+
B(int),
16+
C { a: int },
17+
}
18+
19+
impl Foo {
20+
pub fn foo() {}
21+
pub fn bar(&self) {}
22+
}
23+
}
24+
25+
mod m {
26+
pub use m2::Foo::*;
27+
}
28+
29+
pub fn main() {
30+
use m2::Foo::*;
31+
32+
foo(); //~ ERROR unresolved name `foo`
33+
m::foo(); //~ ERROR unresolved name `m::foo`
34+
bar(); //~ ERROR unresolved name `bar`
35+
m::bar(); //~ ERROR unresolved name `m::bar`
36+
}

src/test/compile-fail/use-from-trait-xc.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
extern crate use_from_trait_xc;
1414

1515
use use_from_trait_xc::Trait::foo;
16-
//~^ ERROR unresolved import `use_from_trait_xc::Trait::foo`. Cannot import from a trait or type imp
16+
//~^ ERROR `foo` is not directly importable
1717

1818
use use_from_trait_xc::Foo::new;
19-
//~^ ERROR unresolved import `use_from_trait_xc::Foo::new`. Cannot import from a trait or type imple
19+
//~^ ERROR `new` is not directly importable
2020

21-
use use_from_trait_xc::Bar::new;
22-
//~^ ERROR unresolved import `use_from_trait_xc::Bar::new`. Cannot import from a trait or type
21+
use use_from_trait_xc::Bar::new as bnew;
22+
//~^ ERROR `bnew` is not directly importable
23+
24+
use use_from_trait_xc::Baz::new as baznew;
25+
//~^ ERROR `baznew` is not directly importable
2326

2427
fn main() {}

src/test/compile-fail/use-from-trait.rs

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

1111
use Trait::foo;
12-
//~^ ERROR unresolved import `Trait::foo`. Cannot import from a trait or type implementation
12+
//~^ ERROR `foo` is not directly importable
1313
use Foo::new;
14-
//~^ ERROR unresolved import `Foo::new`. Cannot import from a trait or type implementation
14+
//~^ ERROR `new` is not directly importable
1515

1616
pub trait Trait {
1717
fn foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2014 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+
// aux-build:namespaced_enums.rs
12+
#![feature(globs, struct_variant)]
13+
14+
extern crate namespaced_enums;
15+
16+
fn _f(f: namespaced_enums::Foo) {
17+
use namespaced_enums::Foo::*;
18+
19+
match f {
20+
A | B(_) | C { .. } => {}
21+
}
22+
}
23+
24+
mod m {
25+
pub use namespaced_enums::Foo::*;
26+
}
27+
28+
fn _f2(f: namespaced_enums::Foo) {
29+
match f {
30+
m::A | m::B(_) | m::C { .. } => {}
31+
}
32+
}
33+
34+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2014 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+
#![feature(globs, struct_variant)]
11+
12+
mod m2 {
13+
pub enum Foo {
14+
A,
15+
B(int),
16+
C { a: int },
17+
}
18+
19+
impl Foo {
20+
pub fn foo() {}
21+
}
22+
}
23+
24+
mod m {
25+
pub use m2::Foo::*;
26+
}
27+
28+
fn _f(f: m2::Foo) {
29+
use m2::Foo::*;
30+
31+
match f {
32+
A | B(_) | C { .. } => {}
33+
}
34+
}
35+
36+
fn _f2(f: m2::Foo) {
37+
match f {
38+
m::A | m::B(_) | m::C { .. } => {}
39+
}
40+
}
41+
42+
pub fn main() {}

0 commit comments

Comments
 (0)