Skip to content

Commit 8762219

Browse files
committed
Fix deferred cast checks using the wrong body for determining constness
1 parent 5538a06 commit 8762219

File tree

4 files changed

+9
-20
lines changed

4 files changed

+9
-20
lines changed

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_ast::util::parser::ExprPrecedence;
3232
use rustc_data_structures::fx::FxHashSet;
3333
use rustc_errors::codes::*;
3434
use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
35-
use rustc_hir::def_id::DefId;
35+
use rustc_hir::def_id::{DefId, LocalDefId};
3636
use rustc_hir::{self as hir, ExprKind};
3737
use rustc_infer::infer::DefineOpaqueTypes;
3838
use rustc_macros::{TypeFoldable, TypeVisitable};
@@ -63,6 +63,7 @@ pub(crate) struct CastCheck<'tcx> {
6363
cast_ty: Ty<'tcx>,
6464
cast_span: Span,
6565
span: Span,
66+
pub body_id: LocalDefId,
6667
}
6768

6869
/// The kind of pointer and associated metadata (thin, length or vtable) - we
@@ -244,7 +245,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
244245
span: Span,
245246
) -> Result<CastCheck<'tcx>, ErrorGuaranteed> {
246247
let expr_span = expr.span.find_ancestor_inside(span).unwrap_or(expr.span);
247-
let check = CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span };
248+
let check =
249+
CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span, body_id: fcx.body_id };
248250

249251
// For better error messages, check for some obviously unsized
250252
// cases now. We do a more thorough check at the end, once

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::ops::Deref;
2-
use std::{fmt, iter, mem};
2+
use std::{fmt, iter};
33

44
use itertools::Itertools;
55
use rustc_data_structures::fx::FxIndexSet;
@@ -72,16 +72,13 @@ pub(crate) enum DivergingBlockBehavior {
7272

7373
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7474
pub(in super::super) fn check_casts(&mut self) {
75-
// don't hold the borrow to deferred_cast_checks while checking to avoid borrow checker errors
76-
// when writing to `self.param_env`.
77-
let mut deferred_cast_checks = mem::take(&mut *self.deferred_cast_checks.borrow_mut());
78-
75+
let mut deferred_cast_checks = self.root_ctxt.deferred_cast_checks.borrow_mut();
7976
debug!("FnCtxt::check_casts: {} deferred checks", deferred_cast_checks.len());
8077
for cast in deferred_cast_checks.drain(..) {
78+
let body_id = std::mem::replace(&mut self.body_id, cast.body_id);
8179
cast.check(self);
80+
self.body_id = body_id;
8281
}
83-
84-
*self.deferred_cast_checks.borrow_mut() = deferred_cast_checks;
8582
}
8683

8784
pub(in super::super) fn check_asms(&self) {

tests/ui/traits/const-traits/issue-103677.cnst.stderr

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
//@[stock] check-pass
1+
//@ check-pass
22
//@ revisions: stock cnst
33
#![cfg_attr(cnst, feature(const_trait_impl))]
44

55
const _: fn(&String) = |s| {
66
&*s as &str;
7-
//[cnst]~^ ERROR: the trait bound `String: const Deref` is not satisfied
87
};
98

109
fn main() {}

0 commit comments

Comments
 (0)