Auto merge of #11130 - smoelius:fix-10535, r=Jarcho

Fix ICE in #10535

Fixes #10535

r? `@Jarcho`

changelog: Eliminate ICE described in #10535
This commit is contained in:
bors 2023-07-09 00:05:19 +00:00
commit 757fe499ae
4 changed files with 40 additions and 11 deletions

View file

@ -1296,8 +1296,8 @@ fn referent_used_exactly_once<'tcx>(
possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>,
reference: &Expr<'tcx>,
) -> bool {
let mir = enclosing_mir(cx.tcx, reference.hir_id);
if let Some(local) = expr_local(cx.tcx, reference)
if let Some(mir) = enclosing_mir(cx.tcx, reference.hir_id)
&& let Some(local) = expr_local(cx.tcx, reference)
&& let [location] = *local_assignments(mir, local).as_slice()
&& let Some(statement) = mir.basic_blocks[location.block].statements.get(location.statement_index)
&& let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind

View file

@ -101,21 +101,26 @@ pub fn used_exactly_once(mir: &rustc_middle::mir::Body<'_>, local: rustc_middle:
/// Returns the `mir::Body` containing the node associated with `hir_id`.
#[allow(clippy::module_name_repetitions)]
pub fn enclosing_mir(tcx: TyCtxt<'_>, hir_id: HirId) -> &Body<'_> {
pub fn enclosing_mir(tcx: TyCtxt<'_>, hir_id: HirId) -> Option<&Body<'_>> {
let body_owner_local_def_id = tcx.hir().enclosing_body_owner(hir_id);
tcx.optimized_mir(body_owner_local_def_id.to_def_id())
if tcx.hir().body_owner_kind(body_owner_local_def_id).is_fn_or_closure() {
Some(tcx.optimized_mir(body_owner_local_def_id.to_def_id()))
} else {
None
}
}
/// Tries to determine the `Local` corresponding to `expr`, if any.
/// This function is expensive and should be used sparingly.
pub fn expr_local(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> Option<Local> {
let mir = enclosing_mir(tcx, expr.hir_id);
mir.local_decls.iter_enumerated().find_map(|(local, local_decl)| {
if local_decl.source_info.span == expr.span {
Some(local)
} else {
None
}
enclosing_mir(tcx, expr.hir_id).and_then(|mir| {
mir.local_decls.iter_enumerated().find_map(|(local, local_decl)| {
if local_decl.source_info.span == expr.span {
Some(local)
} else {
None
}
})
})
}

View file

@ -492,3 +492,15 @@ mod issue_9782_method_variant {
S.foo::<&[u8; 100]>(&a);
}
}
mod issue_10535 {
static SOME_STATIC: String = String::new();
static UNIT: () = compute(&SOME_STATIC);
pub const fn compute<T>(_: T)
where
T: Copy,
{
}
}

View file

@ -492,3 +492,15 @@ mod issue_9782_method_variant {
S.foo::<&[u8; 100]>(&a);
}
}
mod issue_10535 {
static SOME_STATIC: String = String::new();
static UNIT: () = compute(&SOME_STATIC);
pub const fn compute<T>(_: T)
where
T: Copy,
{
}
}