rust-lang / rust-lang/rust

Associated function from super traits not accessible via sub traits

Open
#124,438 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-diagnostics A-dyn-trait A-resolve D-lack-of-suggestion E-needs-bisection P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Rust Playground example with rustc 1.77.2 and compare the behavior of Sub::assoc() with Super::assoc(). Investigate the compiler's trait-associated function resolution and diagnostics; done means the intended call through the subtrait compiles or produces a focused, correct diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.