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

Resolution problem with associated types with defaults #26681

Closed
arielb1 opened this issue Jun 30, 2015 · 2 comments · Fixed by #61812
Closed

Resolution problem with associated types with defaults #26681

arielb1 opened this issue Jun 30, 2015 · 2 comments · Fixed by #61812
Assignees
Labels
A-associated-items Area: Associated items (types, constants & functions) A-type-system Area: Type system C-bug Category: This is a bug. F-associated_type_defaults `#![feature(associated_type_defaults)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@arielb1
Copy link
Contributor

arielb1 commented Jun 30, 2015

STR

#![feature(associated_consts)]

trait Foo {
    type Bar;
}

#[cfg(not(works))]
trait Baz {
    type Fv: Foo = u8;
    const C: <Self::Fv as Foo>::Bar = 6665;
}

#[cfg(works)]
trait Baz {
    type Fv: Foo = u8;
    const C: <Self::Fv as Foo>::Bar;
}

Results

<anon>:9:15: 9:23 error: failed to resolve. Use of undeclared type or module `Self`
<anon>:9     const C: <Self::Fv as Foo>::Bar = 6665;
                       ^~~~~~~~
<anon>:9:15: 9:23 error: use of `Self` outside of an impl or trait
<anon>:9     const C: <Self::Fv as Foo>::Bar = 6665;
                       ^~~~~~~~
error: aborting due to 2 previous errors

Removing the default (#[cfg(works)]) removes the error. cc @quantheory

@steveklabnik steveklabnik added the A-type-system Area: Type system label Jul 6, 2015
@quantheory
Copy link
Contributor

This is at least blocked on the generic const RFC, though I do intend to take a crack at implementing that, or at least the groundwork, in the coming weeks.

On top of that, handling this example probably requires some changes both to the code and to how we deal with casts/coercion in constant expressions. Right now, I think that unsuffixed literals always have a specific integer type inferred. But you want the type here to be inferred as <Self::Fv as Foo>::Bar, which is a projection, and not even required to be an integer type. To handle this, we would need some bound on Bar that means "an integer literal can be cast to this type at compile time".

@nagisa nagisa added the A-associated-items Area: Associated items (types, constants & functions) label May 16, 2016
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 13, 2019
@jonas-schievink
Copy link
Contributor

Updated code:

#![feature(associated_type_defaults)]
#![feature(associated_consts)]

trait Foo {
    type Bar;
}

trait Baz {
    type Fv: Foo = u8;
    const C: <Self::Fv as Foo>::Bar = 6665;
}

Fails to compile with:

error[E0308]: mismatched types
  --> src/lib.rs:10:39
   |
10 |     const C: <Self::Fv as Foo>::Bar = 6665;
   |                                       ^^^^ expected associated type, found integer
   |
   = note: expected type `<<Self as Baz>::Fv as Foo>::Bar`
              found type `{integer}`

Removing the default value of the constant makes it build. According to RFC 2532, I believe this is working as expected: Trait items may not assume that assoc. type defaults hold.

@jonas-schievink jonas-schievink added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jun 14, 2019
@jonas-schievink jonas-schievink added the F-associated_type_defaults `#![feature(associated_type_defaults)]` label Sep 14, 2019
@jonas-schievink jonas-schievink added the requires-nightly This issue requires a nightly compiler in some way. label Jan 4, 2020
@bors bors closed this as completed in 3a0d106 Feb 26, 2020
@fmease fmease added A-type-system Area: Type system and removed A-type-system Area: Type system labels Dec 21, 2024
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-type-system Area: Type system C-bug Category: This is a bug. F-associated_type_defaults `#![feature(associated_type_defaults)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants