Skip to content

better error message when using dyn type  #46683

Closed
@nikomatsakis

Description

@nikomatsakis

The error that you get when using a dyn type with a fn that expects a Sized type parameter is not particularly enlightening:

#![feature(dyn_trait)]

trait Something { }
impl Something for () { }

fn foo<T: Something>(_: &T) { }

fn main() {
    let x: &dyn Something = &();
    foo(x);
}

yields:

error[E0277]: the trait bound `Something: std::marker::Sized` is not satisfied
  --> src/main.rs:10:5
   |
10 |     foo(x);
   |     ^^^ `Something` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `Something`
   = note: required by `foo`

This came up when attempting to do some refactorings to use a dyn Trait where previously we had used a type parameter. I'm not exactly sure what sort of error we should report here -- but it seems like it'd be nice to talk about the definition site.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions