about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-02-26 02:07:05 +0100
committerGitHub <noreply@github.com>2020-02-26 02:07:05 +0100
commit0860f5aebd1939b821cf9af5ba568cbd6be8077e (patch)
tree803b98b762167a21f0a72ccb14bf5b02a167a11a
parent6fd8798f4de63328d743eb2a9a9c12e202a4a182 (diff)
parent9c3ee1bc351fcfabcfd47947dc44b0c020427181 (diff)
downloadrust-0860f5aebd1939b821cf9af5ba568cbd6be8077e.tar.gz
rust-0860f5aebd1939b821cf9af5ba568cbd6be8077e.zip
Rollup merge of #67637 - Mark-Simulacrum:primitive-mod, r=dtolnay
Add primitive module to libcore

This re-exports the primitive types from libcore at `core::primitive` to allow
macro authors to have a reliable location to use them from.

Fixes #44865
-rw-r--r--src/libcore/lib.rs3
-rw-r--r--src/libcore/primitive.rs67
-rw-r--r--src/libstd/lib.rs5
-rw-r--r--src/test/ui/resolve/resolve-primitive-fallback.stderr5
-rw-r--r--src/test/ui/shadow-bool.rs18
5 files changed, 97 insertions, 1 deletions
diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs
index bca96b77812..7bd1d00e84c 100644
--- a/src/libcore/lib.rs
+++ b/src/libcore/lib.rs
@@ -267,6 +267,9 @@ mod bool;
 mod tuple;
 mod unit;
 
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub mod primitive;
+
 // Pull in the `core_arch` crate directly into libcore. The contents of
 // `core_arch` are in a different repository: rust-lang/stdarch.
 //
diff --git a/src/libcore/primitive.rs b/src/libcore/primitive.rs
new file mode 100644
index 00000000000..e20b2c5c938
--- /dev/null
+++ b/src/libcore/primitive.rs
@@ -0,0 +1,67 @@
+//! This module reexports the primitive types to allow usage that is not
+//! possibly shadowed by other declared types.
+//!
+//! This is normally only useful in macro generated code.
+//!
+//! An example of this is when generating a new struct and an impl for it:
+//!
+//! ```rust,compile_fail
+//! pub struct bool;
+//!
+//! impl QueryId for bool {
+//!     const SOME_PROPERTY: bool = true;
+//! }
+//!
+//! # trait QueryId { const SOME_PROPERTY: core::primitive::bool; }
+//! ```
+//!
+//! Note that the `SOME_PROPERTY` associated constant would not compile, as its
+//! type `bool` refers to the struct, rather than to the primitive bool type.
+//!
+//! A correct implementation could look like:
+//!
+//! ```rust
+//! # #[allow(non_camel_case_types)]
+//! pub struct bool;
+//!
+//! impl QueryId for bool {
+//!     const SOME_PROPERTY: core::primitive::bool = true;
+//! }
+//!
+//! # trait QueryId { const SOME_PROPERTY: core::primitive::bool; }
+//! ```
+
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use bool;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use char;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use f32;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use f64;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use i128;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use i16;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use i32;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use i64;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use i8;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use isize;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use str;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use u128;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use u16;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use u32;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use u64;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use u8;
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use usize;
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs
index 799585ffc0a..7b3c702b929 100644
--- a/src/libstd/lib.rs
+++ b/src/libstd/lib.rs
@@ -233,12 +233,12 @@
 #![feature(allocator_internals)]
 #![feature(allow_internal_unsafe)]
 #![feature(allow_internal_unstable)]
-#![feature(atomic_mut_ptr)]
 #![feature(arbitrary_self_types)]
 #![feature(array_error_internals)]
 #![feature(asm)]
 #![feature(assoc_int_consts)]
 #![feature(associated_type_bounds)]
+#![feature(atomic_mut_ptr)]
 #![feature(box_syntax)]
 #![feature(c_variadic)]
 #![feature(cfg_target_has_atomic)]
@@ -551,6 +551,9 @@ pub use core::{
     trace_macros,
 };
 
+#[stable(feature = "core_primitive", since = "1.43.0")]
+pub use core::primitive;
+
 // Include a number of private modules that exist solely to provide
 // the rustdoc documentation for primitive types. Using `include!`
 // because rustdoc only looks for these modules at the crate level.
diff --git a/src/test/ui/resolve/resolve-primitive-fallback.stderr b/src/test/ui/resolve/resolve-primitive-fallback.stderr
index 6d61d2f16ca..9d381a8a94e 100644
--- a/src/test/ui/resolve/resolve-primitive-fallback.stderr
+++ b/src/test/ui/resolve/resolve-primitive-fallback.stderr
@@ -9,6 +9,11 @@ error[E0412]: cannot find type `u8` in the crate root
    |
 LL |     let _: ::u8;
    |              ^^ not found in the crate root
+   |
+help: possible candidate is found in another module, you can import it into scope
+   |
+LL | use std::primitive::u8;
+   |
 
 error[E0061]: this function takes 0 arguments but 1 argument was supplied
   --> $DIR/resolve-primitive-fallback.rs:3:5
diff --git a/src/test/ui/shadow-bool.rs b/src/test/ui/shadow-bool.rs
new file mode 100644
index 00000000000..f290a329eaa
--- /dev/null
+++ b/src/test/ui/shadow-bool.rs
@@ -0,0 +1,18 @@
+// check-pass
+
+mod bar {
+    pub trait QueryId {
+        const SOME_PROPERTY: bool;
+    }
+}
+
+use bar::QueryId;
+
+#[allow(non_camel_case_types)]
+pub struct bool;
+
+impl QueryId for bool {
+    const SOME_PROPERTY: core::primitive::bool = true;
+}
+
+fn main() {}