-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #47156 - petrochenkov:extpath, r=nikomatsakis
Support `extern` in paths Implement the primary alternative to #46613 + #45771, achieving the same effect without requiring changes to other imports. Both need to be experimentally evaluated before making further progress. The PR also adds docs for all these related features into the unstable book. cc #44660 r? @nikomatsakis
- Loading branch information
Showing
17 changed files
with
322 additions
and
26 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/doc/unstable-book/src/language-features/crate_in_paths.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# `crate_in_paths` | ||
|
||
The tracking issue for this feature is: [#44660] | ||
|
||
[#44660]: https://github.com/rust-lang/rust/issues/44660 | ||
|
||
------------------------ | ||
|
||
The `crate_in_paths` feature allows to explicitly refer to the crate root in absolute paths | ||
using keyword `crate`. | ||
|
||
`crate` can be used *only* in absolute paths, i.e. either in `::crate::a::b::c` form or in `use` | ||
items where the starting `::` is added implicitly. | ||
Paths like `crate::a::b::c` are not accepted currently. | ||
|
||
This feature is required in `feature(extern_absolute_paths)` mode to refer to any absolute path | ||
in the local crate (absolute paths refer to extern crates by default in that mode), but can be | ||
used without `feature(extern_absolute_paths)` as well. | ||
|
||
```rust | ||
#![feature(crate_in_paths)] | ||
|
||
// Imports, `::` is added implicitly | ||
use crate::m::f; | ||
use crate as root; | ||
|
||
mod m { | ||
pub fn f() -> u8 { 1 } | ||
pub fn g() -> u8 { 2 } | ||
pub fn h() -> u8 { 3 } | ||
|
||
// OK, visibilities implicitly add starting `::` as well, like imports | ||
pub(in crate::m) struct S; | ||
} | ||
|
||
mod n | ||
{ | ||
use crate::m::f; | ||
use crate as root; | ||
pub fn check() { | ||
assert_eq!(f(), 1); | ||
// `::` is required in non-import paths | ||
assert_eq!(::crate::m::g(), 2); | ||
assert_eq!(root::m::h(), 3); | ||
} | ||
} | ||
|
||
fn main() { | ||
assert_eq!(f(), 1); | ||
assert_eq!(::crate::m::g(), 2); | ||
assert_eq!(root::m::h(), 3); | ||
n::check(); | ||
} | ||
``` |
43 changes: 43 additions & 0 deletions
43
src/doc/unstable-book/src/language-features/extern_absolute_paths.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# `extern_absolute_paths` | ||
|
||
The tracking issue for this feature is: [#44660] | ||
|
||
[#44660]: https://github.com/rust-lang/rust/issues/44660 | ||
|
||
------------------------ | ||
|
||
The `extern_absolute_paths` feature enables mode allowing to refer to names from other crates | ||
"inline", without introducing `extern crate` items, using absolute paths like `::my_crate::a::b`. | ||
|
||
`::my_crate::a::b` will resolve to path `a::b` in crate `my_crate`. | ||
|
||
`feature(crate_in_paths)` can be used in `feature(extern_absolute_paths)` mode for referring | ||
to absolute paths in the local crate (`::crate::a::b`). | ||
|
||
`feature(extern_in_paths)` provides the same effect by using keyword `extern` to refer to | ||
paths from other crates (`extern::my_crate::a::b`). | ||
|
||
```rust,ignore | ||
#![feature(extern_absolute_paths)] | ||
// Suppose we have a dependency crate `xcrate` available through `Cargo.toml`, or `--extern` | ||
// options, or standard Rust distribution, or some other means. | ||
use xcrate::Z; | ||
fn f() { | ||
use xcrate; | ||
use xcrate as ycrate; | ||
let s = xcrate::S; | ||
assert_eq!(format!("{:?}", s), "S"); | ||
let z = ycrate::Z; | ||
assert_eq!(format!("{:?}", z), "Z"); | ||
} | ||
fn main() { | ||
let s = ::xcrate::S; | ||
assert_eq!(format!("{:?}", s), "S"); | ||
let z = Z; | ||
assert_eq!(format!("{:?}", z), "Z"); | ||
} | ||
``` |
40 changes: 40 additions & 0 deletions
40
src/doc/unstable-book/src/language-features/extern_in_paths.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# `extern_in_paths` | ||
|
||
The tracking issue for this feature is: [#44660] | ||
|
||
[#44660]: https://github.com/rust-lang/rust/issues/44660 | ||
|
||
------------------------ | ||
|
||
The `extern_in_paths` feature allows to refer to names from other crates "inline", without | ||
introducing `extern crate` items, using keyword `extern`. | ||
|
||
For example, `extern::my_crat::a::b` will resolve to path `a::b` in crate `my_crate`. | ||
|
||
`feature(extern_absolute_paths)` mode provides the same effect by resolving absolute paths like | ||
`::my_crate::a::b` to paths from extern crates by default. | ||
|
||
```rust,ignore | ||
#![feature(extern_in_paths)] | ||
// Suppose we have a dependency crate `xcrate` available through `Cargo.toml`, or `--extern` | ||
// options, or standard Rust distribution, or some other means. | ||
use extern::xcrate::Z; | ||
fn f() { | ||
use extern::xcrate; | ||
use extern::xcrate as ycrate; | ||
let s = xcrate::S; | ||
assert_eq!(format!("{:?}", s), "S"); | ||
let z = ycrate::Z; | ||
assert_eq!(format!("{:?}", z), "Z"); | ||
} | ||
fn main() { | ||
let s = extern::xcrate::S; | ||
assert_eq!(format!("{:?}", s), "S"); | ||
let z = Z; | ||
assert_eq!(format!("{:?}", z), "Z"); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test/compile-fail/rfc-2126-extern-in-paths/auxiliary/xcrate.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
15 changes: 15 additions & 0 deletions
15
src/test/compile-fail/rfc-2126-extern-in-paths/non-existent-1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_in_paths)] | ||
|
||
use extern::xcrate::S; //~ ERROR can't find crate for `xcrate` | ||
|
||
fn main() {} |
15 changes: 15 additions & 0 deletions
15
src/test/compile-fail/rfc-2126-extern-in-paths/non-existent-2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_in_paths)] | ||
|
||
fn main() { | ||
let s = extern::xcrate::S; //~ ERROR can't find crate for `xcrate` | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/compile-fail/rfc-2126-extern-in-paths/non-existent-3.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_in_paths)] | ||
|
||
use extern::ycrate; //~ ERROR can't find crate for `ycrate` | ||
|
||
fn main() {} |
Oops, something went wrong.