diff --git a/src/libcore/prelude/mod.rs b/src/libcore/prelude/mod.rs index 99b1947c84e4f..d785e82ed5a9b 100644 --- a/src/libcore/prelude/mod.rs +++ b/src/libcore/prelude/mod.rs @@ -13,3 +13,4 @@ #![stable(feature = "core_prelude", since = "1.4.0")] pub mod v1; +pub mod rust2018; diff --git a/src/libcore/prelude/rust2018.rs b/src/libcore/prelude/rust2018.rs new file mode 100644 index 0000000000000..5ff34dcae03fa --- /dev/null +++ b/src/libcore/prelude/rust2018.rs @@ -0,0 +1,53 @@ +// Copyright 2018 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! The 2018 edition core prelude +//! +//! This module is intended for users of libcore which do not link to libstd as +//! well. This module is imported by default when `#![no_std]` is used in the +//! same manner as the standard library's prelude. + +#![unstable(feature = "rust2018_prelude", issue = "51418")] + +// Re-exported core operators +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use marker::{Copy, Send, Sized, Sync}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use ops::{Drop, Fn, FnMut, FnOnce}; + +// Re-exported functions +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use mem::drop; + +// Re-exported types and traits +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use clone::Clone; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use default::Default; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use iter::{Iterator, Extend, IntoIterator}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use iter::{DoubleEndedIterator, ExactSizeIterator}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use option::Option::{self, Some, None}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use result::Result::{self, Ok, Err}; + + +// Contents so far are equivalent to v1.rs + + +// Not in v1.rs because of breakage: https://github.com/rust-lang/rust/pull/49518 +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use convert::{TryFrom, TryInto}; diff --git a/src/libcore/prelude/v1.rs b/src/libcore/prelude/v1.rs index 45f629a64424c..4ce4274c6b0bd 100644 --- a/src/libcore/prelude/v1.rs +++ b/src/libcore/prelude/v1.rs @@ -18,39 +18,28 @@ // Re-exported core operators #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use marker::{Copy, Send, Sized, Sync}; +#[doc(no_inline)] pub use marker::{Copy, Send, Sized, Sync}; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use ops::{Drop, Fn, FnMut, FnOnce}; +#[doc(no_inline)] pub use ops::{Drop, Fn, FnMut, FnOnce}; // Re-exported functions #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use mem::drop; +#[doc(no_inline)] pub use mem::drop; // Re-exported types and traits #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use clone::Clone; +#[doc(no_inline)] pub use clone::Clone; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use cmp::{PartialEq, PartialOrd, Eq, Ord}; +#[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord}; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use convert::{AsRef, AsMut, Into, From}; +#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From}; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use default::Default; +#[doc(no_inline)] pub use default::Default; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use iter::{Iterator, Extend, IntoIterator}; +#[doc(no_inline)] pub use iter::{Iterator, Extend, IntoIterator}; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use iter::{DoubleEndedIterator, ExactSizeIterator}; +#[doc(no_inline)] pub use iter::{DoubleEndedIterator, ExactSizeIterator}; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use option::Option::{self, Some, None}; +#[doc(no_inline)] pub use option::Option::{self, Some, None}; #[stable(feature = "core_prelude", since = "1.4.0")] -#[doc(no_inline)] -pub use result::Result::{self, Ok, Err}; +#[doc(no_inline)] pub use result::Result::{self, Ok, Err}; diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 37f8bff964f5b..56e38ae2af927 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -850,7 +850,7 @@ where krate = time(sess, "crate injection", || { let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s); - syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name) + syntax::std_inject::maybe_inject_crates_ref(sess.edition(), krate, alt_std_name) }); let mut addl_plugins = Some(addl_plugins); diff --git a/src/libstd/prelude/mod.rs b/src/libstd/prelude/mod.rs index 919e033f2b4bd..43ed5e37e3374 100644 --- a/src/libstd/prelude/mod.rs +++ b/src/libstd/prelude/mod.rs @@ -146,3 +146,4 @@ #![stable(feature = "rust1", since = "1.0.0")] pub mod v1; +pub mod rust2018; diff --git a/src/libstd/prelude/rust2018.rs b/src/libstd/prelude/rust2018.rs new file mode 100644 index 0000000000000..2ac5235cbd195 --- /dev/null +++ b/src/libstd/prelude/rust2018.rs @@ -0,0 +1,69 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +//! The 2018 edition of the prelude of The Rust Standard Library. +//! +//! See the [module-level documentation](../index.html) for more. + + +#![unstable(feature = "rust2018_prelude", issue = "51418")] + +// Re-exported core operators +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use marker::{Copy, Send, Sized, Sync}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use ops::{Drop, Fn, FnMut, FnOnce}; + +// Re-exported functions +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use mem::drop; + +// Re-exported types and traits +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use clone::Clone; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use default::Default; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use iter::{Iterator, Extend, IntoIterator}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use iter::{DoubleEndedIterator, ExactSizeIterator}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use option::Option::{self, Some, None}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use result::Result::{self, Ok, Err}; + + +// Contents so far are equivalent to src/libcore/prelude/v1.rs + + +// Re-exported types and traits that involve memory allocation +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use boxed::Box; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use borrow::ToOwned; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use slice::SliceConcatExt; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use string::{String, ToString}; +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use vec::Vec; + + +// Contents so far are equivalent to v1.rs + + +// Not in v1.rs because of breakage: https://github.com/rust-lang/rust/pull/49518 +#[unstable(feature = "rust2018_prelude", issue = "51418")] +#[doc(no_inline)] pub use convert::{TryFrom, TryInto}; diff --git a/src/libstd/prelude/v1.rs b/src/libstd/prelude/v1.rs index feedd4e1abe5f..ddcb9181de9ab 100644 --- a/src/libstd/prelude/v1.rs +++ b/src/libstd/prelude/v1.rs @@ -8,10 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. + //! The first version of the prelude of The Rust Standard Library. //! //! See the [module-level documentation](../index.html) for more. + #![stable(feature = "rust1", since = "1.0.0")] // Re-exported core operators @@ -26,10 +28,6 @@ // Re-exported types and traits #[stable(feature = "rust1", since = "1.0.0")] -#[doc(no_inline)] pub use boxed::Box; -#[stable(feature = "rust1", since = "1.0.0")] -#[doc(no_inline)] pub use borrow::ToOwned; -#[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use clone::Clone; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord}; @@ -45,6 +43,16 @@ #[doc(no_inline)] pub use option::Option::{self, Some, None}; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use result::Result::{self, Ok, Err}; + + +// Contents so far are equivalent to src/libcore/prelude/v1.rs + + +// Re-exported types and traits that involve memory allocation +#[stable(feature = "rust1", since = "1.0.0")] +#[doc(no_inline)] pub use boxed::Box; +#[stable(feature = "rust1", since = "1.0.0")] +#[doc(no_inline)] pub use borrow::ToOwned; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use slice::SliceConcatExt; #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index e9cd7adb9c166..f1f02b8248789 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -11,6 +11,7 @@ use ast; use attr; use std::cell::Cell; +use edition::Edition; use ext::hygiene::{Mark, SyntaxContext}; use symbol::{Symbol, keywords}; use syntax_pos::{DUMMY_SP, Span}; @@ -44,7 +45,11 @@ thread_local! { static INJECTED_CRATE_NAME: Cell> = Cell::new(None); } -pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate { +pub fn maybe_inject_crates_ref( + edition: Edition, + mut krate: ast::Crate, + alt_std_name: Option<&str>, +) -> ast::Crate { // the first name in this list is the crate name of the crate with the prelude let names: &[&str] = if attr::contains_name(&krate.attrs, "no_core") { return krate; @@ -92,7 +97,7 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str> vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited), node: ast::ItemKind::Use(P(ast::UseTree { prefix: ast::Path { - segments: [name, "prelude", "v1"].into_iter().map(|name| { + segments: [name, "prelude", edition.prelude_module()].into_iter().map(|name| { ast::PathSegment::from_ident(ast::Ident::from_str(name)) }).collect(), span, diff --git a/src/libsyntax_pos/edition.rs b/src/libsyntax_pos/edition.rs index 18446c109964d..ae4c3d97d452c 100644 --- a/src/libsyntax_pos/edition.rs +++ b/src/libsyntax_pos/edition.rs @@ -68,6 +68,14 @@ impl Edition { Edition::Edition2018 => false, } } + + /// Name of the respective sub-modules of `std::prelude` and `core::prelude`. + pub fn prelude_module(&self) -> &'static str { + match *self { + Edition::Edition2015 => "v1", + Edition::Edition2018 => "rust2018", + } + } } impl FromStr for Edition { diff --git a/src/test/run-pass/edition-2018-prelude-no-std.rs b/src/test/run-pass/edition-2018-prelude-no-std.rs new file mode 100644 index 0000000000000..c1b58a8865c9f --- /dev/null +++ b/src/test/run-pass/edition-2018-prelude-no-std.rs @@ -0,0 +1,20 @@ +// Copyright 2018 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --edition=2018 + +#![no_std] +#![feature(try_from)] + +extern crate std; + +fn main() { + u8::try_from(4_u32).unwrap(); // Using the TryFrom trait from core::prelude::rust2018 +} diff --git a/src/test/run-pass/edition-2018-prelude.rs b/src/test/run-pass/edition-2018-prelude.rs new file mode 100644 index 0000000000000..dade1cf40d975 --- /dev/null +++ b/src/test/run-pass/edition-2018-prelude.rs @@ -0,0 +1,17 @@ +// Copyright 2018 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --edition=2018 + +#![feature(try_from)] + +fn main() { + u8::try_from(4_u32).unwrap(); // Using the TryFrom trait from std::prelude::rust2018 +} diff --git a/src/test/ui/issue-35675.stderr b/src/test/ui/issue-35675.stderr index fef8de3a28d9e..b85a5795d8645 100644 --- a/src/test/ui/issue-35675.stderr +++ b/src/test/ui/issue-35675.stderr @@ -42,6 +42,7 @@ error[E0573]: expected type, found variant `Ok` LL | fn foo() -> Ok { | ^^ not a type | + = help: there is an enum variant `std::prelude::rust2018::Ok`, try using `std::prelude::rust2018`? = help: there is an enum variant `std::prelude::v1::Ok`, try using `std::prelude::v1`? = help: there is an enum variant `std::result::Result::Ok`, try using `std::result::Result`? @@ -60,6 +61,7 @@ error[E0573]: expected type, found variant `Some` LL | fn qux() -> Some { | ^^^^ not a type | + = help: there is an enum variant `std::prelude::rust2018::Some`, try using `std::prelude::rust2018`? = help: there is an enum variant `std::prelude::v1::Option::Some`, try using `std::prelude::v1::Option`? = help: there is an enum variant `std::prelude::v1::Some`, try using `std::prelude::v1`?