1

Experimental feature postfix match by RossSmyth · Pull Request #121619 · rust-la...

 4 weeks ago
source link: https://github.com/rust-lang/rust/pull/121619
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Experimental feature postfix match #121619

Conversation

Contributor

This has a basic experimental implementation for the RFC postfix match (rust-lang/rfcs#3295, #121618). Liaison is @scottmcm with the lang team's experimental feature gate process.

This feature has had an RFC for a while, and there has been discussion on it for a while. It would probably be valuable to see it out in the field rather than continue discussing it. This feature also allows to see how popular postfix expressions like this are for the postfix macros RFC, as those will take more time to implement.

It is entirely implemented in the parser, so it should be relatively easy to remove if needed.

This PR is split in to 5 commits to ease review.

  1. The implementation of the feature & gating.
  2. Add a MatchKind field, fix uses, fix pretty.
  3. Basic rustfmt impl, as rustfmt crashes upon seeing this syntax without a fix.
  4. Add new MatchSource to HIR for Clippy & other HIR consumers
nerodono, bend-n, mcmah309, mo8it, LeoniePhiline, and slanterns reacted with thumbs up emojifr0staman, Ternvein, teohhanhui, and novacrazy reacted with thumbs down emojikennytm reacted with confused emojipetamoriken reacted with eyes emoji

Collaborator

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

rustbot

added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

labels

Feb 26, 2024

Collaborator

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

This comment has been minimized.

Contributor

Author

UI tests need to be culled

This comment has been minimized.

Contributor

@ytmimi ytmimi

left a comment

The rustfmt team prefers that support for experimental features be added in a PR to the rust-lang/rustfmt repo. I've proposed an alternative set of changes that should prevent rustfmt from removing the experimental postfix expressions, but not apply formatting to it.

Comment on lines

+173 to +174

ast::ExprKind::Match(ref cond, ref arms, kind) => {

rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs, kind)

Contributor

Given that the postfix match is an experimental feature I'd prefer not to add formatting support in this PR. For now I think it makes sene to add just enough support so that rustfmt doesn't completely remove the postfix match.

I think the following changes would achieve that.

match kind {
    MatchKind::Prefix => rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs),
    MatchKind::Postfix => {
        // experimental feature postfix match (link to RFC)
        Some(context.snippet(expr.span).to_owned())
    }
}

cc: @calebcartwright

Contributor

Author

That is what I did originally and iirc it caused rustfmt to crash.

Contributor

The only way I can think for context.snippet(expr.span).to_owned() to crash is if the expr.span isn't valid, but that seems unlikely. You could also try returning None from the MatchKind::Postfix match arm.

I actually don't have any concerns with the formatting implementation proposed here as this is rather trivial syntactically, a reasonable initial formatting, and in accordance with the style guide policy on nightly-only/experimental syntax.

the more intriguing part for me was the mention of it "crashing", the details of which I'd be interested in seeing (though that could be shared with us separately in Zulip)

ytmimi reacted with thumbs up emoji

Contributor

Author

Some sort of panic occurred. I'll try to replicate it when I work on this and post it on Zulip. Probably won't be until Friday or Saturday though.

Contributor

Author

Ok, here's a branch with the panic: https://github.com/RossSmyth/rust/tree/rustfmt_test_panic

It be may as designed? Looking at it again after just running x.py test rustfmt

Ran 589 system tests.
assertion `left == right` failed: 1 system tests failed
  left: 1
 right: 0
Failed to join a test thread: Any { .. }


failures:
    test::system_tests

test result: FAILED. 171 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 7.15s

There are two panics that occur.

Fails with left == right failed: 1 idempotent tests failed

Fails with panicked at src\\tools\\rustfmt\\src\\test\\mod.rs:76:10:\nFailed to join a test thread: Any { .. }

Is it designed to just panic if a test fails? It also dumps a pretty large JSON object out.

Collaborator

Some changes occurred in match checking

cc @Nadrieril

This comment has been minimized.

Contributor

Author

Two new commits:

  1. Add a new MatchSource to HIR & fix up uses
  2. Add basic support to clippy for postfix match

I'm not sure if the second commit is needed or desired. There is more worked needed, but as far as I can tell it doesn't break clippy. Since there are many other MatchSources, clippy guards against the other kinds well. So in the end it results in Clippy ignoring postfix match expression and not emitting any lints. A quick rg shows twenty uses of MatchSource::Normal in Clippy. Probably each needs to be looked at & possibly adjusted.

Contributor

Author

Hmm I'm not sure why this fails in CI. It works on my computer. It looks like ui_tests isn't normalizing the test paths for some reason. Which is strange as every other Clippy UI test uses $DIR. I may just move this commit to a different branch a separate PR to Clippy. Clippy doesn't do anything bad without it.

Contributor

Hmm I'm not sure why this fails in CI. It works on my computer. It looks like ui_tests isn't normalizing the test paths for some reason. Which is strange as every other Clippy UI test uses $DIR. I may just move this commit to a different branch a separate PR to Clippy. Clippy doesn't do anything bad without it.

There are too many tests in tests/ui now, you could fix it by moving the new tests into some subdirectory, like match or parser.

petrochenkov

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

Mar 5, 2024

Contributor

Author

Hmm I'm not sure why this fails in CI. It works on my computer. It looks like ui_tests isn't normalizing the test paths for some reason. Which is strange as every other Clippy UI test uses $DIR. I may just move this commit to a different branch a separate PR to Clippy. Clippy doesn't do anything bad without it.

There are too many tests in tests/ui now, you could fix it by moving the new tests into some subdirectory, like match or parser.

I am aware of that. I am talking about the Clippy UI test failure.

This comment has been minimized.

This comment has been minimized.

Contributor

r=me on the implementation.
r? @scottmcm for the lang team process.

Contributor

Author

@rustbot ready

Collaborator

Failed to set assignee to ready: cannot assign: response: {"message":"Not Found","documentation_url":"https://docs.github.com/rest/issues/assignees#add-assignees-to-an-issue"}

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

rustbot

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

Mar 10, 2024

Member

Hello fellow lang folks! I'm not sure exactly what the process is on our side to approve this, but since it's not a one-way door let's try a poll:

@rfcbot poll T-lang Are you ok with an experimental implementation of postfix match in nightly?

I'm not planning on waiting for full checkboxes, since we can always revert the nightly implementation if necessary, but I want to give an opportunity for folks to raise objections just in case.

rfcbot

commented

Mar 14, 2024

edited by tmandry

Team member @scottmcm has asked teams: T-lang, for consensus on:

Are you ok with an experimental implementation of postfix match in nightly?

scottmcm

added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label

Mar 14, 2024

apiraino

added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label

Mar 14, 2024

Member

@bors r=petrochenkov

Contributor

📌 Commit 567c98b has been approved by petrochenkov

It is now in the queue for this repository.

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. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label).

labels

Mar 22, 2024

bors

merged commit 783778c into

rust-lang:master

Mar 22, 2024

11 checks passed

rust-timer

added a commit to rust-lang-ci/rust that referenced this pull request

Mar 22, 2024

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

Reviewers

calebcartwright

calebcartwright left review comments

ytmimi

ytmimi requested changes

Centri3

Centri3 left review comments

petrochenkov

petrochenkov approved these changes
Assignees

scottmcm

Labels
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-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects

None yet

Milestone

1.79.0

Development

Successfully merging this pull request may close these issues.

None yet

11 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK