diagnostics: Suggest fn binding type for unstable closure for<> binders - #160478
Conversation
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
rustbot has assigned @JonathanBrouwer. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
b3e5c81 to
8a03a04
Compare
This comment has been minimized.
This comment has been minimized.
8a03a04 to
1fccd4c
Compare
This comment has been minimized.
This comment has been minimized.
When `for<'a>` appears on a closure without `closure_lifetime_binder`, suggest rewriting a simple let-bound closure to a stable `for<'a> fn(...)` binding type annotation when that rewrite is safe.
Cover MachineApplicable rewrites, MaybeIncorrect capture cases, and macro-expanded binders where structured suggestions must be suppressed.
1fccd4c to
7b0d9e4
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
This looks cool but I might not have time to review this in the next 2 weeks as it would require me to do some reading about how |
|
I will do a perf run, just in case, since this does some extra work |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…er_sugg, r=<try> diagnostics: Suggest fn binding type for unstable closure for<> binders
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (44fa895): comparison URL. Overall result: no relevant changes - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countThis perf run didn't have relevant results for this metric. Max RSS (memory usage)Results (secondary -0.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 2.6%, secondary -0.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 455.478s -> 454.351s (-0.25%) |
This comment was marked as resolved.
This comment was marked as resolved.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
This PR looks highly overengineered to me. I'd suggest to undo the changes and just change the error message, like
gate_all!(
closure_lifetime_binder,
"`for<...>` binders for closures are experimental",
- "consider removing `for<...>`"
+ "consider using a type annotation instead : `let closure: for<...> fn(...) -> ... = /* closure */;`"
);It's not as good a suggestion as you've written here, but the implementation is trivial.
| @@ -0,0 +1,34 @@ | |||
| //@ edition: 2024 | |||
| //@ compile-flags: --error-format=json | |||
There was a problem hiding this comment.
Why are you using error-format=json here? Same for the other tests.
There was a problem hiding this comment.
yeah that was just so i could check applicability (MaybeIncorrect vs MachineApplicable). normal stderr doesn't print that, so i dumped the raw json and grepped the field. I agree that is kinda ugly, and those tests only existed for the structured suggestion anyway.
I will dropping them!
| // Pre-expansion gate so `#[cfg(false)]` code is still rejected. The post-expansion | ||
| // visitor may replace this with a richer diagnostic when the AST is available. |
There was a problem hiding this comment.
This comment is redundant, that's the point of using GatedSpans.
There was a problem hiding this comment.
yeah fair point. I was just reminding myself why the parser still records the span and that's already what GatedSpans is for, so I agree that the comment is noise. I'm removing it rn.
There was a problem hiding this comment.
This sort of complexity should not be added in a place like this - I will not accept any changes to the feature gating logic that are not either ~obviously correct or absolutely necessary.
There was a problem hiding this comment.
i know, this is too much for this file.
gate_all! only has the span, so i can't tell if it's a let cl = for<'a> |...| we could rewrite. that's why i gated in the visitor (where i have the AST) and then still had to emit something for spans that never show up after expansion, like #[cfg(false)]. that's how the extra bookkeeping showed up.
imo the rewrite is nicer, but I agree that it's not worth bending the gate logic for a help message.
|
Reminder, once the PR becomes ready for a review, use |
The visitor-based rewrite needed extra gate bookkeeping for cfg'd-out spans and was too much complexity for feature_gate. Keep gate_all! and point people at a for<> fn type annotation instead.
7b0d9e4 to
916b10c
Compare
|
@mejrs, please let me know if you still think that something isn't good! |
|
@rustbot ready |
…fetime_binder_sugg, r=mejrs diagnostics: Suggest fn binding type for unstable closure for<> binders Fixes rust-lang#160431 `for<'a> |x: &'a T| -> U { ... }` currently just says "consider removing for<...>", which idk is kinda useless when someone landed there while fighting HRTB errors. This rewrites the obvious case to `let cl: for<'a> fn(...) = |x| { ... }` instead. Visitor emits the gate so we have AST context for the rewrite, and we still keep a pre-expansion fallback for `#[cfg(false)]` etc. MachineApplicable only when we're pretty sure: by-value bindings, lifetime-only unbounded binders, no `_` in the sig, no macro expansion. Capture checking is a conservative single-segment free-path heuristic since this runs pre-resolution/pre-typeck; when that looks uncertain we fall back to MaybeIncorrect (or just "consider removing"). imo that's the right tradeoff here: better a maybe-wrong help than rustfix auto-applying into E0308. fyi free-fn / `None` / `Some` cases also hit MaybeIncorrect for the same reason. ltm if the heuristic feels too conservative and y'all want it tightened later.
…uwer Rollup of 17 pull requests Successful merges: - #161017 (Library: enforce clippy deref lints in CI) - #160416 (std: fix unix socket address truncation without a trailing NUL) - #161006 ([CI] Build newer `binutils` before building `gcc`) - #161141 (Add documentation for BPF targets) - #161157 (bootstrap: Move several items out of the crate root) - #161185 (std: guard against unwinds in queue-based `Once`) - #161186 (miri subtree update) - #159855 (std: retry waitid on EINTR in the pidfd wait path) - #160478 (diagnostics: Suggest fn binding type for unstable closure for<> binders) - #161053 (Add regression test for borrow of array drop type in const) - #161073 (Add regression test for path printing with infinitely many visible names) - #161099 (Add regression test for unstable def_ident_span fingerprint with incremental recompilation) - #161103 (cleanup: rip out unnecessary `iter().last()` and `iter().next()`) - #161136 (Add BPF test for Rust ABI stack arguments) - #161146 (Switch to c8a EC2 runner for auto merges) - #161148 ([rustdoc] Put back one removed flaky GUI test (which hopefully isn't flaky anymore)) - #161181 (Add back flaky gui rustdoc test `tests/rustdoc-gui/headers-color.goml`)
Rollup merge of #160478 - Dnreikronos:diagnostics/closure_lifetime_binder_sugg, r=mejrs diagnostics: Suggest fn binding type for unstable closure for<> binders Fixes #160431 `for<'a> |x: &'a T| -> U { ... }` currently just says "consider removing for<...>", which idk is kinda useless when someone landed there while fighting HRTB errors. This rewrites the obvious case to `let cl: for<'a> fn(...) = |x| { ... }` instead. Visitor emits the gate so we have AST context for the rewrite, and we still keep a pre-expansion fallback for `#[cfg(false)]` etc. MachineApplicable only when we're pretty sure: by-value bindings, lifetime-only unbounded binders, no `_` in the sig, no macro expansion. Capture checking is a conservative single-segment free-path heuristic since this runs pre-resolution/pre-typeck; when that looks uncertain we fall back to MaybeIncorrect (or just "consider removing"). imo that's the right tradeoff here: better a maybe-wrong help than rustfix auto-applying into E0308. fyi free-fn / `None` / `Some` cases also hit MaybeIncorrect for the same reason. ltm if the heuristic feels too conservative and y'all want it tightened later.
…uwer Rollup of 17 pull requests Successful merges: - rust-lang/rust#161017 (Library: enforce clippy deref lints in CI) - rust-lang/rust#160416 (std: fix unix socket address truncation without a trailing NUL) - rust-lang/rust#161006 ([CI] Build newer `binutils` before building `gcc`) - rust-lang/rust#161141 (Add documentation for BPF targets) - rust-lang/rust#161157 (bootstrap: Move several items out of the crate root) - rust-lang/rust#161185 (std: guard against unwinds in queue-based `Once`) - rust-lang/rust#161186 (miri subtree update) - rust-lang/rust#159855 (std: retry waitid on EINTR in the pidfd wait path) - rust-lang/rust#160478 (diagnostics: Suggest fn binding type for unstable closure for<> binders) - rust-lang/rust#161053 (Add regression test for borrow of array drop type in const) - rust-lang/rust#161073 (Add regression test for path printing with infinitely many visible names) - rust-lang/rust#161099 (Add regression test for unstable def_ident_span fingerprint with incremental recompilation) - rust-lang/rust#161103 (cleanup: rip out unnecessary `iter().last()` and `iter().next()`) - rust-lang/rust#161136 (Add BPF test for Rust ABI stack arguments) - rust-lang/rust#161146 (Switch to c8a EC2 runner for auto merges) - rust-lang/rust#161148 ([rustdoc] Put back one removed flaky GUI test (which hopefully isn't flaky anymore)) - rust-lang/rust#161181 (Add back flaky gui rustdoc test `tests/rustdoc-gui/headers-color.goml`)
View all comments
Fixes #160431
for<'a> |x: &'a T| -> U { ... }currently just says "consider removing for<...>", which idk is kinda useless when someone landed there while fighting HRTB errors. This rewrites the obvious case tolet cl: for<'a> fn(...) = |x| { ... }instead.Visitor emits the gate so we have AST context for the rewrite, and we still keep a pre-expansion fallback for
#[cfg(false)]etc. MachineApplicable only when we're pretty sure: by-value bindings, lifetime-only unbounded binders, no_in the sig, no macro expansion. Capture checking is a conservative single-segment free-path heuristic since this runs pre-resolution/pre-typeck; when that looks uncertain we fall back to MaybeIncorrect (or just "consider removing"). imo that's the right tradeoff here: better a maybe-wrong help than rustfix auto-applying into E0308. fyi free-fn /None/Somecases also hit MaybeIncorrect for the same reason.ltm if the heuristic feels too conservative and y'all want it tightened later.