Skip to content

Commit

Permalink
Rollup merge of rust-lang#72380 - lcnr:const_context, r=estebank
Browse files Browse the repository at this point in the history
Fix `is_const_context`, update `check_for_cast`

A better version of rust-lang#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)
  • Loading branch information
Dylan-DPC authored Jun 11, 2020
2 parents 3ddf480 + 6da17d2 commit 298467e
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 245 deletions.
24 changes: 12 additions & 12 deletions src/librustc_middle/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,16 @@ impl<'hir> Map<'hir> {
}
}

pub fn enclosing_body_owner(&self, hir_id: HirId) -> HirId {
for (parent, _) in self.parent_iter(hir_id) {
if let Some(body) = self.maybe_body_owned_by(parent) {
return self.body_owner(body);
}
}

bug!("no `enclosing_body_owner` for hir_id `{}`", hir_id);
}

/// Returns the `HirId` that corresponds to the definition of
/// which this is the body of, i.e., a `fn`, `const` or `static`
/// item (possibly associated), a closure, or a `hir::AnonConst`.
Expand Down Expand Up @@ -537,18 +547,8 @@ impl<'hir> Map<'hir> {

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

/// Whether `hir_id` corresponds to a `mod` or a crate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let closure_id = hir.as_local_hir_id(self.mir_def_id);
let fn_call_id = hir.get_parent_node(closure_id);
let node = hir.get(fn_call_id);
let item_id = hir.get_parent_item(fn_call_id);
let item_id = hir.enclosing_body_owner(fn_call_id);
let mut look_at_return = true;
// If we can detect the expression to be an `fn` call where the closure was an argument,
// we point at the `fn` definition argument...
Expand Down
Loading

0 comments on commit 298467e

Please sign in to comment.