Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when labeling arguments for call_once and friends #129320

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

jder
Copy link
Contributor

@jder jder commented Aug 20, 2024

When calling a method on Fn* traits explicitly, argument diagnostics should point at the called method (eg Fn::call_once), not the underlying callee.

This PR makes 3 main changes:

  • It uses TupleArguments to detect if the user called a Fn* method directly (my_fn.call_once(…)) or implicitly (my_fn(…)). If it was explicit, argument diagnostics should point at the call_once method, not the underlying callable.
  • The previous state was causing confusion between the two arguments lists (which could be different lengths), causing an out-of-bounds slice indexing in ICE: fn_ctxt: index out of bounds #128848. I added a length assert to capture the requirement in case this regresses or happens in another case.
  • Unfortunately, this assert tripped when the required arguments information was not available (self.get_hir_params_with_generics was returning an empty Vec), so I've updated that to return None when that information is not available. (cc @strottos if you have any comments, since you added this function in Better reporting on generic argument mismatchs #121595) Sorry this causes a bunch of indentation changes, recommend reviewing ignoring whitespace.)

This is my first rustc PR, so please call out if you'd like this split into more commits (or PRs), style nits, etc. I will add a few comments/questions inline. Thank you!

Fixes #128848

@rustbot
Copy link
Collaborator

rustbot commented Aug 20, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cjgillot (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 20, 2024
@@ -57,7 +57,7 @@ pub fn check_legal_trait_for_method_call(
enum CallStep<'tcx> {
Builtin(Ty<'tcx>),
DeferredClosure(LocalDefId, ty::FnSig<'tcx>),
/// E.g., enum variant constructors.
/// Call overloading when callee implements one of the Fn* traits.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a drive-by change which I think is more accurate.

.skip(if is_method { 1 } else { 0 })
.collect();

generic_params.into_iter().zip(params).collect()
assert!(generic_params.len() == params.len());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a little paranoid but if we're calling zip with lists of different lengths, the result will likely be subtly wrong.

let mut generics_with_unmatched_params = Vec::new();
if let Some(params_with_generics) = self.get_hir_params_with_generics(def_id, is_method)
{
assert_eq!(params_with_generics.len(), matched_inputs.len());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't tell from surrounding code if this should be an assert!, a debug_assert!, or a bug!. Feedback welcome!

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

You can just delete the 128848 test since you already added it as a regular UI test I think

@jder
Copy link
Contributor Author

jder commented Aug 20, 2024

@compiler-errors thanks, done! (Should I have known that test was there somehow?)

@strottos
Copy link
Contributor

strottos commented Aug 21, 2024

@jder Looks good to me FWIW (though I'm not the expert here, merely the perpetrator :) ), thanks for fixing it.

I wasn't thrilled about zipping potentially unequal sizes for sure, but I couldn't see any way they wouldn't be equal offhand, but the worry now is with these asserts if they weren't equal it would throw an ICE again. Though I suspect in these cases the reasoning is obscure enough (like this kind of a bug) that it's (at least borderline) acceptable in such a case. However over to those who know more than I. Actually the zipping unequal sizes might cause a panic too, would have to check.

FYI, @compiler-errors I suspect knew to remove that test because Github actions threw that up. Always worth checking that passes. which it now has, rustc has a fantastic test suite that CI will run for you whenever you make changes.

Also for anyone reviewing, the changes are much smaller if you ignore whitespace I found.

@jder
Copy link
Contributor Author

jder commented Sep 3, 2024

Hey @cjgillot, this has been open a couple weeks, would you have time to take a look? Let me know if you'd like me to break it up to make it easier to review, or if I should ping someone else. Thanks!

@bors
Copy link
Contributor

bors commented Sep 12, 2024

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

@compiler-errors
Copy link
Member

LGTM -- can you rebase, and fix that one nit I left? Also, I'm thinking those assert should be debug_assert -- this is error reporting code after all.

…should

point at the called method (eg Fn::call_once), not the underlying callee.

Fixes 128848
@compiler-errors
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 13, 2024

📌 Commit 57de750 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 13, 2024
@jder
Copy link
Contributor Author

jder commented Sep 13, 2024

@compiler-errors thanks for the review!

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 13, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#129320 (Fix crash when labeling arguments for call_once and friends)
 - rust-lang#130266 (target: default to the medium code model on LoongArch targets)
 - rust-lang#130297 (Dataflow cleanups)
 - rust-lang#130299 (Add set_dcx to ParseSess)
 - rust-lang#130301 (some fixes for clashing_extern_declarations lint)
 - rust-lang#130305 (Clippy: consider msrv for const context for const_float_bits_conv)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 13, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#129320 (Fix crash when labeling arguments for call_once and friends)
 - rust-lang#130266 (target: default to the medium code model on LoongArch targets)
 - rust-lang#130297 (Dataflow cleanups)
 - rust-lang#130299 (Add set_dcx to ParseSess)
 - rust-lang#130301 (some fixes for clashing_extern_declarations lint)
 - rust-lang#130305 (Clippy: consider msrv for const context for const_float_bits_conv)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 24da940 into rust-lang:master Sep 13, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Sep 13, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 13, 2024
Rollup merge of rust-lang#129320 - jder:issue-128848, r=compiler-errors

Fix crash when labeling arguments for call_once and friends

When calling a method on Fn* traits explicitly, argument diagnostics should point at the called method (eg Fn::call_once), not the underlying callee.

This PR makes 3 main changes:

* It uses TupleArguments to detect if the user called a Fn* method directly (`my_fn.call_once(…)`) or implicitly (`my_fn(…)`). If it was explicit, argument diagnostics should point at the call_once method, not the underlying callable.
* The previous state was causing confusion between the two arguments lists (which could be different lengths), causing an out-of-bounds slice indexing in rust-lang#128848. I added a length assert to capture the requirement in case this regresses or happens in another case.
* Unfortunately, this assert tripped when the required arguments information was not available (`self.get_hir_params_with_generics` was returning an empty Vec), so I've updated that to return None when that information is not available. (cc `@strottos` if you have any comments, since you added this function in rust-lang#121595) Sorry this causes a bunch of indentation changes, recommend reviewing [ignoring whitespace](https://github.com/rust-lang/rust/pull/129320/files?w=1).)

This is my first rustc PR, so please call out if you'd like this split into more commits (or PRs), style nits, etc. I will add a few comments/questions inline. Thank you!

Fixes rust-lang#128848
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 18, 2024
Fix circular fn_sig queries to correct number of args for methods

Fixes rust-lang#130400. This was a [debug assert](https://github.com/rust-lang/rust/blob/28e8f01c2a2f33fb4214925a704e3223b372cad5/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs#L2557) added to some argument error reporting code in rust-lang#129320 which verified that the number of params (from the HIR) matched the `matched_inputs` which ultimately come from ty::FnSig. In the reduced test case:

```
fn foo(&mut self) -> _ {
    foo()
}
```

There is a circular dependency computing the ty::FnSig -- when trying to compute it, we try to figure out the return value, which again depends on this ty::FnSig. In rust-lang#105162, this was supported by short-circuiting the cycle by synthesizing a FnSig with error types for parameters. The [code in question](https://github.com/rust-lang/rust/pull/105162/files#diff-a65feec6bfffb19fbdc60a80becd1030c82a56c16b177182cd277478fdb04592R44) computes the number of parameters by taking the number of parameters from the hir::FnDecl and adding 1 if there is an implicit self parameter.

I might be missing a subtlety here, but AFAICT the adjustment for implicit self args is unnecessary and results in one too many args. For example, for this non-errorful code:

```
trait Foo {
    fn bar(&self) {}
}
```

The resulting hir::FnDecl and ty::FnSig both have the same number of inputs -- 1. So, this PR removes that adjustment and adds a test for the debug ICE.

r? `@compiler-errors`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 18, 2024
Rollup merge of rust-lang#130496 - jder:issue-130400, r=compiler-errors

Fix circular fn_sig queries to correct number of args for methods

Fixes rust-lang#130400. This was a [debug assert](https://github.com/rust-lang/rust/blob/28e8f01c2a2f33fb4214925a704e3223b372cad5/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs#L2557) added to some argument error reporting code in rust-lang#129320 which verified that the number of params (from the HIR) matched the `matched_inputs` which ultimately come from ty::FnSig. In the reduced test case:

```
fn foo(&mut self) -> _ {
    foo()
}
```

There is a circular dependency computing the ty::FnSig -- when trying to compute it, we try to figure out the return value, which again depends on this ty::FnSig. In rust-lang#105162, this was supported by short-circuiting the cycle by synthesizing a FnSig with error types for parameters. The [code in question](https://github.com/rust-lang/rust/pull/105162/files#diff-a65feec6bfffb19fbdc60a80becd1030c82a56c16b177182cd277478fdb04592R44) computes the number of parameters by taking the number of parameters from the hir::FnDecl and adding 1 if there is an implicit self parameter.

I might be missing a subtlety here, but AFAICT the adjustment for implicit self args is unnecessary and results in one too many args. For example, for this non-errorful code:

```
trait Foo {
    fn bar(&self) {}
}
```

The resulting hir::FnDecl and ty::FnSig both have the same number of inputs -- 1. So, this PR removes that adjustment and adds a test for the debug ICE.

r? `@compiler-errors`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: fn_ctxt: index out of bounds
7 participants