Rollup merge of #148287 - oli-obk:push-osluqnunqkyl, r=fee1-dead

Fix deferred cast checks using the wrong body for determining constness

re-introduces https://github.com/rust-lang/rust/pull/103683

it was lost in 4fec845c3f

deferred checks possibly have more such issues, needs investigation.

r? `@fee1-dead`
This commit is contained in:
Stuart Cook 2025-10-31 22:41:19 +11:00 committed by GitHub
commit 43cb58dbb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 10 deletions

View file

@ -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<CastCheck<'tcx>, 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

View file

@ -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) {

View file

@ -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() {}