Skip to content

Commit

Permalink
Rollup merge of rust-lang#125711 - oli-obk:const_block_ice2, r=Nadrieril
Browse files Browse the repository at this point in the history
Make `body_owned_by` return the `Body` instead of just the `BodyId`

fixes rust-lang#125677

Almost all `body_owned_by` callers immediately called `body`, too, so just return `Body` directly.

This makes the inline-const query feeding more robust, as all calls to `body_owned_by` will now yield a body for inline consts, too.

I have not yet figured out a good way to make `tcx.hir().body()` return an inline-const body, but that can be done as a follow-up
  • Loading branch information
matthiaskrgr committed May 30, 2024
2 parents d38920f + f44a6a7 commit e2e5731
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/default_numeric_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ declare_clippy_lint! {
declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);

impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &Body<'tcx>) {
let hir = cx.tcx.hir();
let is_parent_const = matches!(
hir.body_const_context(hir.body_owner_def_id(body.id())),
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
}
}

fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &Body<'_>) {
if Some(body.id()) == self.current_body {
for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
let replacements = pat.replacements;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/implicit_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<'a, 'b, 'tcx> ImplicitHasherConstructorVisitor<'a, 'b, 'tcx> {
impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 'tcx> {
type NestedFilter = nested_filter::OnlyBodies;

fn visit_body(&mut self, body: &'tcx Body<'_>) {
fn visit_body(&mut self, body: &Body<'tcx>) {
let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body.id()));
walk_body(self, body);
self.maybe_typeck_results = old_maybe_typeck_results;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/macro_metavars_in_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BodyVisitor<'a, 'tcx> {
}

impl<'tcx> LateLintPass<'tcx> for ExprMetavarsInUnsafe {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx rustc_hir::Body<'tcx>) {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &rustc_hir::Body<'tcx>) {
if is_lint_allowed(cx, MACRO_METAVARS_IN_UNSAFE, body.value.hir_id) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/option_map_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub(super) fn check<'tcx>(
};

let map = cx.tcx.hir();
let body = map.body(map.body_owned_by(map.enclosing_body_owner(expr.hir_id)));
reference_visitor.visit_body(body);
let body = map.body_owned_by(map.enclosing_body_owner(expr.hir_id));
reference_visitor.visit_body(&body);

if reference_visitor.found_reference {
return;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/needless_borrows_for_generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
}
}

fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &Body<'_>) {
if self.possible_borrowers.last().map_or(false, |&(local_def_id, _)| {
local_def_id == cx.tcx.hir().body_owner_def_id(body.id())
}) {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/only_used_in_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub struct OnlyUsedInRecursion {
}

impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'tcx>) {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &Body<'tcx>) {
if body.value.span.from_expansion() {
return;
}
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
}
}

fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'tcx>) {
fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &Body<'tcx>) {
if self.entered_body == Some(body.value.hir_id) {
self.entered_body = None;
self.params.flag_for_linting();
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/operators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,11 +868,11 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
self.arithmetic_context.expr_post(e.hir_id);
}

fn check_body(&mut self, cx: &LateContext<'tcx>, b: &'tcx Body<'_>) {
fn check_body(&mut self, cx: &LateContext<'tcx>, b: &Body<'_>) {
self.arithmetic_context.enter_body(cx, b);
}

fn check_body_post(&mut self, cx: &LateContext<'tcx>, b: &'tcx Body<'_>) {
fn check_body_post(&mut self, cx: &LateContext<'tcx>, b: &Body<'_>) {
self.arithmetic_context.body_post(cx, b);
}
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
}
}

fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &Body<'tcx>) {
let hir = cx.tcx.hir();
let mut parents = hir.parent_iter(body.value.hir_id);
let (item_id, sig, is_trait_item) = match parents.next() {
Expand Down Expand Up @@ -525,7 +525,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
})
}

fn check_mut_from_ref<'tcx>(cx: &LateContext<'tcx>, sig: &FnSig<'_>, body: Option<&'tcx Body<'_>>) {
fn check_mut_from_ref<'tcx>(cx: &LateContext<'tcx>, sig: &FnSig<'_>, body: Option<&Body<'tcx>>) {
if let FnRetTy::Return(ty) = sig.decl.output
&& let Some((out, Mutability::Mut, _)) = get_ref_lm(ty)
{
Expand Down Expand Up @@ -559,7 +559,7 @@ fn check_mut_from_ref<'tcx>(cx: &LateContext<'tcx>, sig: &FnSig<'_>, body: Optio
}

#[expect(clippy::too_many_lines)]
fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args: &[PtrArg<'tcx>]) -> Vec<PtrArgResult> {
fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &Body<'tcx>, args: &[PtrArg<'tcx>]) -> Vec<PtrArgResult> {
struct V<'cx, 'tcx> {
cx: &'cx LateContext<'tcx>,
/// Map from a local id to which argument it came from (index into `Self::args` and
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,11 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
}
}

fn check_body(&mut self, _: &LateContext<'tcx>, _: &'tcx Body<'tcx>) {
fn check_body(&mut self, _: &LateContext<'tcx>, _: &Body<'tcx>) {
self.try_block_depth_stack.push(0);
}

fn check_body_post(&mut self, _: &LateContext<'tcx>, _: &'tcx Body<'tcx>) {
fn check_body_post(&mut self, _: &LateContext<'tcx>, _: &Body<'tcx>) {
self.try_block_depth_stack.pop();
}

Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/single_call_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl SingleCallFn {
.tcx
.hir()
.maybe_body_owned_by(fn_def_id)
.map(|body| cx.tcx.hir().body(body))
.map_or(true, |body| is_in_test_function(cx.tcx, body.value.hir_id))
|| match cx.tcx.hir_node(fn_hir_id) {
Node::Item(item) => is_from_proc_macro(cx, item),
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/transmute/missing_transmute_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId)

fn is_function_block(cx: &LateContext<'_>, expr_hir_id: HirId) -> bool {
let def_id = cx.tcx.hir().enclosing_body_owner(expr_hir_id);
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(def_id) {
let body = cx.tcx.hir().body(body_id);
if let Some(body) = cx.tcx.hir().maybe_body_owned_by(def_id) {
let body = cx.tcx.hir().body(body.id());
return body.value.peel_blocks().hir_id == expr_hir_id;
}
false
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/unconditional_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl UnconditionalRecursion {
// We need to use typeck here to infer the actual function being called.
&& let body_def_id = cx.tcx.hir().enclosing_body_owner(call_expr.hir_id)
&& let Some(body_owner) = cx.tcx.hir().maybe_body_owned_by(body_def_id)
&& let typeck = cx.tcx.typeck_body(body_owner)
&& let typeck = cx.tcx.typeck_body(body_owner.id())
&& let Some(call_def_id) = typeck.type_dependent_def_id(call_expr.hir_id)
{
self.default_impl_for_type.insert(self_def_id, call_def_id);
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ impl<'tcx> LateLintPass<'tcx> for Author {

fn check_item(cx: &LateContext<'_>, hir_id: HirId) {
let hir = cx.tcx.hir();
if let Some(body_id) = hir.maybe_body_owned_by(hir_id.expect_owner().def_id) {
if let Some(body) = hir.maybe_body_owned_by(hir_id.expect_owner().def_id) {
check_node(cx, hir_id, |v| {
v.expr(&v.bind("expr", hir.body(body_id).value));
v.expr(&v.bind("expr", body.value));
});
}
}
Expand Down

0 comments on commit e2e5731

Please sign in to comment.