diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 40b21c45bc564..3f13a102684e0 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -32,7 +32,7 @@ use rustc_ast::util::parser::ExprPrecedence; use rustc_data_structures::fx::FxHashSet; use rustc_errors::codes::*; use rustc_errors::{Applicability, Diag, ErrorGuaranteed}; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{self as hir, ExprKind}; use rustc_infer::infer::DefineOpaqueTypes; use rustc_macros::{TypeFoldable, TypeVisitable}; @@ -63,6 +63,7 @@ pub(crate) struct CastCheck<'tcx> { cast_ty: Ty<'tcx>, cast_span: Span, span: Span, + pub body_id: LocalDefId, } /// The kind of pointer and associated metadata (thin, length or vtable) - we @@ -244,7 +245,8 @@ impl<'a, 'tcx> CastCheck<'tcx> { span: Span, ) -> Result, ErrorGuaranteed> { let expr_span = expr.span.find_ancestor_inside(span).unwrap_or(expr.span); - let check = CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span }; + let check = + CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span, body_id: fcx.body_id }; // For better error messages, check for some obviously unsized // cases now. We do a more thorough check at the end, once diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 4d1c7be391977..7b75d23ed9580 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -1,5 +1,5 @@ use std::ops::Deref; -use std::{fmt, iter, mem}; +use std::{fmt, iter}; use itertools::Itertools; use rustc_data_structures::fx::FxIndexSet; @@ -72,16 +72,13 @@ pub(crate) enum DivergingBlockBehavior { impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub(in super::super) fn check_casts(&mut self) { - // don't hold the borrow to deferred_cast_checks while checking to avoid borrow checker errors - // when writing to `self.param_env`. - let mut deferred_cast_checks = mem::take(&mut *self.deferred_cast_checks.borrow_mut()); - + let mut deferred_cast_checks = self.root_ctxt.deferred_cast_checks.borrow_mut(); debug!("FnCtxt::check_casts: {} deferred checks", deferred_cast_checks.len()); for cast in deferred_cast_checks.drain(..) { + let body_id = std::mem::replace(&mut self.body_id, cast.body_id); cast.check(self); + self.body_id = body_id; } - - *self.deferred_cast_checks.borrow_mut() = deferred_cast_checks; } pub(in super::super) fn check_asms(&self) { diff --git a/tests/ui/traits/const-traits/issue-103677.rs b/tests/ui/traits/const-traits/issue-103677.rs index c032cc7a68803..8117e393753fd 100644 --- a/tests/ui/traits/const-traits/issue-103677.rs +++ b/tests/ui/traits/const-traits/issue-103677.rs @@ -1,5 +1,9 @@ //@ check-pass +//@ revisions: stock cnst +#![cfg_attr(cnst, feature(const_trait_impl))] -const _: fn(&String) = |s| { &*s as &str; }; +const _: fn(&String) = |s| { + &*s as &str; +}; fn main() {}