Skip to content

Documentation for core::panic::PanicInfo should not use unwrap() #51768

Closed
@gamozolabs

Description

@gamozolabs

Current documentation for core::panic::PanicInfo uses examples with downcast_ref and an unwrap. I was just porting over some old no_std code and copied over the examples for testing. However in a no_std codebase this leads to infinite recursion and probably stack exhaustion or other weird behavior depending on the design of the system implementing the panic.

I think that the example should be changed from:

println!("panic occurred: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());

to something like:

println!("panic occurred: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap_or("Payload unavailable"));

Or switching to if let to handle the None case.

Not really that big of an issue, but I think any panic handler examples in the documentation should not be able to recursively panic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsP-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions