Skip to content
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

Add an extended prelude for the 2018 editions #51434

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcore/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
#![stable(feature = "core_prelude", since = "1.4.0")]

pub mod v1;
pub mod rust2018;
53 changes: 53 additions & 0 deletions src/libcore/prelude/rust2018.rs
Original file line number Diff line number Diff line change
@@ -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 <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.

//! 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};
33 changes: 11 additions & 22 deletions src/libcore/prelude/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
2 changes: 1 addition & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/libstd/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,4 @@
#![stable(feature = "rust1", since = "1.0.0")]

pub mod v1;
pub mod rust2018;
69 changes: 69 additions & 0 deletions src/libstd/prelude/rust2018.rs
Original file line number Diff line number Diff line change
@@ -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 <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.


//! 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};
16 changes: 12 additions & 4 deletions src/libstd/prelude/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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};
Expand All @@ -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")]
Expand Down
9 changes: 7 additions & 2 deletions src/libsyntax/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -44,7 +45,11 @@ thread_local! {
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = 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;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax_pos/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions src/test/run-pass/edition-2018-prelude-no-std.rs
Original file line number Diff line number Diff line change
@@ -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 <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.

// 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
}
17 changes: 17 additions & 0 deletions src/test/run-pass/edition-2018-prelude.rs
Original file line number Diff line number Diff line change
@@ -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 <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.

// compile-flags: --edition=2018

#![feature(try_from)]

fn main() {
u8::try_from(4_u32).unwrap(); // Using the TryFrom trait from std::prelude::rust2018
}
2 changes: 2 additions & 0 deletions src/test/ui/issue-35675.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this terrible suggestion. std::prelude::v1 is not an enum!

= help: there is an enum variant `std::result::Result::Ok`, try using `std::result::Result`?

Expand All @@ -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`?

Expand Down