Skip to content

Commit 865ba84

Browse files
committed
Improve dead code analysis for structs and traits defined locally
1 parent 64ebd39 commit 865ba84

File tree

54 files changed

+566
-319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+566
-319
lines changed

compiler/rustc_incremental/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,4 @@ incremental_unrecognized_depnode = unrecognized `DepNode` variant: {$name}
9999
100100
incremental_unrecognized_depnode_label = dep-node label `{$label}` not recognized
101101
102-
incremental_write_dep_graph = failed to write dependency graph to `{$path}`: {$err}
103-
104102
incremental_write_new = failed to write {$name} to `{$path}`: {$err}

compiler/rustc_incremental/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,6 @@ pub struct LoadDepGraph {
272272
pub err: std::io::Error,
273273
}
274274

275-
#[derive(Diagnostic)]
276-
#[diag(incremental_write_dep_graph)]
277-
pub struct WriteDepGraph<'a> {
278-
pub path: &'a Path,
279-
pub err: std::io::Error,
280-
}
281-
282275
#[derive(Diagnostic)]
283276
#[diag(incremental_move_dep_graph)]
284277
pub struct MoveDepGraph<'a> {

compiler/rustc_passes/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,6 @@ passes_must_not_suspend =
471471
`must_not_suspend` attribute should be applied to a struct, enum, or trait
472472
.label = is not a struct, enum, or trait
473473
474-
passes_must_use_async =
475-
`must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within
476-
.label = this attribute does nothing, the `Future`s returned by async functions are already `must_use`
477-
478474
passes_must_use_no_effect =
479475
`#[must_use]` has no effect when applied to {$article} {$target}
480476

compiler/rustc_passes/src/dead.rs

+222-122
Large diffs are not rendered by default.

compiler/rustc_passes/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,6 @@ pub struct FfiConstInvalidTarget {
371371
pub attr_span: Span,
372372
}
373373

374-
#[derive(LintDiagnostic)]
375-
#[diag(passes_must_use_async)]
376-
pub struct MustUseAsync {
377-
#[label]
378-
pub span: Span,
379-
}
380-
381374
#[derive(LintDiagnostic)]
382375
#[diag(passes_must_use_no_effect)]
383376
pub struct MustUseNoEffect {

compiler/rustc_transmute/src/maybe_transmutable/query_context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::layout;
44
pub(crate) trait QueryContext {
55
type Def: layout::Def;
66
type Ref: layout::Ref;
7-
type Scope: Copy;
87
}
98

109
#[cfg(test)]
@@ -28,20 +27,17 @@ pub(crate) mod test {
2827
impl QueryContext for UltraMinimal {
2928
type Def = Def;
3029
type Ref = !;
31-
type Scope = ();
3230
}
3331
}
3432

3533
#[cfg(feature = "rustc")]
3634
mod rustc {
37-
use rustc_middle::ty::{Ty, TyCtxt};
35+
use rustc_middle::ty::TyCtxt;
3836

3937
use super::*;
4038

4139
impl<'tcx> super::QueryContext for TyCtxt<'tcx> {
4240
type Def = layout::rustc::Def<'tcx>;
4341
type Ref = layout::rustc::Ref<'tcx>;
44-
45-
type Scope = Ty<'tcx>;
4642
}
4743
}

library/alloc/tests/boxed.rs

-122
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::alloc::{AllocError, Allocator, Layout};
21
use core::cell::Cell;
32
use core::mem::MaybeUninit;
43
use core::ptr::NonNull;
@@ -58,124 +57,3 @@ fn box_deref_lval() {
5857
x.set(1000);
5958
assert_eq!(x.get(), 1000);
6059
}
61-
62-
pub struct ConstAllocator;
63-
64-
unsafe impl Allocator for ConstAllocator {
65-
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
66-
match layout.size() {
67-
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
68-
_ => unsafe {
69-
let ptr = core::intrinsics::const_allocate(layout.size(), layout.align());
70-
Ok(NonNull::new_unchecked(ptr as *mut [u8; 0] as *mut [u8]))
71-
},
72-
}
73-
}
74-
75-
unsafe fn deallocate(&self, _ptr: NonNull<u8>, layout: Layout) {
76-
match layout.size() {
77-
0 => { /* do nothing */ }
78-
_ => { /* do nothing too */ }
79-
}
80-
}
81-
82-
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
83-
let ptr = self.allocate(layout)?;
84-
if layout.size() > 0 {
85-
unsafe {
86-
ptr.as_mut_ptr().write_bytes(0, layout.size());
87-
}
88-
}
89-
Ok(ptr)
90-
}
91-
92-
unsafe fn grow(
93-
&self,
94-
ptr: NonNull<u8>,
95-
old_layout: Layout,
96-
new_layout: Layout,
97-
) -> Result<NonNull<[u8]>, AllocError> {
98-
debug_assert!(
99-
new_layout.size() >= old_layout.size(),
100-
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
101-
);
102-
103-
let new_ptr = self.allocate(new_layout)?;
104-
if new_layout.size() > 0 {
105-
// Safety: `new_ptr` is valid for writes and `ptr` for reads of
106-
// `old_layout.size()`, because `new_layout.size() >=
107-
// old_layout.size()` (which is an invariant that must be upheld by
108-
// callers).
109-
unsafe {
110-
new_ptr.as_mut_ptr().copy_from_nonoverlapping(ptr.as_ptr(), old_layout.size());
111-
}
112-
// Safety: `ptr` is never used again is also an invariant which must
113-
// be upheld by callers.
114-
unsafe {
115-
self.deallocate(ptr, old_layout);
116-
}
117-
}
118-
Ok(new_ptr)
119-
}
120-
121-
unsafe fn grow_zeroed(
122-
&self,
123-
ptr: NonNull<u8>,
124-
old_layout: Layout,
125-
new_layout: Layout,
126-
) -> Result<NonNull<[u8]>, AllocError> {
127-
// Safety: Invariants of `grow_zeroed` and `grow` are the same, and must
128-
// be enforced by callers.
129-
let new_ptr = unsafe { self.grow(ptr, old_layout, new_layout)? };
130-
if new_layout.size() > 0 {
131-
let old_size = old_layout.size();
132-
let new_size = new_layout.size();
133-
let raw_ptr = new_ptr.as_mut_ptr();
134-
// Safety:
135-
// - `grow` returned Ok, so the returned pointer must be valid for
136-
// `new_size` bytes
137-
// - `new_size` must be larger than `old_size`, which is an
138-
// invariant which must be upheld by callers.
139-
unsafe {
140-
raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
141-
}
142-
}
143-
Ok(new_ptr)
144-
}
145-
146-
unsafe fn shrink(
147-
&self,
148-
ptr: NonNull<u8>,
149-
old_layout: Layout,
150-
new_layout: Layout,
151-
) -> Result<NonNull<[u8]>, AllocError> {
152-
debug_assert!(
153-
new_layout.size() <= old_layout.size(),
154-
"`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
155-
);
156-
157-
let new_ptr = self.allocate(new_layout)?;
158-
if new_layout.size() > 0 {
159-
// Safety: `new_ptr` and `ptr` are valid for reads/writes of
160-
// `new_layout.size()` because of the invariants of shrink, which
161-
// include `new_layout.size()` being smaller than (or equal to)
162-
// `old_layout.size()`.
163-
unsafe {
164-
new_ptr.as_mut_ptr().copy_from_nonoverlapping(ptr.as_ptr(), new_layout.size());
165-
}
166-
// Safety: `ptr` is never used again is also an invariant which must
167-
// be upheld by callers.
168-
unsafe {
169-
self.deallocate(ptr, old_layout);
170-
}
171-
}
172-
Ok(new_ptr)
173-
}
174-
175-
fn by_ref(&self) -> &Self
176-
where
177-
Self: Sized,
178-
{
179-
self
180-
}
181-
}

library/core/src/default.rs

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ 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)]
107106
pub trait Default: Sized {
108107
/// Returns the "default value" for a type.
109108
///

tests/ui/associated-type-bounds/union-bounds.rs

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

55
trait Tr1: Copy { type As1: Copy; }
66
trait Tr2: Copy { type As2: Copy; }
7-
trait Tr3: Copy { type As3: Copy; }
7+
trait Tr3: Copy { #[allow(dead_code)] type As3: Copy; }
88
trait Tr4<'a>: Copy { type As4: Copy; }
99
trait Tr5: Copy { type As5: Copy; }
1010

tests/ui/associated-types/impl-wf-cycle-5.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ impl Fiz for bool {}
1111

1212
trait Grault {
1313
type A;
14+
#[allow(dead_code)]
1415
type B;
1516
}
1617

tests/ui/associated-types/impl-wf-cycle-5.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ impl Fiz for bool {}
1111

1212
trait Grault {
1313
type A;
14+
#[allow(dead_code)]
1415
type B;
1516
}
1617

tests/ui/associated-types/impl-wf-cycle-5.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
2-
--> $DIR/impl-wf-cycle-5.rs:22:1
2+
--> $DIR/impl-wf-cycle-5.rs:23:1
33
|
44
LL | / impl<T> Grault for (T,)
55
LL | |
@@ -12,7 +12,7 @@ LL | type A = ();
1212
| ------ associated type `<(T,) as Grault>::A` is specified here
1313
|
1414
note: required for `(T,)` to implement `Grault`
15-
--> $DIR/impl-wf-cycle-5.rs:22:9
15+
--> $DIR/impl-wf-cycle-5.rs:23:9
1616
|
1717
LL | impl<T> Grault for (T,)
1818
| ^^^^^^ ^^^^

tests/ui/associated-types/impl-wf-cycle-6.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ impl Fiz for bool {}
1111

1212
trait Grault {
1313
type A;
14+
#[allow(dead_code)]
1415
type B;
1516
}
1617

tests/ui/associated-types/impl-wf-cycle-6.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ impl Fiz for bool {}
1111

1212
trait Grault {
1313
type A;
14+
#[allow(dead_code)]
1415
type B;
1516
}
1617

tests/ui/associated-types/impl-wf-cycle-6.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
2-
--> $DIR/impl-wf-cycle-6.rs:22:1
2+
--> $DIR/impl-wf-cycle-6.rs:23:1
33
|
44
LL | / impl<T: Grault> Grault for (T,)
55
LL | |
@@ -11,7 +11,7 @@ LL | type A = ();
1111
| ------ associated type `<(T,) as Grault>::A` is specified here
1212
|
1313
note: required for `(T,)` to implement `Grault`
14-
--> $DIR/impl-wf-cycle-6.rs:22:17
14+
--> $DIR/impl-wf-cycle-6.rs:23:17
1515
|
1616
LL | impl<T: Grault> Grault for (T,)
1717
| ^^^^^^ ^^^^

tests/ui/const-generics/cross_crate_complex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async fn foo() {
1111
async_in_foo(async_out_foo::<4>().await).await;
1212
}
1313

14-
struct Faz<const N: usize>;
14+
struct Faz<const N: usize>; //~ WARN struct `Faz` is never constructed
1515

1616
impl<const N: usize> Foo<N> for Faz<N> {}
1717
impl<const N: usize> Bar<N> for Faz<N> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: struct `Faz` is never constructed
2+
--> $DIR/cross_crate_complex.rs:14:8
3+
|
4+
LL | struct Faz<const N: usize>;
5+
| ^^^
6+
|
7+
= note: `#[warn(dead_code)]` on by default
8+
9+
warning: 1 warning emitted
10+

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

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

12-
struct Bar<const N: &'static ()>;
12+
struct Bar<const N: &'static ()>; //~ WARN struct `Bar` is never constructed
1313
impl<const N: &'static ()> Foo for Bar<N> {
1414
const ASSOC_C: usize = 3;
1515

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: struct `Bar` is never constructed
2+
--> $DIR/issue-86535-2.rs:12:8
3+
|
4+
LL | struct Bar<const N: &'static ()>;
5+
| ^^^
6+
|
7+
= note: `#[warn(dead_code)]` on by default
8+
9+
warning: 1 warning emitted
10+

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

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

5-
struct F<const S: &'static str>;
5+
struct F<const S: &'static str>; //~ WARN struct `F` is never constructed
66
impl<const S: &'static str> X for F<{ S }> {
77
const W: usize = 3;
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: struct `F` is never constructed
2+
--> $DIR/issue-86535.rs:5:8
3+
|
4+
LL | struct F<const S: &'static str>;
5+
| ^
6+
|
7+
= note: `#[warn(dead_code)]` on by default
8+
9+
warning: 1 warning emitted
10+

tests/ui/deriving/deriving-in-macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ macro_rules! define_vec {
66
() => (
77
mod foo {
88
#[derive(PartialEq)]
9-
pub struct bar;
9+
pub struct bar; //~ WARN struct `bar` is never constructed
1010
}
1111
)
1212
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: struct `bar` is never constructed
2+
--> $DIR/deriving-in-macro.rs:9:24
3+
|
4+
LL | pub struct bar;
5+
| ^^^
6+
...
7+
LL | define_vec![];
8+
| ------------- in this macro invocation
9+
|
10+
= note: `#[warn(dead_code)]` on by default
11+
= note: this warning originates in the macro `define_vec` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
13+
warning: 1 warning emitted
14+

tests/ui/generic-associated-types/collections.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ trait Collection<T> {
1010
type Iter<'iter>: Iterator<Item=&'iter T> where T: 'iter, Self: 'iter;
1111
type Family: CollectionFamily;
1212
// Test associated type defaults with parameters
13+
#[allow(dead_code)]
1314
type Sibling<U>: Collection<U> =
1415
<<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
1516

tests/ui/generic-associated-types/missing-bounds.fixed

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

3+
#![allow(dead_code)]
4+
35
use std::ops::Add;
46

57
struct A<B>(B);

tests/ui/generic-associated-types/missing-bounds.rs

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

3+
#![allow(dead_code)]
4+
35
use std::ops::Add;
46

57
struct A<B>(B);

0 commit comments

Comments
 (0)