Rollup merge of #107465 - WaffleLapkin:has_allow_dead_code_or_lang_attr, r=Nilstrieb

`has_allow_dead_code_or_lang_attr` micro refactor

r? `@Nilstrieb`
This commit is contained in:
Matthias Krüger 2023-01-30 17:50:11 +01:00 committed by GitHub
commit 3c155dc7b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -459,30 +459,32 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
} }
fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
if tcx.has_attr(def_id.to_def_id(), sym::lang) { fn has_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
return true; tcx.has_attr(def_id.to_def_id(), sym::lang)
}
// Stable attribute for #[lang = "panic_impl"] // Stable attribute for #[lang = "panic_impl"]
if tcx.has_attr(def_id.to_def_id(), sym::panic_handler) { || tcx.has_attr(def_id.to_def_id(), sym::panic_handler)
return true;
} }
if tcx.def_kind(def_id).has_codegen_attrs() { fn has_allow_dead_code(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0 == lint::Allow
}
fn has_used_like_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
tcx.def_kind(def_id).has_codegen_attrs() && {
let cg_attrs = tcx.codegen_fn_attrs(def_id); let cg_attrs = tcx.codegen_fn_attrs(def_id);
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive // #[used], #[no_mangle], #[export_name], etc also keeps the item alive
// forcefully, e.g., for placing it in a specific section. // forcefully, e.g., for placing it in a specific section.
if cg_attrs.contains_extern_indicator() cg_attrs.contains_extern_indicator()
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED) || cg_attrs.flags.contains(CodegenFnAttrFlags::USED)
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) || cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
{
return true;
} }
} }
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); has_allow_dead_code(tcx, def_id)
tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0 == lint::Allow || has_used_like_attr(tcx, def_id)
|| has_lang_attr(tcx, def_id)
} }
// These check_* functions seeds items that // These check_* functions seeds items that