Skip to content

Commit eb51dfd

Browse files
committed
Add an extended prelude for the 2018 editions
1 parent 1a1acdc commit eb51dfd

File tree

10 files changed

+179
-3
lines changed

10 files changed

+179
-3
lines changed

src/libcore/prelude/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
#![stable(feature = "core_prelude", since = "1.4.0")]
1414

1515
pub mod v1;
16+
pub mod rust2018;

src/libcore/prelude/rust2018.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2018 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+
//! The 2018 edition core prelude
12+
//!
13+
//! This module is intended for users of libcore which do not link to libstd as
14+
//! well. This module is imported by default when `#![no_std]` is used in the
15+
//! same manner as the standard library's prelude.
16+
17+
#![unstable(feature = "rust2018_prelude", issue = "51418")]
18+
19+
// Re-exported core operators
20+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
21+
#[doc(no_inline)] pub use marker::{Copy, Send, Sized, Sync};
22+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
23+
#[doc(no_inline)] pub use ops::{Drop, Fn, FnMut, FnOnce};
24+
25+
// Re-exported functions
26+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
27+
#[doc(no_inline)] pub use mem::drop;
28+
29+
// Re-exported types and traits
30+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
31+
#[doc(no_inline)] pub use clone::Clone;
32+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
33+
#[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
34+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
35+
#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From};
36+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
37+
#[doc(no_inline)] pub use default::Default;
38+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
39+
#[doc(no_inline)] pub use iter::{Iterator, Extend, IntoIterator};
40+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
41+
#[doc(no_inline)] pub use iter::{DoubleEndedIterator, ExactSizeIterator};
42+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
43+
#[doc(no_inline)] pub use option::Option::{self, Some, None};
44+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
45+
#[doc(no_inline)] pub use result::Result::{self, Ok, Err};
46+
47+
48+
// Contents so far are equivalent to v1.rs
49+
50+
51+
// Not in v1.rs because of breakage: https://github.com/rust-lang/rust/pull/49518
52+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
53+
#[doc(no_inline)] pub use convert::{TryFrom, TryInto};

src/librustc_driver/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ where
850850

851851
krate = time(sess, "crate injection", || {
852852
let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s);
853-
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name)
853+
syntax::std_inject::maybe_inject_crates_ref(sess.edition(), krate, alt_std_name)
854854
});
855855

856856
let mut addl_plugins = Some(addl_plugins);

src/libstd/prelude/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,4 @@
146146
#![stable(feature = "rust1", since = "1.0.0")]
147147

148148
pub mod v1;
149+
pub mod rust2018;

src/libstd/prelude/rust2018.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2013 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+
12+
//! The 2018 edition of the prelude of The Rust Standard Library.
13+
//!
14+
//! See the [module-level documentation](../index.html) for more.
15+
16+
17+
#![unstable(feature = "rust2018_prelude", issue = "51418")]
18+
19+
// Re-exported core operators
20+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
21+
#[doc(no_inline)] pub use marker::{Copy, Send, Sized, Sync};
22+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
23+
#[doc(no_inline)] pub use ops::{Drop, Fn, FnMut, FnOnce};
24+
25+
// Re-exported functions
26+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
27+
#[doc(no_inline)] pub use mem::drop;
28+
29+
// Re-exported types and traits
30+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
31+
#[doc(no_inline)] pub use clone::Clone;
32+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
33+
#[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
34+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
35+
#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From};
36+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
37+
#[doc(no_inline)] pub use default::Default;
38+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
39+
#[doc(no_inline)] pub use iter::{Iterator, Extend, IntoIterator};
40+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
41+
#[doc(no_inline)] pub use iter::{DoubleEndedIterator, ExactSizeIterator};
42+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
43+
#[doc(no_inline)] pub use option::Option::{self, Some, None};
44+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
45+
#[doc(no_inline)] pub use result::Result::{self, Ok, Err};
46+
47+
48+
// Contents so far are equivalent to src/libcore/prelude/v1.rs
49+
50+
51+
// Re-exported types and traits that involve memory allocation
52+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
53+
#[doc(no_inline)] pub use boxed::Box;
54+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
55+
#[doc(no_inline)] pub use borrow::ToOwned;
56+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
57+
#[doc(no_inline)] pub use slice::SliceConcatExt;
58+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
59+
#[doc(no_inline)] pub use string::{String, ToString};
60+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
61+
#[doc(no_inline)] pub use vec::Vec;
62+
63+
64+
// Contents so far are equivalent to v1.rs
65+
66+
67+
// Not in v1.rs because of breakage: https://github.com/rust-lang/rust/pull/49518
68+
#[unstable(feature = "rust2018_prelude", issue = "51418")]
69+
#[doc(no_inline)] pub use convert::{TryFrom, TryInto};

