Associated function from super traits not accessible via sub traits #124438
Open
Description
opened on Apr 27, 2024
I tried this code:
trait Super {
fn assoc() -> Self;
}
trait Sub: Super {}
fn f<T: Sub>() -> T {
// doesn't work
Sub::assoc()
// works
// Super::assoc()
}
(play)
I expected to see this happen: the code compiles with either Sub::assoc()
or Super::assoc()
.
Instead, this happened: five compilation errors which is very confusing:
error[E0782]: trait objects must include the `dyn` keyword
--> src/lib.rs:9:5
|
9 | Sub::assoc()
| ^^^
|
help: add `dyn` keyword before this trait
|
9 | <dyn Sub>::assoc()
| ++++ +
error[E0038]: the trait `Sub` cannot be made into an object
--> src/lib.rs:9:5
|
9 | Sub::assoc()
| ^^^ `Sub` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> src/lib.rs:2:8
|
2 | fn assoc() -> Self;
| ^^^^^ ...because associated function `assoc` has no `self` parameter
...
5 | trait Sub: Super {}
| --- this trait cannot be made into an object...
help: consider turning `assoc` into a method by giving it a `&self` argument
|
2 | fn assoc(&self) -> Self;
| +++++
help: alternatively, consider constraining `assoc` so it does not apply to trait objects
|
2 | fn assoc() -> Self where Self: Sized;
| +++++++++++++++++
error[E0308]: mismatched types
--> src/lib.rs:9:5
|
7 | fn f<T: Sub>() -> T {
| - -
| | |
| | expected `T` because of return type
| | help: consider using an impl return type: `impl Sub`
| expected this type parameter
8 | // doesn't work
9 | Sub::assoc()
| ^^^^^^^^^^^^ expected type parameter `T`, found `dyn Sub`
|
= note: expected type parameter `T`
found trait object `dyn Sub`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
error[E0038]: the trait `Sub` cannot be made into an object
--> src/lib.rs:9:5
|
9 | Sub::assoc()
| ^^^^^^^^^^^^ `Sub` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> src/lib.rs:2:8
|
2 | fn assoc() -> Self;
| ^^^^^ ...because associated function `assoc` has no `self` parameter
...
5 | trait Sub: Super {}
| --- this trait cannot be made into an object...
help: consider turning `assoc` into a method by giving it a `&self` argument
|
2 | fn assoc(&self) -> Self;
| +++++
help: alternatively, consider constraining `assoc` so it does not apply to trait objects
|
2 | fn assoc() -> Self where Self: Sized;
| +++++++++++++++++
error[E0277]: the size for values of type `dyn Sub` cannot be known at compilation time
--> src/lib.rs:9:5
|
9 | Sub::assoc()
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Sub`
= note: the return type of a function must have a statically known size
Meta
rustc
version: 1.77.2.
Metadata
Assignees
Labels
Area: Associated items (types, constants & functions)Area: Messages for errors, warnings, and lintsArea: Name/path resolution done by `rustc_resolve` specificallyArea: trait objects, vtable layoutCategory: This is a bug.Diagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: Too much output caused by a single piece of incorrect code.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity