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

Don't flatten a block containing a single macro call #4496

Merged
merged 1 commit into from
Nov 3, 2020

Conversation

Aaron1011
Copy link
Member

We no longer flatten a block that looks like this:

match val {
    pat => { macro_call!() }
}

Currently, rust ignores trailing semicolons in macro expansion in
expression position (see rust-lang/rust#33953)

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. pat => macro_call!())

We no longer flatten a block that looks like this:

```rust
match val {
    pat => { macro_call!() }
}
```

Currently, rust ignores trailing semicolons in macro expansion in
expression position (see rust-lang/rust#33953)

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. `pat => macro_call!()`)
@calebcartwright
Copy link
Member

I started tracing through the issues to rust-lang/rust#78449 and the trailing semi on the return and may have gotten myself confused. I assume there's no expected relation between that and flattening of the blocks?

@Aaron1011
Copy link
Member Author

@calebcartwright: That's correct. PR rust-lang/rust#78449 is due to a different macro-related issue in rustfmt - I'll be opening a separate PR for that.

@calebcartwright
Copy link
Member

calebcartwright commented Oct 28, 2020

@calebcartwright: That's correct. PR rust-lang/rust#78449 is due to a different macro-related issue in rustfmt - I'll be opening a separate PR for that.

Thanks for the clarification. Happy to discuss on that PR but my initial reaction is that's the correct formatting as the return expression should have a trailing semi https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/statements.md#expressions-in-statement-position

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. pat => macro_call!())

I understand the motivation for this change, though the fact that the semis are currently ignored along with the rustfmt stability guarantee that bans formatting changes is going to make this tough to release. Are there planned changes upstream around how those semis will be handled?

Maybe one way around this, and to avoid having to wait until this becomes an explicit breaking bug, would be to update the style guide with an additional clause around when the bodies can be flattened
https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/expressions.md#match

@topecongiro - do you have any thoughts on how to best proceed with this? Seems like the right thing to do, just more a question of how/when for me

@Aaron1011
Copy link
Member Author

From that document:

All expressions in statement position should be terminated with a semi-colon, unless they end with a block or are used as the value for a block.

The macro expansion determines what get used as the value for a block. However, there's no explicit mention of macro expressions, which is kind of unfortunate.

Maybe one way around this, and to avoid having to wait until this becomes an explicit breaking bug, would be to update the style guide with an additional clause around when the bodies can be flattened

We have a sort of cyclic dependency here. At some point, we will probably want to start warning about code using a trailing-semi macro expansion in expression position. However, I think it will be extremely frustating to users if rustfmt is actively undoing their attempts to fix the warning.

cc @petrochenkov

@calebcartwright
Copy link
Member

However, I think it will be extremely frustating to users if rustfmt is actively undoing their attempts to fix the warning.

Agreed, and I think that's where updating the Style Guide will give us the CYA we need to be able to justify making breaking formatting changes on the rustfmt side. The Style Guide is rustfmt's proverbial north star, so if we augment the Style Guide with these clarifications then that makes this more of a bug fix/style guide re-alignment change that won't impact our stability guarantee.

@calebcartwright
Copy link
Member

We've updated the style guide with some guidance on when block-wrapping the body is require with mac call expressions. Given all of the discussion and context, this change makes sense as we don't want rustfmt to forcibly flatten the arm bodies in these cases and instead defer to the programmer.

We will still need to backport this for the next 1.x release (1.4.24 since 1.4.23 is already out the door) so we can revisit if you have any concerns @topecongiro but going to go ahead merge 👍

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@calebcartwright calebcartwright merged commit f3ce2ce into rust-lang:master Nov 3, 2020
@karyon
Copy link
Contributor

karyon commented Oct 25, 2021

backported in #4514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants