Skip to content

supertraits do not resolve correctly for dynamic dispatch trait bounds #78700

@drahnr

Description

@drahnr

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7e579f79329f28e5441b0291d22d60f9

where X is the supertrait in question.

use std::fmt;

pub trait X: 'static + fmt::Debug + fmt::Display + Send + Sync {}

impl<T: 'static + fmt::Debug + fmt::Display + Send + Sync> X for T {}


#[derive(Debug)]
enum Skippy {
    V(Box<dyn X>),
    Alpha,
    Beta,
}

impl fmt::Display for Skippy {
 	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		writeln!(fmt, "yak")
	}
}


// commenting out either of them makes is fail to compile

// impl From<Box<dyn X + Send + Sync>> for Skippy {
// 	fn from(e: Box<dyn X + Send + Sync>) -> Self {
// 		Self::V(e)
// 	}
// }


impl From<Box<dyn X>> for Skippy {
	fn from(e: Box<dyn X>) -> Self {
 		Self::V(e)
	}
}


#[inline(never)]
fn alpha() -> Box<dyn X> {
    Box::new(Skippy::Alpha)
}


#[inline(never)]
fn beta() -> Box<dyn X + Send + Sync + 'static> {
    Box::new(Skippy::Beta)
}

fn main() {
    Skippy::from(alpha());
    Skippy::from(beta());
}

I expected to see this happen: explanation

Work fine with just one From<Box<dyn X>> impl, since the traits are already included in the super trait.

Instead, this happened: explanation

Requirement to impl for multiple summed trait bounds.

Meta

rustc --version --verbose:

1.47.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.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