You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
internals: add _defaultctor function for defining ctors (JuliaLang#57317)
Deferring this part of defining the ctor code allows it to do some
value-based optimizations, without the awkwardness previously of needing
to do conditional lowering of all of the possible versions that might be
useful just from syntax transforms. This feels very like a generated
function: how it expands just the function body, except it is flipped so
that then we wrap the result in a single Method instead of being created
from a Method!
Avoids lowering inaccuracies where a variable appears but is not used.
These warnings are now prevented:
```julia
julia> struct Foo{T}
x::(T; Any)
end
WARNING: method definition for Foo at REPL[1]:2 declares type variable T but does not use it.
julia> struct Foo{T}
x::Union{Any, T}
end
WARNING: method definition for Foo at REPL[1]:2 declares type variable T but does not use it.
```
Avoids hitting JuliaLang#31542. This
lowering mistake is now avoided:
```julia
julia> struct Foo{T}
x::(Val{T} where T)
end
ERROR: syntax: invalid variable expression in "where" around REPL[1]:2
```
As a minor optimization, this avoids generating a `convert` call when
the user declares explicit `::Any` declarations, the same as if the user
didn't annotate the field type:
```julia
julia> struct Foo
x::Any
y::Int
z
end
julia> code_lowered(Foo)[2]
CodeInfo(
@ REPL[1]:2 within `unknown scope`
1 ─ %1 = builtin Core.fieldtype(#ctor-self#, 2)
│ %2 = y
│ @_4 = %2
│ %4 = builtin @_4 isa %1
└── goto mmtk#3 if not %4
2 ─ goto mmtk#4
3 ─ @_4 = Base.convert(%1, @_4)
4 ┄ %8 = @_4
│ %9 = %new(#ctor-self#, x, %8, z)
└── return %9
)
```
The outer/inner names might be a bit historical at this point (predating
where clauses allowing specifying them flexibly inside or outside of the
struct def): they are really exact-type-type&convert-args-from-any /
exact-arg-types&apply-type-from-args if named for precisely what they
do.
0 commit comments