Skip to content

Commit cfe4039

Browse files
Revert "Rollup merge of rust-lang#127107 - mu001999-contrib:dead/enhance-2, r=pnkfelix"
This reverts commit 31fe962, reversing changes made to f203078.
1 parent 8a0cd61 commit cfe4039

21 files changed

+60
-164
lines changed

compiler/rustc_passes/src/dead.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
278278
pats: &[hir::PatField<'_>],
279279
) {
280280
let variant = match self.typeck_results().node_type(lhs.hir_id).kind() {
281-
ty::Adt(adt, _) => {
282-
self.check_def_id(adt.did());
283-
adt.variant_of_res(res)
284-
}
281+
ty::Adt(adt, _) => adt.variant_of_res(res),
285282
_ => span_bug!(lhs.span, "non-ADT in struct pattern"),
286283
};
287284
for pat in pats {
@@ -301,10 +298,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
301298
dotdot: hir::DotDotPos,
302299
) {
303300
let variant = match self.typeck_results().node_type(lhs.hir_id).kind() {
304-
ty::Adt(adt, _) => {
305-
self.check_def_id(adt.did());
306-
adt.variant_of_res(res)
307-
}
301+
ty::Adt(adt, _) => adt.variant_of_res(res),
308302
_ => {
309303
self.tcx.dcx().span_delayed_bug(lhs.span, "non-ADT in tuple struct pattern");
310304
return;
@@ -409,6 +403,31 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
409403
return false;
410404
}
411405

406+
// don't ignore impls for Enums and pub Structs whose methods don't have self receiver,
407+
// cause external crate may call such methods to construct values of these types
408+
if let Some(local_impl_of) = impl_of.as_local()
409+
&& let Some(local_def_id) = def_id.as_local()
410+
&& let Some(fn_sig) =
411+
self.tcx.hir().fn_sig_by_hir_id(self.tcx.local_def_id_to_hir_id(local_def_id))
412+
&& matches!(fn_sig.decl.implicit_self, hir::ImplicitSelfKind::None)
413+
&& let TyKind::Path(hir::QPath::Resolved(_, path)) =
414+
self.tcx.hir().expect_item(local_impl_of).expect_impl().self_ty.kind
415+
&& let Res::Def(def_kind, did) = path.res
416+
{
417+
match def_kind {
418+
// for example, #[derive(Default)] pub struct T(i32);
419+
// external crate can call T::default() to construct T,
420+
// so that don't ignore impl Default for pub Enum and Structs
421+
DefKind::Struct | DefKind::Union if self.tcx.visibility(did).is_public() => {
422+
return false;
423+
}
424+
// don't ignore impl Default for Enums,
425+
// cause we don't know which variant is constructed
426+
DefKind::Enum => return false,
427+
_ => (),
428+
};
429+
}
430+
412431
if let Some(trait_of) = self.tcx.trait_id_of_impl(impl_of)
413432
&& self.tcx.has_attr(trait_of, sym::rustc_trivial_field_reads)
414433
{
@@ -672,9 +691,6 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
672691
self.handle_field_pattern_match(pat, res, fields);
673692
}
674693
PatKind::Path(ref qpath) => {
675-
if let ty::Adt(adt, _) = self.typeck_results().node_type(pat.hir_id).kind() {
676-
self.check_def_id(adt.did());
677-
}
678694
let res = self.typeck_results().qpath_res(qpath, pat.hir_id);
679695
self.handle_res(res);
680696
}
@@ -830,7 +846,7 @@ fn check_item<'tcx>(
830846
// mark the method live if the self_ty is public,
831847
// or the method is public and may construct self
832848
if tcx.visibility(local_def_id).is_public()
833-
&& (ty_and_all_fields_are_public || (ty_is_public && may_construct_self))
849+
&& (ty_and_all_fields_are_public || may_construct_self)
834850
{
835851
// if the impl item is public,
836852
// and the ty may be constructed or can be constructed in foreign crates,

library/core/src/default.rs

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ use crate::ascii::Char as AsciiChar;
103103
/// ```
104104
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
105105
#[stable(feature = "rust1", since = "1.0.0")]
106+
#[cfg_attr(not(bootstrap), rustc_trivial_field_reads)]
106107
pub trait Default: Sized {
107108
/// Returns the "default value" for a type.
108109
///

src/tools/rust-analyzer/crates/salsa/salsa-macros/src/database_storage.rs

+8
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,11 @@ impl Parse for QueryGroup {
241241
Ok(QueryGroup { group_path })
242242
}
243243
}
244+
245+
struct Nothing;
246+
247+
impl Parse for Nothing {
248+
fn parse(_input: ParseStream<'_>) -> syn::Result<Self> {
249+
Ok(Nothing)
250+
}
251+
}

tests/ui-fulldeps/deriving-global.rs

-3
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@ mod submod {
1717
// if any of these are implemented without global calls for any
1818
// function calls, then being in a submodule will (correctly)
1919
// cause errors about unrecognised module `std` (or `extra`)
20-
#[allow(dead_code)]
2120
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Encodable, Decodable)]
2221
enum A {
2322
A1(usize),
2423
A2(isize),
2524
}
2625

27-
#[allow(dead_code)]
2826
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Encodable, Decodable)]
2927
struct B {
3028
x: usize,
3129
y: isize,
3230
}
3331

34-
#[allow(dead_code)]
3532
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Encodable, Decodable)]
3633
struct C(usize, isize);
3734
}

tests/ui-fulldeps/deriving-hygiene.rs

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub const s: u8 = 1;
2020
pub const state: u8 = 1;
2121
pub const cmp: u8 = 1;
2222

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

tests/ui/const-generics/issues/issue-86535-2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub trait Foo {
99
[(); Self::ASSOC_C]:;
1010
}
1111

12-
#[allow(dead_code)]
1312
struct Bar<const N: &'static ()>;
1413
impl<const N: &'static ()> Foo for Bar<N> {
1514
const ASSOC_C: usize = 3;

tests/ui/const-generics/issues/issue-86535.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![feature(adt_const_params, unsized_const_params, generic_const_exprs)]
33
#![allow(incomplete_features, unused_variables)]
44

5-
#[allow(dead_code)]
65
struct F<const S: &'static str>;
76
impl<const S: &'static str> X for F<{ S }> {
87
const W: usize = 3;

tests/ui/impl-trait/extra-impl-in-trait-impl.fixed

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//@ run-rustfix
22

3-
#[allow(dead_code)]
43
struct S<T>(T);
5-
#[allow(dead_code)]
64
struct S2;
75

86
impl<T: Default> Default for S<T> {

tests/ui/impl-trait/extra-impl-in-trait-impl.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//@ run-rustfix
22

3-
#[allow(dead_code)]
43
struct S<T>(T);
5-
#[allow(dead_code)]
64
struct S2;
75

86
impl<T: Default> impl Default for S<T> {

tests/ui/impl-trait/extra-impl-in-trait-impl.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
error: unexpected `impl` keyword
2-
--> $DIR/extra-impl-in-trait-impl.rs:8:18
2+
--> $DIR/extra-impl-in-trait-impl.rs:6:18
33
|
44
LL | impl<T: Default> impl Default for S<T> {
55
| ^^^^^ help: remove the extra `impl`
66
|
77
note: this is parsed as an `impl Trait` type, but a trait is expected at this position
8-
--> $DIR/extra-impl-in-trait-impl.rs:8:18
8+
--> $DIR/extra-impl-in-trait-impl.rs:6:18
99
|
1010
LL | impl<T: Default> impl Default for S<T> {
1111
| ^^^^^^^^^^^^
1212

1313
error: unexpected `impl` keyword
14-
--> $DIR/extra-impl-in-trait-impl.rs:14:6
14+
--> $DIR/extra-impl-in-trait-impl.rs:12:6
1515
|
1616
LL | impl impl Default for S2 {
1717
| ^^^^^ help: remove the extra `impl`
1818
|
1919
note: this is parsed as an `impl Trait` type, but a trait is expected at this position
20-
--> $DIR/extra-impl-in-trait-impl.rs:14:6
20+
--> $DIR/extra-impl-in-trait-impl.rs:12:6
2121
|
2222
LL | impl impl Default for S2 {
2323
| ^^^^^^^^^^^^

tests/ui/lint/dead-code/issue-59003.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#![deny(dead_code)]
66

7-
#[allow(dead_code)]
87
struct Foo {
8+
#[allow(dead_code)]
99
inner: u32,
1010
}
1111

tests/ui/lint/dead-code/lint-unused-adt-appeared-in-pattern.rs

-37
This file was deleted.

tests/ui/lint/dead-code/lint-unused-adt-appeared-in-pattern.stderr

-20
This file was deleted.

tests/ui/lint/dead-code/not-lint-used-adt-appeared-in-pattern.rs

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![deny(dead_code)]
22

33
struct T1; //~ ERROR struct `T1` is never constructed
4-
struct T2; //~ ERROR struct `T2` is never constructed
5-
pub struct T3(i32); //~ ERROR struct `T3` is never constructed
6-
pub struct T4(i32); //~ ERROR field `0` is never read
4+
pub struct T2(i32); //~ ERROR struct `T2` is never constructed
5+
struct T3;
76

87
trait Trait1 { //~ ERROR trait `Trait1` is never used
98
const UNUSED: i32;
@@ -12,13 +11,13 @@ trait Trait1 { //~ ERROR trait `Trait1` is never used
1211
}
1312

1413
pub trait Trait2 {
15-
const MAY_USED: i32;
16-
fn may_used(&self) {}
14+
const USED: i32;
15+
fn used(&self) {}
1716
}
1817

1918
pub trait Trait3 {
20-
const MAY_USED: i32;
21-
fn may_used() -> Self;
19+
const USED: i32;
20+
fn construct_self() -> Self;
2221
}
2322

2423
impl Trait1 for T1 {
@@ -31,34 +30,23 @@ impl Trait1 for T1 {
3130
impl Trait1 for T2 {
3231
const UNUSED: i32 = 0;
3332
fn construct_self() -> Self {
34-
Self
33+
T2(0)
3534
}
3635
}
3736

3837
impl Trait2 for T1 {
39-
const MAY_USED: i32 = 0;
38+
const USED: i32 = 0;
4039
}
4140

4241
impl Trait2 for T2 {
43-
const MAY_USED: i32 = 0;
44-
}
45-
46-
impl Trait2 for T3 {
47-
const MAY_USED: i32 = 0;
42+
const USED: i32 = 0;
4843
}
4944

50-
impl Trait3 for T2 {
51-
const MAY_USED: i32 = 0;
52-
fn may_used() -> Self {
45+
impl Trait3 for T3 {
46+
const USED: i32 = 0;
47+
fn construct_self() -> Self {
5348
Self
5449
}
5550
}
5651

57-
impl Trait3 for T4 {
58-
const MAY_USED: i32 = 0;
59-
fn may_used() -> Self {
60-
T4(0)
61-
}
62-
}
63-
6452
fn main() {}

tests/ui/lint/dead-code/unused-adt-impl-pub-trait-with-assoc-const.stderr

+4-20
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,16 @@ LL | #![deny(dead_code)]
1111
| ^^^^^^^^^
1212

1313
error: struct `T2` is never constructed
14-
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:4:8
14+
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:4:12
1515
|
16-
LL | struct T2;
17-
| ^^
18-
19-
error: struct `T3` is never constructed
20-
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:5:12
21-
|
22-
LL | pub struct T3(i32);
16+
LL | pub struct T2(i32);
2317
| ^^
2418

25-
error: field `0` is never read
26-
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:6:15
27-
|
28-
LL | pub struct T4(i32);
29-
| -- ^^^
30-
| |
31-
| field in this struct
32-
|
33-
= help: consider removing this field
34-
3519
error: trait `Trait1` is never used
36-
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:8:7
20+
--> $DIR/unused-adt-impl-pub-trait-with-assoc-const.rs:7:7
3721
|
3822
LL | trait Trait1 {
3923
| ^^^^^^
4024

41-
error: aborting due to 5 previous errors
25+
error: aborting due to 3 previous errors
4226

0 commit comments

Comments
 (0)