Inference bug when using nested generators?

sum(x^2 for x in f(5)) is inferable while sum(x for x in f(5)) isn’t. Both use the most generic sum(a; kw...) = sum(identity, a; kw...) in reduce.jl, and I followed the trail through mapreduce, mapfoldl, mapfoldl_impl, and a recursive _xfadjoint. The type instability seems to be rooted in MappingRF’s 2nd type parameter not being inferred in _xfadjoint for the x^2 case.

I think it runs Base._xfadjoint(Base.BottomRF(Base.add_sum), Base.Generator(identity, (x^2 for x in f(5)) ) ). The x^2 case returns a MappingRF instance that contains another MappingRF instance, while the x case returns an unnested MappingRF instance. Test.@inferred is able to tell that the x^2 case is not inferable, but @code_warntype prints blue text for the call’s return type, a weird discrepancy I’ve noticed before in much stranger circumstances.

1 Like