Skip to content

Commit 6b59a4c

Browse files
committed
Document built-in macros and no_implicit_prelude
Macros are part of the standard library prelude. One might expect these to not be in scope when `no_implicit_prelude` is used. But certain macros still are. The history is that these macros were once built in to the language; when this was changed, in Rust PR 62086, a hack was added. Then in Rust PR 139493 this list grew to include `panic!` and `unreachable!`.
1 parent 33aa4f4 commit 6b59a4c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/names/preludes.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,41 @@ The `no_implicit_prelude` attribute may be used any number of times on a form.
192192
r[names.preludes.no_implicit_prelude.excluded-preludes]
193193
The `no_implicit_prelude` attribute prevents the [standard library prelude], [extern prelude], [`macro_use` prelude], and the [tool prelude] from being brought into scope for the module and its descendants.
194194
195+
r[names.preludes.no_implicit_prelude.implicitly-imported-macros]
196+
> [!NOTE]
197+
> Despite `#![no_implicit_prelude]`, `rustc` currently brings certain macros implicitly into scope. Those macros are:
198+
>
199+
> - [assert!]
200+
> - [cfg!]
201+
> - [cfg_select!]
202+
> - [column!]
203+
> - [compile_error!]
204+
> - [concat!]
205+
> - [concat_bytes!]
206+
> - [env!]
207+
> - [file!]
208+
> - [format_args!]
209+
> - [include!]
210+
> - [include_bytes!]
211+
> - [include_str!]
212+
> - [line!]
213+
> - [module_path!]
214+
> - [option_env!]
215+
> - [panic!]
216+
> - [stringify!]
217+
> - [unreachable!]
218+
>
219+
> E.g., this works:
220+
>
221+
> ```rust
222+
> #![no_implicit_prelude]
223+
> fn main() { assert!(true); }
224+
> ```
225+
>
226+
> Don't rely on this behavior; it may be removed in the future. Always bring the items you need into scope explicitly when using `#![no_implicit_prelude]`.
227+
>
228+
> For details, see [Rust PR #62086](https://github.com/rust-lang/rust/pull/62086) and [Rust PR #139493](https://github.com/rust-lang/rust/pull/139493).
229+
195230
r[names.preludes.no_implicit_prelude.lang]
196231
The `no_implicit_prelude` attribute does not affect the [language prelude].
197232

0 commit comments

Comments
 (0)