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

add constant inference of isdefined #22304

Merged
merged 2 commits into from
Jun 11, 2017
Merged

add constant inference of isdefined #22304

merged 2 commits into from
Jun 11, 2017

Conversation

JeffBezanson
Copy link
Member

A bit taken from #16580.

@JeffBezanson JeffBezanson added the compiler:inference Type inference label Jun 9, 2017
return Bool
end
if isleaftype(a1)
if a1 <: Array # TODO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TODO can be removed now.

return Bool
end
if isleaftype(a1)
if a1 <: Array # TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is deprecated now, should just remove this case along with the TODO

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll still need a case for this --- what will isdefined return on an Array when the deprecation is removed? Will it throw an error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be an error. Until the implementation of Array changes, in which case it might return a Bool again. In either case it should be a subtype of Bool.

if isType(a1)
return Bool
end
if isleaftype(a1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems much stricter than necessary. I think isa(a1, DataType) && !a1.abstract should be sufficient

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can do a1 = unwrap_unionall(a1) first (before the isType) check as well.

end
if isleaftype(a1)
if a1 <: Array # TODO
elseif a1 === Module # TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the code for this case on https://github.com/JuliaLang/julia/pull/22281/files#diff-c0d983631c937ee5acfd0137cd7332deR2082. It's simple enough it's probably worth just including in this PR:

sym = args[2]
Symbol <: widenconst(sym) || return Bottom
if isa(sym, Const) && isa(sym.val, Symbol) && isdefined(a1, sym.val)
    return Const(true)
end

end
Bool
end
add_tfunc(isdefined, 1, IInf, isdefined_tfunc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upper end is actually 2, not IInf (noticed this on my PR https://github.com/JuliaLang/julia/pull/22281/files#diff-c0d983631c937ee5acfd0137cd7332deR545). (although when we remove the deprecations after v0.7, this'll just become constant at 2 args anyways)

if isa(val, Symbol)
idx = fieldindex(a1, val, false)
elseif isa(val, Int)
idx = val::Int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type assert is redundant with branch on isa Int and declaration of idx as being of type Int

@test Base.return_types(f, (Tuple{Int,Int},)) == Any[Int]
@test Base.return_types(f, (Tuple{Int,},)) == Any[String]
end
let f(x) = isdefined(x,:re) ? 1 : ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces after commas

end
let f(x) = isdefined(x,:re) ? 1 : ""
@test Base.return_types(f, (Complex64,)) == Any[Int]
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need test coverage for:

  • isdefined with a symbol that isn't a valid field name
  • isdefined with a non-symbol-or-integer
  • isdefined with x is an: SimpleVector, Module, Type, Array, Symbol
  • isdefined on a type where the field is not necessarily assigned
  • isdefined on an abstract type
  • isdefined on a UnionAll type

@JeffBezanson
Copy link
Member Author

Comments addressed.

return Bool
end
a1 = unwrap_unionall(a1)
if isa(a1, DataType) && !a1.abstract
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also need to verify that !isa(a1.parameters[end], Vararg)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good one --- that actually exposed a separate bug, fix added here.

@JeffBezanson JeffBezanson force-pushed the jb/isdefined_tfunc branch 2 times, most recently from 9e1b5cc to ca8b223 Compare June 10, 2017 00:19
@JeffBezanson JeffBezanson merged commit 6319a3e into master Jun 11, 2017
@JeffBezanson JeffBezanson deleted the jb/isdefined_tfunc branch June 11, 2017 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:inference Type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants