From 173fcfa6d5f0d9f2c9fbee64b896eaba90350455 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 13:58:15 -0700 Subject: [PATCH 1/8] Unwrap the no_std attribute --- src/names/preludes.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/names/preludes.md b/src/names/preludes.md index 97edc59fc..fe5aa53fc 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -80,15 +80,10 @@ r[names.preludes.extern.no_std] ### The `no_std` attribute r[names.preludes.extern.no_std.intro] -By default, the standard library is automatically included in the crate root -module. The [`std`] crate is added to the root, along with an implicit -[`macro_use` attribute] pulling in all macros exported from `std` into the -[`macro_use` prelude]. Both [`core`] and [`std`] are added to the [extern -prelude]. +By default, the standard library is automatically included in the crate root module. The [`std`] crate is added to the root, along with an implicit [`macro_use` attribute] pulling in all macros exported from `std` into the [`macro_use` prelude]. Both [`core`] and [`std`] are added to the [extern prelude]. r[names.preludes.extern.no_std.allowed-positions] -The *`no_std` [attribute]* may be applied at the crate level to prevent the -[`std`] crate from being automatically added into scope. +The *`no_std` [attribute]* may be applied at the crate level to prevent the [`std`] crate from being automatically added into scope. It does three things: @@ -97,8 +92,7 @@ r[names.preludes.extern.no_std.extern] r[names.preludes.extern.no_std.module] * Affects which module is used to make up the [standard library prelude] (as described above). r[names.preludes.extern.no_std.core] -* Injects the [`core`] crate into the crate root instead of [`std`], and pulls - in all macros exported from `core` in the [`macro_use` prelude]. +* Injects the [`core`] crate into the crate root instead of [`std`], and pulls in all macros exported from `core` in the [`macro_use` prelude]. > [!NOTE] > Using the core prelude over the standard prelude is useful when either the crate is targeting a platform that does not support the standard library or is purposefully not using the capabilities of the standard library. Those capabilities are mainly dynamic memory allocation (e.g. `Box` and `Vec`) and file and network capabilities (e.g. `std::fs` and `std::io`). From 6e8d16486f2c5ab3cfcbb583e6611f2bade0532b Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 14:01:40 -0700 Subject: [PATCH 2/8] Add names.preludes.extern.no_std.syntax --- src/names/preludes.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/names/preludes.md b/src/names/preludes.md index fe5aa53fc..764efd878 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -82,6 +82,9 @@ r[names.preludes.extern.no_std] r[names.preludes.extern.no_std.intro] By default, the standard library is automatically included in the crate root module. The [`std`] crate is added to the root, along with an implicit [`macro_use` attribute] pulling in all macros exported from `std` into the [`macro_use` prelude]. Both [`core`] and [`std`] are added to the [extern prelude]. +r[names.preludes.extern.no_std.syntax] +The `no_std` attribute uses the [MetaWord] syntax and thus does not take any inputs. + r[names.preludes.extern.no_std.allowed-positions] The *`no_std` [attribute]* may be applied at the crate level to prevent the [`std`] crate from being automatically added into scope. From 3cd28ccf5be4b795e2973f71d294f57d4777204a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 15:31:05 -0700 Subject: [PATCH 3/8] Remove duplicate link definition --- src/names/preludes.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/names/preludes.md b/src/names/preludes.md index 764efd878..6064c2a5a 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -156,7 +156,6 @@ r[names.preludes.no_implicit_prelude.edition2018] [`macro_use` attribute]: ../macros-by-example.md#the-macro_use-attribute [`macro_use` prelude]: #macro_use-prelude [`no_std` attribute]: #the-no_std-attribute -[`no_std` attribute]: #the-no_std-attribute [attribute]: ../attributes.md [Boolean type]: ../types/boolean.md [Built-in attributes]: ../attributes.md#built-in-attributes-index From 703980b32d825f61f6664ab242e53e8a4f4ab615 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 15:32:19 -0700 Subject: [PATCH 4/8] Add example to no_std intro --- src/names/preludes.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/names/preludes.md b/src/names/preludes.md index 6064c2a5a..0a2f8f6b4 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -82,6 +82,12 @@ r[names.preludes.extern.no_std] r[names.preludes.extern.no_std.intro] By default, the standard library is automatically included in the crate root module. The [`std`] crate is added to the root, along with an implicit [`macro_use` attribute] pulling in all macros exported from `std` into the [`macro_use` prelude]. Both [`core`] and [`std`] are added to the [extern prelude]. +> [!EXAMPLE] +> +> ```rust,ignore +> #![no_std] +> ``` + r[names.preludes.extern.no_std.syntax] The `no_std` attribute uses the [MetaWord] syntax and thus does not take any inputs. From 16428de0022583d152da7b43a8e90f779114b976 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 15:33:00 -0700 Subject: [PATCH 5/8] Move note and warning to the intro These don't really belong where they are, and are just generally helpful things about the attribute. --- src/names/preludes.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/names/preludes.md b/src/names/preludes.md index 0a2f8f6b4..65dd60b67 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -88,6 +88,12 @@ By default, the standard library is automatically included in the crate root mod > #![no_std] > ``` +> [!NOTE] +> Using `no_std` is useful when either the crate is targeting a platform that does not support the standard library or is purposefully not using the capabilities of the standard library. Those capabilities are mainly dynamic memory allocation (e.g. `Box` and `Vec`) and file and network capabilities (e.g. `std::fs` and `std::io`). + +> [!WARNING] +> Using `no_std` does not prevent the standard library from being linked in. It is still valid to put `extern crate std;` into the crate and dependencies can also link it in. + r[names.preludes.extern.no_std.syntax] The `no_std` attribute uses the [MetaWord] syntax and thus does not take any inputs. @@ -103,11 +109,7 @@ r[names.preludes.extern.no_std.module] r[names.preludes.extern.no_std.core] * Injects the [`core`] crate into the crate root instead of [`std`], and pulls in all macros exported from `core` in the [`macro_use` prelude]. -> [!NOTE] -> Using the core prelude over the standard prelude is useful when either the crate is targeting a platform that does not support the standard library or is purposefully not using the capabilities of the standard library. Those capabilities are mainly dynamic memory allocation (e.g. `Box` and `Vec`) and file and network capabilities (e.g. `std::fs` and `std::io`). -> [!WARNING] -> Using `no_std` does not prevent the standard library from being linked in. It is still valid to put `extern crate std;` into the crate and dependencies can also link it in. r[names.preludes.lang] ## Language prelude From 068b94d252be1bbc4424a2162908737a2e36ccec Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 15:37:55 -0700 Subject: [PATCH 6/8] Rewrite no_std The current text was a little bit of a mess and had some subtle inaccuracies. This reworks so that the intro follows the template, and just generally introduces the attribute. `allowed-positions` now just specifies the allowed positions. I reworked the behavior changes caused by no_std into proper separate rules. `std` is not injected into the crate root anymore starting with edition 2018. --- src/names/preludes.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/names/preludes.md b/src/names/preludes.md index 65dd60b67..470672409 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -80,7 +80,7 @@ r[names.preludes.extern.no_std] ### The `no_std` attribute r[names.preludes.extern.no_std.intro] -By default, the standard library is automatically included in the crate root module. The [`std`] crate is added to the root, along with an implicit [`macro_use` attribute] pulling in all macros exported from `std` into the [`macro_use` prelude]. Both [`core`] and [`std`] are added to the [extern prelude]. +The *`no_std` [attribute][attributes]* is used to prevent the automatic linking of the [`std`] crate, deferring to [`core`] instead. > [!EXAMPLE] > @@ -98,18 +98,16 @@ r[names.preludes.extern.no_std.syntax] The `no_std` attribute uses the [MetaWord] syntax and thus does not take any inputs. r[names.preludes.extern.no_std.allowed-positions] -The *`no_std` [attribute]* may be applied at the crate level to prevent the [`std`] crate from being automatically added into scope. +The `no_std` attribute may only be applied to the crate root. -It does three things: -r[names.preludes.extern.no_std.extern] -* Prevents `std` from being added to the [extern prelude](#extern-prelude). r[names.preludes.extern.no_std.module] -* Affects which module is used to make up the [standard library prelude] (as described above). -r[names.preludes.extern.no_std.core] -* Injects the [`core`] crate into the crate root instead of [`std`], and pulls in all macros exported from `core` in the [`macro_use` prelude]. +The `no_std` attribute changes the [standard library prelude] to use the `core` prelude instead of `std`. +r[names.preludes.extern.no_std.inject] +By default, the [`std`] crate is injected into the [extern prelude], and all macros exported from `std` are added to the [`macro_use` prelude]. +If the `no_std` attribute is specified, then the [`core`] crate is used instead of `std`, and similarly all macros exported from `core` are placed into the [`macro_use` prelude]. r[names.preludes.lang] ## Language prelude From 01d7f32a2e63b0d1449fb463f0805b94bf860d1e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 15:41:28 -0700 Subject: [PATCH 7/8] Add a rule for `std` injection in edition 2018 I do not know how this was missing, I've looked at it a million times. I feel like I was maybe missing something, but I really can't find anything that directly refers to this, nor can I find any open issues. I believe this is now done with this relatively subtle bit: https://github.com/rust-lang/rust/blob/4d08223c054cf5a56d9761ca925fd46ffebe7115/compiler/rustc_builtin_macros/src/standard_library_imports.rs#L42 In the past it used to use a different approach of emulating `extern crate std as _;`: https://github.com/rust-lang/rust/blob/c8cf9f5a025bb475804b5a90f54aca310b952526/src/libsyntax_ext/standard_library_imports.rs#L42-L46 --- src/names/preludes.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/names/preludes.md b/src/names/preludes.md index 470672409..33e0bace9 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -109,6 +109,10 @@ By default, the [`std`] crate is injected into the [extern prelude], and all mac If the `no_std` attribute is specified, then the [`core`] crate is used instead of `std`, and similarly all macros exported from `core` are placed into the [`macro_use` prelude]. +r[names.preludes.extern.no_std.edition2018] +> [!EDITION-2018] +> Before the 2018 edition, `std` is also injected into the crate root. `core` is injected instead of `std` if `no_std` is specified. Starting with the 2018 edition, these are not injected into the crate root. + r[names.preludes.lang] ## Language prelude From e80645a91febcf4501c2c445f0c3c63cfdc0d0bb Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 31 May 2025 15:41:36 -0700 Subject: [PATCH 8/8] Add names.preludes.extern.no_std.duplicates --- src/names/preludes.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/names/preludes.md b/src/names/preludes.md index 33e0bace9..bc5868764 100644 --- a/src/names/preludes.md +++ b/src/names/preludes.md @@ -100,6 +100,11 @@ The `no_std` attribute uses the [MetaWord] syntax and thus does not take any inp r[names.preludes.extern.no_std.allowed-positions] The `no_std` attribute may only be applied to the crate root. +r[names.preludes.extern.no_std.duplicates] +Duplicate instances of the `no_std` attribute have no effect. + +> [!NOTE] +> `rustc` currently warns on subsequent duplicate `no_std` attributes. r[names.preludes.extern.no_std.module] The `no_std` attribute changes the [standard library prelude] to use the `core` prelude instead of `std`.