Skip to content

Move hir::Params from Body of functions to FnDecl - #163758

Draft
JonathanBrouwer wants to merge 16 commits into
rust-lang:mainfrom
JonathanBrouwer:param
Draft

JonathanBrouwer wants to merge 16 commits into
rust-lang:mainfrom
JonathanBrouwer:param

Conversation

@JonathanBrouwer

@JonathanBrouwer JonathanBrouwer commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

View all comments

This PR implements MCP rust-lang/compiler-team#1044.
It is split up into commits, each commit passes tests, and reviewing commit by commit is therefore heavily recommended. Each commit contains a description. The first commit makes the big change, and the other commits are cleanup.

This PR causes a minor performance regression, specifically on the libc and externs benchmark:
#163758 (comment)
This is caused by that parameters on foreign functions are now assigned HIR IDs and Params are allocated for them, so slightly more work is needed.
Both of these crates are basically only foreign functions, so not very representative of an average rust program.

r? @Kivooeo

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 4, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 4, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 4, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

💔 Test for be17f02 failed: CI. Failed jobs:

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 4, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: b0e36da (b0e36dabc37e5dc5550a520d05ab86f3c07ef73a)
Base parent: f48b3e6 (f48b3e61eeece268609bd920fcca1ded047db7ca)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (b0e36da): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.1% [0.1%, 0.1%] 3
Regressions ❌
(secondary)
1.8% [0.3%, 3.2%] 12
Improvements ✅
(primary)
-0.2% [-0.6%, -0.1%] 52
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.0%] 56
All ❌✅ (primary) -0.2% [-0.6%, 0.1%] 55

Max RSS (memory usage)

Results (secondary 0.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.3% [2.0%, 2.9%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.6% [-6.6%, -6.6%] 1
All ❌✅ (primary) - - 0

Cycles

Results (secondary 3.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.3% [2.2%, 6.8%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-1.6%, -1.6%] 1
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 490.122s -> 491.234s (0.23%)
Artifact size: 408.65 MiB -> 409.03 MiB (0.09%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 4, 2026
@rust-bors

This comment has been minimized.

@JonathanBrouwer
JonathanBrouwer force-pushed the param branch 3 times, most recently from 10e9505 to 67a82db Compare October 6, 2026 08:27
@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Oct 6, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 6, 2026
@Kivooeo

Kivooeo commented Nov 8, 2026

Copy link
Copy Markdown
Member

I will need few more days to click green button, thanks for working on it and fixing everything so fast!

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

Take your time, a few days is already pretty fast for such a big PR :)

@ada4a ada4a left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, thank you so much:) just a few follow-up nits, but otherwise lgtm

View changes since this review

@teor2345 teor2345 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for cleaning up the CVarArgs mess and the pretty printing, I struggled with those a bit when making changes to this code.

Just some nits, none of them blocking.

View changes since this review

Comment thread compiler/rustc_hir/src/hir.rs Resolved

let mut ty = self.lower_ty(&param.ty, itctx);
// So code using the HIR can assume `CVarArgs` are always the last parameter
if matches!(ty.kind, hir::TyKind::CVarArgs) && param_idx != decl.inputs.len() - 1 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is already checked in the AST validation pass?
Or is there a reason to check it again?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed already checked in de AST pass, note that this only emits a delayed bug.
There are quite a few places in the HIR that assume that if there is a CVarArgs argument, it is at the end. The easiest way to make these places work as expected, is to change any CVarArgs that is not at the end, into a Err variant

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Kivooeo Kivooeo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to say like "it's fine" and send ten more nits to fix, but suddenly decided ask LLM to ask to try break this PR and it found few more things to fix... So below there is my nits and cursed rust code, also this is the last round of review

View changes since this review

Comment on lines 668 to 672
pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &Body<'v>) -> V::Result {
let Body { params, value } = body;
walk_list!(visitor, visit_param, *params);
let Body { value } = body;
visitor.visit_expr(*value)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is an cursed rust code under no less cursed unstable feature that borrow checker now miss

#![feature(guard_patterns)]

struct NonCopy(u8);

pub fn probe(
    (_ if const {
        let x = NonCopy(0);
        let y = x;
        let z = x;
        true
    }): u8,
) {}

since it's unstable feature and this syntax will be forbid once this feature will get more attention and will only work inside match guards (not everywhere like nowadays)

but more important to me that this is a borrow checker related and it kinda can shoot in a feet anytime right, while llm couldn't find a real hole without this feature, no guarantees it won't break surface in future

// The liberated version of this signature should be a subtype
// of the liberated form of the expectation.
for ((hir_ty, &supplied_ty), expected_ty) in iter::zip(
for ((hir_pram, &supplied_ty), expected_ty) in iter::zip(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for ((hir_pram, &supplied_ty), expected_ty) in iter::zip(
for ((hir_param, &supplied_ty), expected_ty) in iter::zip(

@Kivooeo Kivooeo Nov 17, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uh, not sure what to do with this one, but this now 🧊

pub fn probe((_ if (|| true)()): u8) {}

Comment on lines 195 to 207
WellFormedLoc::Param { param_idx, .. } => {
let fn_decl = tcx.hir_fn_decl_by_hir_id(hir_id).unwrap();
// Get return type
if param_idx as usize == fn_decl.inputs.len() {
if param_idx == fn_decl.inputs.len() {
match fn_decl.output {
hir::FnRetTy::Return(ty) => vec![ty],
// The unit type `()` is always well-formed
hir::FnRetTy::DefaultReturn(_span) => vec![],
}
} else {
vec![&fn_decl.inputs[param_idx as usize]]
vec![fn_decl.inputs[param_idx].ty]
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some very unknown to me reason, this changes give this regression in diagnostics

code:

struct NeedsCopy<T: Copy>(T);

unsafe extern "C" {
    fn f(x: i32, ...) -> Option<NeedsCopy<String>>;
}

before:

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> test.rs:4:33
  |
4 |     fn f(x: i32, ...) -> Option<NeedsCopy<String>>;
  |                                 ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`

after:

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> test.rs:4:26
  |
4 |     fn f(x: i32, ...) -> Option<NeedsCopy<String>>;
  |                          ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`

Comment on lines +174 to +176
for input in decl.inputs {
self.check_pat(cx, input.pat);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will only check for root pattern, not recursive all nested patterns, as a result:

#![warn(clippy::shadow_same, clippy::shadow_reuse, clippy::shadow_unrelated)]
#![allow(dead_code, unused_variables)]

fn plain_same(x: u8) { let x = x; }
fn tuple_same((x,): (u8,)) { let x = x; }

fn plain_reuse(x: u8) { let x = x + 1; }
fn tuple_reuse((x,): (u8,)) { let x = x + 1; }

fn plain_unrelated(x: u8) { let x = 1u8; }
fn tuple_unrelated((x,): (u8,)) { let x = 1u8; }

on upstream all function will trigger a lint while on this PR only plain_* ones

@@ -3072,9 +3071,9 @@ impl<'hir> TraitItem<'hir> {

/// Represents a trait method's body (or just argument names).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Represents a trait method's body (or just argument names).
/// Represents a trait method's body.

///
/// `fn_node_id`: Node Id of the function.
///
/// `fn_hir_id`: Hir Id of the function. Used for attribute parsing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was deleted

Comment on lines 1961 to 1963
// Skip the `...` (`CVarArgs`) trailing arguments from the AST,
// as they are not explicit in HIR/Ty function signatures.
// (instead, the `c_variadic` flag is set to `true`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i read correctly this comment is now doesn't reflect actual situation around how variadics handled

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be very excited to get rid of the separate c_variadic flag, it is a huge pain to have to propagate through the system and remember to check at various significant places.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and I also absolutely still want to do this! This will be on the planning for the second attempt

Comment on lines +2149 to +2176
let fn_inputs = sig
.decl
.inputs
.get(is_method as usize..(sig.decl.inputs.len() - sig.decl.c_variadic() as usize))?
.iter()
.map(|param| {
if let hir::TyKind::Path(QPath::Resolved(
_,
&hir::Path { res: Res::Def(_, res_def_id), .. },
)) = param.ty.kind
{
generics
.params
.iter()
.position(|param| param.def_id.to_def_id() == res_def_id)
.map(GenericIdx::from_usize)
} else {
None
}
});

let mut params_with_generics =
fn_inputs
.zip(sig.decl.inputs.get(
is_method as usize..sig.decl.inputs.len() - sig.decl.c_variadic() as usize,
)?)
.map(|(idx, param)| (idx, FnParam::Param(param)))
.collect::<IndexVec<_, _>>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fn_inputs = sig
.decl
.inputs
.get(is_method as usize..(sig.decl.inputs.len() - sig.decl.c_variadic() as usize))?
.iter()
.map(|param| {
if let hir::TyKind::Path(QPath::Resolved(
_,
&hir::Path { res: Res::Def(_, res_def_id), .. },
)) = param.ty.kind
{
generics
.params
.iter()
.position(|param| param.def_id.to_def_id() == res_def_id)
.map(GenericIdx::from_usize)
} else {
None
}
});
let mut params_with_generics =
fn_inputs
.zip(sig.decl.inputs.get(
is_method as usize..sig.decl.inputs.len() - sig.decl.c_variadic() as usize,
)?)
.map(|(idx, param)| (idx, FnParam::Param(param)))
.collect::<IndexVec<_, _>>();
let params = sig
.decl
.inputs
.get(is_method as usize..sig.decl.inputs.len() - sig.decl.c_variadic() as usize)?;
let mut params_with_generics = params
.iter()
.map(|param| {
let idx = if let hir::TyKind::Path(QPath::Resolved(
_,
&hir::Path { res: Res::Def(_, res_def_id), .. },
)) = param.ty.kind
{
generics
.params
.iter()
.position(|generic_param| generic_param.def_id.to_def_id() == res_def_id)
.map(GenericIdx::from_usize)
} else {
None
};
(idx, FnParam::Param(param))
})
.collect::<IndexVec<_, _>>();

some smol optimizations to code

fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
// Ignore patterns in foreign items for now
// FIXME(#33995) remove this check
if let Node::ForeignItem(_) = cx

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think, it would be better to first check if it's Binding

    let PatKind::Binding(_, hid, ident, _) = p.kind else {
        return;
    };

then do this foreign item check and remove Binding check below

    if let hir::Node::PatField(field) = cx.tcx.parent_hir_node(hid) {
        if !field.is_shorthand {
            self.check_snake_case(cx, "variable", &ident);
        }
        return;
    }

@JonathanBrouwer

Copy link
Copy Markdown
Member Author

After thinking about this for a few hours I no longer think this mega-pr is the right aproach (I guess I should have known...). While I'm sure I could fix the few more serious problem Kivooeo kindly found and get this PR merged, I'd not sleep well at night, because I feel like there are more problems yet to be found.

This is inherent to such a big PR, but I especially don't like how this PR changed the relation between Params and visit_body, this has been the cause of basically all problems found so far and even if we fix all problems now, this will make new code having bugs in the future more likely.

I have come up with a new way forwards, and will do my best to do this in way smaller steps.
Thanks all for your review so far <3

@rust-bors

rust-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #166141) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants