rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.04k stars 12.54k forks source link

consider bannig empty `panic`/`unreachable`/ `bug`s from rustc codebase #118955

Open matthiaskrgr opened 9 months ago

matthiaskrgr commented 9 months ago
git grep "[^e]bug\!()" compiler   130 
git grep "panic\!()" compiler  43 
git grep "unreachable\!()" compiler  384

I wonder if it makes sense to try to fill these up with at least somewhat contextual error/ice messages instead of just a "panicked at" or "impossible case reached"?

Unfortunately I could not find a rustc lint that warns on empty panics like these

workingjubilee commented 9 months ago

13 of the panic!() cases are in comments, 4 are not in .rs files, and 2 are in rustc_codegen_cranelift as example code.

That leaves 24 unlabeled panics, as far as I can tell:

rustc_expand/src/parse/tests.rs
325:        let ast::ItemKind::Mod(_, mod_kind) = &item.kind else { panic!() };

rustc_hir_pretty/src/lib.rs
2182:                            _ => panic!(),

rustc_metadata/src/creader.rs
599:            _ => panic!(),

rustc_resolve/src/late.rs
2540:            panic!()

rustc_hir_analysis/src/collect/type_of.rs
23:    let Node::AnonConst(_) = tcx.hir().get(hir_id) else { panic!() };

rustc_span/src/caching_source_map_view.rs
208:                panic!();

rustc_middle/src/dep_graph/dep_node.rs
110:                    panic!();

rustc_ast_pretty/src/pprust/state.rs
1558:                _ => panic!(),

rustc_ast_lowering/src/path.rs
288:            Some(_) => panic!(),

rustc_abi/src/layout.rs
603:                    _ => panic!(),
631:                    panic!();
684:                    _ => panic!(),
761:            _ => panic!(),
1157:                            _ => panic!(),

rustc_lint/src/early.rs
317:        panic!()

rustc_lint/src/passes.rs
128:                panic!()
243:                panic!()

rustc_lint/src/late.rs
330:        panic!()

rustc_query_system/src/query/plumbing.rs
174:                QueryResult::Poisoned => panic!(),
195:                QueryResult::Poisoned => panic!(),

rustc_codegen_gcc/src/common.rs
254:                    panic!()

rustc_codegen_gcc/src/builder.rs
1160:            panic!();
1191:                panic!();
compiler-errors commented 9 months ago

I'll take a stab at annotating some of the ones in rustc_hir*, rustc_trait*, and rustc_infer.

workingjubilee commented 9 months ago

Assuming the currently outstanding PRs are accepted, the remaining unlabeled panic!() instances are in rustc_codegen_gcc, rustc_lint, rustc_metadata, rustc_query_system, and rustc_resolve.

I have not even glanced at where all the unreachable!() instances are since there are... many.

fee1-dead commented 9 months ago

would it be worth it to add an internal lint for this?

jieyouxu commented 6 months ago

Update (2024-02-27):

git grep "[^e]bug\!()" compiler | count       111
git grep "panic\!()" compiler | count         30
git grep "unreachable\!()" compiler | count   358