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

fieldtype returns too-general type #18037

Closed
eschnett opened this issue Aug 15, 2016 · 5 comments · Fixed by #18045
Closed

fieldtype returns too-general type #18037

eschnett opened this issue Aug 15, 2016 · 5 comments · Fixed by #18045
Labels
regression Regression in behavior compared to a previous version
Milestone

Comments

@eschnett
Copy link
Contributor

In the function below, Julia returns Any for the field type of a tuple, although Julia actually knows that the field has type Int:

function f{T}(::T)
    @show T
    @show [fieldtype(T, i) for i in 1:nfields(T)]
    nothing
end
f(((nothing, nothing), (nothing, 4)))

I see the output:

T = Tuple{Tuple{Void,Void},Tuple{Void,Int64}}
[fieldtype(T,i) for i = 1:nfields(T)] = DataType[Tuple{Void,Any},Tuple{Void,Any}]

This is different from Julia 0.5, which returns the fully specific type:

T = Tuple{Tuple{Void,Void},Tuple{Void,Int64}}
[fieldtype(T,i) for i = 1:nfields(T)] = DataType[Tuple{Void,Void},Tuple{Void,Int64}]

#12793 might be related.

@vtjnash
Copy link
Member

vtjnash commented Aug 15, 2016

backport is pending #17953

@vtjnash vtjnash closed this as completed Aug 15, 2016
@eschnett
Copy link
Contributor Author

@vtjnash That went the other way around than I expected. I expected 0.6 to return the more specific type, not 0.5 to return a less specific type.

Put differently: How can I access the specific type information that is output by @show?

@vtjnash vtjnash reopened this Aug 15, 2016
@vtjnash
Copy link
Member

vtjnash commented Aug 15, 2016

ok, I read that backwards: probably /broken/ by #17953

@JeffBezanson JeffBezanson added the regression Regression in behavior compared to a previous version label Aug 15, 2016
@JeffBezanson JeffBezanson added this to the 0.6.0 milestone Aug 15, 2016
@eschnett
Copy link
Contributor Author

Here is a work-around (it's not pretty):

@generated function ft{T, i}(x::T, ::Type{Val{i}})
    :($(fieldtype(x, i)))
end

function f{T}(x::T)
    @show T
    @show [fieldtype(T, i) for i in 1:nfields(T)]
    @show [ft(x, Val{i}) for i in 1:nfields(T)]
    nothing
end

f(((nothing, nothing), (nothing, 4)))

This outputs

T = Tuple{Tuple{Void,Void},Tuple{Void,Int64}}
[fieldtype(T,i) for i = 1:nfields(T)] = DataType[Tuple{Void,Any},Tuple{Void,Any}]
[ft(x,Val{i}) for i = 1:nfields(T)] = DataType[Tuple{Void,Void},Tuple{Void,Int64}]

@vtjnash
Copy link
Member

vtjnash commented Aug 15, 2016

yes, the @generated ensures that the compiler can't optimize it

@vtjnash vtjnash modified the milestones: 0.5.0, 0.6.0 Aug 16, 2016
vtjnash added a commit that referenced this issue Aug 16, 2016
the getfield_tfunc was missing a test for whether
the fields were all equivalent before concluding
that the result type was exact

fixes the fix #17953
fix #18037
vtjnash added a commit that referenced this issue Aug 16, 2016
the getfield_tfunc was missing a test for whether
the fields were all equivalent before concluding
that the result type was exact

fixes the fix #17953
fix #18037
tkelman pushed a commit that referenced this issue Aug 20, 2016
the getfield_tfunc was missing a test for whether
the fields were all equivalent before concluding
that the result type was exact

fixes the fix #17953
fix #18037

(cherry picked from commit 06a8d89)
ref #18045
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
the getfield_tfunc was missing a test for whether
the fields were all equivalent before concluding
that the result type was exact

fixes the fix JuliaLang#17953
fix JuliaLang#18037
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression Regression in behavior compared to a previous version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants