Skip to content

Commit fb2f090

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

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7878

7979
debug!("FnCtxt::check_casts: {} deferred checks", deferred_cast_checks.len());
8080
for cast in deferred_cast_checks.drain(..) {
81+
let body_id = std::mem::replace(&mut self.body_id, cast.body_id);
8182
cast.check(self);
83+
self.body_id = body_id;
8284
}
8385

8486
*self.deferred_cast_checks.borrow_mut() = deferred_cast_checks;

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)