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

Ensure that drop order of async fn matches fn and that users cannot refer to generated arguments. #60437

Merged
merged 3 commits into from
May 2, 2019

Conversation

davidtwco
Copy link
Member

@davidtwco davidtwco commented May 1, 2019

Fixes #60236 and fixes #60438.

This PR modifies the lowering of async fn arguments so that the
drop order matches the equivalent fn.

Previously, async function arguments were lowered as shown below:

async fn foo(<pattern>: <ty>) {
  async move {
  }
} // <-- dropped as you "exit" the fn

// ...becomes...
fn foo(__arg0: <ty>) {
  async move {
    let <pattern> = __arg0;
  } // <-- dropped as you "exit" the async block
}

After this PR, async function arguments will be lowered as:

async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
  async move {
  }
} // <-- dropped as you "exit" the fn

// ...becomes...
fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
  async move {
    let __arg2 = __arg2;
    let <pattern> = __arg2;
    let __arg1 = __arg1;
    let <pattern> = __arg1;
    let __arg0 = __arg0;
    let <pattern> = __arg0;
  } // <-- dropped as you "exit" the async block
}

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

This PR also stops users from referring to the generated __argN
identifiers.

r? @nikomatsakis

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 1, 2019
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

r=me with a "master comment" detailing the strategy and a renamed test file =)

src/test/run-pass/issue-54716.rs Outdated Show resolved Hide resolved
src/librustc/hir/lowering.rs Show resolved Hide resolved
This commit modifies the lowering of `async fn` arguments so that the
drop order matches the equivalent `fn`.

Previously, async function arguments were lowered as shown below:

    async fn foo(<pattern>: <ty>) {
      async move {
      }
    } // <-- dropped as you "exit" the fn

    // ...becomes...
    fn foo(__arg0: <ty>) {
      async move {
        let <pattern> = __arg0;
      } // <-- dropped as you "exit" the async block
    }

After this PR, async function arguments will be lowered as:

    async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
      async move {
      }
    } // <-- dropped as you "exit" the fn

    // ...becomes...
    fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
      async move {
        let __arg2 = __arg2;
        let <pattern> = __arg2;
        let __arg1 = __arg1;
        let <pattern> = __arg1;
        let __arg0 = __arg0;
        let <pattern> = __arg0;
      } // <-- dropped as you "exit" the async block
    }

If `<pattern>` is a simple ident, then it is lowered to a single
`let <pattern> = <pattern>;` statement as an optimization.
@davidtwco davidtwco changed the title Ensure that drop order of async fn matches fn. Ensure that drop order of async fn matches fn and that users cannot refer to generated arguments. May 1, 2019
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

Actually can we simplify the new test?

src/test/ui/async-await-drop-order-locals-are-hidden.rs Outdated Show resolved Hide resolved
This commit gensyms the generated ident for replacement arguments so
that users cannot refer to them. It also ensures that levenshtein
distance suggestions do not suggest gensymed identifiers.
@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 1, 2019

📌 Commit 1fedb0a has been approved by nikomatsakis

@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 May 1, 2019
Centril added a commit to Centril/rust that referenced this pull request May 1, 2019
Ensure that drop order of `async fn` matches `fn` and that users cannot refer to generated arguments.

Fixes rust-lang#60236 and fixes rust-lang#60438.

This PR modifies the lowering of `async fn` arguments so that the
drop order matches the equivalent `fn`.

Previously, async function arguments were lowered as shown below:

    async fn foo(<pattern>: <ty>) {
      async move {
      }
    } // <-- dropped as you "exit" the fn

    // ...becomes...
    fn foo(__arg0: <ty>) {
      async move {
        let <pattern> = __arg0;
      } // <-- dropped as you "exit" the async block
    }

After this PR, async function arguments will be lowered as:

    async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
      async move {
      }
    } // <-- dropped as you "exit" the fn

    // ...becomes...
    fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
      async move {
        let __arg2 = __arg2;
        let <pattern> = __arg2;
        let __arg1 = __arg1;
        let <pattern> = __arg1;
        let __arg0 = __arg0;
        let <pattern> = __arg0;
      } // <-- dropped as you "exit" the async block
    }

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

This PR also stops users from referring to the generated `__argN`
identifiers.

r? @nikomatsakis
bors added a commit that referenced this pull request May 2, 2019
Rollup of 7 pull requests

Successful merges:

 - #59634 (Added an explanation for the E0704 error.)
 - #60348 (move some functions from parser.rs to diagostics.rs)
 - #60385 (Emit metadata files earlier)
 - #60428 (Refactor `eval_body_using_ecx` so that it doesn't need to query for MIR)
 - #60437 (Ensure that drop order of `async fn` matches `fn` and that users cannot refer to generated arguments.)
 - #60439 (doc: Warn about possible zombie apocalypse)
 - #60452 (Remove Context and ContextKind)

Failed merges:

r? @ghost
@bors bors merged commit 1fedb0a into rust-lang:master May 2, 2019
@davidtwco davidtwco deleted the issue-60236 branch May 2, 2019 18:33
davidtwco pushed a commit to davidtwco/rust that referenced this pull request Jun 3, 2019
Here follows the main reverts applied in order to make this commit:

Revert "Rollup merge of rust-lang#60676 - davidtwco:issue-60674, r=cramertj"

This reverts commit 45b0945, reversing
changes made to f6df1f6.

Revert "Rollup merge of rust-lang#60437 - davidtwco:issue-60236, r=nikomatsakis"

This reverts commit 16939a5, reversing
changes made to 12bf981.

Revert "Rollup merge of rust-lang#59823 - davidtwco:issue-54716, r=cramertj"

This reverts commit 62d1574, reversing
changes made to 4eff852.
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.
Projects
None yet
4 participants