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 rustdoc rendering of by-value mutable arguments in async fn #76730

Merged
merged 3 commits into from
Nov 13, 2020

Conversation

ebkalderon
Copy link
Contributor

r? @jyn514

Fixes #76517.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jyn514 (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 15, 2020
@ebkalderon
Copy link
Contributor Author

Would you like me to prepare any tests for this fix?

@jyn514
Copy link
Member

jyn514 commented Sep 15, 2020

Yes please; add them to src/test/rustdoc. There are examples there and instructions at https://rustc-dev-guide.rust-lang.org/rustdoc-internals.html#dotting-is-and-crossing-ts.

@jyn514
Copy link
Member

jyn514 commented Sep 15, 2020

 error[E0596]: cannot borrow `vec` as mutable, as it is not declared as mutable
  --> /checkout/src/test/ui/async-await/argument-patterns.rs:12:20
   |
LL | async fn b(n: u32, ref mut vec: A) {
   |                    ^^^^^^^^^^^
   |                    |
   |                    cannot borrow as mutable
   |                    help: consider changing this to be mutable: `mut vec`

error: aborting due to previous error

Welp, looks like this broke something. Not sure why.

r? @ecstatic-morse for review or re-assignment

@jyn514 jyn514 added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-HIR Area: The high-level intermediate representation (HIR) labels Sep 15, 2020
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, _) => {
(ident, true)
}
hir::PatKind::Binding(_, _, ident, _) => (ident, true),
Copy link
Member

Choose a reason for hiding this comment

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

Actually, I wonder if this would work if you changed it to Unannotated | Mutable instead; that would avoid changing the logic for ref mut and I don't think that's valid in function parameters anyway.

Suggested change
hir::PatKind::Binding(_, _, ident, _) => (ident, true),
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated | hir::BindingAnnotation::Mutable, _, ident, _) => (ident, true),

Copy link
Member

Choose a reason for hiding this comment

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

and I don't think that's valid in function parameters anyway

It clearly is, see the error message above. So yeah, I'm not sure how to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue, it seems, is as described in the comments above:

// If `<pattern>` is a simple ident, then it is lowered to a single
// `let <pattern> = <pattern>;` statement as an optimization.

I don't think that this is considered valid:

let mut bad1 = mut bad1;
let ref mut bad2 = ref mut bad2;

Copy link
Contributor Author

@ebkalderon ebkalderon Sep 15, 2020

Choose a reason for hiding this comment

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

This comment a little further down the page elaborates on the reasoning:

if is_simple_parameter {
// If this is the simple case, then we only insert one statement that is
// `let <pat> = <pat>;`. We re-use the original argument's pattern so that
// `HirId`s are densely assigned.

It seems that not reusing the original argument's pattern will mean that the HirIds will no longer be densely assigned. I wonder if this is still a pressing concern that must be addressed before considering this PR for merging, the current CI failures aside.

Is setting aside a special case for mutable assignment patterns even a good idea? Sure, it would fix the rendering issue in rustdoc, but perhaps this change to the AST lowering logic would have other implications elsewhere in the compiler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that the tests now pass with the suggested change from @jyn514, but this is not yet ready to merge. I'm assuming that although, for example, let ref mut foo = ref mut foo; is invalid Rust, CI is passing because there are no tests in place to catch this. Once I add tests in place, we can check whether this change is safe or not.

Copy link
Member

Choose a reason for hiding this comment

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

perhaps this change to the AST lowering logic would have other implications elsewhere in the compiler?

Right, this is why I asked for feedback from ecstatic-morse. Rustdoc doesn't really have a way to fix this on its end.

@ebkalderon ebkalderon force-pushed the rustdoc-fix-mut-args-async-fn branch from dac7e70 to 12a0c16 Compare September 15, 2020 04:39
@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Sep 15, 2020

r? @cramertj maybe? I don't know this code.

I would guess that there's no correct "quick fix" here. If that's correct, we would need to preserve a mapping from async fn parameters to their patterns for use by rustdoc to fix #76517.

@Dylan-DPC-zz
Copy link

r? @Mark-Simulacrum

@tmandry
Copy link
Member

tmandry commented Oct 6, 2020

In the simple case we aren't actually inserting let <pat> = <pat>; as the comment says. We're inserting the statement let <pat> = <ident>; where ident is the original identifier named in the function signature. That means we were effectively doing the following in the ref mut case:

fn foo(ref mut x: i32) {
    let ref mut y = x;
}

i.e. trying to take a mutable reference to x, which itself is a &mut i32. But x is not a mutable binding, i.e. the integer x points to could be changed, but which integer x points to cannot be changed. Taking a mutable reference to x would violate that, so this is a compiler error.

I think we don't want this is_simple_parameter behavior anytime ref is involved. Which means that changing to hir::BindingAnnotation::Unannotated | hir::BindingAnnotation::Mutable was partly right.

However, we do actually want to preserve the original identifier for rustdoc to use, anytime the pattern is a binding. For ref cases, I don't see any harm in preserving the identifier and setting is_simple_parameter to false.

P.S. Keeping HirId's dense is just an optimization, not as important as correctness.

@jyn514
Copy link
Member

jyn514 commented Oct 6, 2020

Ok, that makes a lot of sense! So something like this would work:

                let (ident, is_simple_parameter) = match parameter.pat.kind {
                    hir::PatKind::Binding(
                        hir::BindingAnnotation::Unannotated | hir::BindingAnnotation::Mutable,
                        _,
                        ident,
                        _,
                    ) => (ident, true),
                    hir::PatKind::Binding(_, _, ident, _) => (ident, false),

@ebkalderon do you mind implementing that?

@jyn514
Copy link
Member

jyn514 commented Oct 6, 2020

Also, please add a test for both of these cases: async fn f(ref mut x: i32) and async fn f(mut x: i32).

@jyn514 jyn514 assigned jyn514 and unassigned Mark-Simulacrum Oct 6, 2020
@ebkalderon
Copy link
Contributor Author

Yep, I'm on it!

@tmandry
Copy link
Member

tmandry commented Oct 20, 2020

Ping from triage. @ebkalderon, are you still working on this?

@jyn514 jyn514 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 1, 2020
@jyn514
Copy link
Member

jyn514 commented Nov 8, 2020

@ebkalderon do you mind if I take over this PR? It's most of the way there, I'd hate for it to be lost in limbo.

@jyn514 jyn514 force-pushed the rustdoc-fix-mut-args-async-fn branch from 12a0c16 to 7abe582 Compare November 8, 2020 16:47
@rust-highfive rust-highfive assigned tmandry and unassigned jyn514 Nov 8, 2020
@jyn514 jyn514 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 8, 2020
@jyn514 jyn514 force-pushed the rustdoc-fix-mut-args-async-fn branch from 7abe582 to 26b11d6 Compare November 8, 2020 19:02
@jyn514 jyn514 added the A-async-await Area: Async & Await label Nov 8, 2020
@tmandry
Copy link
Member

tmandry commented Nov 12, 2020

@bors r+ rollup

Thanks all!

@bors
Copy link
Contributor

bors commented Nov 12, 2020

📌 Commit 96793d3 has been approved by tmandry

@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 Nov 12, 2020
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Nov 12, 2020
…nc-fn, r=tmandry

Fix rustdoc rendering of by-value mutable arguments in async fn

r? `@jyn514`

Fixes rust-lang#76517.
@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Nov 12, 2020

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 12, 2020
@jyn514
Copy link
Member

jyn514 commented Nov 12, 2020

Heh, the test @lcnr added for the wrong behavior in #78916 failed. Just needs a rebase.

ebkalderon and others added 3 commits November 12, 2020 11:13
This should fix `rustdoc` rendering of by-value mutable arguments in
`async fn` contexts.
Reusing bindings causes errors later in lowering:

```
 error[E0596]: cannot borrow `vec` as mutable, as it is not declared as mutable
  --> /checkout/src/test/ui/async-await/argument-patterns.rs:12:20
   |
LL | async fn b(n: u32, ref mut vec: A) {
   |                    ^^^^^^^^^^^
   |                    |
   |                    cannot borrow as mutable
   |                    help: consider changing this to be mutable: `mut vec`
```
@jyn514 jyn514 force-pushed the rustdoc-fix-mut-args-async-fn branch from 96793d3 to 38127ca Compare November 12, 2020 16:14
@jyn514
Copy link
Member

jyn514 commented Nov 12, 2020

@bors r=tmandry rollup

@bors
Copy link
Contributor

bors commented Nov 12, 2020

📌 Commit 38127ca has been approved by tmandry

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 12, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 12, 2020
Rollup of 7 pull requests

Successful merges:

 - rust-lang#76730 (Fix rustdoc rendering of by-value mutable arguments in async fn)
 - rust-lang#78836 (Implement destructuring assignment for structs and slices)
 - rust-lang#78857 (Improve BinaryHeap performance)
 - rust-lang#78950 (Add asm register information for SPIR-V)
 - rust-lang#78970 (update rustfmt to v1.4.25)
 - rust-lang#78972 (Update cargo)
 - rust-lang#78987 (extend min_const_generics param ty tests)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 4b0b42a into rust-lang:master Nov 13, 2020
@rustbot rustbot added this to the 1.49.0 milestone Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-HIR Area: The high-level intermediate representation (HIR) 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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustdoc renames parameters with mut in async fn