Inference bug when using nested generators?

This eltype is just due to eltype(::Generator) falling back to the fallback definition. Adding a specialized version does not fix this, as I already mentioned above:

julia> Base.eltype(g::Base.Generator) = Union{Core.Compiler.return_types(g.f, (eltype(g.iter),))...}

julia> f(n) = (i^2 for i in 1:n)
f (generic function with 1 method)

julia> g(n) = sum(x^2 for x in f(n))
g (generic function with 1 method)

julia> @code_warntype g(3)
MethodInstance for g(::Int64)
  from g(n)
     @ Main REPL[3]:1
Arguments
  #self#::Core.Const(g)
  n::Int64
Locals
  #5::var"#5#6"
Body::Any
1 ─      (#5 = %new(Main.:(var"#5#6")))
│   %2 = #5::Core.Const(var"#5#6"())
│   %3 = Main.f(n)::Core.PartialStruct(Base.Generator{UnitRange{Int64}, var"#3#4"}, Any[Core.Const(var"#3#4"()), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])])
│   %4 = Base.Generator(%2, %3)::Core.PartialStruct(Base.Generator{Base.Generator{UnitRange{Int64}, var"#3#4"}, var"#5#6"}, Any[Core.Const(var"#5#6"()), Core.PartialStruct(Base.Generator{UnitRange{Int64}, var"#3#4"}, Any[Core.Const(var"#3#4"()), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])])])
│   %5 = Main.sum(%4)::Any
└──      return %5

However, calling eltype(f(3)) then subsequently does make this infer, without ever calling g.

Either way, specializing sum for Generator instead of going through sum(identity, gen) already fixes this. I’m not sure how much more general you want to go with this investigation though.

I’m inclined to think that this has to do with some invalidation/specialization that happens when g or f is actually called.