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

When the return type of a default trait method returns an associated type (that has a default), there's always a mismatched type error #106968

Closed
makoConstruct opened this issue Jan 17, 2023 · 3 comments · Fixed by #107304
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@makoConstruct
Copy link

playground

#![feature(
    associated_type_defaults,
)]


pub trait Trait {
    type Res = isize;
    fn f() -> Self::Res {
        2
    }
}

At line of '2': error[E0308]: mismatched types expected associated type, found integer

@makoConstruct makoConstruct added the C-bug Category: This is a bug. label Jan 17, 2023
@makoConstruct makoConstruct changed the title When the return type of a default trait method returns an associated type (that has a default), there's always mismatched type error When the return type of a default trait method returns an associated type (that has a default), there's always a mismatched type error Jan 17, 2023
@Noratrieb
Copy link
Member

This is correct, as I could write the following impl:

impl Trait for () {
  type Res = ();
}

Now, Self::Res is (), which is not an integer.

@makoConstruct
Copy link
Author

makoConstruct commented Jan 17, 2023

Yeah, well, it's correct to reject the code at least, it just occurred to me that default associated types shouldn't be accessible from default trait methods (broadly: the trait method might be inherited by impls where the associated type has changed and violated the assumptions that the default method made about what the type would be.)

I guess this is an error reporting problem, then. It took me like, maybe 3 hours to figure out what was actually going on. The message it gives in this situation is basically a falsehood. I'm not sure what to recommend instead. At minimum, a note: Default trait methods cannot access associated default types. See explanation: <link to explanation>, this note would only need to be shown in type errors concerning associated types that have defaults, coming from default trait methods.

@Noratrieb Noratrieb added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 17, 2023
@makoConstruct
Copy link
Author

makoConstruct commented Jan 17, 2023

As to how I got into that situation: I wanted to define a default associated type that I actually didn't expect impls to ever override, because it was just a sort of convenience function of other associated types (that impls would define).

pub trait Graph<K, V, E> {
    type Index: Debug + Display;
    type Res<T> = Result<T, GraphError<Self::Index>>;
    ...
}

Of course, that can run into the same problem, but the code that I had written, where the error was firing, wasn't running afoul of that problem, so it didn't occur to me that the problem existed, or that it was actually about it.

@Noratrieb Noratrieb self-assigned this Jan 17, 2023
@estebank estebank removed the C-bug Category: This is a bug. label Jan 17, 2023
@bors bors closed this as completed in 3aeafca Jan 26, 2023
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 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