diff options
| author | Michael Goulet <michael@errs.io> | 2024-11-19 17:18:54 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2024-11-22 16:54:41 +0000 |
| commit | af0d566e76a709caa2a89123202f583cbcc877e2 (patch) | |
| tree | 423a4b388450803052248d342deebf74eeab3d5f /compiler/rustc_const_eval/src/check_consts/check.rs | |
| parent | 20882608529a969bd878ad787cf0038716c021df (diff) | |
| download | rust-af0d566e76a709caa2a89123202f583cbcc877e2.tar.gz rust-af0d566e76a709caa2a89123202f583cbcc877e2.zip | |
Deduplicate checking drop terminator
Diffstat (limited to 'compiler/rustc_const_eval/src/check_consts/check.rs')
| -rw-r--r-- | compiler/rustc_const_eval/src/check_consts/check.rs | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/compiler/rustc_const_eval/src/check_consts/check.rs b/compiler/rustc_const_eval/src/check_consts/check.rs index a088a210e26..9f21c864487 100644 --- a/compiler/rustc_const_eval/src/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/check_consts/check.rs @@ -324,14 +324,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> { } } - /// Emits an error at the given `span` if an expression cannot be evaluated in the current - /// context. This is meant for use in a post-const-checker pass such as the const precise - /// live drops lint. - pub fn check_op_spanned_post<O: NonConstOp<'tcx>>(mut self, op: O, span: Span) { - self.check_op_spanned(op, span); - assert!(self.secondary_errors.is_empty()); - } - fn check_static(&mut self, def_id: DefId, span: Span) { if self.tcx.is_thread_local_static(def_id) { self.tcx.dcx().span_bug(span, "tls access is checked in `Rvalue::ThreadLocalRef`"); @@ -429,6 +421,43 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> { true } + + pub fn check_drop_terminator( + &mut self, + dropped_place: Place<'tcx>, + location: Location, + terminator_span: Span, + ) { + let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty; + + let needs_drop = if let Some(local) = dropped_place.as_local() { + self.qualifs.needs_drop(self.ccx, local, location) + } else { + qualifs::NeedsDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place) + }; + // If this type doesn't need a drop at all, then there's nothing to enforce. + if !needs_drop { + return; + } + + let mut err_span = self.span; + let needs_non_const_drop = if let Some(local) = dropped_place.as_local() { + // Use the span where the local was declared as the span of the drop error. + err_span = self.body.local_decls[local].source_info.span; + self.qualifs.needs_non_const_drop(self.ccx, local, location) + } else { + qualifs::NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place) + }; + + self.check_op_spanned( + ops::LiveDrop { + dropped_at: terminator_span, + dropped_ty: ty_of_dropped_place, + needs_non_const_drop, + }, + err_span, + ); + } } impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { @@ -874,35 +903,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { return; } - let mut err_span = self.span; - let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty; - - let needs_drop = if let Some(local) = dropped_place.as_local() { - self.qualifs.needs_drop(self.ccx, local, location) - } else { - qualifs::NeedsDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place) - }; - // If this type doesn't need a drop at all, then there's nothing to enforce. - if !needs_drop { - return; - } - - let needs_non_const_drop = if let Some(local) = dropped_place.as_local() { - // Use the span where the local was declared as the span of the drop error. - err_span = self.body.local_decls[local].source_info.span; - self.qualifs.needs_non_const_drop(self.ccx, local, location) - } else { - qualifs::NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place) - }; - - self.check_op_spanned( - ops::LiveDrop { - dropped_at: Some(terminator.source_info.span), - dropped_ty: ty_of_dropped_place, - needs_non_const_drop, - }, - err_span, - ); + self.check_drop_terminator(*dropped_place, location, terminator.source_info.span); } TerminatorKind::InlineAsm { .. } => self.check_op(ops::InlineAsm), |
