+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_formatter): write! dropped while still borrowed #3067

Merged
merged 1 commit into from
Aug 16, 2022

Conversation

MichaReiser
Copy link
Contributor

This fixes an issue where using write! with a borrowed value didn't compile at the end of a block

error[E0597]: `right` does not live long enough
   --> crates/rome_js_formatter/src/utils/binary_like_expression.rs:529:70
    |
529 |                 write!(f, [format_sub_expression(parent.operator()?, &right)])
    |                            ------------------------------------------^^^^^^-
    |                            |                                         |
    |                            |                                         borrowed value does not live long enough
    |                            a temporary with access to the borrow is created here ...
530 |             }
    |             -
    |             |
    |             `right` dropped here while still borrowed
    |             ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `impl rome_formatter::Format<context::JsFormatContext>`
    |
    = note: the temporary is part of an expression at the end of a block;
            consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
   --> |/home/micha/git/rome/crates/rome_formatter/src/macros.rs:70:9
    |
70  |         let x = $dst.write_fmt($crate::format_args!($($arg),+)); x

This PR implements the recommended fix as part of the macro, similar to std::write!

Test

Having the write! call as the last expression in the block compiles successfully (see binary expression formatting)

@MichaReiser MichaReiser requested a review from ematipico as a code owner August 16, 2022 11:54
@MichaReiser MichaReiser temporarily deployed to aws August 16, 2022 11:54 Inactive
@MichaReiser MichaReiser requested a review from leops August 16, 2022 11:54
@MichaReiser MichaReiser changed the title Refactor binary expression formatting fix(rome_formatter): write dropped while still borrowed Aug 16, 2022
@MichaReiser MichaReiser changed the title fix(rome_formatter): write dropped while still borrowed fix(rome_formatter): write! dropped while still borrowed Aug 16, 2022
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Aug 16, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 87e48f3
Status: ✅  Deploy successful!
Preview URL: https://eabfabef.tools-8rn.pages.dev
Branch Preview URL: https://fix-write-borrow-not-live-lo.tools-8rn.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Aug 16, 2022

This fixes an issue where using `write!` with a borrowed value didn't compile at the end of a block

```
error[E0597]: `right` does not live long enough
   --> crates/rome_js_formatter/src/utils/binary_like_expression.rs:529:70
    |
529 |                 write!(f, [format_sub_expression(parent.operator()?, &right)])
    |                            ------------------------------------------^^^^^^-
    |                            |                                         |
    |                            |                                         borrowed value does not live long enough
    |                            a temporary with access to the borrow is created here ...
530 |             }
    |             -
    |             |
    |             `right` dropped here while still borrowed
    |             ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `impl rome_formatter::Format<context::JsFormatContext>`
    |
    = note: the temporary is part of an expression at the end of a block;
            consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
   --> |/home/micha/git/rome/crates/rome_formatter/src/macros.rs:70:9
    |
70  |         let x = $dst.write_fmt($crate::format_args!($($arg),+)); x
```

This PR implements the recommended fix as part of the macro, similar to `std::write!`

## Test

Having the `write!` call being the last expression in the block compiles successfully
@MichaReiser MichaReiser force-pushed the fix/write-borrow-not-live-long-enough branch from 0a604ea to 87e48f3 Compare August 16, 2022 12:02
@MichaReiser MichaReiser temporarily deployed to aws August 16, 2022 12:02 Inactive
@MichaReiser MichaReiser merged commit 5b8730f into main Aug 16, 2022
@MichaReiser MichaReiser deleted the fix/write-borrow-not-live-long-enough branch August 16, 2022 12:58
IWANABETHATGUY pushed a commit to IWANABETHATGUY/tools that referenced this pull request Aug 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载