Skip to content

Commit feb1d72

Browse files
Rollup merge of rust-lang#158327 - jschillem:docs-move-attributes-and-keywords-to-core, r=GuillaumeGomez
Move attribute and keyword docs from `std` to `core` Move the documentation for attributes and keywords into the `core` crate. Apart from strictly moving the module, I had to make a few small changes to certain docs to avoid using `std` types when possible, as well as fixing a few suggestions related to linking to primitives. Pre-requisite for work on rust-lang#157604. r? @GuillaumeGomez Verified documentation using: `./x doc library/core` and `./x doc library/std`, as well as running doc-tests for both crates.
2 parents 527396f + e5d79ed commit feb1d72

4 files changed

Lines changed: 54 additions & 46 deletions

File tree

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ mod break_keyword {}
195195
/// to be most things that would be reasonable to have in a constant (barring `const fn`s). For
196196
/// example, you can't have a [`File`] as a `const`.
197197
///
198-
/// [`File`]: crate::fs::File
198+
/// [`File`]: ../std/fs/struct.File.html
199199
///
200200
/// The only lifetime allowed in a constant is `'static`, which is the lifetime that encompasses
201201
/// all others in a Rust program. For example, if you wanted to define a constant string, it would
@@ -484,7 +484,7 @@ mod extern_keyword {}
484484

485485
#[doc(keyword = "false")]
486486
//
487-
/// A value of type [`bool`] representing logical **false**.
487+
/// A value of type [`prim@bool`] representing logical **false**.
488488
///
489489
/// `false` is the logical opposite of [`true`].
490490
///
@@ -1235,31 +1235,18 @@ mod ref_keyword {}
12351235
/// `return` returns from the function immediately (an "early return"):
12361236
///
12371237
/// ```no_run
1238-
/// use std::fs::File;
1239-
/// use std::io::{Error, ErrorKind, Read, Result};
1238+
/// fn main() -> Result<(), &'static str> {
1239+
/// let contents = "Hello, world!";
12401240
///
1241-
/// fn main() -> Result<()> {
1242-
/// let mut file = match File::open("foo.txt") {
1243-
/// Ok(f) => f,
1244-
/// Err(e) => return Err(e),
1245-
/// };
1246-
///
1247-
/// let mut contents = String::new();
1248-
/// let size = match file.read_to_string(&mut contents) {
1249-
/// Ok(s) => s,
1250-
/// Err(e) => return Err(e),
1251-
/// };
1241+
/// if contents.contains("impossible!") {
1242+
/// return Err("oh no!");
1243+
/// }
12521244
///
1253-
/// if contents.contains("impossible!") {
1254-
/// return Err(Error::new(ErrorKind::Other, "oh no!"));
1255-
/// }
1256-
///
1257-
/// if size > 9000 {
1258-
/// return Err(Error::new(ErrorKind::Other, "over 9000!"));
1259-
/// }
1245+
/// if contents.len() > 9000 {
1246+
/// return Err("over 9000!");
1247+
/// }
12601248
///
1261-
/// assert_eq!(contents, "Hello, world!");
1262-
/// Ok(())
1249+
/// Ok(())
12631250
/// }
12641251
/// ```
12651252
///
@@ -1631,8 +1618,8 @@ mod self_upper_keyword {}
16311618
/// [`extern`]: keyword.extern.html
16321619
/// [`mut`]: keyword.mut.html
16331620
/// [`unsafe`]: keyword.unsafe.html
1634-
/// [`Mutex`]: sync::Mutex
1635-
/// [`OnceLock`]: sync::OnceLock
1621+
/// [`Mutex`]: ../std/sync/struct.Mutex.html
1622+
/// [`OnceLock`]: ../std/sync/struct.OnceLock.html
16361623
/// [`RefCell`]: cell::RefCell
16371624
/// [atomic]: sync::atomic
16381625
/// [Reference]: ../reference/items/static-items.html
@@ -1959,7 +1946,7 @@ mod trait_keyword {}
19591946

19601947
#[doc(keyword = "true")]
19611948
//
1962-
/// A value of type [`bool`] representing logical **true**.
1949+
/// A value of type [`prim@bool`] representing logical **true**.
19631950
///
19641951
/// Logically `true` is not equal to [`false`].
19651952
///
@@ -2146,22 +2133,30 @@ mod type_keyword {}
21462133
/// Since `unsafe fn` and `unsafe trait` indicate that there is a safety
21472134
/// contract that the compiler cannot enforce, documenting it is important. The
21482135
/// standard library has many examples of this, like the following which is an
2149-
/// extract from [`Vec::set_len`]. The `# Safety` section explains the contract
2136+
/// extract from [`ptr::replace`]. The `# Safety` section explains the contract
21502137
/// that must be fulfilled to safely call the function.
21512138
///
21522139
/// ```rust,ignore (stub-to-show-doc-example)
2153-
/// /// Forces the length of the vector to `new_len`.
2140+
/// /// Moves `src` into the pointed `dst`, returning the previous `dst` value.
21542141
/// ///
2155-
/// /// This is a low-level operation that maintains none of the normal
2156-
/// /// invariants of the type. Normally changing the length of a vector
2157-
/// /// is done using one of the safe operations instead, such as
2158-
/// /// `truncate`, `resize`, `extend`, or `clear`.
2142+
/// /// Neither value is dropped.
2143+
/// ///
2144+
/// /// This function is semantically equivalent to [`mem::replace`] except that it
2145+
/// /// operates on raw pointers instead of references. When references are
2146+
/// /// available, [`mem::replace`] should be preferred.
21592147
/// ///
21602148
/// /// # Safety
21612149
/// ///
2162-
/// /// - `new_len` must be less than or equal to `capacity()`.
2163-
/// /// - The elements at `old_len..new_len` must be initialized.
2164-
/// pub unsafe fn set_len(&mut self, new_len: usize)
2150+
/// /// Behavior is undefined if any of the following conditions are violated:
2151+
/// ///
2152+
/// /// * `dst` must be [valid] for both reads and writes or `T` must be a ZST.
2153+
/// ///
2154+
/// /// * `dst` must be properly aligned.
2155+
/// ///
2156+
/// /// * `dst` must point to a properly initialized value of type `T`.
2157+
/// ///
2158+
/// /// Note that even if `T` has size `0`, the pointer must be properly aligned.
2159+
/// pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T
21652160
/// ```
21662161
///
21672162
/// ## Using `unsafe {}` blocks and `impl`s
@@ -2502,7 +2497,7 @@ mod use_keyword {}
25022497
/// ```
25032498
///
25042499
/// `where` is available anywhere generic and lifetime parameters are available,
2505-
/// as can be seen with the [`Cow`](crate::borrow::Cow) type from the standard
2500+
/// as can be seen with the [`Cow`](../std/borrow/enum.Cow.html) type from the standard
25062501
/// library:
25072502
///
25082503
/// ```rust

library/core/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,17 @@ pub mod simd {
397397
pub use crate::core_simd::simd::*;
398398
}
399399

400+
// Include private modules that exist solely to provide rustdoc
401+
// documentation for built-in attributes. Using `include!` because rustdoc
402+
// only looks for these modules at the crate level.
403+
include!("attribute_docs.rs");
404+
405+
// Include a number of private modules that exist solely to provide
406+
// the rustdoc documentation for the existing keywords. Using `include!`
407+
// because rustdoc only looks for these modules at the crate level.
408+
include!("keyword_docs.rs");
409+
410+
// Include a number of private modules that exist solely to provide
411+
// the rustdoc documentation for primitive types. Using `include!`
412+
// because rustdoc only looks for these modules at the crate level.
400413
include!("primitive_docs.rs");

library/std/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -777,20 +777,20 @@ pub mod from {
777777
pub use core::from::From;
778778
}
779779

780-
// Include a number of private modules that exist solely to provide
781-
// the rustdoc documentation for primitive types. Using `include!`
782-
// because rustdoc only looks for these modules at the crate level.
783-
include!("../../core/src/primitive_docs.rs");
780+
// Include private modules that exist solely to provide rustdoc
781+
// documentation for built-in attributes. Using `include!` because rustdoc
782+
// only looks for these modules at the crate level.
783+
include!("../../core/src/attribute_docs.rs");
784784

785785
// Include a number of private modules that exist solely to provide
786786
// the rustdoc documentation for the existing keywords. Using `include!`
787787
// because rustdoc only looks for these modules at the crate level.
788-
include!("keyword_docs.rs");
788+
include!("../../core/src/keyword_docs.rs");
789789

790-
// Include private modules that exist solely to provide rustdoc
791-
// documentation for built-in attributes. Using `include!` because rustdoc
792-
// only looks for these modules at the crate level.
793-
include!("attribute_docs.rs");
790+
// Include a number of private modules that exist solely to provide
791+
// the rustdoc documentation for primitive types. Using `include!`
792+
// because rustdoc only looks for these modules at the crate level.
793+
include!("../../core/src/primitive_docs.rs");
794794

795795
// This is required to avoid an unstable error when `restricted-std` is not
796796
// enabled. The use of #![feature(restricted_std)] in rustc-std-workspace-std

0 commit comments

Comments
 (0)