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 11 pull requests #84454

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5b9905b
Added CharIndices::offset function
TrolledWoods Feb 27, 2021
772543a
Removed trailing whitespace
TrolledWoods Feb 27, 2021
907eab8
Added feature flag to doc test
TrolledWoods Feb 27, 2021
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
53a1105
On stable, suggest removing `#![feature]` for features that have been…
jyn514 Mar 31, 2021
fa1624c
Added tracking issue number
TrolledWoods Apr 5, 2021
37a5b51
implement TrustedRandomAccess for Take iterator adapter
the8472 Apr 8, 2021
03900e4
move core::hint::black_box under its own feature gate
RalfJung Apr 15, 2021
4873271
bootstrap: use bash on illumos to run install scripts
jclulow Apr 16, 2021
e85f19b
:arrow_up: rust-analyzer
lnicola Apr 20, 2021
f505d61
Remove duplicated fn(Box<[T]>) -> Vec<T>
calebsander Apr 16, 2021
62226ee
Improve BinaryHeap::retain.
m-ou-se Nov 2, 2020
f5d72ab
Add better test for BinaryHeap::retain.
m-ou-se Nov 2, 2020
fc2a74c
RustWrapper: work around unification of diagnostic handlers
durin42 Mar 23, 2021
d6e5bba
Rollup merge of #78681 - m-ou-se:binary-heap-retain, r=Amanieu
Dylan-DPC Apr 22, 2021
9f7b36a
Rollup merge of #82585 - TrolledWoods:master, r=dtolnay
Dylan-DPC Apr 22, 2021
8903387
Rollup merge of #83004 - sunjay:field-never-read-issue-81658, r=pnkfelix
Dylan-DPC Apr 22, 2021
a714418
Rollup merge of #83425 - durin42:llvm-update, r=nagisa
Dylan-DPC Apr 22, 2021
9c15d9b
Rollup merge of #83722 - jyn514:stable-help, r=estebank
Dylan-DPC Apr 22, 2021
b6dbf13
Rollup merge of #83729 - JohnTitor:issue-43913, r=estebank
Dylan-DPC Apr 22, 2021
151c0ed
Rollup merge of #83990 - the8472:take-trusted-len, r=dtolnay
Dylan-DPC Apr 22, 2021
4413950
Rollup merge of #84216 - RalfJung:black-box, r=Mark-Simulacrum
Dylan-DPC Apr 22, 2021
46b7644
Rollup merge of #84248 - calebsander:refactor/vec-functions, r=Amanieu
Dylan-DPC Apr 22, 2021
8c7b58c
Rollup merge of #84250 - jclulow:illumos-bash-bootstrap, r=Mark-Simul…
Dylan-DPC Apr 22, 2021
030f4a2
Rollup merge of #84359 - lnicola:rust-analyzer-2021-04-20, r=jonas-sc…
Dylan-DPC Apr 22, 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
36 changes: 33 additions & 3 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,16 +727,46 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
}

fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
use rustc_errors::Applicability;

if !sess.opts.unstable_features.is_nightly_build() {
let lang_features = &sess.features_untracked().declared_lang_features;
for attr in krate.attrs.iter().filter(|attr| sess.check_name(attr, sym::feature)) {
struct_span_err!(
let mut err = struct_span_err!(
sess.parse_sess.span_diagnostic,
attr.span,
E0554,
"`#![feature]` may not be used on the {} release channel",
option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
)
.emit();
);
let mut all_stable = true;
for ident in
attr.meta_item_list().into_iter().flatten().map(|nested| nested.ident()).flatten()
{
let name = ident.name;
let stable_since = lang_features
.iter()
.flat_map(|&(feature, _, since)| if feature == name { since } else { None })
.next();
if let Some(since) = stable_since {
err.help(&format!(
"the feature `{}` has been stable since {} and no longer requires \
an attribute to enable",
name, since
));
} else {
all_stable = false;
}
}
if all_stable {
err.span_suggestion(
attr.span,
"remove the attribute",
String::new(),
Applicability::MachineApplicable,
);
}
err.emit();
}
}
}
Expand Down
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!
```
1 change: 1 addition & 0 deletions compiler/rustc_index/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(allow_internal_unstable)]
#![feature(bench_black_box)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(extend_one)]
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,9 +1300,19 @@ extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {

DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SMDiagnostic, LLVMSMDiagnosticRef)

#if LLVM_VERSION_LT(13, 0)
using LLVMInlineAsmDiagHandlerTy = LLVMContext::InlineAsmDiagHandlerTy;
#else
using LLVMInlineAsmDiagHandlerTy = void*;
#endif

extern "C" void LLVMRustSetInlineAsmDiagnosticHandler(
LLVMContextRef C, LLVMContext::InlineAsmDiagHandlerTy H, void *CX) {
LLVMContextRef C, LLVMInlineAsmDiagHandlerTy H, void *CX) {
// Diagnostic handlers were unified in LLVM change 5de2d189e6ad, so starting
// with LLVM 13 this function is gone.
#if LLVM_VERSION_LT(13, 0)
unwrap(C)->setInlineAsmDiagnosticHandler(H, CX);
#endif
}

extern "C" bool LLVMRustUnpackSMDiagnostic(LLVMSMDiagnosticRef DRef,
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
85 changes: 53 additions & 32 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,43 @@ impl<T: Ord> BinaryHeap<T> {
unsafe { self.sift_up(start, pos) };
}

/// Rebuild assuming data[0..start] is still a proper heap.
fn rebuild_tail(&mut self, start: usize) {
if start == self.len() {
return;
}

let tail_len = self.len() - start;

#[inline(always)]
fn log2_fast(x: usize) -> usize {
(usize::BITS - x.leading_zeros() - 1) as usize
}

// `rebuild` takes O(self.len()) operations
// and about 2 * self.len() comparisons in the worst case
// while repeating `sift_up` takes O(tail_len * log(start)) operations
// and about 1 * tail_len * log_2(start) comparisons in the worst case,
// assuming start >= tail_len. For larger heaps, the crossover point
// no longer follows this reasoning and was determined empirically.
let better_to_rebuild = if start < tail_len {
true
} else if self.len() <= 2048 {
2 * self.len() < tail_len * log2_fast(start)
} else {
2 * self.len() < tail_len * 11
};

if better_to_rebuild {
self.rebuild();
} else {
for i in start..self.len() {
// SAFETY: The index `i` is always less than self.len().
unsafe { self.sift_up(0, i) };
}
}
}

fn rebuild(&mut self) {
let mut n = self.len() / 2;
while n > 0 {
Expand Down Expand Up @@ -689,37 +726,11 @@ impl<T: Ord> BinaryHeap<T> {
swap(self, other);
}

if other.is_empty() {
return;
}

#[inline(always)]
fn log2_fast(x: usize) -> usize {
(usize::BITS - x.leading_zeros() - 1) as usize
}
let start = self.data.len();

// `rebuild` takes O(len1 + len2) operations
// and about 2 * (len1 + len2) comparisons in the worst case
// while `extend` takes O(len2 * log(len1)) operations
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
// assuming len1 >= len2. For larger heaps, the crossover point
// no longer follows this reasoning and was determined empirically.
#[inline]
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
let tot_len = len1 + len2;
if tot_len <= 2048 {
2 * tot_len < len2 * log2_fast(len1)
} else {
2 * tot_len < len2 * 11
}
}
self.data.append(&mut other.data);

if better_to_rebuild(self.len(), other.len()) {
self.data.append(&mut other.data);
self.rebuild();
} else {
self.extend(other.drain());
}
self.rebuild_tail(start);
}

/// Returns an iterator which retrieves elements in heap order.
Expand Down Expand Up @@ -770,12 +781,22 @@ impl<T: Ord> BinaryHeap<T> {
/// assert_eq!(heap.into_sorted_vec(), [-10, 2, 4])
/// ```
#[unstable(feature = "binary_heap_retain", issue = "71503")]
pub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
{
self.data.retain(f);
self.rebuild();
let mut first_removed = self.len();
let mut i = 0;
self.data.retain(|e| {
let keep = f(e);
if !keep && i < first_removed {
first_removed = i;
}
i += 1;
keep
});
// data[0..first_removed] is untouched, so we only need to rebuild the tail:
self.rebuild_tail(first_removed);
}
}

Expand Down
Loading