Skip to content

Allow dyn trait + Sized  #79701

@CreepySkeleton

Description

@CreepySkeleton

I would expect this code to work:

let a: Box<dyn Any + Sized> = Box::new(5);
error[E0225]: only auto traits can be used as additional traits in a trait object
  --> src/main.rs:22:26
   |
22 |     let a: Box<dyn Any + Sized> = Box::new(5);
   |                    ---   ^^^^^ additional non-auto trait
   |                    |
   |                    first non-auto trait
   |
   = help: consider creating a new trait with all of these as super-traits and using that trait here instead: `trait NewTrait: Any + Sized {}`
   = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

I realize that trait objects don't work with multiple traits because of complications with multiple vtables. Sized is also technically not an auto trait, but for all intends and purposes here (no methods in Sized - no vtables) it should be equivalent to Send or Sync or whatever.

A somewhat more realistic example:

struct Arg {
    typed_validator: Box<dyn Fn(&str) -> Result<dyn Any + Sized, Error>>
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions