Skip to content

Commit 33aa4f4

Browse files
committed
Document the panic! hack to name resolution
In Rust PR 139493, we added a (hopefully temporary) hack to name resolution involving `panic` to avoid breakage. Let's document this in an admonition.
1 parent 73a35e8 commit 33aa4f4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/names/name-resolution.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,38 @@ const _: () = {
229229
r[names.resolution.expansion.imports.ambiguity.glob-vs-outer]
230230
Names in imports and macro invocations may not be resolved through glob imports when there is another candidate available in an [outer scope].
231231

232+
r[names.resolution.expansion.imports.ambiguity.panic-hack]
233+
> [!NOTE]
234+
> When one of [std::panic!] or [core::panic!] is brought into scope due to the [standard library prelude], `rustc` currently accepts the other being resolved through a user-written [glob import]; that glob import takes precedence.
235+
>
236+
> In later editions, [core::panic!] and [std::panic!] operate identically, but we can tell the difference between them in Rust 2018 and earlier where only in [std::panic!] is a [String] accepted as the format argument.
237+
>
238+
> E.g., due to that, this is an error:
239+
>
240+
> ```rust,edition2018,compile_fail,E0308
241+
> extern crate core;
242+
> use ::core::prelude::v1::*;
243+
> fn main() {
244+
> panic!(std::string::String::new()); // ERROR
245+
> }
246+
> ```
247+
>
248+
> And this is accepted:
249+
>
250+
> <!-- ignore: Can't test with `no_std`. -->
251+
> ```rust,edition2018,ignore
252+
> #![no_std]
253+
> extern crate std;
254+
> use ::std::prelude::v1::*;
255+
> fn main() {
256+
> panic!(std::string::String::new()); // ERROR
257+
> }
258+
> ```
259+
>
260+
> Don't rely on this behavior; the plan is to remove it.
261+
>
262+
> For details, see [Rust issue #147319](https://github.com/rust-lang/rust/issues/147319).
263+
232264
```rust,compile_fail,E0659
233265
mod glob {
234266
pub mod ambig {
@@ -545,6 +577,7 @@ r[names.resolution.type-relative]
545577
[`use` declarations]: ../items/use-declarations.md
546578
[`use` glob shadowing]: ../items/use-declarations.md#r-items.use.glob.shadowing
547579
[derive helper scope]: ../procedural-macros.md#r-macro.proc.derive.attributes.scope
580+
[glob import]: items.use.glob
548581
[item definitions]: ../items.md
549582
[macro invocations]: ../macros.md#macro-invocation
550583
[macro textual scope shadowing]: ../macros-by-example.md#r-macro.decl.scope.textual.shadow

src/names/preludes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ Edition | `no_std` not applied | `no_std` applied
4040
>
4141
> [`core::prelude::rust_2015`] and [`core::prelude::rust_2018`] have the same contents as [`core::prelude::v1`].
4242
43+
> [!NOTE]
44+
> When one of [std::panic!] or [core::panic!] is brought into scope due to the [standard library prelude], `rustc` currently accepts the other being resolved through a user-written [glob import]; that glob import takes precedence.
45+
>
46+
> For details, see [names.resolution.expansion.imports.ambiguity.panic-hack].
47+
4348
r[names.preludes.extern]
4449
## Extern prelude
4550

@@ -203,6 +208,7 @@ r[names.preludes.no_implicit_prelude.edition2018]
203208
[Built-in attributes]: ../attributes.md#built-in-attributes-index
204209
[extern prelude]: #extern-prelude
205210
[floating-point types]: ../types/numeric.md#floating-point-types
211+
[glob import]: items.use.glob
206212
[Integer types]: ../types/numeric.md#integer-types
207213
[Language prelude]: #language-prelude
208214
[Machine-dependent integer types]: ../types/numeric.md#machine-dependent-integer-types

0 commit comments

Comments
 (0)