This repository was archived by the owner on Sep 12, 2024. It is now read-only.
This repository was archived by the owner on Sep 12, 2024. It is now read-only.
no-panic as a language feature #49
Open
Description
A common request I see is for the ability to require that the compiler prove a function can't ever panic. There are already libraries / tools that provide functionality like this.
- https://github.com/Kixunil/dont_panic
- https://github.com/dtolnay/no-panic
- https://github.com/Technolution/rustig
As I see it we need to answer the following questions:
- What are the use cases of no-panic?
- Performance optimizations
- no-panic was used to prove the absence of panics for https://github.com/dtolnay/ryu
- Controlling binary size
- ???
- Performance optimizations
- What are the use cases for which
no-panic
andrustig
are insufficient? (or, How do we benefit by making this a language feature?)- ???
As well as resolve the following issues:
- interaction with optimizations
- Frequently the codepaths in question that people wish to assert don't panic do have panics present which are removed when the optimizer is able to prove that they're unnecessary. This severely limits how we can implement this feature, since we'd need to check if there are still panics very late in the compilation pipeline.
no-panic
handles this by using linker shenanigans to produce an error when panics are still compiled in. rustig handles this by analyzing the final generated binary. It's unclear which strategy we'd follow when implementing this as a language feature. - Existing implementations don't work at all when panics are set to abort instead of unwind
- The existing implementations make it so you can no longer compile in debug mode, or that you must enable some optimizations in debug mode.
- Frequently the codepaths in question that people wish to assert don't panic do have panics present which are removed when the optimizer is able to prove that they're unnecessary. This severely limits how we can implement this feature, since we'd need to check if there are still panics very late in the compilation pipeline.
- interaction with stability guarantees
- By making compiler errors depend on optimizations we risk introducing significant stability hazards. Changing a function such that the compiler can no longer prove that it doesn't panic or even introducing a panic would break code that previously successfully asserted that said function does not panic. It may be that we could never make this feature available except on nightly since using it would also effectively be opting into treating existing non-breaking changes as breaking changes.
And here are some previous discussions related to this topic:
- https://internals.rust-lang.org/t/pre-rfc-panic-checker/4722
- https://internals.rust-lang.org/t/enforcing-no-std-and-no-panic-during-build/14505
- https://internals.rust-lang.org/t/no-panic/1356
- https://internals.rust-lang.org/t/no-panic-again/5350
- https://www.reddit.com/r/rust/comments/djpwxs/languagelevel_nopanic_attribute/
- https://www.reddit.com/r/rust/comments/9mlpg5/an_idea_for_making_panics_more_visible_in/
- https://www.reddit.com/r/rust/comments/9mj5xc/nonpanicking_rust/
- https://www.reddit.com/r/rust/comments/7tcyh1/rust2018_no_need_to_panic/