Skip to content

Specialization works only if type annotation is provided #67918

Open
@kdy1

Description

@kdy1

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<O>, 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<O>
    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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions