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 introspection macros support for dot syntax #35522

Merged
merged 2 commits into from
Apr 24, 2020

Conversation

Liozou
Copy link
Member

@Liozou Liozou commented Apr 19, 2020

Fixes #28615, fixes #14637, closes #20620. Actually the last one was already fixed on master it seems, so I only added a test.

Broadcasting

This PR enables using the code_ macros with dot expressions, like so:

julia> @code_warntype max.(1 .+ 3, 5 - 7)
Variables
  #self#::Core.Compiler.Const(var"##dotfunction#282#116"(), false)
  x1::Int64
  x2::Int64
  x3::Int64

Body::Int64
1%1 = Base.broadcasted(Main.:+, x1, x2)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0},Nothing,typeof(+),Tuple{Int64,Int64}}%2 = Base.broadcasted(Main.max, %1, x3)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0},Nothing,typeof(max),Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0},Nothing,typeof(+),Tuple{Int64,Int64}},Int64}}
│   %3 = Base.materialize(%2)::Int64
└──      return %3

This works by wrapping the dot expression in a function and applying the introspection to the created function. In the example above, the newly created function is

dotfunction(x1, x2, x3) = max.(x1 .+ x2, x3)

Note that the 5 - 7 argument has been transformed into a single argument for the new function, since it is not broadcasted upon.

Other introspection macros such as @which or @edit now error with a message pointing the user towards Meta.@lower:

julia> @which abs.([1,2])
ERROR: dot expressions are not lowered to a single function call, so @which cannot analyze them. You may want to use Meta.@lower to identify which function call to target.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at none:1

I also added a small message mentioning Meta.@lower in the general error message for the introspection macros because I think it deserves to be there, but let me know what you think of it.

getproperty

The dot used for broadcasting and the dot used for getproperty have different context so there is no confusion when handling them. When both are present, the outermost dominates (I don't know how to phrase it, but I think it just follows intuition):

julia> @code_typed optimize=false Ref.(["foo", 4])[1].x
CodeInfo(
1%1 = Base.getfield(x, f)::String
└──      return %1
) => String

Finally, @which can now handle fully qualified symbols (#14637):

julia> @which Main.Base.nothing
Core

@JeffBezanson JeffBezanson merged commit 4c7ae77 into JuliaLang:master Apr 24, 2020
@MasonProtter
Copy link
Contributor

This should probably have a NEWS.md entry, right?

@Liozou
Copy link
Member Author

Liozou commented Apr 24, 2020

Well it's arguably a bugfix. But if people are in favor I can PR a NEWS entry joined with #35283, it would make sense.

@MasonProtter
Copy link
Contributor

I see it as a feature enhancement.

@Liozou Liozou deleted the dotintrospection branch April 25, 2020 15:48
mbauman added a commit that referenced this pull request Apr 28, 2020
* origin/master: (833 commits)
  Improve typesubtract for tuples (#35600)
  Make searchsorted*/findnext/findprev return values of keytype (#32978)
  fix buggy rand(RandomDevice(), Bool) (#35590)
  remove `Ref` allocation on task switch (#35606)
  Revert "partr: fix multiqueue resorting stability" (#35589)
  exclude types with free variables from `type_morespecific` (#35555)
  fix small typo in NEWS.md (#35611)
  enable inline allocation of structs with pointers (#34126)
  SparseArrays: Speed up right-division by diagonal matrices (#35533)
  Allow hashing 1D OffsetArrays
  NEWS item for introspection macros (#35594)
  Special case empty covec-diagonal-vec product (#35557)
  [GCChecker] fix a few tests by looking through casts
  Use norm instead of abs in generic lu factorization (#34575)
  [GCChecker,NFC] run clang-format -style=llvm
  [GCChecker] fix tests and add Makefile
  Add introspection macros support for dot syntax (#35522)
  Specialize `union` for `OneTo` (#35577)
  add pop!(vector, idx, [default]) (#35513)
  bump Pkg version (#35584)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants