Skip to content
Open
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
22 changes: 21 additions & 1 deletion compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> {
}

fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
if self.needs_non_const_drop {
let mut err = if self.needs_non_const_drop {
ccx.dcx().create_err(errors::LiveDrop {
span,
dropped_ty: self.dropped_ty,
Expand All @@ -608,7 +608,27 @@ impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> {
},
sym::const_destruct,
)
};

// If the dropped type is a type parameter, suggest adding a `[const] Destruct` bound.
if let Param(param_ty) = self.dropped_ty.kind() {
let tcx = ccx.tcx;
let caller = ccx.def_id();
if let Some(generics) = tcx.hir_node_by_def_id(caller).generics() {
let destruct_def_id = tcx.lang_items().destruct_trait();
suggest_constraining_type_param(
tcx,
generics,
&mut err,
param_ty.name.as_str(),
"[const] Destruct",
destruct_def_id,
None,
);
}
}

err
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/consts/min_const_fn/min_const_fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ LL | const fn no_apit(_x: impl std::fmt::Debug) {}
| ^^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
|
help: consider restricting opaque type `impl std::fmt::Debug` with unstable trait `Destruct`
|
LL | const fn no_apit(_x: impl std::fmt::Debug + [const] Destruct) {}
| ++++++++++++++++++

error: aborting due to 9 previous errors

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/consts/unstable-const-fn-in-libcore.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | const fn unwrap_or_else<F: [const] FnOnce() -> T>(self, f: F) -> T {
...
LL | }
| - value is dropped here
|
help: consider further restricting type parameter `F` with unstable trait `Destruct`
|
LL | const fn unwrap_or_else<F: [const] FnOnce() -> T + [const] Destruct>(self, f: F) -> T {
| ++++++++++++++++++

error[E0493]: destructor of `Opt<T>` cannot be evaluated at compile-time
--> $DIR/unstable-const-fn-in-libcore.rs:19:55
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/static/static-drop-scope.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ LL | const fn const_drop<T>(_: T) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
|
help: consider restricting type parameter `T` with unstable trait `Destruct`
|
LL | const fn const_drop<T: [const] Destruct>(_: T) {}
| ++++++++++++++++++

error[E0493]: destructor of `(T, ())` cannot be evaluated at compile-time
--> $DIR/static-drop-scope.rs:17:5
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/traits/const-traits/minicore-drop-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ LL | const fn drop_arbitrary<T>(_: T) {
LL |
LL | }
| - value is dropped here
|
help: consider restricting type parameter `T` with trait `Destruct`
|
LL | const fn drop_arbitrary<T: [const] Destruct>(_: T) {
| ++++++++++++++++++

error: aborting due to 4 previous errors

Expand Down
Loading