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 12 pull requests #84473

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
30fc601
Tests for field is never read diagnostic
sunjay Mar 11, 2021
321aace
Added suggestion and note for when a field is never used
sunjay Mar 12, 2021
7faaf39
Updating test stderr files
sunjay Mar 12, 2021
789186d
Trying out a new message that works a little better for values *and* …
sunjay Mar 12, 2021
2acd8eb
New shorter diagnostic note that is different for items versus fields
sunjay Mar 13, 2021
3e34eb8
Putting help message only under the identifier that needs to be prefixed
sunjay Mar 25, 2021
539242a
Add a suggestion when using a type alias instead of trait alias
JohnTitor Mar 31, 2021
eea27b8
Mention trait alias on the E0404 note
JohnTitor Mar 31, 2021
569096c
rustdoc: use details tag for trait implementors
jsha Apr 18, 2021
e85f19b
:arrow_up: rust-analyzer
lnicola Apr 20, 2021
f505d61
Remove duplicated fn(Box<[T]>) -> Vec<T>
calebsander Apr 16, 2021
9cabbd0
Move `sys_common::poison` to `sync::poison`
CDirkx Apr 21, 2021
3794fc9
Clean up .gitignore
CDirkx Apr 20, 2021
c0d05d2
Remove "Version control"
CDirkx Apr 20, 2021
bfbf4ce
doc/platform-support: clarify UEFI support
dvdhrm Apr 22, 2021
a03cba3
Prevent control, shift and alt keys to make search input lose focus
GuillaumeGomez Apr 22, 2021
c247055
Get rid of "[+] show undocumented items" toggle on numeric From impls
notriddle Apr 22, 2021
a07bf2e
Fix ICE if original_span(fn_sig) returns a span not in body sourcefile
richkadel Apr 22, 2021
e1d9b3c
Take ItemType instead of TypeKind in record_extern_fqn
jyn514 Apr 23, 2021
3170536
Use ItemType in cache
jyn514 Apr 23, 2021
2df886d
Remove `TypeKind`
jyn514 Apr 23, 2021
26f2705
Remove unused `impl Clean<ItemType> for DefKind`
jyn514 Apr 23, 2021
a344282
Don't repeat `hir::def::DefKind` so much
jyn514 Apr 23, 2021
6c066ab
Fix macro bug in `impl From<DefKind> for ItemType`
jyn514 Apr 23, 2021
ce3773f
Rollup merge of #83004 - sunjay:field-never-read-issue-81658, r=pnkfelix
GuillaumeGomez Apr 23, 2021
78e3463
Rollup merge of #83729 - JohnTitor:issue-43913, r=estebank
GuillaumeGomez Apr 23, 2021
13bcf03
Rollup merge of #84248 - calebsander:refactor/vec-functions, r=Amanieu
GuillaumeGomez Apr 23, 2021
40bd335
Rollup merge of #84320 - jsha:details-implementors, r=Manishearth,Nem…
GuillaumeGomez Apr 23, 2021
e3b90b0
Rollup merge of #84359 - lnicola:rust-analyzer-2021-04-20, r=jonas-sc…
GuillaumeGomez Apr 23, 2021
71b03dc
Rollup merge of #84374 - CDirkx:gitignore, r=Mark-Simulacrum
GuillaumeGomez Apr 23, 2021
94ef942
Rollup merge of #84387 - CDirkx:poison, r=m-ou-se
GuillaumeGomez Apr 23, 2021
75cd8dd
Rollup merge of #84430 - dvdhrm:rw/uefidoc, r=Amanieu
GuillaumeGomez Apr 23, 2021
79a9723
Rollup merge of #84433 - GuillaumeGomez:search-input-blur, r=jsha
GuillaumeGomez Apr 23, 2021
c00bb72
Rollup merge of #84444 - notriddle:num-docs-from-undocumented-items-t…
GuillaumeGomez Apr 23, 2021
d3b4655
Rollup merge of #84456 - richkadel:issue-84421, r=tmandry
GuillaumeGomez Apr 23, 2021
f75bb0c
Rollup merge of #84464 - jyn514:type-kind, r=CraftSpider
GuillaumeGomez Apr 23, 2021
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
70 changes: 44 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,71 @@
# created during manual debugging and many people like to clean up instead of
# having git ignore such leftovers. You can use `.git/info/exclude` to
# configure your local ignore list.
# FIXME: This needs cleanup.
*~

## File system
.DS_Store
desktop.ini

## Editor
*.swp
*.swo
.#*
.DS_Store
Session.vim
.cproject
.hg/
.hgignore
.idea
*.iml
__pycache__/
*.py[cod]
*$py.class
.vscode
.project
.favorites.json
.settings/

## Tool
.valgrindrc
.vscode
.favorites.json
/Makefile
/build/
.cargo
# Included because it is part of the test case
!/src/test/run-make/thumb-none-qemu/example/.cargo

## Configuration
/config.toml
/dist/
/Makefile
config.mk
config.stamp
no_llvm_build

## Build
/dl/
/doc/
/inst/
/llvm/
/mingw-build/
/src/tools/x/target
# Created by default with `src/ci/docker/run.sh`:
/obj/
/build/
/dist/
/unicode-downloads
/target
# Generated by compiletest for incremental:
/src/tools/x/target
# Generated by compiletest for incremental
/tmp/
# Created by default with `src/ci/docker/run.sh`
/obj/

## Temporary files
*~
\#*
\#*\#
.#*

## Tags
tags
tags.*
TAGS
TAGS.*
\#*
\#*\#
config.mk
config.stamp
Session.vim
.cargo
!/src/test/run-make/thumb-none-qemu/example/.cargo
no_llvm_build

## Python
__pycache__/
*.py[cod]
*$py.class

## Node
**node_modules
**package-lock.json

# Before adding new lines, see the comment at the top.
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!
```
3 changes: 2 additions & 1 deletion compiler/rustc_mir/src/transform/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
let body_span = hir_body.value.span;
let source_file = source_map.lookup_source_file(body_span.lo());
let fn_sig_span = match some_fn_sig.filter(|fn_sig| {
Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.hi()))
fn_sig.span.ctxt() == body_span.ctxt()
&& Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.hi()))
}) {
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),
None => body_span.shrink_to_lo(),
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_mir/src/transform/coverage/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
/// to be).
pub(super) fn generate_coverage_spans(
mir_body: &'a mir::Body<'tcx>,
fn_sig_span: Span,
fn_sig_span: Span, // Ensured to be same SourceFile and SyntaxContext as `body_span`
body_span: Span,
basic_coverage_blocks: &'a CoverageGraph,
) -> Vec<CoverageSpan> {
let mut coverage_spans = CoverageSpans {
mir_body,
fn_sig_span: fn_sig_source_span(fn_sig_span, body_span),
fn_sig_span,
body_span,
basic_coverage_blocks,
sorted_spans_iter: None,
Expand Down Expand Up @@ -731,11 +731,6 @@ pub(super) fn filtered_terminator_span(
}
}

#[inline]
fn fn_sig_source_span(fn_sig_span: Span, body_span: Span) -> Span {
original_sp(fn_sig_span, body_span).with_ctxt(body_span.ctxt())
}

#[inline]
fn function_source_span(span: Span, body_span: Span) -> Span {
let span = original_sp(span, body_span).with_ctxt(body_span.ctxt());
Expand Down
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
3 changes: 1 addition & 2 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2810,8 +2810,7 @@ impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
/// assert_eq!(Vec::from(b), vec![1, 2, 3]);
/// ```
fn from(s: Box<[T], A>) -> Self {
let len = s.len();
Self { buf: RawVec::from_box(s), len }
s.into_vec()
}
}

Expand Down
Loading