Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close inference failure #14919

Closed
Kimundi opened this issue Jun 15, 2014 · 2 comments · Fixed by #17721
Closed

Close inference failure #14919

Kimundi opened this issue Jun 15, 2014 · 2 comments · Fixed by #17721
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@Kimundi
Copy link
Member

Kimundi commented Jun 15, 2014

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

@Kimundi Kimundi changed the title cannot determine a type for this bounded type parameter with a closure Close inference failure Jun 16, 2014
@nikomatsakis
Copy link
Contributor

I don't know precisely what's going on but I do know that we do some funky things with closure types. Probably this is an interaction with our over-reliance on expected types somehow, though it's not yet quite obvious to me.

@ghost
Copy link

ghost commented Oct 1, 2014

This is fine now but needs a test case.

@alexcrichton alexcrichton added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants