Skip to content

Bad error message using associated type #45447

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
leonardo-m opened this issue Oct 22, 2017 · 12 comments
Closed

Bad error message using associated type #45447

leonardo-m opened this issue Oct 22, 2017 · 12 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@leonardo-m
Copy link

leonardo-m commented Oct 22, 2017

I think this code shows a bad error message:

trait Foo { const FOO: Self; }
impl Foo for u32 { const FOO: Self = 1; }
fn bar<T: Foo>(n: T) {
    const BASE: T = T::FOO;
}
fn main() {}

rustc 1.22.0-nightly (4279e2b 2017-10-21) gives:

error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
 --> ...\test.rs:4:17
  |
4 |     const BASE: T = T::FOO;
  |                 ^ use of type variable from outer function

error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
 --> ...\test.rs:4:21
  |
4 |     const BASE: T = T::FOO;
  |                     ^^^^^^ use of type variable from outer function

error: aborting due to 2 previous errors
@TimNN
Copy link
Contributor

TimNN commented Oct 22, 2017

Could you explain why you think the error message is bad? AFAIK this is intended behaviour: Items (such as const definitions) inside of function are shared between all monomorphized instances of a function and as such cannot use type parameters from the function (you shouldn't need associated types to trigger this error).

@leonardo-m
Copy link
Author

I see. (The semantics of D language I'm used to create a const instance for each template instantiation). The "outer function" confused me, because there is no outer function. If you think the wording of the error message is acceptable then I will close this issue down.

@TimNN
Copy link
Contributor

TimNN commented Oct 22, 2017

Mh, the "outer function" wording seems to be indeed a bit tailored to functions-in-functions, we can keep this issue open I think.

@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Oct 22, 2017
@estebank estebank added the WG-diagnostics Working group: Diagnostics label Oct 31, 2017
@estebank
Copy link
Contributor

Current output is buggy:

error[E0401]: can't use generic parameters from outer function
 --> file.rs:4:17
  |
3 | fn bar<T: Foo>(n: T) {
  |    --- - type variable from outer function
  |    |
  |    try adding a local generic parameter in this method instead
4 |     const BASE: T = T::FOO;
  |                 ^ use of generic parameter from outer function

error[E0401]: can't use generic parameters from outer function
 --> file.rs:4:21
  |
3 | fn bar<T: Foo>(n: T) {
  |    --- - type variable from outer function
  |    |
  |    try adding a local generic parameter in this method instead
4 |     const BASE: T = T::FOO;
  |                     ^^^^^^ use of generic parameter from outer function

It should be closer to the following

error[E0401]: can't use generic parameters for `const`s
 --> file.rs:4:17
  |
3 | fn bar<T: Foo>(n: T) {
  |        - type variable
4 |     const BASE: T = T::FOO;
  |                 ^ can't use generic parameter for a `const`

error[E0401]: can't use generic parameters for `const`s
 --> file.rs:4:21
  |
3 | fn bar<T: Foo>(n: T) {
  |        - type variable from outer function
4 |     const BASE: T = T::FOO;
  |                     ^^^^^^ can't use generic parameter for a `const`

@spunit262
Copy link
Contributor

error[E0401]: can't use generic parameters from outer function
 --> src/lib.rs:3:14
  |
1 | fn foo<T>() {
  |        - type variable from outer function
2 |     //fn bar
  |          --- try adding a local generic parameter in this method instead
3 |     type U = T;
  |              ^ use of generic parameter from outer function

It appears to just blindly do a search for fn as it suggest annotating a comment if it find it in one.

@estebank
Copy link
Contributor

This is now fixed.

@Timmmm
Copy link
Contributor

Timmmm commented Jul 16, 2020

I still get this, on Rust 1.43:

pub fn encode_rle_to<T: UnsignedInt>(input: &[T], output: &mut impl Output<T>) -> Result<()> {
  const MAX_LEN: usize = (T::MAX).into() >> 1;
error[E0401]: can't use generic parameters from outer function
  --> rle/src/encode.rs:15:27
   |
14 | pub fn encode_rle_to<T: UnsignedInt>(input: &[T], output: &mut impl Output<T>) -> Result<()> {
   |                      - type parameter from outer function
15 |   const MAX_LEN: usize = (T::MAX).into() >> 1;
   |                           ^^^^^^ use of generic parameter from outer function

@Timmmm
Copy link
Contributor

Timmmm commented Jul 16, 2020

Can I suggest a clearer message: "const definitions inside generic functions are shared and therefore cannot use generic parameters from the function. Consider using let instead."

@dwrensha
Copy link
Contributor

dwrensha commented Apr 17, 2021

AFAIK this is intended behaviour: Items (such as const definitions) inside of function are shared between all monomorphized instances of a function and as such cannot use type parameters from the function

Is there any fundamental reason for this limitation? I recently found myself wanting to use associated constants in const definitions, as part of follow-on work to capnproto/capnproto-rust#225

@estebank
Copy link
Contributor

@dwrensha This is something that you should probably bring up to T-lang, maybe on Zulip?

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
@estebank
Copy link
Contributor

Current output:

error[E0401]: can't use generic parameters from outer item
 --> src/main.rs:4:17
  |
3 | fn bar<T: Foo>(n: T) {
  |        - type parameter from outer item
4 |     const BASE: T = T::FOO;
  |                 ^ use of generic parameter from outer item

error[E0401]: can't use generic parameters from outer item
 --> src/main.rs:4:21
  |
3 | fn bar<T: Foo>(n: T) {
  |        - type parameter from outer item
4 |     const BASE: T = T::FOO;
  |                     ^^^^^^ use of generic parameter from outer item

@leonardo-m
Copy link
Author

I think now it's OK to close this issue down.

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants