-
Notifications
You must be signed in to change notification settings - Fork 35
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
Workaround UndefVarError: A not defined
#156
base: master
Are you sure you want to change the base?
Conversation
The above error happens when I do using ForwardDiff2: D; using ModelingToolkit
@variables a[1:3]
D(D(prod))(a)' |
I would like to understand this a bit better first, which of the two types are undefined? This needs an upstream bug-report as well. The failing tests are #155 |
With diff --git a/src/context.jl b/src/context.jl
index d02a16d..d98b74f 100644
--- a/src/context.jl
+++ b/src/context.jl
@@ -451,7 +451,9 @@ See also: [`canrecurse`](@ref), [`overdub`](@ref), [`recurse`](@ref), [`prehook
# to `Core.apply_type`. In the future, it would be best for Julia's compiler to better handle
# varargs calls to such functions with type arguments, or at least provide a better way to
# force specialization on the type arguments.
-@inline call(::ContextUntagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = @isdefined(A) && @isdefined(B) ? f(A, B) : f(a, b)
+@inline function call(::ContextUntagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B}
+ @isdefined(A) && @isdefined(B) ? f(A, B) : (Main.__a[] = a; f(@show(a), b))
+end
@inline call(::ContextTagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = @isdefined(A) && @isdefined(B) ? f(A, B) : f(a, b)
""" I got julia> using ForwardDiff2: D; using ModelingToolkit
julia> @variables v[1:3];
julia> __a = Ref{Any}();
julia> D(D(prod))(v)';
a = ForwardDiff2.Dual{ForwardDiff2.Tag{Nothing},V,P} where P
julia> dump(__a[])
UnionAll
var: TypeVar
name: Symbol P
lb: Core.TypeofBottom Union{}
ub: Any
body: ForwardDiff2.Dual{ForwardDiff2.Tag{Nothing},V,P} <: Real
value::V
partials::P That is super weird... |
|
@inline call(::ContextUntagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = @isdefined(A) && @isdefined(B) ? f(A, B) : f(a, b) | ||
@inline call(::ContextTagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = @isdefined(A) && @isdefined(B) ? f(A, B) : f(a, b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@inline call(::ContextUntagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = @isdefined(A) && @isdefined(B) ? f(A, B) : f(a, b) | |
@inline call(::ContextTagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = @isdefined(A) && @isdefined(B) ? f(A, B) : f(a, b) | |
@inline call(::ContextUntagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = f(a, b) | |
@inline call(::ContextTagged, f::typeof(Core.apply_type), a::Type{A}, b::Type{B}) where {A,B} = f(a, b) |
where {A}
preserves only the equality of a type and not its identity. Thus, especially in a context like this, you must always use the value, to avoid corrupting the meaning of the expression. For example, the former may equate Dict{K,V} where {K,V}
with Dict{K,V} where {V,K}
since they are equivalent types, but the call to apply_type
should yield very different results for those two based upon their distinct identity.
This PR fixes the following error, though, maybe something more sinister is going on. Unfortunately, I cannot find an MWE. I understand that this PR is not perfect. However, for the time being, I wonder if this PR can be merged and tagged since it is blocking the release of ForwardDiff2.