Skip to content

Commit

Permalink
Revert "Rollup merge of rust-lang#127107 - mu001999-contrib:dead/enha…
Browse files Browse the repository at this point in the history
…nce-2, r=pnkfelix"

This reverts commit 31fe962, reversing
changes made to f203078.
  • Loading branch information
compiler-errors committed Aug 3, 2024
1 parent 08328a3 commit d99ba60
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 164 deletions.
40 changes: 28 additions & 12 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
pats: &[hir::PatField<'_>],
) {
let variant = match self.typeck_results().node_type(lhs.hir_id).kind() {
ty::Adt(adt, _) => {
self.check_def_id(adt.did());
adt.variant_of_res(res)
}
ty::Adt(adt, _) => adt.variant_of_res(res),
_ => span_bug!(lhs.span, "non-ADT in struct pattern"),
};
for pat in pats {
Expand All @@ -300,10 +297,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
dotdot: hir::DotDotPos,
) {
let variant = match self.typeck_results().node_type(lhs.hir_id).kind() {
ty::Adt(adt, _) => {
self.check_def_id(adt.did());
adt.variant_of_res(res)
}
ty::Adt(adt, _) => adt.variant_of_res(res),
_ => {
self.tcx.dcx().span_delayed_bug(lhs.span, "non-ADT in tuple struct pattern");
return;
Expand Down Expand Up @@ -408,6 +402,31 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
return false;
}

// don't ignore impls for Enums and pub Structs whose methods don't have self receiver,
// cause external crate may call such methods to construct values of these types
if let Some(local_impl_of) = impl_of.as_local()
&& let Some(local_def_id) = def_id.as_local()
&& let Some(fn_sig) =
self.tcx.hir().fn_sig_by_hir_id(self.tcx.local_def_id_to_hir_id(local_def_id))
&& matches!(fn_sig.decl.implicit_self, hir::ImplicitSelfKind::None)
&& let TyKind::Path(hir::QPath::Resolved(_, path)) =
self.tcx.hir().expect_item(local_impl_of).expect_impl().self_ty.kind
&& let Res::Def(def_kind, did) = path.res
{
match def_kind {
// for example, #[derive(Default)] pub struct T(i32);
// external crate can call T::default() to construct T,
// so that don't ignore impl Default for pub Enum and Structs
DefKind::Struct | DefKind::Union if self.tcx.visibility(did).is_public() => {
return false;
}
// don't ignore impl Default for Enums,
// cause we don't know which variant is constructed
DefKind::Enum => return false,
_ => (),
};
}

if let Some(trait_of) = self.tcx.trait_id_of_impl(impl_of)
&& self.tcx.has_attr(trait_of, sym::rustc_trivial_field_reads)
{
Expand Down Expand Up @@ -671,9 +690,6 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
self.handle_field_pattern_match(pat, res, fields);
}
PatKind::Path(ref qpath) => {
if let ty::Adt(adt, _) = self.typeck_results().node_type(pat.hir_id).kind() {
self.check_def_id(adt.did());
}
let res = self.typeck_results().qpath_res(qpath, pat.hir_id);
self.handle_res(res);
}
Expand Down Expand Up @@ -829,7 +845,7 @@ fn check_item<'tcx>(
// mark the method live if the self_ty is public,
// or the method is public and may construct self
if tcx.visibility(local_def_id).is_public()
&& (ty_and_all_fields_are_public || (ty_is_public && may_construct_self))
&& (ty_and_all_fields_are_public || may_construct_self)
{
// if the impl item is public,
// and the ty may be constructed or can be constructed in foreign crates,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ use crate::ascii::Char as AsciiChar;
/// ```
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_trivial_field_reads)]
pub trait Default: Sized {
/// Returns the "default value" for a type.
///
Expand Down
3 changes: 0 additions & 3 deletions tests/ui-fulldeps/deriving-global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ mod submod {
// if any of these are implemented without global calls for any
// function calls, then being in a submodule will (correctly)
// cause errors about unrecognised module `std` (or `extra`)
#[allow(dead_code)]
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Encodable, Decodable)]
enum A {
A1(usize),
A2(isize),
}

#[allow(dead_code)]
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Encodable, Decodable)]
struct B {
x: usize,
y: isize,
}

#[allow(dead_code)]
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Encodable, Decodable)]
struct C(usize, isize);
}
Expand Down
1 change: 0 additions & 1 deletion tests/ui-fulldeps/deriving-hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub const s: u8 = 1;
pub const state: u8 = 1;
pub const cmp: u8 = 1;

#[allow(dead_code)]
#[derive(Ord, Eq, PartialOrd, PartialEq, Debug, Decodable, Encodable, Hash)]
struct Foo {}

Expand Down
1 change: 0 additions & 1 deletion tests/ui/const-generics/issues/issue-86535-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub trait Foo {
fn foo() where [(); Self::ASSOC_C]:;
}

#[allow(dead_code)]
struct Bar<const N: &'static ()>;
impl<const N: &'static ()> Foo for Bar<N> {
const ASSOC_C: usize = 3;
Expand Down
1 change: 0 additions & 1 deletion tests/ui/const-generics/issues/issue-86535.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![feature(adt_const_params, generic_const_exprs)]
#![allow(incomplete_features, unused_variables)]

#[allow(dead_code)]
struct F<const S: &'static str>;
impl<const S: &'static str> X for F<{ S }> {
const W: usize = 3;
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/impl-trait/extra-impl-in-trait-impl.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//@ run-rustfix

#[allow(dead_code)]
struct S<T>(T);
#[allow(dead_code)]
struct S2;

impl<T: Default> Default for S<T> {
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/impl-trait/extra-impl-in-trait-impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//@ run-rustfix

#[allow(dead_code)]
struct S<T>(T);
#[allow(dead_code)]
struct S2;

impl<T: Default> impl Default for S<T> {
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/impl-trait/extra-impl-in-trait-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error: unexpected `impl` keyword
--> $DIR/extra-impl-in-trait-impl.rs:8:18
--> $DIR/extra-impl-in-trait-impl.rs:6:18
|
LL | impl<T: Default> impl Default for S<T> {
| ^^^^^ help: remove the extra `impl`
|
note: this is parsed as an `impl Trait` type, but a trait is expected at this position
--> $DIR/extra-impl-in-trait-impl.rs:8:18
--> $DIR/extra-impl-in-trait-impl.rs:6:18
|
LL | impl<T: Default> impl Default for S<T> {
| ^^^^^^^^^^^^

error: unexpected `impl` keyword
--> $DIR/extra-impl-in-trait-impl.rs:14:6
--> $DIR/extra-impl-in-trait-impl.rs:12:6
|
LL | impl impl Default for S2 {
| ^^^^^ help: remove the extra `impl`
|
note: this is parsed as an `impl Trait` type, but a trait is expected at this position
--> $DIR/extra-impl-in-trait-impl.rs:14:6
--> $DIR/extra-impl-in-trait-impl.rs:12:6
|
LL | impl impl Default for S2 {
| ^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/dead-code/issue-59003.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#![deny(dead_code)]

#[allow(dead_code)]
struct Foo {
#[allow(dead_code)]
inner: u32,
}

Expand Down
37 changes: 0 additions & 37 deletions tests/ui/lint/dead-code/lint-unused-adt-appeared-in-pattern.rs

This file was deleted.

20 changes: 0 additions & 20 deletions tests/ui/lint/dead-code/lint-unused-adt-appeared-in-pattern.stderr

This file was deleted.

32 changes: 0 additions & 32 deletions tests/ui/lint/dead-code/not-lint-used-adt-appeared-in-pattern.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![deny(dead_code)]

struct T1; //~ ERROR struct `T1` is never constructed
struct T2; //~ ERROR struct `T2` is never constructed
pub struct T3(i32); //~ ERROR struct `T3` is never constructed
pub struct T4(i32); //~ ERROR field `0` is never read
pub struct T2(i32); //~ ERROR struct `T2` is never constructed
struct T3;

trait Trait1 { //~ ERROR trait `Trait1` is never used
const UNUSED: i32;
Expand All @@ -12,13 +11,13 @@ trait Trait1 { //~ ERROR trait `Trait1` is never used
}

pub trait Trait2 {
const MAY_USED: i32;
fn may_used(&self) {}
const USED: i32;
fn used(&self) {}
}

pub trait Trait3 {
const MAY_USED: i32;
fn may_used() -> Self;
const USED: i32;
fn construct_self() -> Self;
}

impl Trait1 for T1 {
Expand All @@ -31,34 +30,23 @@ impl Trait1 for T1 {
impl Trait1 for T2 {
const UNUSED: i32 = 0;
fn construct_self() -> Self {
Self
T2(0)
}
}

impl Trait2 for T1 {
const MAY_USED: i32 = 0;
const USED: i32 = 0;
}

impl Trait2 for T2 {
const MAY_USED: i32 = 0;
}

impl Trait2 for T3 {
const MAY_USED: i32 = 0;
const USED: i32 = 0;
}

impl Trait3 for T2 {
const MAY_USED: i32 = 0;
fn may_used() -> Self {
impl Trait3 for T3 {
const USED: i32 = 0;
fn construct_self() -> Self {
Self
}
}

impl Trait3 for T4 {
const MAY_USED: i32 = 0;
fn may_used() -> Self {
T4(0)
}
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,16 @@ LL | #![deny(dead_code)]
| ^^^^^^^^^

error: struct `T2` is never constructed
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:4:8
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:4:12
|
LL | struct T2;
| ^^

error: struct `T3` is never constructed
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:5:12
|
LL | pub struct T3(i32);
LL | pub struct T2(i32);
| ^^

error: field `0` is never read
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:6:15
|
LL | pub struct T4(i32);
| -- ^^^
| |
| field in this struct
|
= help: consider removing this field

error: trait `Trait1` is never used
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:8:7
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:7:7
|
LL | trait Trait1 {
| ^^^^^^

error: aborting due to 5 previous errors
error: aborting due to 3 previous errors

1 change: 0 additions & 1 deletion tests/ui/lint/dead-code/unused-struct-derive-default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ pub struct T2 {

fn main() {
let _x: Used = Default::default();
let _e: E = Default::default();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: struct `T` is never constructed
LL | struct T;
| ^
|
= note: `T` has a derived impl for the trait `Default`, but this is intentionally ignored during dead code analysis
note: the lint level is defined here
--> $DIR/unused-struct-derive-default.rs:1:9
|
Expand Down
Loading

0 comments on commit d99ba60

Please sign in to comment.