Skip to content

Commit

Permalink
Revert "Rollup merge of rust-lang#128104 - mu001999-contrib:fix/12805…
Browse files Browse the repository at this point in the history
…3, r=petrochenkov"

This reverts commit 91b18a0, reversing
changes made to 9aedec9.
  • Loading branch information
compiler-errors committed Jul 30, 2024
1 parent 595316b commit efdf219
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 83 deletions.
36 changes: 17 additions & 19 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,24 @@ 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| {
// treat PhantomData and positional ZST as public,
// we don't want to lint types which only have them,
// cause it's a common way to use such types to check things like well-formedness
tcx.adt_def(id).all_fields().all(|field| {
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()
})
if field_type.is_phantom_data() {
return true;
}
let is_positional = field.name.as_str().starts_with(|c: char| c.is_ascii_digit());
if is_positional
&& tcx
.layout_of(tcx.param_env(field.did).and(field_type))
.map_or(true, |layout| layout.is_zst())
{
return true;
}
field.vis.is_public()
})
}

/// check struct and its fields are public or not,
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/derives/clone-debug-dead-code-in-the-same-struct.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![forbid(dead_code)]

#[derive(Debug)]
pub struct Whatever {
pub struct Whatever { //~ ERROR struct `Whatever` is never constructed
pub field0: (),
field1: (), //~ ERROR fields `field1`, `field2`, `field3`, and `field4` are never read
field1: (),
field2: (),
field3: (),
field4: (),
Expand Down
16 changes: 3 additions & 13 deletions tests/ui/derives/clone-debug-dead-code-in-the-same-struct.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
error: fields `field1`, `field2`, `field3`, and `field4` are never read
--> $DIR/clone-debug-dead-code-in-the-same-struct.rs:6:5
error: struct `Whatever` is never constructed
--> $DIR/clone-debug-dead-code-in-the-same-struct.rs:4:12
|
LL | pub struct Whatever {
| -------- fields in this struct
LL | pub field0: (),
LL | field1: (),
| ^^^^^^
LL | field2: (),
| ^^^^^^
LL | field3: (),
| ^^^^^^
LL | field4: (),
| ^^^^^^
| ^^^^^^^^
|
= note: `Whatever` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
note: the lint level is defined here
--> $DIR/clone-debug-dead-code-in-the-same-struct.rs:1:11
|
Expand Down
35 changes: 0 additions & 35 deletions tests/ui/lint/dead-code/unconstructible-pub-struct.rs

This file was deleted.

14 changes: 0 additions & 14 deletions tests/ui/lint/dead-code/unconstructible-pub-struct.stderr

This file was deleted.

0 comments on commit efdf219

Please sign in to comment.