Skip to content

Commit 1447f9d

Browse files
committed
Auto merge of #122869 - matthiaskrgr:rollup-0navj4l, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #121619 (Experimental feature postfix match) - #122370 (Gracefully handle `AnonConst` in `diagnostic_hir_wf_check()`) - #122537 (interpret/allocation: fix aliasing issue in interpreter and refactor getters a bit) - #122542 (coverage: Clean up marker statements that aren't needed later) - #122800 (Add `NonNull::<[T]>::is_empty`.) - #122820 (Stop using `<DefId as Ord>` in various diagnostic situations) - #122847 (Suggest `RUST_MIN_STACK` workaround on overflow) - #122855 (Fix Itanium mangling usizes) - #122863 (add more ice tests ) r? `@ghost` `@rustbot` modify labels: rollup
2 parents eff958c + a5de4fb commit 1447f9d

File tree

73 files changed

+1200
-248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1200
-248
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,7 @@ name = "rustc_middle"
42364236
version = "0.0.0"
42374237
dependencies = [
42384238
"bitflags 2.4.2",
4239+
"derivative",
42394240
"either",
42404241
"field-offset",
42414242
"gsgdt",

Diff for: compiler/rustc_ast/src/ast.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ pub enum ExprKind {
14371437
/// `'label: loop { block }`
14381438
Loop(P<Block>, Option<Label>, Span),
14391439
/// A `match` block.
1440-
Match(P<Expr>, ThinVec<Arm>),
1440+
Match(P<Expr>, ThinVec<Arm>, MatchKind),
14411441
/// A closure (e.g., `move |a, b, c| a + b + c`).
14421442
Closure(Box<Closure>),
14431443
/// A block (`'label: { ... }`).
@@ -1762,6 +1762,15 @@ pub enum StrStyle {
17621762
Raw(u8),
17631763
}
17641764

1765+
/// The kind of match expression
1766+
#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq)]
1767+
pub enum MatchKind {
1768+
/// match expr { ... }
1769+
Prefix,
1770+
/// expr.match { ... }
1771+
Postfix,
1772+
}
1773+
17651774
/// A literal in a meta item.
17661775
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
17671776
pub struct MetaItemLit {

Diff for: compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
14251425
visit_opt(label, |label| vis.visit_label(label));
14261426
vis.visit_span(span);
14271427
}
1428-
ExprKind::Match(expr, arms) => {
1428+
ExprKind::Match(expr, arms, _kind) => {
14291429
vis.visit_expr(expr);
14301430
arms.flat_map_in_place(|arm| vis.flat_map_arm(arm));
14311431
}

Diff for: compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
923923
visit_opt!(visitor, visit_label, opt_label);
924924
try_visit!(visitor.visit_block(block));
925925
}
926-
ExprKind::Match(subexpression, arms) => {
926+
ExprKind::Match(subexpression, arms, _kind) => {
927927
try_visit!(visitor.visit_expr(subexpression));
928928
walk_list!(visitor, visit_arm, arms);
929929
}

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
181181
)
182182
}),
183183
ExprKind::TryBlock(body) => self.lower_expr_try_block(body),
184-
ExprKind::Match(expr, arms) => hir::ExprKind::Match(
184+
ExprKind::Match(expr, arms, kind) => hir::ExprKind::Match(
185185
self.lower_expr(expr),
186186
self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
187-
hir::MatchSource::Normal,
187+
match kind {
188+
MatchKind::Prefix => hir::MatchSource::Normal,
189+
MatchKind::Postfix => hir::MatchSource::Postfix,
190+
},
188191
),
189192
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
190193
ExprKind::Closure(box Closure {

Diff for: compiler/rustc_ast_passes/src/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
564564
gate_all!(generic_const_items, "generic const items are experimental");
565565
gate_all!(unnamed_fields, "unnamed fields are not yet fully implemented");
566566
gate_all!(fn_delegation, "functions delegation is not yet fully implemented");
567+
gate_all!(postfix_match, "postfix match is experimental");
567568

568569
if !visitor.features.never_patterns {
569570
if let Some(spans) = spans.get(&sym::never_patterns) {

Diff for: compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::pp::Breaks::Inconsistent;
22
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
3-
use ast::ForLoopKind;
3+
use ast::{ForLoopKind, MatchKind};
44
use itertools::{Itertools, Position};
55
use rustc_ast::ptr::P;
66
use rustc_ast::token;
@@ -589,12 +589,22 @@ impl<'a> State<'a> {
589589
self.word_nbsp("loop");
590590
self.print_block_with_attrs(blk, attrs);
591591
}
592-
ast::ExprKind::Match(expr, arms) => {
592+
ast::ExprKind::Match(expr, arms, match_kind) => {
593593
self.cbox(0);
594594
self.ibox(0);
595-
self.word_nbsp("match");
596-
self.print_expr_as_cond(expr);
597-
self.space();
595+
596+
match match_kind {
597+
MatchKind::Prefix => {
598+
self.word_nbsp("match");
599+
self.print_expr_as_cond(expr);
600+
self.space();
601+
}
602+
MatchKind::Postfix => {
603+
self.print_expr_as_cond(expr);
604+
self.word_nbsp(".match");
605+
}
606+
}
607+
598608
self.bopen();
599609
self.print_inner_attributes_no_trailing_hardbreak(attrs);
600610
for arm in arms {

Diff for: compiler/rustc_builtin_macros/src/assert/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
245245
ExprKind::Let(_, local_expr, _, _) => {
246246
self.manage_cond_expr(local_expr);
247247
}
248-
ExprKind::Match(local_expr, _) => {
248+
ExprKind::Match(local_expr, ..) => {
249249
self.manage_cond_expr(local_expr);
250250
}
251251
ExprKind::MethodCall(call) => {

Diff for: compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn cs_partial_cmp(
132132
// Reference: https://github.com/rust-lang/rust/pull/103659#issuecomment-1328126354
133133

134134
if !tag_then_data
135-
&& let ExprKind::Match(_, arms) = &mut expr1.kind
135+
&& let ExprKind::Match(_, arms, _) = &mut expr1.kind
136136
&& let Some(last) = arms.last_mut()
137137
&& let PatKind::Wild = last.pat.kind
138138
{

Diff for: compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::path_std;
44

5-
use ast::EnumDef;
6-
use rustc_ast::{self as ast, MetaItem};
5+
use rustc_ast::{self as ast, EnumDef, MetaItem};
76
use rustc_expand::base::{Annotatable, ExtCtxt};
87
use rustc_span::symbol::{sym, Ident, Symbol};
98
use rustc_span::Span;

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
8585

8686
let bx = self;
8787

88-
match coverage.kind {
89-
// Marker statements have no effect during codegen,
90-
// so return early and don't create `func_coverage`.
91-
CoverageKind::SpanMarker | CoverageKind::BlockMarker { .. } => return,
92-
// Match exhaustively to ensure that newly-added kinds are classified correctly.
93-
CoverageKind::CounterIncrement { .. } | CoverageKind::ExpressionUsed { .. } => {}
94-
}
95-
9688
let Some(function_coverage_info) =
9789
bx.tcx.instance_mir(instance.def).function_coverage_info.as_deref()
9890
else {
@@ -109,7 +101,7 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
109101
let Coverage { kind } = coverage;
110102
match *kind {
111103
CoverageKind::SpanMarker | CoverageKind::BlockMarker { .. } => unreachable!(
112-
"unexpected marker statement {kind:?} should have caused an early return"
104+
"marker statement {kind:?} should have been removed by CleanupPostBorrowck"
113105
),
114106
CoverageKind::CounterIncrement { id } => {
115107
func_coverage.mark_counter_id_seen(id);

Diff for: compiler/rustc_const_eval/src/interpret/memory.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1159,11 +1159,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11591159
};
11601160

11611161
// Side-step AllocRef and directly access the underlying bytes more efficiently.
1162-
// (We are staying inside the bounds here so all is good.)
1162+
// (We are staying inside the bounds here and all bytes do get overwritten so all is good.)
11631163
let alloc_id = alloc_ref.alloc_id;
11641164
let bytes = alloc_ref
11651165
.alloc
1166-
.get_bytes_mut(&alloc_ref.tcx, alloc_ref.range)
1166+
.get_bytes_unchecked_for_overwrite(&alloc_ref.tcx, alloc_ref.range)
11671167
.map_err(move |e| e.to_interp_error(alloc_id))?;
11681168
// `zip` would stop when the first iterator ends; we want to definitely
11691169
// cover all of `bytes`.
@@ -1184,6 +1184,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11841184
self.mem_copy_repeatedly(src, dest, size, 1, nonoverlapping)
11851185
}
11861186

1187+
/// Performs `num_copies` many copies of `size` many bytes from `src` to `dest + i*size` (where
1188+
/// `i` is the index of the copy).
1189+
///
1190+
/// Either `nonoverlapping` must be true or `num_copies` must be 1; doing repeated copies that
1191+
/// may overlap is not supported.
11871192
pub fn mem_copy_repeatedly(
11881193
&mut self,
11891194
src: Pointer<Option<M::Provenance>>,
@@ -1245,8 +1250,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12451250
(dest_alloc_id, dest_prov),
12461251
dest_range,
12471252
)?;
1253+
// Yes we do overwrite all bytes in `dest_bytes`.
12481254
let dest_bytes = dest_alloc
1249-
.get_bytes_mut_ptr(&tcx, dest_range)
1255+
.get_bytes_unchecked_for_overwrite_ptr(&tcx, dest_range)
12501256
.map_err(|e| e.to_interp_error(dest_alloc_id))?
12511257
.as_mut_ptr();
12521258

@@ -1280,6 +1286,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12801286
}
12811287
}
12821288
}
1289+
if num_copies > 1 {
1290+
assert!(nonoverlapping, "multi-copy only supported in non-overlapping mode");
1291+
}
12831292

12841293
let size_in_bytes = size.bytes_usize();
12851294
// For particularly large arrays (where this is perf-sensitive) it's common that
@@ -1292,6 +1301,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
12921301
} else if src_alloc_id == dest_alloc_id {
12931302
let mut dest_ptr = dest_bytes;
12941303
for _ in 0..num_copies {
1304+
// Here we rely on `src` and `dest` being non-overlapping if there is more than
1305+
// one copy.
12951306
ptr::copy(src_bytes, dest_ptr, size_in_bytes);
12961307
dest_ptr = dest_ptr.add(size_in_bytes);
12971308
}

Diff for: compiler/rustc_const_eval/src/transform/validate.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_index::bit_set::BitSet;
55
use rustc_index::IndexVec;
66
use rustc_infer::traits::Reveal;
7+
use rustc_middle::mir::coverage::CoverageKind;
78
use rustc_middle::mir::interpret::Scalar;
89
use rustc_middle::mir::visit::{NonUseContext, PlaceContext, Visitor};
910
use rustc_middle::mir::*;
@@ -345,11 +346,21 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
345346
self.fail(location, format!("explicit `{kind:?}` is forbidden"));
346347
}
347348
}
349+
StatementKind::Coverage(coverage) => {
350+
let kind = &coverage.kind;
351+
if self.mir_phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup)
352+
&& let CoverageKind::BlockMarker { .. } | CoverageKind::SpanMarker { .. } = kind
353+
{
354+
self.fail(
355+
location,
356+
format!("{kind:?} should have been removed after analysis"),
357+
);
358+
}
359+
}
348360
StatementKind::Assign(..)
349361
| StatementKind::StorageLive(_)
350362
| StatementKind::StorageDead(_)
351363
| StatementKind::Intrinsic(_)
352-
| StatementKind::Coverage(_)
353364
| StatementKind::ConstEvalCounter
354365
| StatementKind::PlaceMention(..)
355366
| StatementKind::Nop => {}

Diff for: compiler/rustc_driver_impl/src/signal_handler.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Signal handler for rustc
22
//! Primarily used to extract a backtrace from stack overflow
33
4+
use rustc_interface::util::{DEFAULT_STACK_SIZE, STACK_SIZE};
45
use std::alloc::{alloc, Layout};
56
use std::{fmt, mem, ptr};
67

@@ -100,7 +101,10 @@ extern "C" fn print_stack_trace(_: libc::c_int) {
100101
written += 1;
101102
}
102103
raw_errln!("note: we would appreciate a report at https://github.com/rust-lang/rust");
103-
written += 1;
104+
// get the current stack size WITHOUT blocking and double it
105+
let new_size = STACK_SIZE.get().copied().unwrap_or(DEFAULT_STACK_SIZE) * 2;
106+
raw_errln!("help: you can increase rustc's stack size by setting RUST_MIN_STACK={new_size}");
107+
written += 2;
104108
if written > 24 {
105109
// We probably just scrolled the earlier "we got SIGSEGV" message off the terminal
106110
raw_errln!("note: backtrace dumped due to SIGSEGV! resuming signal");

Diff for: compiler/rustc_expand/src/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::base::ExtCtxt;
22
use rustc_ast::ptr::P;
3-
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp};
3+
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, MatchKind, PatKind, UnOp};
44
use rustc_ast::{attr, token, util::literal};
55
use rustc_span::source_map::Spanned;
66
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -524,7 +524,7 @@ impl<'a> ExtCtxt<'a> {
524524
}
525525

526526
pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: ThinVec<ast::Arm>) -> P<Expr> {
527-
self.expr(span, ast::ExprKind::Match(arg, arms))
527+
self.expr(span, ast::ExprKind::Match(arg, arms, MatchKind::Prefix))
528528
}
529529

530530
pub fn expr_if(

Diff for: compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ declare_features! (
559559
(unstable, offset_of_nested, "1.77.0", Some(120140)),
560560
/// Allows using `#[optimize(X)]`.
561561
(unstable, optimize_attribute, "1.34.0", Some(54882)),
562+
/// Allows postfix match `expr.match { ... }`
563+
(unstable, postfix_match, "CURRENT_RUSTC_VERSION", Some(121618)),
562564
/// Allows macro attributes on expressions, statements and non-inline modules.
563565
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
564566
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.

Diff for: compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,8 @@ pub enum LocalSource {
20152015
pub enum MatchSource {
20162016
/// A `match _ { .. }`.
20172017
Normal,
2018+
/// A `expr.match { .. }`.
2019+
Postfix,
20182020
/// A desugared `for _ in _ { .. }` loop.
20192021
ForLoopDesugar,
20202022
/// A desugared `?` operator.
@@ -2031,6 +2033,7 @@ impl MatchSource {
20312033
use MatchSource::*;
20322034
match self {
20332035
Normal => "match",
2036+
Postfix => ".match",
20342037
ForLoopDesugar => "for",
20352038
TryDesugar(_) => "?",
20362039
AwaitDesugar => ".await",

Diff for: compiler/rustc_hir_analysis/src/hir_wf_check.rs

+10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ fn diagnostic_hir_wf_check<'tcx>(
163163
kind: hir::GenericParamKind::Type { default: Some(ty), .. },
164164
..
165165
}) => vec![*ty],
166+
hir::Node::AnonConst(_)
167+
if let Some(const_param_id) =
168+
tcx.hir().opt_const_param_default_param_def_id(hir_id)
169+
&& let hir::Node::GenericParam(hir::GenericParam {
170+
kind: hir::GenericParamKind::Const { ty, .. },
171+
..
172+
}) = tcx.hir_node_by_def_id(const_param_id) =>
173+
{
174+
vec![*ty]
175+
}
166176
ref node => bug!("Unexpected node {:?}", node),
167177
},
168178
WellFormedLoc::Param { function: _, param_idx } => {

Diff for: compiler/rustc_interface/src/util.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ pub fn add_configuration(cfg: &mut Cfg, sess: &mut Session, codegen_backend: &dy
4848
}
4949
}
5050

51-
const STACK_SIZE: usize = 8 * 1024 * 1024;
52-
53-
fn get_stack_size() -> Option<usize> {
54-
// FIXME: Hacks on hacks. If the env is trying to override the stack size
55-
// then *don't* set it explicitly.
56-
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
51+
pub static STACK_SIZE: OnceLock<usize> = OnceLock::new();
52+
pub const DEFAULT_STACK_SIZE: usize = 8 * 1024 * 1024;
53+
54+
fn init_stack_size() -> usize {
55+
// Obey the environment setting or default
56+
*STACK_SIZE.get_or_init(|| {
57+
env::var_os("RUST_MIN_STACK")
58+
.map(|os_str| os_str.to_string_lossy().into_owned())
59+
// ignore if it is set to nothing
60+
.filter(|s| s.trim() != "")
61+
.map(|s| s.trim().parse::<usize>().unwrap())
62+
// otherwise pick a consistent default
63+
.unwrap_or(DEFAULT_STACK_SIZE)
64+
})
5765
}
5866

5967
pub(crate) fn run_in_thread_with_globals<F: FnOnce() -> R + Send, R: Send>(
@@ -66,10 +74,7 @@ pub(crate) fn run_in_thread_with_globals<F: FnOnce() -> R + Send, R: Send>(
6674
// the parallel compiler, in particular to ensure there is no accidental
6775
// sharing of data between the main thread and the compilation thread
6876
// (which might cause problems for the parallel compiler).
69-
let mut builder = thread::Builder::new().name("rustc".to_string());
70-
if let Some(size) = get_stack_size() {
71-
builder = builder.stack_size(size);
72-
}
77+
let builder = thread::Builder::new().name("rustc".to_string()).stack_size(init_stack_size());
7378

7479
// We build the session globals and run `f` on the spawned thread, because
7580
// `SessionGlobals` does not impl `Send` in the non-parallel compiler.
@@ -120,7 +125,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
120125
});
121126
}
122127

123-
let mut builder = rayon::ThreadPoolBuilder::new()
128+
let builder = rayon::ThreadPoolBuilder::new()
124129
.thread_name(|_| "rustc".to_string())
125130
.acquire_thread_handler(jobserver::acquire_thread)
126131
.release_thread_handler(jobserver::release_thread)
@@ -144,10 +149,8 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
144149
on_panic.disable();
145150
})
146151
.unwrap();
147-
});
148-
if let Some(size) = get_stack_size() {
149-
builder = builder.stack_size(size);
150-
}
152+
})
153+
.stack_size(init_stack_size());
151154

152155
// We create the session globals on the main thread, then create the thread
153156
// pool. Upon creation, each worker thread created gets a copy of the

0 commit comments

Comments
 (0)