{"id":13648665,"url":"https://github.com/dtolnay/no-panic","last_synced_at":"2025-05-13T21:12:24.644Z","repository":{"id":55451215,"uuid":"143469762","full_name":"dtolnay/no-panic","owner":"dtolnay","description":"Attribute macro to require that the compiler prove a function can't ever panic","archived":false,"fork":false,"pushed_at":"2025-03-10T22:33:02.000Z","size":292,"stargazers_count":1124,"open_issues_count":5,"forks_count":15,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-29T13:59:28.693Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dtolnay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"dtolnay"}},"created_at":"2018-08-03T20:17:43.000Z","updated_at":"2025-04-29T13:11:37.000Z","dependencies_parsed_at":"2023-11-22T20:35:56.922Z","dependency_job_id":"9874f586-e010-48d1-a31d-b168856a16e6","html_url":"https://github.com/dtolnay/no-panic","commit_stats":{"total_commits":187,"total_committers":3,"mean_commits":"62.333333333333336","dds":0.0267379679144385,"last_synced_commit":"907ef1821d0dd8fe991185ad51613aa7c1f814cb"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fno-panic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fno-panic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fno-panic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dtolnay%2Fno-panic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dtolnay","download_url":"https://codeload.github.com/dtolnay/no-panic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254029008,"owners_count":22002284,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T01:04:26.391Z","updated_at":"2025-05-13T21:12:19.624Z","avatar_url":"https://github.com/dtolnay.png","language":"Rust","funding_links":["https://github.com/sponsors/dtolnay"],"categories":["Rust"],"sub_categories":[],"readme":"\\#\\[no\\_panic\\]\n===============\n\n[\u003cimg alt=\"github\" src=\"https://img.shields.io/badge/github-dtolnay/no--panic-8da0cb?style=for-the-badge\u0026labelColor=555555\u0026logo=github\" height=\"20\"\u003e](https://github.com/dtolnay/no-panic)\n[\u003cimg alt=\"crates.io\" src=\"https://img.shields.io/crates/v/no-panic.svg?style=for-the-badge\u0026color=fc8d62\u0026logo=rust\" height=\"20\"\u003e](https://crates.io/crates/no-panic)\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/badge/docs.rs-no--panic-66c2a5?style=for-the-badge\u0026labelColor=555555\u0026logo=docs.rs\" height=\"20\"\u003e](https://docs.rs/no-panic)\n[\u003cimg alt=\"build status\" src=\"https://img.shields.io/github/actions/workflow/status/dtolnay/no-panic/ci.yml?branch=master\u0026style=for-the-badge\" height=\"20\"\u003e](https://github.com/dtolnay/no-panic/actions?query=branch%3Amaster)\n\nA Rust attribute macro to require that the compiler prove a function can't ever\npanic.\n\n```toml\n[dependencies]\nno-panic = \"0.1\"\n```\n\n```rust\nuse no_panic::no_panic;\n\n#[no_panic]\nfn demo(s: \u0026str) -\u003e \u0026str {\n    \u0026s[1..]\n}\n\nfn main() {\n    println!(\"{}\", demo(\"input string\"));\n}\n```\n\nIf the function does panic (or the compiler fails to prove that the function\ncannot panic), the program fails to compile with a linker error that identifies\nthe function name. Let's trigger that by passing a string that cannot be sliced\nat the first byte:\n\n```rust\nfn main() {\n    println!(\"{}\", demo(\"\\u{1f980}input string\"));\n}\n```\n\n```console\n   Compiling no-panic-demo v0.0.1\nerror: linking with `cc` failed: exit code: 1\n  |\n  = note: /no-panic-demo/target/release/deps/no_panic_demo-7170785b672ae322.no_p\nanic_demo1-cba7f4b666ccdbcbbf02b7348e5df1b2.rs.rcgu.o: In function `_$LT$no_pani\nc_demo..demo..__NoPanic$u20$as$u20$core..ops..drop..Drop$GT$::drop::h72f8f423002\nb8d9f':\n          no_panic_demo1-cba7f4b666ccdbcbbf02b7348e5df1b2.rs:(.text._ZN72_$LT$no\n_panic_demo..demo..__NoPanic$u20$as$u20$core..ops..drop..Drop$GT$4drop17h72f8f42\n3002b8d9fE+0x2): undefined reference to `\n\n          ERROR[no-panic]: detected panic in function `demo`\n          '\n          collect2: error: ld returned 1 exit status\n```\n\nThe error is not stellar but notice the ERROR\\[no-panic\\] part at the end that\nprovides the name of the offending function.\n\n\u003cbr\u003e\n\n### Caveats\n\n- Functions that require some amount of optimization to prove that they do not\n  panic may no longer compile in debug mode after being marked `#[no_panic]`.\n\n- Panic detection happens at link time across the entire dependency graph, so\n  any Cargo commands that do not invoke a linker will not trigger panic\n  detection. This includes `cargo build` of library crates and `cargo check` of\n  binary and library crates.\n\n- The attribute is useless in code built with `panic = \"abort\"`. Code must be\n  built with `panic = \"unwind\"` (the default) in order for any panics to be\n  detected. After confirming absence of panics, you can of course still ship\n  your software as a `panic = \"abort\"` build.\n\n- Const functions are not supported. The attribute will fail to compile if\n  placed on a `const fn`.\n\nIf you find that code requires optimization to pass `#[no_panic]`, either make\nno-panic an optional dependency that you only enable in release builds, or add a\nsection like the following to your Cargo.toml or .cargo/config.toml to enable\nvery basic optimization in debug builds.\n\n```toml\n[profile.dev]\nopt-level = 1\n```\n\nIf the code that you need to prove isn't panicking makes function calls to\nnon-generic non-inline functions from a different crate, you may need thin LTO\nenabled for the linker to deduce those do not panic.\n\n```toml\n[profile.release]\nlto = \"thin\"\n```\n\nIf thin LTO isn't cutting it, the next thing to try would be fat LTO with a\nsingle codegen unit:\n\n```toml\n[profile.release]\nlto = \"fat\"\ncodegen-units = 1\n```\n\nIf you want no\\_panic to just assume that some function you call doesn't panic,\nand get Undefined Behavior if it does at runtime, see [dtolnay/no-panic#16]; try\nwrapping that call in an `unsafe extern \"C\"` wrapper.\n\n[dtolnay/no-panic#16]: https://github.com/dtolnay/no-panic/issues/16\n\n\u003cbr\u003e\n\n### Acknowledgments\n\nThe linker error technique is based on [Kixunil]'s crate [`dont_panic`]. Check\nout that crate for other convenient ways to require absence of panics.\n\n[Kixunil]: https://github.com/Kixunil\n[`dont_panic`]: https://github.com/Kixunil/dont_panic\n\n\u003cbr\u003e\n\n#### License\n\n\u003csup\u003e\nLicensed under either of \u003ca href=\"LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\n\u003c/sup\u003e\n\n\u003cbr\u003e\n\n\u003csub\u003e\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\n\u003c/sub\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtolnay%2Fno-panic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdtolnay%2Fno-panic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdtolnay%2Fno-panic/lists"}