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

E0562 should mention impl Trait in argument position #80476

Closed
kpreid opened this issue Dec 29, 2020 · 1 comment · Fixed by #116431
Closed

E0562 should mention impl Trait in argument position #80476

kpreid opened this issue Dec 29, 2020 · 1 comment · Fixed by #116431
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kpreid
Copy link
Contributor

kpreid commented Dec 29, 2020

impl Trait as a type is permitted in function parameter lists. However, the error message E0562 incorrectly states that it is only permitted in function or inherent method return positions.

/// OK
fn a(x: impl Into<String>) {}
/// OK
fn b() -> impl Into<String> { "bees" }
/// Error
struct C {
    x: impl Into<String>,
}
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
 --> src/lib.rs:4:8
  |
4 |     x: impl Into<String>,
  |        ^^^^^^^^^^^^^^^^^

Being precise in this message will assist beginners (such as myself a few months ago) in understanding the boundaries of the language. Particularly, one might reasonably imagine equivalence between a function's parameter list and a single parameter whose type is a struct bundling several parameters, but impl Trait is available in the former and not the latter — whereas the error message implies that it is allowed in neither of those places.

Tangentially, it would be neat if the compiler gave suggestions to rewrite disallowed impl Trait as either an explicit parameter,struct C<S: impl Into<String>> { x: S }, or (especially in the case of impl Into) a constructor function, but that's much more work and more dependent on the context and perhaps even the specific trait.

Meta

This occurs on both stable 1.48.0 and nightly 1.50.0-nightly (2020-12-22 bb1fbbf84455fbad9afd), and the message is present in the same form in the current source code.

@kpreid kpreid added the C-bug Category: This is a bug. label Dec 29, 2020
@JohnTitor JohnTitor added A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 31, 2020
@Dylan-DPC
Copy link
Member

Current output:

error[[E0562]](https://doc.rust-lang.org/nightly/error_codes/E0562.html): `impl Trait` only allowed in function and inherent method return types, not in field types
 --> src/lib.rs:7:8
  |
7 |     x: impl Into<String>,
  |        ^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0562`.
error: could not compile `playground` (lib) due to previous error

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 5, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 5, 2023
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Oct 5, 2023
@bors bors closed this as completed in 041e54b Oct 5, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 5, 2023
Rollup merge of rust-lang#116431 - estebank:issue-80476, r=compiler-errors

Tweak wording of E0562

Fix rust-lang#80476.
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-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants