-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Resolve absolute paths as extern under a feature flag #46613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,14 @@ use self::ImportDirectiveSubclass::*; | |
|
||
use {AmbiguityError, Module, PerNS}; | ||
use Namespace::{self, TypeNS, MacroNS}; | ||
use {NameBinding, NameBindingKind, PathResult, PrivacyError}; | ||
use {NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyError}; | ||
use Resolver; | ||
use {names_to_string, module_to_string}; | ||
use {resolve_error, ResolutionError}; | ||
|
||
use rustc::ty; | ||
use rustc::lint::builtin::PUB_USE_OF_PRIVATE_EXTERN_CRATE; | ||
use rustc::hir::def_id::DefId; | ||
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId}; | ||
use rustc::hir::def::*; | ||
use rustc::session::DiagnosticMessageId; | ||
use rustc::util::nodemap::{FxHashMap, FxHashSet}; | ||
|
@@ -602,8 +602,60 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { | |
// If appropriate, returns an error to report. | ||
fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<(Span, String)> { | ||
self.current_module = directive.parent; | ||
|
||
let ImportDirective { ref module_path, span, .. } = *directive; | ||
|
||
// Extern crate mode for absolute paths needs some | ||
// special support for single-segment imports. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The part about single-segment imports ( |
||
let extern_absolute_paths = self.session.features.borrow().extern_absolute_paths; | ||
if module_path.len() == 1 && module_path[0].node.name == keywords::CrateRoot.name() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: as above, helper function? |
||
match directive.subclass { | ||
GlobImport { .. } if extern_absolute_paths => { | ||
return Some((directive.span, | ||
"cannot glob-import all possible crates".to_string())); | ||
} | ||
SingleImport { source, target, .. } => { | ||
let crate_root = if source.name == keywords::Crate.name() { | ||
if target.name == keywords::Crate.name() { | ||
return Some((directive.span, | ||
"crate root imports need to be explicitly named: \ | ||
`use crate as name;`".to_string())); | ||
} else { | ||
Some(self.resolve_crate_root(source.ctxt.modern())) | ||
} | ||
} else if extern_absolute_paths && | ||
!token::Ident(source).is_path_segment_keyword() { | ||
let crate_id = | ||
self.crate_loader.resolve_crate_from_path(source.name, directive.span); | ||
let crate_root = | ||
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX }); | ||
self.populate_module_if_necessary(crate_root); | ||
Some(crate_root) | ||
} else { | ||
None | ||
}; | ||
|
||
if let Some(crate_root) = crate_root { | ||
let binding = (crate_root, ty::Visibility::Public, directive.span, | ||
directive.expansion).to_name_binding(self.arenas); | ||
let binding = self.arenas.alloc_name_binding(NameBinding { | ||
kind: NameBindingKind::Import { | ||
binding, | ||
directive, | ||
used: Cell::new(false), | ||
legacy_self_import: false, | ||
}, | ||
vis: directive.vis.get(), | ||
span: directive.span, | ||
expansion: directive.expansion, | ||
}); | ||
let _ = self.try_define(directive.parent, target, TypeNS, binding); | ||
return None; | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
let module_result = self.resolve_path(&module_path, None, true, span); | ||
let module = match module_result { | ||
PathResult::Module(module) => module, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017 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. | ||
|
||
#[derive(Debug)] | ||
pub struct S; | ||
|
||
#[derive(Debug)] | ||
pub struct Z; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017 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(extern_absolute_paths)] | ||
|
||
use xcrate::S; //~ ERROR can't find crate for `xcrate` | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017 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(extern_absolute_paths)] | ||
|
||
fn main() { | ||
let s = ::xcrate::S; //~ ERROR can't find crate for `xcrate` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017 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(extern_absolute_paths)] | ||
|
||
use ycrate; //~ ERROR can't find crate for `ycrate` | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2017 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. | ||
|
||
// aux-build:xcrate.rs | ||
|
||
#![feature(crate_in_paths)] | ||
#![feature(extern_absolute_paths)] | ||
|
||
use crate; //~ ERROR unresolved import `crate` | ||
//~^ NOTE crate root imports need to be explicitly named: `use crate as name;` | ||
use *; //~ ERROR unresolved import `*` | ||
//~^ NOTE cannot glob-import all possible crates | ||
|
||
fn main() { | ||
let s = ::xcrate; //~ ERROR expected value, found module `xcrate` | ||
//~^ NOTE not a value | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017 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. | ||
|
||
#[derive(Debug)] | ||
pub struct S; | ||
|
||
#[derive(Debug)] | ||
pub struct Z; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: isn't there some helper function we can use here for this test against
keywords::CrateRoot.name()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no such existing function.
(Also there are too many "crate roots" with different properties now, it's better to be explicit.)