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

Generic type parameter deduction fails when used with associated constant defaults #44539

Closed
mehcode opened this issue Sep 13, 2017 · 4 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mehcode
Copy link
Contributor

mehcode commented Sep 13, 2017

Compiling playground v0.0.1 (file:///playground)
error[E0283]: type annotations required: cannot resolve `_: Foo`
  --> src/main.rs:12:14
   |
12 |   take_debug(Foo::BAR);
   |              ^^^^^^^^
   |
   = note: required by `Foo::BAR`

error: aborting due to previous error
use std::fmt;

trait Foo {
  const BAR: u8 = 32;
}

fn take_debug<F: fmt::Debug>(f: F) {
    println!("{:?}", f);
}

fn main() {
  // error
  take_debug(Foo::BAR);
}

https://play.rust-lang.org/?gist=b8610b4b6fa35a3c913434e43f18de1c&version=nightly
https://play.rust-lang.org/?gist=fb75d70964d26c4ce1f75ae98895edec&version=nightly


$ rustc --version
rustc 1.22.0-nightly (eba374fb2 2017-09-11)
@bluss
Copy link
Member

bluss commented Sep 13, 2017

I think this is expected-- a specific implementation of Foo is still needed to resolve the value of the constant. Indeed, is there even a constant to resolve in a program with no implementations of the trait. The role of the default is a default for each implementation.

@Mark-Simulacrum
Copy link
Member

Agreed that this is an expected problem, but the error message can be improved I think, perhaps something like the below.

error[EXXXX]: cannot take value of associated constant without concrete impl
  --> test.rs:13:14
   |
13 |   take_debug(Foo::BAR);
   |              ^^^^^^^^

error: aborting due to previous error

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Sep 17, 2017
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items (types, constants & functions) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 29, 2019
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Dec 13, 2019
@estebank
Copy link
Contributor

Current output:

error[E0283]: type annotations needed
  --> src/main.rs:12:14
   |
4  |   const BAR: u8 = 32;
   |   ------------------- required by `Foo::BAR`
...
12 |   take_debug(Foo::BAR);
   |              ^^^^^^^^ cannot infer type
   |
   = note: cannot resolve `_: Foo`
error[E0283]: type annotations needed
  --> src/main.rs:20:12
   |
2  |   const BAR: BAR = BAR;
   |   --------------------- required by `Foo::BAR`
...
20 |   take_foo(Foo::BAR);
   |            ^^^^^^^^ cannot infer type
   |
   = note: cannot resolve `_: Foo`

The second one should suggest take_foo(<BAR as Foo>::BAR); or enumerate all of the possible implementors of Foo. The first one should mention that no implementor of Foo has been found. Both cases should mention that associated types can only be used from specific impls, not directly from a trait.

Centril added a commit to Centril/rust that referenced this issue Jan 8, 2020
Explain that associated types and consts can't be accessed directly on the trait's path

Partially address rust-lang#44539.
@estebank
Copy link
Contributor

Current output:

error[E0283]: type annotations needed
  --> file13.rs:12:16
   |
4  |     const BAR: u8 = 32;
   |     ------------------- required by `Foo::BAR`
...
12 |     take_debug(Foo::BAR);
   |                ^^^^^^^^
   |                |
   |                cannot infer type
   |                help: use the fully qualified path to an implementation: `<Type as Foo>::BAR`
   |
   = note: cannot satisfy `_: Foo`
   = note: associated constants cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants