-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
type inference problem with captured type in closures #23618
Comments
|
Is this the same as #15276 ? |
Depending on what do you call the scope of #15276. Both of these are related to closures but the sources are quite different. All cases in #15276 are about the frontend not able to tell if a variable can be mutated in the closure/while the closure is executing so it creates a |
x-ref https://discourse.julialang.org/t/performance-issue-with-use-of-eltype/5764. That is, there is a secondary issue that performance is slow even with explicit typing due to type instability in the inner comprehension loop. |
A MWE is: julia> let f = Int
typeof(x -> f(x))
end
var"#51#52"{DataType} This comes up in julia> let f = Int ∘ identity
@inferred f(1)
end
ERROR: return type Int64 does not match inferred return type Any
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] top-level scope at REPL[71]:2 A simple workaround is: julia> begin
prefer_singleton_callable(::Type{T}) where T = SingletonCallable{T}()
prefer_singleton_callable(f) = f
struct SingletonCallable{T} end
(::SingletonCallable{T})(x) where T = T(x)
end;
julia> let f = prefer_singleton_callable(Int) ∘ identity
@inferred f(1)
end
1 Can this be done during lowering? |
I just stumbled upon this issue and am surprised that it is that old. |
If something seems important but has been sitting around for a long time, it's usually a safe bet that it's difficult. |
One of the few certain fixes is to allow lowering to call type inference. Currently lowering is written in lisp and is firmly "upstream" of type inference---we can't run type inference until code is presented in lowered form. If there isn't a simpler solution that would be truly reliable, I am not sure I can think of a bug fix that would require more re-engineering than this one. Care to tackle it, @schlichtanders? 😄 |
Seems this issue is almost fixed in #40985, just need a bit more. |
Yeah, I guess this one is a bit easier than #15276, which is the one that really annoys me. |
Very impressive indeed. Thanks for sharing this summary. I tried to optimize my package ExtensibleEffects.jl when I stumbled upon this, so I am more on the user side of things and won't have enough time and resources to improve Core Julia here ;-) |
The minimum working example doesn't trigger this anymore
but the initial issue seems to persist:
|
Still seems to be an issue |
@code_warntype
shows an inference failure inb
andc
.Changing to
c = Set{T}[Set{T}() for x in a]
fixesc
's inference issue.The text was updated successfully, but these errors were encountered: