Skip to content

Commit 235a7a7

Browse files
committed
Change self in an import list use foo::{self, ...} to only import a module or enum foo.
1 parent aee21e2 commit 235a7a7

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

src/librustc_resolve/build_reduced_graph.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ impl<'a> Resolver<'a> {
168168
target: binding,
169169
source: source,
170170
result: self.per_ns(|_, _| Cell::new(Err(Undetermined))),
171+
type_ns_only: false,
171172
};
172173
self.add_import_directive(
173174
module_path, subclass, view_path.span, item.id, vis, expansion,
@@ -195,10 +196,10 @@ impl<'a> Resolver<'a> {
195196

196197
for source_item in source_items {
197198
let node = source_item.node;
198-
let (module_path, ident, rename) = {
199+
let (module_path, ident, rename, type_ns_only) = {
199200
if node.name.name != keywords::SelfValue.name() {
200201
let rename = node.rename.unwrap_or(node.name);
201-
(module_path.clone(), node.name, rename)
202+
(module_path.clone(), node.name, rename, false)
202203
} else {
203204
let ident = *module_path.last().unwrap();
204205
if ident.name == keywords::CrateRoot.name() {
@@ -212,13 +213,14 @@ impl<'a> Resolver<'a> {
212213
}
213214
let module_path = module_path.split_last().unwrap().1;
214215
let rename = node.rename.unwrap_or(ident);
215-
(module_path.to_vec(), ident, rename)
216+
(module_path.to_vec(), ident, rename, true)
216217
}
217218
};
218219
let subclass = SingleImport {
219220
target: rename,
220221
source: ident,
221222
result: self.per_ns(|_, _| Cell::new(Err(Undetermined))),
223+
type_ns_only: type_ns_only,
222224
};
223225
let id = source_item.node.id;
224226
self.add_import_directive(

src/librustc_resolve/resolve_imports.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub enum ImportDirectiveSubclass<'a> {
4141
target: Ident,
4242
source: Ident,
4343
result: PerNS<Cell<Result<&'a NameBinding<'a>, Determinacy>>>,
44+
type_ns_only: bool,
4445
},
4546
GlobImport {
4647
is_prelude: bool,
@@ -503,8 +504,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
503504
};
504505

505506
directive.imported_module.set(Some(module));
506-
let (source, target, result) = match directive.subclass {
507-
SingleImport { source, target, ref result } => (source, target, result),
507+
let (source, target, result, type_ns_only) = match directive.subclass {
508+
SingleImport { source, target, ref result, type_ns_only } =>
509+
(source, target, result, type_ns_only),
508510
GlobImport { .. } => {
509511
self.resolve_glob_import(directive);
510512
return true;
@@ -513,7 +515,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
513515
};
514516

515517
let mut indeterminate = false;
516-
self.per_ns(|this, ns| {
518+
self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
517519
if let Err(Undetermined) = result[ns].get() {
518520
result[ns].set(this.resolve_ident_in_module(module, source, ns, false, None));
519521
} else {
@@ -573,8 +575,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
573575
_ => return None,
574576
};
575577

576-
let (ident, result) = match directive.subclass {
577-
SingleImport { source, ref result, .. } => (source, result),
578+
let (ident, result, type_ns_only) = match directive.subclass {
579+
SingleImport { source, ref result, type_ns_only, .. } => (source, result, type_ns_only),
578580
GlobImport { .. } if module.def_id() == directive.parent.def_id() => {
579581
// Importing a module into itself is not allowed.
580582
return Some("Cannot glob-import a module into itself.".to_string());
@@ -592,7 +594,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
592594
};
593595

594596
let mut all_ns_err = true;
595-
self.per_ns(|this, ns| {
597+
self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
596598
if let Ok(binding) = result[ns].get() {
597599
all_ns_err = false;
598600
if this.record_use(ident, ns, binding, directive.span) {
@@ -604,7 +606,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
604606

605607
if all_ns_err {
606608
let mut all_ns_failed = true;
607-
self.per_ns(|this, ns| {
609+
self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
608610
match this.resolve_ident_in_module(module, ident, ns, false, Some(span)) {
609611
Ok(_) => all_ns_failed = false,
610612
_ => {}

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

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ extern crate lint_stability;
1818

1919
use lint_stability::{unstable, deprecated}; //~ ERROR use of unstable library feature 'test_feature'
2020

21-
use lint_stability::unstable::{self as u}; //~ ERROR use of unstable library feature 'test_feature'
22-
2321
fn main() {
2422
}

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

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// Test that `fn foo::bar::{self}` only imports `bar` in the type namespace.
12+
13+
mod foo {
14+
pub fn f() { }
15+
}
16+
use foo::f::{self};
17+
//~^ ERROR unresolved import
18+
//~| NOTE no `f` in `foo`
19+
20+
mod bar {
21+
pub fn baz() {}
22+
pub mod baz {}
23+
}
24+
use bar::baz::{self};
25+
26+
fn main() {
27+
baz();
28+
//~^ ERROR unresolved name `baz`
29+
//~| NOTE unresolved name
30+
//~| HELP module `baz` cannot be used as an expression
31+
}

0 commit comments

Comments
 (0)