Skip to content

Commit 178de46

Browse files
Add primitive module to libcore/std
This re-exports the primitive types from libcore at `core::primitive` to allow macro authors to have a reliable location to use them from.
1 parent 1f8df25 commit 178de46

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

src/libcore/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ mod bool;
262262
mod tuple;
263263
mod unit;
264264

265+
#[stable(feature = "core_primitive", since = "1.42.0")]
266+
pub mod primitive;
267+
265268
// Pull in the `core_arch` crate directly into libcore. The contents of
266269
// `core_arch` are in a different repository: rust-lang/stdarch.
267270
//

src/libcore/primitive.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//! This module reexports the primitive types to allow usage that is not
2+
//! possibly shadowed by other declared types.
3+
//!
4+
//! This is normally only useful in macro generated code.
5+
//!
6+
//! An example of this is when generating a new struct and an impl for it:
7+
//!
8+
//! ```rust,compile_fail
9+
//! pub struct bool;
10+
//!
11+
//! impl QueryId for bool {
12+
//! const SOME_PROPERTY: bool = true;
13+
//! }
14+
//!
15+
//! # trait QueryId { const SOME_PROPERTY: core::primitive::bool; }
16+
//! ```
17+
//!
18+
//! Note that the `SOME_PROPERTY` associated constant would not compile, as its
19+
//! type `bool` refers to the struct, rather than to the primitive bool type.
20+
//!
21+
//! A correct implementation could look like:
22+
//!
23+
//! ```rust
24+
//! # #[allow(non_camel_case_types)]
25+
//! pub struct bool;
26+
//!
27+
//! impl QueryId for bool {
28+
//! const SOME_PROPERTY: core::primitive::bool = true;
29+
//! }
30+
//!
31+
//! # trait QueryId { const SOME_PROPERTY: core::primitive::bool; }
32+
//! ```
33+
34+
#[stable(feature = "core_primitive", since = "1.42.0")]
35+
pub use bool;
36+
#[stable(feature = "core_primitive", since = "1.42.0")]
37+
pub use char;
38+
#[stable(feature = "core_primitive", since = "1.42.0")]
39+
pub use f32;
40+
#[stable(feature = "core_primitive", since = "1.42.0")]
41+
pub use f64;
42+
#[stable(feature = "core_primitive", since = "1.42.0")]
43+
pub use i128;
44+
#[stable(feature = "core_primitive", since = "1.42.0")]
45+
pub use i16;
46+
#[stable(feature = "core_primitive", since = "1.42.0")]
47+
pub use i32;
48+
#[stable(feature = "core_primitive", since = "1.42.0")]
49+
pub use i64;
50+
#[stable(feature = "core_primitive", since = "1.42.0")]
51+
pub use i8;
52+
#[stable(feature = "core_primitive", since = "1.42.0")]
53+
pub use isize;
54+
#[stable(feature = "core_primitive", since = "1.42.0")]
55+
pub use str;
56+
#[stable(feature = "core_primitive", since = "1.42.0")]
57+
pub use u128;
58+
#[stable(feature = "core_primitive", since = "1.42.0")]
59+
pub use u16;
60+
#[stable(feature = "core_primitive", since = "1.42.0")]
61+
pub use u32;
62+
#[stable(feature = "core_primitive", since = "1.42.0")]
63+
pub use u64;
64+
#[stable(feature = "core_primitive", since = "1.42.0")]
65+
pub use u8;
66+
#[stable(feature = "core_primitive", since = "1.42.0")]
67+
pub use usize;

src/libstd/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@
233233
#![feature(allocator_internals)]
234234
#![feature(allow_internal_unsafe)]
235235
#![feature(allow_internal_unstable)]
236-
#![feature(atomic_mut_ptr)]
237236
#![feature(arbitrary_self_types)]
238237
#![feature(array_error_internals)]
239238
#![feature(asm)]
240239
#![feature(assoc_int_consts)]
241240
#![feature(associated_type_bounds)]
241+
#![feature(atomic_mut_ptr)]
242242
#![feature(box_syntax)]
243243
#![feature(c_variadic)]
244244
#![feature(cfg_target_has_atomic)]
@@ -550,6 +550,9 @@ pub use core::{
550550
trace_macros,
551551
};
552552

553+
#[stable(feature = "core_primitive", since = "1.42.0")]
554+
pub use core::primitive;
555+
553556
// Include a number of private modules that exist solely to provide
554557
// the rustdoc documentation for primitive types. Using `include!`
555558
// because rustdoc only looks for these modules at the crate level.

src/test/ui/resolve/resolve-primitive-fallback.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ error[E0412]: cannot find type `u8` in the crate root
99
|
1010
LL | let _: ::u8;
1111
| ^^ not found in the crate root
12+
|
13+
help: possible candidate is found in another module, you can import it into scope
14+
|
15+
LL | use std::primitive::u8;
16+
|
1217

1318
error[E0061]: this function takes 0 parameters but 1 parameter was supplied
1419
--> $DIR/resolve-primitive-fallback.rs:3:5

src/test/ui/shadow-bool.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
3+
mod bar {
4+
pub trait QueryId {
5+
const SOME_PROPERTY: bool;
6+
}
7+
}
8+
9+
use bar::QueryId;
10+
11+
#[allow(non_camel_case_types)]
12+
pub struct bool;
13+
14+
impl QueryId for bool {
15+
const SOME_PROPERTY: core::primitive::bool = true;
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)