Specialization works only if type annotation is provided · Issue #67918 · rust-lang/rust (original) (raw)

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a26a9e2af3acda5b9911458a6f54a72d

This trait implementation

impl<T, V, O, E> Validate<[T]> for V where Self: Validate<T, Output = Result<O, E>>, { type Output = Result<Vec, E>;

fn validate(&mut self, nodes: &[T]) -> Self::Output {
    nodes.iter().map(|node| self.validate(node)).collect()
}

}

does not like a method defined as

impl Analyzer { /// Validates and store errors if required. pub fn check<T, O>(&mut self, node: &T) -> Option where Self: Validate<T, Output = Result<O, Error>>, { let res: Result<O, _> = self.validate(node); match res { Ok(v) => Some(v), Err(..) => { // handle error None } } } }

However, it works if type annotation is provided

fn main() { let mut a = Analyzer; let expr = Expr; // Uncomment this to see impl for [T] explodes // a.check(&expr);

// This works without error
a.check::<Expr, ()>(&expr);

}