Skip to content

Commit 746589b

Browse files
committed
Prereq2 for async drop - ResumedAfterDrop panic messages
1 parent 7097c0c commit 746589b

File tree

12 files changed

+132
-48
lines changed

12 files changed

+132
-48
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
502502
RemainderByZero(op) => RemainderByZero(eval_to_int(op)?),
503503
ResumedAfterReturn(coroutine_kind) => ResumedAfterReturn(*coroutine_kind),
504504
ResumedAfterPanic(coroutine_kind) => ResumedAfterPanic(*coroutine_kind),
505+
ResumedAfterDrop(coroutine_kind) => ResumedAfterDrop(*coroutine_kind),
505506
MisalignedPointerDereference { ref required, ref found } => {
506507
MisalignedPointerDereference {
507508
required: eval_to_int(required)?,

compiler/rustc_hir/src/lang_items.rs

+4
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ language_item_table! {
317317
PanicAsyncGenFnResumedPanic, sym::panic_const_async_gen_fn_resumed_panic, panic_const_async_gen_fn_resumed_panic, Target::Fn, GenericRequirement::None;
318318
PanicGenFnNonePanic, sym::panic_const_gen_fn_none_panic, panic_const_gen_fn_none_panic, Target::Fn, GenericRequirement::None;
319319
PanicNullPointerDereference, sym::panic_null_pointer_dereference, panic_null_pointer_dereference, Target::Fn, GenericRequirement::None;
320+
PanicCoroutineResumedDrop, sym::panic_const_coroutine_resumed_drop, panic_const_coroutine_resumed_drop, Target::Fn, GenericRequirement::None;
321+
PanicAsyncFnResumedDrop, sym::panic_const_async_fn_resumed_drop, panic_const_async_fn_resumed_drop, Target::Fn, GenericRequirement::None;
322+
PanicAsyncGenFnResumedDrop, sym::panic_const_async_gen_fn_resumed_drop, panic_const_async_gen_fn_resumed_drop, Target::Fn, GenericRequirement::None;
323+
PanicGenFnNoneDrop, sym::panic_const_gen_fn_none_drop, panic_const_gen_fn_none_drop, Target::Fn, GenericRequirement::None;
320324
/// libstd panic entry point. Necessary for const eval to be able to catch it
321325
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
322326

compiler/rustc_middle/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
middle_adjust_for_foreign_abi_error =
22
target architecture {$arch} does not support `extern {$abi}` ABI
33
4+
middle_assert_async_resume_after_drop = `async fn` resumed after async drop
5+
46
middle_assert_async_resume_after_panic = `async fn` resumed after panicking
57
68
middle_assert_async_resume_after_return = `async fn` resumed after completion
79
10+
middle_assert_coroutine_resume_after_drop = coroutine resumed after async drop
11+
812
middle_assert_coroutine_resume_after_panic = coroutine resumed after panicking
913
1014
middle_assert_coroutine_resume_after_return = coroutine resumed after completion
1115
1216
middle_assert_divide_by_zero =
1317
attempt to divide `{$val}` by zero
1418
19+
middle_assert_gen_resume_after_drop = `gen` fn or block cannot be further iterated on after it async dropped
20+
1521
middle_assert_gen_resume_after_panic = `gen` fn or block cannot be further iterated on after it panicked
1622
1723
middle_assert_misaligned_ptr_deref =

compiler/rustc_middle/src/mir/syntax.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ pub enum AssertKind<O> {
11041104
RemainderByZero(O),
11051105
ResumedAfterReturn(CoroutineKind),
11061106
ResumedAfterPanic(CoroutineKind),
1107+
ResumedAfterDrop(CoroutineKind),
11071108
MisalignedPointerDereference { required: O, found: O },
11081109
NullPointerDereference,
11091110
}

compiler/rustc_middle/src/mir/terminator.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ impl<O> AssertKind<O> {
207207
LangItem::PanicGenFnNonePanic
208208
}
209209
NullPointerDereference => LangItem::PanicNullPointerDereference,
210+
ResumedAfterDrop(CoroutineKind::Coroutine(_)) => LangItem::PanicCoroutineResumedDrop,
211+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
212+
LangItem::PanicAsyncFnResumedDrop
213+
}
214+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
215+
LangItem::PanicAsyncGenFnResumedDrop
216+
}
217+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
218+
LangItem::PanicGenFnNoneDrop
219+
}
210220

211221
BoundsCheck { .. } | MisalignedPointerDereference { .. } => {
212222
bug!("Unexpected AssertKind")
@@ -297,6 +307,18 @@ impl<O> AssertKind<O> {
297307
ResumedAfterPanic(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
298308
write!(f, "\"`gen fn` should just keep returning `None` after panicking\"")
299309
}
310+
ResumedAfterDrop(CoroutineKind::Coroutine(_)) => {
311+
write!(f, "\"coroutine resumed after async drop\"")
312+
}
313+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
314+
write!(f, "\"`async fn` resumed after async drop\"")
315+
}
316+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
317+
write!(f, "\"`async gen fn` resumed after async drop\"")
318+
}
319+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
320+
write!(f, "\"`gen fn` resumed after drop\"")
321+
}
300322
}
301323
}
302324

@@ -344,6 +366,19 @@ impl<O> AssertKind<O> {
344366
middle_assert_coroutine_resume_after_panic
345367
}
346368
NullPointerDereference => middle_assert_null_ptr_deref,
369+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
370+
middle_assert_async_resume_after_drop
371+
}
372+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _)) => {
373+
todo!()
374+
}
375+
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Gen, _)) => {
376+
middle_assert_gen_resume_after_drop
377+
}
378+
ResumedAfterDrop(CoroutineKind::Coroutine(_)) => {
379+
middle_assert_coroutine_resume_after_drop
380+
}
381+
347382
MisalignedPointerDereference { .. } => middle_assert_misaligned_ptr_deref,
348383
}
349384
}
@@ -376,7 +411,10 @@ impl<O> AssertKind<O> {
376411
add!("left", format!("{left:#?}"));
377412
add!("right", format!("{right:#?}"));
378413
}
379-
ResumedAfterReturn(_) | ResumedAfterPanic(_) | NullPointerDereference => {}
414+
ResumedAfterReturn(_)
415+
| ResumedAfterPanic(_)
416+
| NullPointerDereference
417+
| ResumedAfterDrop(_) => {}
380418
MisalignedPointerDereference { required, found } => {
381419
add!("required", format!("{required:#?}"));
382420
add!("found", format!("{found:#?}"));

compiler/rustc_middle/src/mir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ macro_rules! make_mir_visitor {
640640
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
641641
self.visit_operand(op, location);
642642
}
643-
ResumedAfterReturn(_) | ResumedAfterPanic(_) | NullPointerDereference => {
643+
ResumedAfterReturn(_) | ResumedAfterPanic(_) | NullPointerDereference | ResumedAfterDrop(_) => {
644644
// Nothing to visit
645645
}
646646
MisalignedPointerDereference { required, found } => {

compiler/rustc_smir/src/rustc_smir/convert/mir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ impl<'tcx> Stable<'tcx> for mir::AssertMessage<'tcx> {
493493
AssertKind::ResumedAfterPanic(coroutine) => {
494494
stable_mir::mir::AssertMessage::ResumedAfterPanic(coroutine.stable(tables))
495495
}
496+
AssertKind::ResumedAfterDrop(coroutine) => {
497+
stable_mir::mir::AssertMessage::ResumedAfterDrop(coroutine.stable(tables))
498+
}
496499
AssertKind::MisalignedPointerDereference { required, found } => {
497500
stable_mir::mir::AssertMessage::MisalignedPointerDereference {
498501
required: required.stable(tables),

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1464,14 +1464,18 @@ symbols! {
14641464
panic_cannot_unwind,
14651465
panic_const_add_overflow,
14661466
panic_const_async_fn_resumed,
1467+
panic_const_async_fn_resumed_drop,
14671468
panic_const_async_fn_resumed_panic,
14681469
panic_const_async_gen_fn_resumed,
1470+
panic_const_async_gen_fn_resumed_drop,
14691471
panic_const_async_gen_fn_resumed_panic,
14701472
panic_const_coroutine_resumed,
1473+
panic_const_coroutine_resumed_drop,
14711474
panic_const_coroutine_resumed_panic,
14721475
panic_const_div_by_zero,
14731476
panic_const_div_overflow,
14741477
panic_const_gen_fn_none,
1478+
panic_const_gen_fn_none_drop,
14751479
panic_const_gen_fn_none_panic,
14761480
panic_const_mul_overflow,
14771481
panic_const_neg_overflow,

compiler/stable_mir/src/mir/body.rs

+17
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ pub enum AssertMessage {
250250
RemainderByZero(Operand),
251251
ResumedAfterReturn(CoroutineKind),
252252
ResumedAfterPanic(CoroutineKind),
253+
ResumedAfterDrop(CoroutineKind),
253254
MisalignedPointerDereference { required: Operand, found: Operand },
254255
NullPointerDereference,
255256
}
@@ -303,6 +304,22 @@ impl AssertMessage {
303304
_,
304305
)) => Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking"),
305306

307+
AssertMessage::ResumedAfterDrop(CoroutineKind::Coroutine(_)) => {
308+
Ok("coroutine resumed after async drop")
309+
}
310+
AssertMessage::ResumedAfterDrop(CoroutineKind::Desugared(
311+
CoroutineDesugaring::Async,
312+
_,
313+
)) => Ok("`async fn` resumed after async drop"),
314+
AssertMessage::ResumedAfterDrop(CoroutineKind::Desugared(
315+
CoroutineDesugaring::Gen,
316+
_,
317+
)) => Ok("`async gen fn` resumed after async drop"),
318+
AssertMessage::ResumedAfterDrop(CoroutineKind::Desugared(
319+
CoroutineDesugaring::AsyncGen,
320+
_,
321+
)) => Ok("`gen fn` should just keep returning `AssertMessage::None` after async drop"),
322+
306323
AssertMessage::BoundsCheck { .. } => Ok("index out of bounds"),
307324
AssertMessage::MisalignedPointerDereference { .. } => {
308325
Ok("misaligned pointer dereference")

compiler/stable_mir/src/mir/pretty.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ fn pretty_assert_message<W: Write>(writer: &mut W, msg: &AssertMessage) -> io::R
301301
AssertMessage::NullPointerDereference => {
302302
write!(writer, "\"null pointer dereference occurred\"")
303303
}
304-
AssertMessage::ResumedAfterReturn(_) | AssertMessage::ResumedAfterPanic(_) => {
304+
AssertMessage::ResumedAfterReturn(_)
305+
| AssertMessage::ResumedAfterPanic(_)
306+
| AssertMessage::ResumedAfterDrop(_) => {
305307
write!(writer, "{}", msg.description().unwrap())
306308
}
307309
}

compiler/stable_mir/src/mir/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ pub trait MirVisitor {
428428
}
429429
AssertMessage::ResumedAfterReturn(_)
430430
| AssertMessage::ResumedAfterPanic(_)
431-
| AssertMessage::NullPointerDereference => {
432-
//nothing to visit
431+
| AssertMessage::NullPointerDereference
432+
| AssertMessage::ResumedAfterDrop(_) => { //nothing to visit
433433
}
434434
AssertMessage::MisalignedPointerDereference { required, found } => {
435435
self.visit_operand(required, location);

library/core/src/panicking.rs

+51-43
Original file line numberDiff line numberDiff line change
@@ -155,56 +155,64 @@ pub const fn panic(expr: &'static str) -> ! {
155155
// reducing binary size impact.
156156
macro_rules! panic_const {
157157
($($lang:ident = $message:expr,)+) => {
158-
pub mod panic_const {
159-
use super::*;
160-
161-
$(
162-
/// This is a panic called with a message that's a result of a MIR-produced Assert.
163-
//
164-
// never inline unless panic_immediate_abort to avoid code
165-
// bloat at the call sites as much as possible
166-
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
167-
#[cfg_attr(feature = "panic_immediate_abort", inline)]
168-
#[track_caller]
169-
#[rustc_const_stable_indirect] // must follow stable const rules since it is exposed to stable
170-
#[lang = stringify!($lang)]
171-
pub const fn $lang() -> ! {
172-
// Use Arguments::new_const instead of format_args!("{expr}") to potentially
173-
// reduce size overhead. The format_args! macro uses str's Display trait to
174-
// write expr, which calls Formatter::pad, which must accommodate string
175-
// truncation and padding (even though none is used here). Using
176-
// Arguments::new_const may allow the compiler to omit Formatter::pad from the
177-
// output binary, saving up to a few kilobytes.
178-
panic_fmt(fmt::Arguments::new_const(&[$message]));
179-
}
180-
)+
181-
}
158+
$(
159+
/// This is a panic called with a message that's a result of a MIR-produced Assert.
160+
//
161+
// never inline unless panic_immediate_abort to avoid code
162+
// bloat at the call sites as much as possible
163+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
164+
#[cfg_attr(feature = "panic_immediate_abort", inline)]
165+
#[track_caller]
166+
#[rustc_const_stable_indirect] // must follow stable const rules since it is exposed to stable
167+
#[lang = stringify!($lang)]
168+
pub const fn $lang() -> ! {
169+
// Use Arguments::new_const instead of format_args!("{expr}") to potentially
170+
// reduce size overhead. The format_args! macro uses str's Display trait to
171+
// write expr, which calls Formatter::pad, which must accommodate string
172+
// truncation and padding (even though none is used here). Using
173+
// Arguments::new_const may allow the compiler to omit Formatter::pad from the
174+
// output binary, saving up to a few kilobytes.
175+
panic_fmt(fmt::Arguments::new_const(&[$message]));
176+
}
177+
)+
182178
}
183179
}
184180

185181
// Unfortunately this set of strings is replicated here and in a few places in the compiler in
186182
// slightly different forms. It's not clear if there's a good way to deduplicate without adding
187183
// special cases to the compiler (e.g., a const generic function wouldn't have a single definition
188184
// shared across crates, which is exactly what we want here).
189-
panic_const! {
190-
panic_const_add_overflow = "attempt to add with overflow",
191-
panic_const_sub_overflow = "attempt to subtract with overflow",
192-
panic_const_mul_overflow = "attempt to multiply with overflow",
193-
panic_const_div_overflow = "attempt to divide with overflow",
194-
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
195-
panic_const_neg_overflow = "attempt to negate with overflow",
196-
panic_const_shr_overflow = "attempt to shift right with overflow",
197-
panic_const_shl_overflow = "attempt to shift left with overflow",
198-
panic_const_div_by_zero = "attempt to divide by zero",
199-
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
200-
panic_const_coroutine_resumed = "coroutine resumed after completion",
201-
panic_const_async_fn_resumed = "`async fn` resumed after completion",
202-
panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
203-
panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
204-
panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
205-
panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
206-
panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
207-
panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
185+
pub mod panic_const {
186+
use super::*;
187+
panic_const! {
188+
panic_const_add_overflow = "attempt to add with overflow",
189+
panic_const_sub_overflow = "attempt to subtract with overflow",
190+
panic_const_mul_overflow = "attempt to multiply with overflow",
191+
panic_const_div_overflow = "attempt to divide with overflow",
192+
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
193+
panic_const_neg_overflow = "attempt to negate with overflow",
194+
panic_const_shr_overflow = "attempt to shift right with overflow",
195+
panic_const_shl_overflow = "attempt to shift left with overflow",
196+
panic_const_div_by_zero = "attempt to divide by zero",
197+
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
198+
panic_const_coroutine_resumed = "coroutine resumed after completion",
199+
panic_const_async_fn_resumed = "`async fn` resumed after completion",
200+
panic_const_async_gen_fn_resumed = "`async gen fn` resumed after completion",
201+
panic_const_gen_fn_none = "`gen fn` should just keep returning `None` after completion",
202+
panic_const_coroutine_resumed_panic = "coroutine resumed after panicking",
203+
panic_const_async_fn_resumed_panic = "`async fn` resumed after panicking",
204+
panic_const_async_gen_fn_resumed_panic = "`async gen fn` resumed after panicking",
205+
panic_const_gen_fn_none_panic = "`gen fn` should just keep returning `None` after panicking",
206+
}
207+
// Separated panic constants list for async drop feature
208+
// (May be joined when the corresponding lang items will be in the bootstrap)
209+
#[cfg(not(bootstrap))]
210+
panic_const! {
211+
panic_const_coroutine_resumed_drop = "coroutine resumed after async drop",
212+
panic_const_async_fn_resumed_drop = "`async fn` resumed after async drop",
213+
panic_const_async_gen_fn_resumed_drop = "`async gen fn` resumed after async drop",
214+
panic_const_gen_fn_none_drop = "`gen fn` resumed after async drop",
215+
}
208216
}
209217

210218
/// Like `panic`, but without unwinding and track_caller to reduce the impact on codesize on the caller.

0 commit comments

Comments
 (0)