Skip to content

Commit

Permalink
Improve dead code analysis for structs and traits defined locally
Browse files Browse the repository at this point in the history
  • Loading branch information
mu001999 committed Aug 4, 2024
1 parent 64ebd39 commit 8507664
Show file tree
Hide file tree
Showing 54 changed files with 586 additions and 317 deletions.
2 changes: 0 additions & 2 deletions compiler/rustc_incremental/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,4 @@ incremental_unrecognized_depnode = unrecognized `DepNode` variant: {$name}
incremental_unrecognized_depnode_label = dep-node label `{$label}` not recognized
incremental_write_dep_graph = failed to write dependency graph to `{$path}`: {$err}
incremental_write_new = failed to write {$name} to `{$path}`: {$err}
7 changes: 0 additions & 7 deletions compiler/rustc_incremental/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,6 @@ pub struct LoadDepGraph {
pub err: std::io::Error,
}

#[derive(Diagnostic)]
#[diag(incremental_write_dep_graph)]
pub struct WriteDepGraph<'a> {
pub path: &'a Path,
pub err: std::io::Error,
}

#[derive(Diagnostic)]
#[diag(incremental_move_dep_graph)]
pub struct MoveDepGraph<'a> {
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,6 @@ passes_must_not_suspend =
`must_not_suspend` attribute should be applied to a struct, enum, or trait
.label = is not a struct, enum, or trait
passes_must_use_async =
`must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within
.label = this attribute does nothing, the `Future`s returned by async functions are already `must_use`
passes_must_use_no_effect =
`#[must_use]` has no effect when applied to {$article} {$target}
Expand Down
363 changes: 242 additions & 121 deletions compiler/rustc_passes/src/dead.rs

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,6 @@ pub struct FfiConstInvalidTarget {
pub attr_span: Span,
}

#[derive(LintDiagnostic)]
#[diag(passes_must_use_async)]
pub struct MustUseAsync {
#[label]
pub span: Span,
}

#[derive(LintDiagnostic)]
#[diag(passes_must_use_no_effect)]
pub struct MustUseNoEffect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::layout;
pub(crate) trait QueryContext {
type Def: layout::Def;
type Ref: layout::Ref;
type Scope: Copy;
}

#[cfg(test)]
Expand All @@ -28,20 +27,17 @@ pub(crate) mod test {
impl QueryContext for UltraMinimal {
type Def = Def;
type Ref = !;
type Scope = ();
}
}

#[cfg(feature = "rustc")]
mod rustc {
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_middle::ty::TyCtxt;

use super::*;

impl<'tcx> super::QueryContext for TyCtxt<'tcx> {
type Def = layout::rustc::Def<'tcx>;
type Ref = layout::rustc::Ref<'tcx>;

type Scope = Ty<'tcx>;
}
}
121 changes: 0 additions & 121 deletions library/alloc/tests/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,124 +58,3 @@ fn box_deref_lval() {
x.set(1000);
assert_eq!(x.get(), 1000);
}

pub struct ConstAllocator;

unsafe impl Allocator for ConstAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
match layout.size() {
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
_ => unsafe {
let ptr = core::intrinsics::const_allocate(layout.size(), layout.align());
Ok(NonNull::new_unchecked(ptr as *mut [u8; 0] as *mut [u8]))
},
}
}

unsafe fn deallocate(&self, _ptr: NonNull<u8>, layout: Layout) {
match layout.size() {
0 => { /* do nothing */ }
_ => { /* do nothing too */ }
}
}

fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let ptr = self.allocate(layout)?;
if layout.size() > 0 {
unsafe {
ptr.as_mut_ptr().write_bytes(0, layout.size());
}
}
Ok(ptr)
}

unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(
new_layout.size() >= old_layout.size(),
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
);

let new_ptr = self.allocate(new_layout)?;
if new_layout.size() > 0 {
// Safety: `new_ptr` is valid for writes and `ptr` for reads of
// `old_layout.size()`, because `new_layout.size() >=
// old_layout.size()` (which is an invariant that must be upheld by
// callers).
unsafe {
new_ptr.as_mut_ptr().copy_from_nonoverlapping(ptr.as_ptr(), old_layout.size());
}
// Safety: `ptr` is never used again is also an invariant which must
// be upheld by callers.
unsafe {
self.deallocate(ptr, old_layout);
}
}
Ok(new_ptr)
}

unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
// Safety: Invariants of `grow_zeroed` and `grow` are the same, and must
// be enforced by callers.
let new_ptr = unsafe { self.grow(ptr, old_layout, new_layout)? };
if new_layout.size() > 0 {
let old_size = old_layout.size();
let new_size = new_layout.size();
let raw_ptr = new_ptr.as_mut_ptr();
// Safety:
// - `grow` returned Ok, so the returned pointer must be valid for
// `new_size` bytes
// - `new_size` must be larger than `old_size`, which is an
// invariant which must be upheld by callers.
unsafe {
raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
}
}
Ok(new_ptr)
}

unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(
new_layout.size() <= old_layout.size(),
"`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
);

let new_ptr = self.allocate(new_layout)?;
if new_layout.size() > 0 {
// Safety: `new_ptr` and `ptr` are valid for reads/writes of
// `new_layout.size()` because of the invariants of shrink, which
// include `new_layout.size()` being smaller than (or equal to)
// `old_layout.size()`.
unsafe {
new_ptr.as_mut_ptr().copy_from_nonoverlapping(ptr.as_ptr(), new_layout.size());
}
// Safety: `ptr` is never used again is also an invariant which must
// be upheld by callers.
unsafe {
self.deallocate(ptr, old_layout);
}
}
Ok(new_ptr)
}

fn by_ref(&self) -> &Self
where
Self: Sized,
{
self
}
}
1 change: 0 additions & 1 deletion library/core/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ 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
2 changes: 1 addition & 1 deletion tests/ui/associated-type-bounds/union-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

trait Tr1: Copy { type As1: Copy; }
trait Tr2: Copy { type As2: Copy; }
trait Tr3: Copy { type As3: Copy; }
trait Tr3: Copy { #[allow(dead_code)] type As3: Copy; }
trait Tr4<'a>: Copy { type As4: Copy; }
trait Tr5: Copy { type As5: Copy; }

Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-5.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/impl-wf-cycle-5.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
--> $DIR/impl-wf-cycle-5.rs:22:1
--> $DIR/impl-wf-cycle-5.rs:23:1
|
LL | / impl<T> Grault for (T,)
LL | |
Expand All @@ -12,7 +12,7 @@ LL | type A = ();
| ------ associated type `<(T,) as Grault>::A` is specified here
|
note: required for `(T,)` to implement `Grault`
--> $DIR/impl-wf-cycle-5.rs:22:9
--> $DIR/impl-wf-cycle-5.rs:23:9
|
LL | impl<T> Grault for (T,)
| ^^^^^^ ^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-6.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/impl-wf-cycle-6.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
--> $DIR/impl-wf-cycle-6.rs:22:1
--> $DIR/impl-wf-cycle-6.rs:23:1
|
LL | / impl<T: Grault> Grault for (T,)
LL | |
Expand All @@ -11,7 +11,7 @@ LL | type A = ();
| ------ associated type `<(T,) as Grault>::A` is specified here
|
note: required for `(T,)` to implement `Grault`
--> $DIR/impl-wf-cycle-6.rs:22:17
--> $DIR/impl-wf-cycle-6.rs:23:17
|
LL | impl<T: Grault> Grault for (T,)
| ^^^^^^ ^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/cross_crate_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn foo() {
async_in_foo(async_out_foo::<4>().await).await;
}

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

impl<const N: usize> Foo<N> for Faz<N> {}
impl<const N: usize> Bar<N> for Faz<N> {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/cross_crate_complex.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: struct `Faz` is never constructed
--> $DIR/cross_crate_complex.rs:14:8
|
LL | struct Faz<const N: usize>;
| ^^^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

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

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

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/issues/issue-86535-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: struct `Bar` is never constructed
--> $DIR/issue-86535-2.rs:12:8
|
LL | struct Bar<const N: &'static ()>;
| ^^^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

2 changes: 1 addition & 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,7 @@
#![feature(adt_const_params, unsized_const_params, generic_const_exprs)]
#![allow(incomplete_features, unused_variables)]

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

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/issues/issue-86535.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: struct `F` is never constructed
--> $DIR/issue-86535.rs:5:8
|
LL | struct F<const S: &'static str>;
| ^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

2 changes: 1 addition & 1 deletion tests/ui/deriving/deriving-in-macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! define_vec {
() => (
mod foo {
#[derive(PartialEq)]
pub struct bar;
pub struct bar; //~ WARN struct `bar` is never constructed
}
)
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/deriving/deriving-in-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
warning: struct `bar` is never constructed
--> $DIR/deriving-in-macro.rs:9:24
|
LL | pub struct bar;
| ^^^
...
LL | define_vec![];
| ------------- in this macro invocation
|
= note: `#[warn(dead_code)]` on by default
= note: this warning originates in the macro `define_vec` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: 1 warning emitted

1 change: 1 addition & 0 deletions tests/ui/generic-associated-types/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ trait Collection<T> {
type Iter<'iter>: Iterator<Item=&'iter T> where T: 'iter, Self: 'iter;
type Family: CollectionFamily;
// Test associated type defaults with parameters
#[allow(dead_code)]
type Sibling<U>: Collection<U> =
<<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generic-associated-types/missing-bounds.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![allow(dead_code)]

use std::ops::Add;

struct A<B>(B);
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generic-associated-types/missing-bounds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![allow(dead_code)]

use std::ops::Add;

struct A<B>(B);
Expand Down
Loading

0 comments on commit 8507664

Please sign in to comment.