Skip to content
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

getproperty(::Type,f::Symbol): type assert when f === :parameters #57630

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion base/Base_compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ macro _boundscheck() Expr(:boundscheck) end
# with ambiguities by defining a few common and critical operations
# (and these don't need the extra convert code)
getproperty(x::Module, f::Symbol) = (@inline; getglobal(x, f))
getproperty(x::Type, f::Symbol) = (@inline; getfield(x, f))
function getproperty(x::Type, f::Symbol)
@inline
if f === :parameters
# Ensure `type.parameters` has perfect type inference even when `type` is
# not completely determined at compile time.
x = x::DataType
end
getfield(x, f)
end
setproperty!(x::Type, f::Symbol, v) = error("setfield! fields of Types should not be changed")
setproperty!(x::Array, f::Symbol, v) = error("setfield! fields of Array should not be changed")
getproperty(x::Tuple, f::Int) = (@inline; getfield(x, f))
Expand Down
6 changes: 6 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,12 @@ function issue23618(a::AbstractVector)
end
@test Base.infer_return_type(issue23618, (Vector{Int},)) == Vector{Set{Int}}

@testset "type inference for getting the `parameters` field of a type" begin
let f(x) = x.parameters
@test Base.infer_return_type(f, Tuple{DataType}) === Base.infer_return_type(f, Tuple{Type})
end
end

# ? syntax
@test (true ? 1 : false ? 2 : 3) == 1

Expand Down