Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid repeating traversals and getting the types of fields #128396

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,22 @@ fn adt_of<'tcx>(ty: &hir::Ty<'tcx>) -> Option<(LocalDefId, DefKind)> {
}

fn struct_all_fields_are_public(tcx: TyCtxt<'_>, id: LocalDefId) -> bool {
let adt_def = tcx.adt_def(id);

// skip types contain fields of unit and never type,
// it's usually intentional to make the type not constructible
let not_require_constructor = adt_def.all_fields().any(|field| {
for field in tcx.adt_def(id).all_fields() {
let field_type = tcx.type_of(field.did).instantiate_identity();
field_type.is_unit() || field_type.is_never()
});

not_require_constructor
|| adt_def.all_fields().all(|field| {
let field_type = tcx.type_of(field.did).instantiate_identity();
// skip fields of PhantomData,
// cause it's a common way to check things like well-formedness
if field_type.is_phantom_data() {
return true;
}

field.vis.is_public()
})
// skip types contain fields of unit and never type,
// it's usually intentional to make the type not constructible
if field_type.is_unit() || field_type.is_never() {
return true;
}

// skip fields of PhantomData,
// cause it's a common way to check things like well-formedness
if !field_type.is_phantom_data() && !field.vis.is_public() {
return false;
}
}
true
}

/// check struct and its fields are public or not,
Expand Down
Loading