@@ -139,15 +139,45 @@ end
139
139
const nonfunction_mt = typename (SimpleVector). mt
140
140
141
141
function get_compileable_sig (method:: Method , @nospecialize (atypes), sparams:: SimpleVector )
142
- isa (atypes, DataType) || return Nothing
142
+ isa (atypes, DataType) || return nothing
143
143
mt = ccall (:jl_method_table_for , Any, (Any,), atypes)
144
144
mt === nothing && return nothing
145
145
return ccall (:jl_normalize_to_compilable_sig , Any, (Any, Any, Any, Any),
146
146
mt, atypes, sparams, method)
147
147
end
148
148
149
+ # eliminate UnionAll vars that might be degenerate due to having identical bounds,
150
+ # or a concrete upper bound and appearing covariantly.
151
+ function subst_trivial_bounds (@nospecialize (atypes))
152
+ if ! isa (atypes, UnionAll)
153
+ return atypes
154
+ end
155
+ v = atypes. var
156
+ if isconcretetype (v. ub) || v. lb === v. ub
157
+ return subst_trivial_bounds (atypes{v. ub})
158
+ end
159
+ return UnionAll (v, subst_trivial_bounds (atypes. body))
160
+ end
161
+
162
+ # If removing trivial vars from atypes results in an equivalent type, use that
163
+ # instead. Otherwise we can get a case like issue #38888, where a signature like
164
+ # f(x::S) where S<:Int
165
+ # gets cached and matches a concrete dispatch case.
166
+ function normalize_typevars (method:: Method , @nospecialize (atypes), sparams:: SimpleVector )
167
+ at2 = subst_trivial_bounds (atypes)
168
+ if at2 != = atypes && at2 == atypes
169
+ atypes = at2
170
+ sp_ = ccall (:jl_type_intersection_with_env , Any, (Any, Any), at2, method. sig):: SimpleVector
171
+ sparams = sp_[2 ]:: SimpleVector
172
+ end
173
+ return atypes, sparams
174
+ end
175
+
149
176
# get a handle to the unique specialization object representing a particular instantiation of a call
150
177
function specialize_method (method:: Method , @nospecialize (atypes), sparams:: SimpleVector , preexisting:: Bool = false , compilesig:: Bool = false )
178
+ if isa (atypes, UnionAll)
179
+ atypes, sparams = normalize_typevars (method, atypes, sparams)
180
+ end
151
181
if compilesig
152
182
new_atypes = get_compileable_sig (method, atypes, sparams)
153
183
new_atypes === nothing && return nothing
0 commit comments