Skip to content

Commit 298467e

Browse files
authored
Rollup merge of #72380 - lcnr:const_context, r=estebank
Fix `is_const_context`, update `check_for_cast` A better version of #71477 Adds `fn enclosing_body_owner` and uses it in `is_const_context`. `is_const_context` now uses the same mechanism as `mir_const_qualif` as it was previously incorrect. Renames `is_const_context` to `is_inside_const_context`. I also updated `check_for_cast` in the second commit, so r? @estebank (I removed one lvl of indentation, so it might be easier to review by hiding whitespace changes)
2 parents 3ddf480 + 6da17d2 commit 298467e

File tree

9 files changed

+228
-245
lines changed

9 files changed

+228
-245
lines changed

src/librustc_middle/hir/map/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ impl<'hir> Map<'hir> {
335335
}
336336
}
337337

338+
pub fn enclosing_body_owner(&self, hir_id: HirId) -> HirId {
339+
for (parent, _) in self.parent_iter(hir_id) {
340+
if let Some(body) = self.maybe_body_owned_by(parent) {
341+
return self.body_owner(body);
342+
}
343+
}
344+
345+
bug!("no `enclosing_body_owner` for hir_id `{}`", hir_id);
346+
}
347+
338348
/// Returns the `HirId` that corresponds to the definition of
339349
/// which this is the body of, i.e., a `fn`, `const` or `static`
340350
/// item (possibly associated), a closure, or a `hir::AnonConst`.
@@ -537,18 +547,8 @@ impl<'hir> Map<'hir> {
537547

538548
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
539549
/// Used exclusively for diagnostics, to avoid suggestion function calls.
540-
pub fn is_const_context(&self, hir_id: HirId) -> bool {
541-
let parent_id = self.get_parent_item(hir_id);
542-
match self.get(parent_id) {
543-
Node::Item(&Item { kind: ItemKind::Const(..) | ItemKind::Static(..), .. })
544-
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })
545-
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })
546-
| Node::AnonConst(_) => true,
547-
Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. }) => {
548-
sig.header.constness == Constness::Const
549-
}
550-
_ => false,
551-
}
550+
pub fn is_inside_const_context(&self, hir_id: HirId) -> bool {
551+
self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some()
552552
}
553553

554554
/// Whether `hir_id` corresponds to a `mod` or a crate.

src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
495495
let closure_id = hir.as_local_hir_id(self.mir_def_id);
496496
let fn_call_id = hir.get_parent_node(closure_id);
497497
let node = hir.get(fn_call_id);
498-
let item_id = hir.get_parent_item(fn_call_id);
498+
let item_id = hir.enclosing_body_owner(fn_call_id);
499499
let mut look_at_return = true;
500500
// If we can detect the expression to be an `fn` call where the closure was an argument,
501501
// we point at the `fn` definition argument...

0 commit comments

Comments
 (0)