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

Experimental feature postfix match #121619

Merged
merged 4 commits into from
Mar 22, 2024
Merged

Experimental feature postfix match #121619

merged 4 commits into from
Mar 22, 2024

Conversation

RossSmyth
Copy link
Contributor

@RossSmyth RossSmyth commented Feb 26, 2024

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

@rustbot
Copy link
Collaborator

rustbot commented Feb 26, 2024

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 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
@rustbot
Copy link
Collaborator

rustbot commented Feb 26, 2024

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@RossSmyth
Copy link
Contributor Author

UI tests need to be culled

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

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

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

@RossSmyth RossSmyth Mar 3, 2024

Choose a reason for hiding this comment

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

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.

  1. // Display results.
    println!("Ran {} idempotent tests.", count);
    assert_eq!(fails, 0, "{} idempotent tests failed", fails);

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

  1. fn run_test_with<F>(test_setting: &TestSetting, f: F)
    where
    F: FnOnce(),
    F: Send + 'static,
    {
    thread::Builder::new()
    .stack_size(test_setting.stack_size)
    .spawn(f)
    .expect("Failed to create a test thread")
    .join()
    .expect("Failed to join a test thread")
    }

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.

@rustbot
Copy link
Collaborator

rustbot commented Mar 3, 2024

Some changes occurred in match checking

cc @Nadrieril

@rust-log-analyzer

This comment has been minimized.

@RossSmyth
Copy link
Contributor Author

RossSmyth commented Mar 3, 2024

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.

@RossSmyth
Copy link
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.

@petrochenkov
Copy link
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.

src/tools/clippy/tests/ui/postfix_match.rs Outdated Show resolved Hide resolved
compiler/rustc_ast/src/mut_visit.rs Outdated Show resolved Hide resolved
compiler/rustc_ast/src/visit.rs Outdated Show resolved Hide resolved
compiler/rustc_builtin_macros/src/assert/context.rs Outdated Show resolved Hide resolved
compiler/rustc_builtin_macros/src/deriving/decodable.rs Outdated Show resolved Hide resolved
compiler/rustc_builtin_macros/src/deriving/generic/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_expand/src/build.rs Outdated Show resolved Hide resolved
compiler/rustc_lint/src/unused.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/check_const.rs Outdated Show resolved Hide resolved
@petrochenkov 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
@RossSmyth
Copy link
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.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 10, 2024

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 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
@scottmcm
Copy link
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
Copy link

rfcbot commented Mar 14, 2024

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

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

@scottmcm scottmcm added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Mar 14, 2024
@apiraino apiraino added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Mar 14, 2024
@scottmcm
Copy link
Member

@bors r=petrochenkov

@bors
Copy link
Contributor

bors commented Mar 22, 2024

📌 Commit 567c98b has been approved by petrochenkov

It is now in the queue for this repository.

@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. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Mar 22, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 22, 2024
Experimental feature postfix match

This has a basic experimental implementation for the RFC postfix match (rust-lang/rfcs#3295, rust-lang#121618). [Liaison is](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Postfix.20Match.20Liaison/near/423301844) `@scottmcm` with the lang team's [experimental feature gate process](https://github.com/rust-lang/lang-team/blob/master/src/how_to/experiment.md).

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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 22, 2024
Experimental feature postfix match

This has a basic experimental implementation for the RFC postfix match (rust-lang/rfcs#3295, rust-lang#121618). [Liaison is](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Postfix.20Match.20Liaison/near/423301844) ``@scottmcm`` with the lang team's [experimental feature gate process](https://github.com/rust-lang/lang-team/blob/master/src/how_to/experiment.md).

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
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 22, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#121619 (Experimental feature postfix match)
 - rust-lang#122370 (Gracefully handle `AnonConst` in `diagnostic_hir_wf_check()`)
 - rust-lang#122537 (interpret/allocation: fix aliasing issue in interpreter and refactor getters a bit)
 - rust-lang#122542 (coverage: Clean up marker statements that aren't needed later)
 - rust-lang#122800 (Add `NonNull::<[T]>::is_empty`.)
 - rust-lang#122820 (Stop using `<DefId as Ord>` in various diagnostic situations)
 - rust-lang#122847 (Suggest `RUST_MIN_STACK` workaround on overflow)
 - rust-lang#122855 (Fix Itanium mangling usizes)
 - rust-lang#122863 (add more ice tests )

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 783778c into rust-lang:master Mar 22, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Mar 22, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 22, 2024
Rollup merge of rust-lang#121619 - RossSmyth:pfix_match, r=petrochenkov

Experimental feature postfix match

This has a basic experimental implementation for the RFC postfix match (rust-lang/rfcs#3295, rust-lang#121618). [Liaison is](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Postfix.20Match.20Liaison/near/423301844) ```@scottmcm``` with the lang team's [experimental feature gate process](https://github.com/rust-lang/lang-team/blob/master/src/how_to/experiment.md).

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
@RossSmyth RossSmyth deleted the pfix_match branch March 22, 2024 17:18
flip1995 pushed a commit to flip1995/rust that referenced this pull request Apr 4, 2024
Experimental feature postfix match

This has a basic experimental implementation for the RFC postfix match (rust-lang/rfcs#3295, rust-lang#121618). [Liaison is](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Postfix.20Match.20Liaison/near/423301844) ```@scottmcm``` with the lang team's [experimental feature gate process](https://github.com/rust-lang/lang-team/blob/master/src/how_to/experiment.md).

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
flip1995 pushed a commit to flip1995/rust that referenced this pull request Apr 4, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#121619 (Experimental feature postfix match)
 - rust-lang#122370 (Gracefully handle `AnonConst` in `diagnostic_hir_wf_check()`)
 - rust-lang#122537 (interpret/allocation: fix aliasing issue in interpreter and refactor getters a bit)
 - rust-lang#122542 (coverage: Clean up marker statements that aren't needed later)
 - rust-lang#122800 (Add `NonNull::<[T]>::is_empty`.)
 - rust-lang#122820 (Stop using `<DefId as Ord>` in various diagnostic situations)
 - rust-lang#122847 (Suggest `RUST_MIN_STACK` workaround on overflow)
 - rust-lang#122855 (Fix Itanium mangling usizes)
 - rust-lang#122863 (add more ice tests )

r? `@ghost`
`@rustbot` modify labels: rollup
calebcartwright pushed a commit to calebcartwright/rust that referenced this pull request Jun 22, 2024
Experimental feature postfix match

This has a basic experimental implementation for the RFC postfix match (rust-lang/rfcs#3295, rust-lang#121618). [Liaison is](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Postfix.20Match.20Liaison/near/423301844) ```@scottmcm``` with the lang team's [experimental feature gate process](https://github.com/rust-lang/lang-team/blob/master/src/how_to/experiment.md).

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
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. 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
Development

Successfully merging this pull request may close these issues.