Display an extra note for trailing semicolon lint with trailing macro#87381
Merged
bors merged 1 commit intorust-lang:masterfrom Jul 25, 2021
Merged
Display an extra note for trailing semicolon lint with trailing macro#87381bors merged 1 commit intorust-lang:masterfrom
bors merged 1 commit intorust-lang:masterfrom
Conversation
Contributor
|
r? @wesleywiser (rust-highfive has picked a reviewer for you, use r? to override) |
Contributor
Author
Contributor
|
@bors r+ |
Collaborator
|
📌 Commit 30112a821c5c4ddd60a5414ef570025576827184 has been approved by |
Collaborator
|
☔ The latest upstream changes (presumably #87296) made this pull request unmergeable. Please resolve the merge conflicts. |
30112a8 to
fc83fe4
Compare
Contributor
Author
|
@bors r=petrochenkov |
Collaborator
|
📌 Commit fc83fe4094eab64fb7e77eccdd0afbb82ac183fa has been approved by |
This comment has been minimized.
This comment has been minimized.
Currently, we parse macros at the end of a block
(e.g. `fn foo() { my_macro!() }`) as expressions, rather than
statements. This means that a macro invoked in this position
cannot expand to items or semicolon-terminated expressions.
In the future, we might want to start parsing these kinds of macros
as statements. This would make expansion more 'token-based'
(i.e. macro expansion behaves (almost) as if you just textually
replaced the macro invocation with its output). However,
this is a breaking change (see PR rust-lang#78991), so it will require
further discussion.
Since the current behavior will not be changing any time soon,
we need to address the interaction with the
`SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` lint. Since we are parsing
the result of macro expansion as an expression, we will emit a lint
if there's a trailing semicolon in the macro output. However, this
results in a somewhat confusing message for users, since it visually
looks like there should be no problem with having a semicolon
at the end of a block
(e.g. `fn foo() { my_macro!() }` => `fn foo() { produced_expr; }`)
To help reduce confusion, this commit adds a note explaining
that the macro is being interpreted as an expression. Additionally,
we suggest adding a semicolon after the macro *invocation* - this
will cause us to parse the macro call as a statement. We do *not*
use a structured suggestion for this, since the user may actually
want to remove the semicolon from the macro definition (allowing
the block to evaluate to the expression produced by the macro).
Contributor
Author
|
@bors r- |
fc83fe4 to
0df5ac8
Compare
Contributor
Author
|
@bors r=petrochenkov |
Collaborator
|
📌 Commit 0df5ac8 has been approved by |
Collaborator
Collaborator
|
☀️ Test successful - checks-actions |
This was referenced Jul 25, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, we parse macros at the end of a block
(e.g.
fn foo() { my_macro!() }) as expressions, rather thanstatements. This means that a macro invoked in this position
cannot expand to items or semicolon-terminated expressions.
In the future, we might want to start parsing these kinds of macros
as statements. This would make expansion more 'token-based'
(i.e. macro expansion behaves (almost) as if you just textually
replaced the macro invocation with its output). However,
this is a breaking change (see PR #78991), so it will require
further discussion.
Since the current behavior will not be changing any time soon,
we need to address the interaction with the
SEMICOLON_IN_EXPRESSIONS_FROM_MACROSlint. Since we are parsingthe result of macro expansion as an expression, we will emit a lint
if there's a trailing semicolon in the macro output. However, this
results in a somewhat confusing message for users, since it visually
looks like there should be no problem with having a semicolon
at the end of a block
(e.g.
fn foo() { my_macro!() }=>fn foo() { produced_expr; })To help reduce confusion, this commit adds a note explaining
that the macro is being interpreted as an expression. Additionally,
we suggest adding a semicolon after the macro invocation - this
will cause us to parse the macro call as a statement. We do not
use a structured suggestion for this, since the user may actually
want to remove the semicolon from the macro definition (allowing
the block to evaluate to the expression produced by the macro).