Skip to content

2024 impl_trait_overcaptures ambiguous + #132853

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

Closed
ehuss opened this issue Nov 10, 2024 · 2 comments · Fixed by #134142
Closed

2024 impl_trait_overcaptures ambiguous + #132853

ehuss opened this issue Nov 10, 2024 · 2 comments · Fixed by #134142
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2024 Area: The 2024 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-impl_trait_overcaptures Lint: impl_trait_overcaptures T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Nov 10, 2024

I tried this code:

#![warn(impl_trait_overcaptures)]

pub fn f(x: &i32) -> &impl Clone {
    x
}

I expected to see this happen: Provides a valid suggestion that is -> &(impl Clone + use<>) with parentheses to avoid ambiguity.

Instead, this happened: Provides a suggestion that includes &impl Clone + use<> which fails to parse with:

error: ambiguous `+` in a type
 --> src/main.rs:3:23
  |
3 | pub fn f(x: &i32) -> &impl Clone + use<> {
  |                       ^^^^^^^^^^^^^^^^^^
  |
help: try adding parentheses
  |
3 | pub fn f(x: &i32) -> &(impl Clone + use<>) {
  |                       +                  +

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (b91a3a056 2024-11-07)
binary: rustc
commit-hash: b91a3a05609a46f73d23e0995ae7ebb4a4f429a5
commit-date: 2024-11-07
host: aarch64-apple-darwin
release: 1.84.0-nightly
LLVM version: 19.1.3
@ehuss ehuss added A-edition-2024 Area: The 2024 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-impl_trait_overcaptures Lint: impl_trait_overcaptures labels Nov 10, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 10, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-diagnostics Area: Messages for errors, warnings, and lints and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. labels Nov 10, 2024
@compiler-errors compiler-errors self-assigned this Nov 11, 2024
@ehuss
Copy link
Contributor Author

ehuss commented Dec 10, 2024

@compiler-errors Just checking if this is still something you could look at, or how complex this might be?

@compiler-errors
Copy link
Member

Sure, let me put up a quick fix.

@bors bors closed this as completed in f103076 Dec 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 11, 2024
Rollup merge of rust-lang#134142 - compiler-errors:paren-sug, r=jieyouxu

Rudimentary heuristic to insert parentheses when needed for RPIT overcaptures lint

We don't have basically any preexisting machinery to detect when parentheses are needed for *types*. AFAICT, all of the diagnostics we have for opaques just... fail when they suggest `+ 'a` when that's ambiguous.

Fixes rust-lang#132853
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2024 Area: The 2024 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-impl_trait_overcaptures Lint: impl_trait_overcaptures T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants