Repository navigation
Move hir::Params from Body of functions to FnDecl - #163758
JonathanBrouwer wants to merge 16 commits into
Conversation
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
💔 Test for be17f02 failed: CI. Failed jobs:
|
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (secondary 3.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 490.122s -> 491.234s (0.23%) |
This comment has been minimized.
This comment has been minimized.
10e9505 to
67a82db
Compare
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I will need few more days to click green button, thanks for working on it and fixing everything so fast! |
|
Take your time, a few days is already pretty fast for such a big PR :) |
|
|
||
| let mut ty = self.lower_ty(¶m.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 { |
There was a problem hiding this comment.
I think this is already checked in the AST validation pass?
Or is there a reason to check it again?
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This commit is by far the biggest one, and achieves the goal of this PR but leaves the code ugly in a lot of places. The next commits will be a lot of cleanup.
The field is now a duplicate of `Param::ty`'s span
This makes the span of parameters point to the entire parameter, for functions without bodies. This matches the behaviour of functions with bodies.
This improves performance :)
|
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. |
| 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) | ||
| } |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
| for ((hir_pram, &supplied_ty), expected_ty) in iter::zip( | |
| for ((hir_param, &supplied_ty), expected_ty) in iter::zip( |
There was a problem hiding this comment.
uh, not sure what to do with this one, but this now 🧊
pub fn probe((_ if (|| true)()): u8) {}| 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] | ||
| } | ||
| } |
There was a problem hiding this comment.
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`| for input in decl.inputs { | ||
| self.check_pat(cx, input.pat); | ||
| } |
There was a problem hiding this comment.
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). | |||
There was a problem hiding this comment.
| /// 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. |
| // 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`) |
There was a problem hiding this comment.
if i read correctly this comment is now doesn't reflect actual situation around how variadics handled
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes and I also absolutely still want to do this! This will be on the planning for the second attempt
| 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<_, _>>(); |
There was a problem hiding this comment.
| 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 |
There was a problem hiding this comment.
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;
}|
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 I have come up with a new way forwards, and will do my best to do this in way smaller steps. |
|
☔ The latest upstream changes (presumably #166141) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
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
libcandexternsbenchmark:#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