Skip to content
Merged
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
2 changes: 0 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4354,7 +4354,6 @@ name = "rustc_mir_transform"
version = "0.0.0"
dependencies = [
"either",
"hashbrown 0.16.1",
"itertools",
"rustc_abi",
"rustc_arena",
Expand Down Expand Up @@ -4565,7 +4564,6 @@ dependencies = [
name = "rustc_query_system"
version = "0.0.0"
dependencies = [
"hashbrown 0.16.1",
"parking_lot",
"rustc_abi",
"rustc_ast",
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub use std::{assert_matches, debug_assert_matches};

pub use atomic_ref::AtomicRef;
pub use ena::{snapshot_vec, undo_log, unify};
// Re-export `hashbrown::hash_table`, because it's part of our API
// (via `ShardedHashMap`), and because it lets other compiler crates use the
// lower-level `HashTable` API without a tricky `hashbrown` dependency.
pub use hashbrown::hash_table;
pub use rustc_index::static_assert_size;
// Re-export some data-structure crates which are part of our public API.
pub use {either, indexmap, smallvec, thin_vec};
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_data_structures/src/sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher};
use std::{iter, mem};

use either::Either;
use hashbrown::hash_table::{Entry, HashTable};
use hashbrown::hash_table::{self, Entry, HashTable};

use crate::fx::FxHasher;
use crate::sync::{CacheAligned, Lock, LockGuard, Mode, is_dyn_thread_safe};
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn shards() -> usize {
1
}

pub type ShardedHashMap<K, V> = Sharded<HashTable<(K, V)>>;
pub type ShardedHashMap<K, V> = Sharded<hash_table::HashTable<(K, V)>>;

impl<K: Eq, V> ShardedHashMap<K, V> {
pub fn with_capacity(cap: usize) -> Self {
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ declare_lint! {
declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);

impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &hir::Expr<'_>) {
match e.kind {
hir::ExprKind::Call(path_expr, [_])
if let hir::ExprKind::Path(qpath) = &path_expr.kind
Expand All @@ -1796,6 +1796,12 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {

for adj in cx.typeck_results().expr_adjustments(e) {
if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(m)) = adj.kind {
if let ty::Ref(_, inner_ty, _) = adj.target.kind()
&& inner_ty.is_box()
{
// If the target type is `&Box<T>` or `&mut Box<T>`, the allocation is necessary
continue;
}
match m {
adjustment::AutoBorrowMutability::Not => {
cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_mir_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2024"
[dependencies]
# tidy-alphabetical-start
either = "1"
hashbrown = { version = "0.16.1", default-features = false }
itertools = "0.12"
rustc_abi = { path = "../rustc_abi" }
rustc_arena = { path = "../rustc_arena" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ use std::borrow::Cow;
use std::hash::{Hash, Hasher};

use either::Either;
use hashbrown::hash_table::{Entry, HashTable};
use itertools::Itertools as _;
use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
use rustc_arena::DroplessArena;
Expand All @@ -99,6 +98,7 @@ use rustc_const_eval::interpret::{
};
use rustc_data_structures::fx::FxHasher;
use rustc_data_structures::graph::dominators::Dominators;
use rustc_data_structures::hash_table::{Entry, HashTable};
use rustc_hir::def::DefKind;
use rustc_index::bit_set::DenseBitSet;
use rustc_index::{IndexVec, newtype_index};
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,12 @@ impl<'a> Parser<'a> {
hi = self.prev_token.span;
let ann = BindingMode(by_ref, mutability);
let fieldpat = self.mk_pat_ident(boxed_span.to(hi), ann, fieldname);
if matches!(
fieldpat.kind,
PatKind::Ident(BindingMode(ByRef::Yes(..), Mutability::Mut), ..)
) {
self.psess.gated_spans.gate(sym::mut_ref, fieldpat.span);
}
let subpat = if is_box {
self.mk_pat(lo.to(hi), PatKind::Box(Box::new(fieldpat)))
} else {
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_query_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,3 @@ rustc_thread_pool = { path = "../rustc_thread_pool" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
tracing = "0.1"
# tidy-alphabetical-end

[dependencies.hashbrown]
version = "0.16.1"
default-features = false
features = ["nightly"] # for may_dangle
5 changes: 2 additions & 3 deletions compiler/rustc_query_system/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use std::fmt::Debug;
use std::hash::Hash;
use std::mem;

use hashbrown::HashTable;
use hashbrown::hash_table::Entry;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::hash_table::{self, Entry, HashTable};
use rustc_data_structures::sharded::{self, Sharded};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::sync::LockGuard;
Expand All @@ -35,7 +34,7 @@ fn equivalent_key<K: Eq, V>(k: &K) -> impl Fn(&(K, V)) -> bool + '_ {
}

pub struct QueryState<'tcx, K> {
active: Sharded<hashbrown::HashTable<(K, QueryResult<'tcx>)>>,
active: Sharded<hash_table::HashTable<(K, QueryResult<'tcx>)>>,
}

/// Indicates the state of a query for a given key in a query map.
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,9 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
/// assert_eq!(format!("{:?}", wrapped), "'a'");
/// ```
#[stable(feature = "fmt_from_fn", since = "1.93.0")]
#[rustc_const_stable(feature = "const_fmt_from_fn", since = "CURRENT_RUSTC_VERSION")]
#[must_use = "returns a type implementing Debug and Display, which do not have any effects unless they are used"]
pub fn from_fn<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result>(f: F) -> FromFn<F> {
pub const fn from_fn<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result>(f: F) -> FromFn<F> {
FromFn(f)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! regression test for <https://github.com/rust-lang/rust/issues/31267>
//@ run-pass
// Regression test for issue #31267


struct Foo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/49824>
fn main() {
let mut x = 0;
|| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: captured variable cannot escape `FnMut` closure body
--> $DIR/issue-49824.rs:4:9
--> $DIR/nested-closure-escape-borrow.rs:5:9
|
LL | let mut x = 0;
| ----- variable defined here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/17651>
// Test that moves of unsized values within closures are caught
// and rejected.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
--> $DIR/issue-17651.rs:5:18
--> $DIR/unsized_value_move.rs:6:18
|
LL | (|| Box::new(*(&[0][..])))();
| -------- ^^^^^^^^^^^ doesn't have a size known at compile-time
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/18767>
//@ run-pass
// Test that regionck uses the right memcat for patterns in for loops
// and doesn't ICE.
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/feature-gates/feature-gate-mut-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ fn main() {
let mut ref x = 10; //~ ERROR [E0658]
#[cfg(false)]
let mut ref mut y = 10; //~ ERROR [E0658]

struct Foo { x: i32 }
let Foo { mut ref x } = Foo { x: 10 }; //~ ERROR [E0658]
}
12 changes: 11 additions & 1 deletion tests/ui/feature-gates/feature-gate-mut-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ LL | let mut ref mut y = 10;
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 4 previous errors
error[E0658]: mutable by-reference bindings are experimental
--> $DIR/feature-gate-mut-ref.rs:15:15
|
LL | let Foo { mut ref x } = Foo { x: 10 };
| ^^^^^^^^^
|
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0658`.
54 changes: 54 additions & 0 deletions tests/ui/lint/unused/unused-allocation-box-ref-issue-151846.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//@ check-pass
// Test for issue #151846: unused_allocation warning should ignore
// allocations to pass Box to things taking self: &Box

#![deny(unused_allocation)]

struct MyStruct;

trait TraitTakesBoxRef {
fn trait_takes_box_ref(&self);
}

impl TraitTakesBoxRef for Box<MyStruct> {
fn trait_takes_box_ref(&self) {}
}

impl MyStruct {
fn inherent_takes_box_ref(self: &Box<Self>) {}
}

fn takes_box_ref(_: &Box<MyStruct>) {}

trait TraitTakesBoxVal {
fn trait_takes_box_val(self);
}

impl TraitTakesBoxVal for Box<MyStruct> {
fn trait_takes_box_val(self) {}
}

impl MyStruct {
fn inherent_takes_box_val(self: Box<Self>) {}
}

fn takes_box_val(_: Box<MyStruct>) {}

pub fn foo() {
// These should NOT warn - the allocation is necessary because
// the method takes &Box<Self>
Box::new(MyStruct).trait_takes_box_ref();
Box::new(MyStruct).inherent_takes_box_ref();
takes_box_ref(&Box::new(MyStruct));

// These already don't warn - the allocation is necessary
Box::new(MyStruct).trait_takes_box_val();
Box::new(MyStruct).inherent_takes_box_val();
takes_box_val(Box::new(MyStruct));

// Fully-qualified syntax also does not warn:
<Box<MyStruct> as TraitTakesBoxRef>::trait_takes_box_ref(&Box::new(MyStruct));
MyStruct::inherent_takes_box_ref(&Box::new(MyStruct));
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/34569>
//@ run-pass
//@ compile-flags:-g

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! regression test for <https://github.com/rust-lang/rust/issues/29740>
//@ check-pass
#![allow(dead_code)]
// Regression test for #29740. Inefficient MIR matching algorithms
// generated way too much code for this sort of case, leading to OOM.
// Inefficient MIR matching algorithms generated way
// too much code for this sort of case, leading to OOM.
#![allow(non_snake_case)]

pub mod KeyboardEventConstants {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/32004>
enum Foo {
Bar(i32),
Baz
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `Foo::Bar`
--> $DIR/issue-32004.rs:10:9
--> $DIR/constructor-type-mismatch.rs:11:9
|
LL | Bar(i32),
| -------- `Foo::Bar` defined here
Expand All @@ -20,7 +20,7 @@ LL + Foo::Baz => {}
|

error[E0532]: expected tuple struct or tuple variant, found unit struct `S`
--> $DIR/issue-32004.rs:16:9
--> $DIR/constructor-type-mismatch.rs:17:9
|
LL | struct S;
| --------- `S` defined here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/3038>
enum F { G(isize, isize) }

enum H { I(J, K) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error[E0416]: identifier `x` is bound more than once in the same pattern
--> $DIR/issue-3038.rs:12:15
--> $DIR/multiple-bindings-on-var.rs:13:15
|
LL | F::G(x, x) => { println!("{}", x + x); }
| ^ used in a pattern more than once

error[E0416]: identifier `x` is bound more than once in the same pattern
--> $DIR/issue-3038.rs:17:32
--> $DIR/multiple-bindings-on-var.rs:18:32
|
LL | H::I(J::L(x, _), K::M(_, x))
| ^ used in a pattern more than once

error[E0416]: identifier `x` is bound more than once in the same pattern
--> $DIR/issue-3038.rs:23:13
--> $DIR/multiple-bindings-on-var.rs:24:13
|
LL | (x, x) => { x }
| ^ used in a pattern more than once
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/2849>
enum Foo { Alpha, Beta(isize) }

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0408]: variable `i` is not bound in all patterns
--> $DIR/issue-2849.rs:5:7
--> $DIR/or-pattern-binding-mismatch.rs:6:7
|
LL | Foo::Alpha | Foo::Beta(i) => {}
| ^^^^^^^^^^ - variable not in all patterns
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! regression test for <https://github.com/rust-lang/rust/issues/2848>
#[allow(non_camel_case_types)]

mod bar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0408]: variable `beta` is not bound in all patterns
--> $DIR/issue-2848.rs:14:7
--> $DIR/or-pattern-mismatched-variable-and-variant.rs:15:7
|
LL | alpha | beta => {}
| ^^^^^ ---- variable not in all patterns
| |
| pattern doesn't bind `beta`

error[E0170]: pattern binding `beta` is named the same as one of the variants of the type `bar::foo`
--> $DIR/issue-2848.rs:14:15
--> $DIR/or-pattern-mismatched-variable-and-variant.rs:15:15
|
LL | alpha | beta => {}
| ^^^^ help: to match on the variant, qualify the path: `bar::foo::beta`
Expand Down
Loading