You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#![feature(unboxed_closures)]traitIteratorExt2<A>{fninspect2<F>(self,f:F) -> Inspect<A,Self,F>whereF:FnMut(&A);}structInspect<A,I,F>whereI:Iterator<A>,F:FnMut(&A){iter:I,f:F,}fnmain(){let xs = [1u,2,3,4];// Triggerletmut n = 0;// Err//let mut n = 0u; // Oklet ys = xs.iter().map(|&x| x).inspect2(|_| n += 1)//~^ error: unable to infer enough type information about `closure`; type annotations required//.inspect2(|&mut: _| n += 1) // This doesn't work either (same error as above).collect::<Vec<uint>>();// NOTE: This constrains `n` to be `uint`assert_eq!(n, xs.len());assert_eq!(xs[], ys[]);}impl<A,I>IteratorExt2<A>forIwhereI:Iterator<A>{fninspect2<F>(self,f:F) -> Inspect<A,I,F>whereF:FnMut(&A){Inspect{iter:self,f: f,}}}impl<A,I,F>Inspect<A,I,F>whereI:Iterator<A>,F:FnMut(&A){#[inline]fndo_inspect(&mutself,elt:Option<A>) -> Option<A>{match elt {Some(ref a) => (self.f)(a),None => ()}
elt
}}impl<A,I,F>Iterator<A>forInspect<A,I,F>whereI:Iterator<A>,F:FnMut(&A){fnnext(&mutself) -> Option<A>{let next = self.iter.next();self.do_inspect(next)}fnsize_hint(&self) -> (uint,Option<uint>){self.iter.size_hint()}}
The code under main is actually a test (see test_inspect in libcoretest/iter.rs) that was passing with the boxed version of Inspect, but after moving Inspect to unboxed closures (as seen above) compiling that test now errors due to a type inference failure.
To fix the compilation n had to be type annotated: let mut n = 0u, however this shouldn't be necessary because assert_eq!(n, xs.len()) should already force n to be uint.
TL;DR This should compile because it has enough type information, but for some reason it doesn't.
It's calling .collect() that triggers the error, it goes away if you provide a hint for n after calling inspect but before collect (or any other iterator method).
AFAICT method resolution shouldn't be able to tell apart unboxed closures so I don't understand why it would be trying to resolve the type of its captures.
My version of main:
fnmain(){let n = 0;let it = Some(1u).into_iter().inspect2(|_| {n;});//~^ error: unable to infer enough type information about `Inspect<uint, core::option::Item<uint>, closure>`; type annotations required// Trigger// let _: uint = n; // Ok
it.count();let _:uint = n;// Err}
STR
Version
#19449 on top of 21ba1d5
The code under main is actually a test (see
test_inspect
inlibcoretest/iter.rs
) that was passing with the boxed version ofInspect
, but after movingInspect
to unboxed closures (as seen above) compiling that test now errors due to a type inference failure.To fix the compilation
n
had to be type annotated:let mut n = 0u
, however this shouldn't be necessary becauseassert_eq!(n, xs.len())
should already forcen
to beuint
.TL;DR This should compile because it has enough type information, but for some reason it doesn't.
cc @nikomatsakis
The text was updated successfully, but these errors were encountered: