Skip to content

Commit b07e26e

Browse files
committed
Resolve absolute paths as extern under a feature flag
1 parent 9552b9c commit b07e26e

File tree

15 files changed

+238
-3
lines changed

15 files changed

+238
-3
lines changed

src/librustc/middle/cstore.rs

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ impl CrateStore for DummyCrateStore {
357357
pub trait CrateLoader {
358358
fn process_item(&mut self, item: &ast::Item, defs: &Definitions);
359359
fn postprocess(&mut self, krate: &ast::Crate);
360+
fn resolve_crate_from_path(&mut self, name: Symbol, span: Span) -> CrateNum;
360361
}
361362

362363
// This method is used when generating the command line to pass through to

src/librustc_metadata/creader.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1087,4 +1087,8 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10871087
_ => {}
10881088
}
10891089
}
1090+
1091+
fn resolve_crate_from_path(&mut self, name: Symbol, span: Span) -> CrateNum {
1092+
self.resolve_crate(&None, name, name, None, span, PathKind::Crate, DepKind::Explicit).0
1093+
}
10901094
}

src/librustc_resolve/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
5858
use syntax::ast::{Local, Mutability, Pat, PatKind, Path};
5959
use syntax::ast::{QSelf, TraitItemKind, TraitRef, Ty, TyKind};
6060
use syntax::feature_gate::{feature_err, emit_feature_err, GateIssue};
61+
use syntax::parse::token;
6162

6263
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
6364
use errors::{DiagnosticBuilder, DiagnosticId};
@@ -2959,6 +2960,16 @@ impl<'a> Resolver<'a> {
29592960
// `$crate::a::b`
29602961
module = Some(self.resolve_crate_root(ident.node.ctxt));
29612962
continue
2963+
} else if i == 1 && self.session.features.borrow().extern_absolute_paths &&
2964+
path[0].node.name == keywords::CrateRoot.name() &&
2965+
!token::Ident(ident.node).is_path_segment_keyword() {
2966+
// `::extern_crate::a::b`
2967+
let crate_id = self.crate_loader.resolve_crate_from_path(name, ident.span);
2968+
let crate_root =
2969+
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
2970+
self.populate_module_if_necessary(crate_root);
2971+
module = Some(crate_root);
2972+
continue
29622973
}
29632974
}
29642975

src/librustc_resolve/resolve_imports.rs

+55-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use self::ImportDirectiveSubclass::*;
1212

1313
use {AmbiguityError, Module, PerNS};
1414
use Namespace::{self, TypeNS, MacroNS};
15-
use {NameBinding, NameBindingKind, PathResult, PrivacyError};
15+
use {NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyError};
1616
use Resolver;
1717
use {names_to_string, module_to_string};
1818
use {resolve_error, ResolutionError};
1919

2020
use rustc::ty;
2121
use rustc::lint::builtin::PUB_USE_OF_PRIVATE_EXTERN_CRATE;
22-
use rustc::hir::def_id::DefId;
22+
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
2323
use rustc::hir::def::*;
2424
use rustc::session::DiagnosticMessageId;
2525
use rustc::util::nodemap::{FxHashMap, FxHashSet};
@@ -602,8 +602,60 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
602602
// If appropriate, returns an error to report.
603603
fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<(Span, String)> {
604604
self.current_module = directive.parent;
605-
606605
let ImportDirective { ref module_path, span, .. } = *directive;
606+
607+
// Extern crate mode for absolute paths needs some
608+
// special support for single-segment imports.
609+
let extern_absolute_paths = self.session.features.borrow().extern_absolute_paths;
610+
if module_path.len() == 1 && module_path[0].node.name == keywords::CrateRoot.name() {
611+
match directive.subclass {
612+
GlobImport { .. } if extern_absolute_paths => {
613+
return Some((directive.span,
614+
"cannot glob-import all possible crates".to_string()));
615+
}
616+
SingleImport { source, target, .. } => {
617+
let crate_root = if source.name == keywords::Crate.name() {
618+
if target.name == keywords::Crate.name() {
619+
return Some((directive.span,
620+
"crate root imports need to be explicitly named: \
621+
`use crate as name;`".to_string()));
622+
} else {
623+
Some(self.resolve_crate_root(source.ctxt.modern()))
624+
}
625+
} else if extern_absolute_paths &&
626+
!token::Ident(source).is_path_segment_keyword() {
627+
let crate_id =
628+
self.crate_loader.resolve_crate_from_path(source.name, directive.span);
629+
let crate_root =
630+
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
631+
self.populate_module_if_necessary(crate_root);
632+
Some(crate_root)
633+
} else {
634+
None
635+
};
636+
637+
if let Some(crate_root) = crate_root {
638+
let binding = (crate_root, ty::Visibility::Public, directive.span,
639+
directive.expansion).to_name_binding(self.arenas);
640+
let binding = self.arenas.alloc_name_binding(NameBinding {
641+
kind: NameBindingKind::Import {
642+
binding,
643+
directive,
644+
used: Cell::new(false),
645+
legacy_self_import: false,
646+
},
647+
vis: directive.vis.get(),
648+
span: directive.span,
649+
expansion: directive.expansion,
650+
});
651+
let _ = self.try_define(directive.parent, target, TypeNS, binding);
652+
return None;
653+
}
654+
}
655+
_ => {}
656+
}
657+
}
658+
607659
let module_result = self.resolve_path(&module_path, None, true, span);
608660
let module = match module_result {
609661
PathResult::Module(module) => module,

src/libsyntax/feature_gate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ declare_features! (
430430

431431
// generic associated types (RFC 1598)
432432
(active, generic_associated_types, "1.23.0", Some(44265)),
433+
434+
// Resolve absolute paths as paths from other crates
435+
(active, extern_absolute_paths, "1.24.0", Some(44660)),
433436
);
434437

435438
declare_features! (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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+
#[derive(Debug)]
12+
pub struct S;
13+
14+
#[derive(Debug)]
15+
pub struct Z;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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(extern_absolute_paths)]
12+
13+
use xcrate::S; //~ ERROR can't find crate for `xcrate`
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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(extern_absolute_paths)]
12+
13+
fn main() {
14+
let s = ::xcrate::S; //~ ERROR can't find crate for `xcrate`
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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(extern_absolute_paths)]
12+
13+
use ycrate; //~ ERROR can't find crate for `ycrate`
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 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:xcrate.rs
12+
13+
#![feature(crate_in_paths)]
14+
#![feature(extern_absolute_paths)]
15+
16+
use crate; //~ ERROR unresolved import `crate`
17+
//~^ NOTE crate root imports need to be explicitly named: `use crate as name;`
18+
use *; //~ ERROR unresolved import `*`
19+
//~^ NOTE cannot glob-import all possible crates
20+
21+
fn main() {
22+
let s = ::xcrate; //~ ERROR expected value, found module `xcrate`
23+
//~^ NOTE not a value
24+
}

src/test/run-pass/rfc-2126-crate-paths/crate-path-absolute.rs

+5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
#![feature(crate_in_paths)]
1212

1313
use crate::m::f;
14+
use crate as root;
1415

1516
mod m {
1617
pub fn f() -> u8 { 1 }
1718
pub fn g() -> u8 { 2 }
19+
pub fn h() -> u8 { 3 }
1820

1921
// OK, visibilities are implicitly absolute like imports
2022
pub(in crate::m) struct S;
@@ -23,14 +25,17 @@ mod m {
2325
mod n
2426
{
2527
use crate::m::f;
28+
use crate as root;
2629
pub fn check() {
2730
assert_eq!(f(), 1);
2831
assert_eq!(::crate::m::g(), 2);
32+
assert_eq!(root::m::h(), 3);
2933
}
3034
}
3135

3236
fn main() {
3337
assert_eq!(f(), 1);
3438
assert_eq!(::crate::m::g(), 2);
39+
assert_eq!(root::m::h(), 3);
3540
n::check();
3641
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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+
#[derive(Debug)]
12+
pub struct S;
13+
14+
#[derive(Debug)]
15+
pub struct Z;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2017 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:xcrate.rs
12+
13+
#![feature(extern_absolute_paths)]
14+
15+
use xcrate::Z;
16+
17+
fn f() {
18+
use xcrate;
19+
use xcrate as ycrate;
20+
let s = xcrate::S;
21+
assert_eq!(format!("{:?}", s), "S");
22+
let z = ycrate::Z;
23+
assert_eq!(format!("{:?}", z), "Z");
24+
}
25+
26+
fn main() {
27+
let s = ::xcrate::S;
28+
assert_eq!(format!("{:?}", s), "S");
29+
let z = Z;
30+
assert_eq!(format!("{:?}", z), "Z");
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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+
use core::default; //~ ERROR unresolved import `core`
12+
13+
fn main() {
14+
let _: u8 = ::core::default::Default(); //~ ERROR failed to resolve
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0432]: unresolved import `core`
2+
--> $DIR/feature-gate-extern_absolute_paths.rs:11:5
3+
|
4+
11 | use core::default; //~ ERROR unresolved import `core`
5+
| ^^^^ Maybe a missing `extern crate core;`?
6+
7+
error[E0433]: failed to resolve. Maybe a missing `extern crate core;`?
8+
--> $DIR/feature-gate-extern_absolute_paths.rs:14:19
9+
|
10+
14 | let _: u8 = ::core::default::Default(); //~ ERROR failed to resolve
11+
| ^^^^ Maybe a missing `extern crate core;`?
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)