Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 4 pull requests #84370

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions compiler/rustc_error_codes/src/error_codes/E0404.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ struct Foo;
struct Bar;

impl Foo for Bar {} // error: `Foo` is not a trait
fn baz<T: Foo>(t: T) {} // error: `Foo` is not a trait
```

Another erroneous code example:

```compile_fail,E0404
struct Foo;
type Foo = Iterator<Item=String>;

fn bar<T: Foo>(t: T) {} // error: `Foo` is not a trait
fn bar<T: Foo>(t: T) {} // error: `Foo` is a type alias
```

Please verify that the trait's name was not misspelled or that the right
Expand All @@ -30,14 +31,27 @@ struct Bar;
impl Foo for Bar { // ok!
// functions implementation
}

fn baz<T: Foo>(t: T) {} // ok!
```

or:
Alternatively, you could introduce a new trait with your desired restrictions
as a super trait:

```
trait Foo {
// some functions
}
# trait Foo {}
# struct Bar;
# impl Foo for Bar {}
trait Qux: Foo {} // Anything that implements Qux also needs to implement Foo
fn baz<T: Qux>(t: T) {} // also ok!
```

Finally, if you are on nightly and want to use a trait alias
instead of a type alias, you should use `#![feature(trait_alias)]`:

```
#![feature(trait_alias)]
trait Foo = Iterator<Item=String>;

fn bar<T: Foo>(t: T) {} // ok!
```
61 changes: 52 additions & 9 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// from live codes are live, and everything else is dead.

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
Expand All @@ -15,7 +16,7 @@ use rustc_middle::middle::privacy;
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_session::lint;

use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{sym, Ident, Symbol};

// Any local node that may call something in its body block should be
// explored. For example, if it's a live Node::Item that is a
Expand Down Expand Up @@ -505,6 +506,13 @@ fn find_live<'tcx>(
symbol_visitor.live_symbols
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ExtraNote {
/// Use this to provide some examples in the diagnostic of potential other purposes for a value
/// or field that is dead code
OtherPurposeExamples,
}

struct DeadVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
live_symbols: FxHashSet<hir::HirId>,
Expand Down Expand Up @@ -570,14 +578,42 @@ impl DeadVisitor<'tcx> {
&mut self,
id: hir::HirId,
span: rustc_span::Span,
name: Symbol,
name: Ident,
participle: &str,
extra_note: Option<ExtraNote>,
) {
if !name.as_str().starts_with('_') {
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
let def_id = self.tcx.hir().local_def_id(id);
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
lint.build(&format!("{} is never {}: `{}`", descr, participle, name)).emit()

let prefixed = vec![(name.span, format!("_{}", name))];

let mut diag =
lint.build(&format!("{} is never {}: `{}`", descr, participle, name));

diag.multipart_suggestion(
"if this is intentional, prefix it with an underscore",
prefixed,
Applicability::MachineApplicable,
);

let mut note = format!(
"the leading underscore signals that this {} serves some other \
purpose even if it isn't used in a way that we can detect.",
descr,
);
if matches!(extra_note, Some(ExtraNote::OtherPurposeExamples)) {
note += " (e.g. for its effect when dropped or in foreign code)";
}

diag.note(&note);

// Force the note we added to the front, before any other subdiagnostics
// added in lint.build(...)
diag.children.rotate_right(1);

diag.emit()
});
}
}
Expand Down Expand Up @@ -623,7 +659,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
_ => "used",
};
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle);
self.warn_dead_code(item.hir_id(), span, item.ident, participle, None);
} else {
// Only continue if we didn't warn
intravisit::walk_item(self, item);
Expand All @@ -637,22 +673,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
id: hir::HirId,
) {
if self.should_warn_about_variant(&variant) {
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
self.warn_dead_code(variant.id, variant.span, variant.ident, "constructed", None);
} else {
intravisit::walk_variant(self, variant, g, id);
}
}

fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
if self.should_warn_about_foreign_item(fi) {
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used");
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident, "used", None);
}
intravisit::walk_foreign_item(self, fi);
}

fn visit_field_def(&mut self, field: &'tcx hir::FieldDef<'tcx>) {
if self.should_warn_about_field(&field) {
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
self.warn_dead_code(
field.hir_id,
field.span,
field.ident,
"read",
Some(ExtraNote::OtherPurposeExamples),
);
}
intravisit::walk_field_def(self, field);
}
Expand All @@ -664,8 +706,9 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
self.warn_dead_code(
impl_item.hir_id(),
impl_item.span,
impl_item.ident.name,
impl_item.ident,
"used",
None,
);
}
self.visit_nested_body(body_id)
Expand All @@ -683,7 +726,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
} else {
impl_item.ident.span
};
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident.name, "used");
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident, "used", None);
}
self.visit_nested_body(body_id)
}
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
`type` alias";
if let Some(span) = self.def_span(def_id) {
err.span_help(span, msg);
if let Ok(snip) = self.r.session.source_map().span_to_snippet(span) {
// The span contains a type alias so we should be able to
// replace `type` with `trait`.
let snip = snip.replacen("type", "trait", 1);
err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
} else {
err.span_help(span, msg);
}
} else {
err.help(msg);
}
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,21 @@ impl_stable_hash_via_hash!(OptLevel);

/// This is what the `LtoCli` values get mapped to after resolving defaults and
/// and taking other command line options into account.
///
/// Note that linker plugin-based LTO is a different mechanism entirely.
#[derive(Clone, PartialEq)]
pub enum Lto {
/// Don't do any LTO whatsoever
/// Don't do any LTO whatsoever.
No,

/// Do a full crate graph LTO with ThinLTO
/// Do a full-crate-graph (inter-crate) LTO with ThinLTO.
Thin,

/// Do a local graph LTO with ThinLTO (only relevant for multiple codegen
/// units).
/// Do a local ThinLTO (intra-crate, over the CodeGen Units of the local crate only). This is
/// only relevant if multiple CGUs are used.
ThinLocal,

/// Do a full crate graph LTO with "fat" LTO
/// Do a full-crate-graph (inter-crate) LTO with "fat" LTO.
Fat,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ error: associated constant is never used: `BAR`
--> $DIR/associated-const-dead-code.rs:6:5
|
LL | const BAR: u32 = 1;
| ^^^^^^^^^^^^^^^^^^^
| ^^^^^^---^^^^^^^^^^
| |
| help: if this is intentional, prefix it with an underscore: `_BAR`
|
= note: the leading underscore signals that this associated constant serves some other purpose even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/associated-const-dead-code.rs:1:9
|
Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/codemap_tests/two_files.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ LL | impl Bar for Baz { }
| ^^^ type aliases cannot be used as traits
|
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
--> $DIR/two_files_data.rs:5:1
|
LL | type Bar = dyn Foo;
| ^^^^^^^^^^^^^^^^^^^
LL | trait Bar = dyn Foo;
|

error: aborting due to previous error

Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/derive-uninhabited-enum-38885.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ warning: variant is never constructed: `Void`
--> $DIR/derive-uninhabited-enum-38885.rs:13:5
|
LL | Void(Void),
| ^^^^^^^^^^
| ----^^^^^^
| |
| help: if this is intentional, prefix it with an underscore: `_Void`
|
= note: the leading underscore signals that this variant serves some other purpose even if it isn't used in a way that we can detect.
= note: `-W dead-code` implied by `-W unused`

warning: 1 warning emitted
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/issues/issue-37515.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ warning: type alias is never used: `Z`
--> $DIR/issue-37515.rs:5:1
|
LL | type Z = dyn for<'x> Send;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^-^^^^^^^^^^^^^^^^^^^^
| |
| help: if this is intentional, prefix it with an underscore: `_Z`
|
= note: the leading underscore signals that this type alias serves some other purpose even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/issue-37515.rs:3:9
|
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/lint/dead-code/basic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ error: function is never used: `foo`
--> $DIR/basic.rs:4:4
|
LL | fn foo() {
| ^^^
| ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
|
= note: the leading underscore signals that this function serves some other purpose even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/basic.rs:1:9
|
Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/lint/dead-code/const-and-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ warning: variant is never constructed: `B`
--> $DIR/const-and-self.rs:33:5
|
LL | B,
| ^
| ^ help: if this is intentional, prefix it with an underscore: `_B`
|
= note: the leading underscore signals that this variant serves some other purpose even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/const-and-self.rs:3:9
|
Expand All @@ -14,7 +15,9 @@ warning: variant is never constructed: `C`
--> $DIR/const-and-self.rs:34:5
|
LL | C,
| ^
| ^ help: if this is intentional, prefix it with an underscore: `_C`
|
= note: the leading underscore signals that this variant serves some other purpose even if it isn't used in a way that we can detect.

warning: 2 warnings emitted

42 changes: 42 additions & 0 deletions src/test/ui/lint/dead-code/drop-only-field-issue-81658.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! The field `guard` is never used directly, but it is still useful for its side effect when
//! dropped. Since rustc doesn't consider a `Drop` impl as a use, we want to make sure we at least
//! produce a helpful diagnostic that points the user to what they can do if they indeed intended to
//! have a field that is only used for its `Drop` side effect.
//!
//! Issue: https://github.com/rust-lang/rust/issues/81658

#![deny(dead_code)]

use std::sync::{Mutex, MutexGuard};

/// Holds a locked value until it is dropped
pub struct Locked<'a, T> {
// Field is kept for its affect when dropped, but otherwise unused
guard: MutexGuard<'a, T>, //~ ERROR field is never read
}

impl<'a, T> Locked<'a, T> {
pub fn new(value: &'a Mutex<T>) -> Self {
Self {
guard: value.lock().unwrap(),
}
}
}

fn main() {
let items = Mutex::new(vec![1, 2, 3]);

// Hold a lock on items while doing something else
let result = {
// The lock will be released at the end of this scope
let _lock = Locked::new(&items);

do_something_else()
};

println!("{}", result);
}

fn do_something_else() -> i32 {
1 + 1
}
17 changes: 17 additions & 0 deletions src/test/ui/lint/dead-code/drop-only-field-issue-81658.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: field is never read: `guard`
--> $DIR/drop-only-field-issue-81658.rs:15:5
|
LL | guard: MutexGuard<'a, T>,
| -----^^^^^^^^^^^^^^^^^^^
| |
| help: if this is intentional, prefix it with an underscore: `_guard`
|
= note: the leading underscore signals that this field serves some other purpose even if it isn't used in a way that we can detect. (e.g. for its effect when dropped or in foreign code)
note: the lint level is defined here
--> $DIR/drop-only-field-issue-81658.rs:8:9
|
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: aborting due to previous error

3 changes: 2 additions & 1 deletion src/test/ui/lint/dead-code/empty-unused-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ error: enum is never used: `E`
--> $DIR/empty-unused-enum.rs:3:6
|
LL | enum E {}
| ^
| ^ help: if this is intentional, prefix it with an underscore: `_E`
|
= note: the leading underscore signals that this enum serves some other purpose even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/empty-unused-enum.rs:1:9
|
Expand Down
Loading