src/libsyntax/std_inject.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ast;
1212
use attr;
1313
use std::cell::Cell;
14+
use edition::Edition;
1415
use ext::hygiene::{Mark, SyntaxContext};
1516
use symbol::{Symbol, keywords};
1617
use syntax_pos::{DUMMY_SP, Span};
@@ -44,7 +45,11 @@ thread_local! {
4445
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
4546
}
4647

47-
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
48+
pub fn maybe_inject_crates_ref(
49+
edition: Edition,
50+
mut krate: ast::Crate,
51+
alt_std_name: Option<&str>,
52+
) -> ast::Crate {
4853
// the first name in this list is the crate name of the crate with the prelude
4954
let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") {
5055
return krate;
@@ -92,7 +97,7 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>
9297
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
9398
node: ast::ItemKind::Use(P(ast::UseTree {
9499
prefix: ast::Path {
95-
segments: [name, "prelude", "v1"].into_iter().map(|name| {
100+
segments: [name, "prelude", edition.prelude_module()].into_iter().map(|name| {
96101
ast::PathSegment::from_ident(ast::Ident::from_str(name))
97102
}).collect(),
98103
span,

src/libsyntax_pos/edition.rs

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ impl Edition {
6868
Edition::Edition2018 => false,
6969
}
7070
}
71+
72+
/// Name of the respective sub-modules of `std::prelude` and `core::prelude`.
73+
pub fn prelude_module(&self) -> &'static str {
74+
match *self {
75+
Edition::Edition2015 => "v1",
76+
Edition::Edition2018 => "rust2018",
77+
}
78+
}
7179
}
7280

7381
impl FromStr for Edition {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 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+
// compile-flags: --edition=2018
12+
13+
#![no_std]
14+
#![feature(try_from)]
15+
16+
extern crate std;
17+
18+
fn main() {
19+
u8::try_from(4_u32).unwrap(); // Using the TryFrom trait from core::prelude::rust2018
20+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 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+
// compile-flags: --edition=2018
12+
13+
#![feature(try_from)]
14+
15+
fn main() {
16+
u8::try_from(4_u32).unwrap(); // Using the TryFrom trait from std::prelude::rust2018
17+
}

src/test/ui/issue-35675.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ error[E0573]: expected type, found variant `Ok`
4242
LL | fn foo() -> Ok {
4343
| ^^ not a type
4444
|
45+
= help: there is an enum variant `std::prelude::rust2018::Ok`, try using `std::prelude::rust2018`?
4546
= help: there is an enum variant `std::prelude::v1::Ok`, try using `std::prelude::v1`?
4647
= help: there is an enum variant `std::result::Result::Ok`, try using `std::result::Result`?
4748

@@ -60,6 +61,7 @@ error[E0573]: expected type, found variant `Some`
6061
LL | fn qux() -> Some {
6162
| ^^^^ not a type
6263
|
64+
= help: there is an enum variant `std::prelude::rust2018::Some`, try using `std::prelude::rust2018`?
6365
= help: there is an enum variant `std::prelude::v1::Option::Some`, try using `std::prelude::v1::Option`?
6466
= help: there is an enum variant `std::prelude::v1::Some`, try using `std::prelude::v1`?
6567

0 commit comments

Comments
 (0)