Skip to content

Close inference failure #14919

Closed
Closed
@Kimundi

Description

@Kimundi

Error:

<anon>:51:16: 51:83 error: cannot determine a type for this bounded type parameter: unconstrained type
<anon>:51     assert_eq!(match_indices(s, |c: char| c == 'b').collect::<Vec<(uint, uint)>>(),
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<std macros>:1:1: 14:2 note: in expansion of assert_eq!
<anon>:51:5: 52:40 note: expansion site
playpen: application terminated with error code 101

Code:

trait Matcher {
    fn next_match(&mut self) -> Option<(uint, uint)>;
}
struct CharPredMatcher<'a, 'b> {
    str: &'a str,
    pred: |char|:'b -> bool
}
impl<'a, 'b> Matcher for CharPredMatcher<'a, 'b> {
    fn next_match(&mut self) -> Option<(uint, uint)> {
        None
    }
}

/////////////////////////

trait IntoMatcher<'a, T> {
    fn into_matcher(self, &'a str) -> T;
}
impl<'a, 'b> IntoMatcher<'a, CharPredMatcher<'a, 'b>> for |char|:'b -> bool {
    fn into_matcher(self, s: &'a str) -> CharPredMatcher<'a, 'b> {
        CharPredMatcher {
            str: s,
            pred: self
        }
    }
}

/////////////////////////

struct MatchIndices<M> {
    matcher: M
}

impl<M: Matcher> Iterator<(uint, uint)> for MatchIndices<M> {
    fn next(&mut self) -> Option<(uint, uint)> {
        self.matcher.next_match()
    }
}

fn match_indices<'a, M, T: IntoMatcher<'a, M>>(s: &'a str, from: T) -> MatchIndices<M> {
    let string_matcher = from.into_matcher(s);
    MatchIndices { matcher: string_matcher }
}

/////////////////////////

fn main() {
    let s = "abcbdef";
    assert_eq!(match_indices(s, |c: char| c == 'b').collect::<Vec<(uint, uint)>>(),
               vec![(1u, 2u), (3, 4)]);
}

It works if the closure gets rooted in its own variable like this:

fn main() {
    let s = "abcbdef";
    let f = |c: char| c == 'b';
    assert_eq!(match_indices(s, f).collect::<Vec<(uint, uint)>>(),
               vec![(1u, 2u), (3, 4)]);
}

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions