- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Enable @edit @time 1+1
, fix #3377
#15282
Closed
+116
−77
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1387,6 +1387,7 @@ export | |
# reflection | ||
@which, | ||
@edit, | ||
@functionloc, | ||
@less, | ||
@code_typed, | ||
@code_warntype, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,8 +256,12 @@ function gen_call_with_extracted_types(fcn, ex0) | |
Expr(:call, typesof, map(esc, args[2:end])...)) | ||
end | ||
exret = Expr(:none) | ||
is_macro = false | ||
ex = expand(ex0) | ||
if !isa(ex, Expr) | ||
if isa(ex0, Expr) && ex0.head == :macrocall # Make @edit @time 1+2 edit the macro | ||
is_macro = true | ||
exret = Expr(:call, fcn, esc(ex0.args[1]), typesof(ex0.args[2:end]...)) | ||
elseif !isa(ex, Expr) | ||
exret = Expr(:call, :error, "expression is not a function call or symbol") | ||
elseif ex.head == :call | ||
if any(e->(isa(e, Expr) && e.head==:(...)), ex0.args) && | ||
|
@@ -280,15 +284,15 @@ function gen_call_with_extracted_types(fcn, ex0) | |
end | ||
end | ||
end | ||
if ex.head == :thunk || exret.head == :none | ||
if (!is_macro && ex.head == :thunk) || exret.head == :none | ||
exret = Expr(:call, :error, "expression is not a function call, " | ||
* "or is too complex for @$fcn to analyze; " | ||
* "break it down to simpler parts if possible") | ||
end | ||
exret | ||
end | ||
|
||
for fname in [:which, :less, :edit, :code_typed, :code_warntype, | ||
for fname in [:which, :less, :edit, :functionloc, :code_typed, :code_warntype, | ||
:code_lowered, :code_llvm, :code_llvm_raw, :code_native] | ||
@eval begin | ||
macro ($fname)(ex0) | ||
|
@@ -297,7 +301,80 @@ for fname in [:which, :less, :edit, :code_typed, :code_warntype, | |
end | ||
end | ||
|
||
# `methodswith` -- shows a list of methods using the type given | ||
""" | ||
@which | ||
Applied to a function or macro call, it evaluates the arguments to the specified call, and | ||
returns the `Method` object for the method that would be called for those arguments. Applied | ||
to a variable, it returns the module in which the variable was bound. It calls out to the | ||
`which` function. | ||
""" | ||
:@which | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run genstdlib ( |
||
|
||
""" | ||
@less | ||
Evaluates the arguments to the function or macro call, determines their types, and calls the `less` | ||
function on the resulting expression. | ||
""" | ||
:@less | ||
|
||
""" | ||
@edit | ||
Evaluates the arguments to the function or macro call, determines their types, and calls the `edit` | ||
function on the resulting expression. | ||
""" | ||
:@edit | ||
|
||
""" | ||
@functionloc | ||
Applied to a function or macro call, it evaluates the arguments to the specified call, and | ||
returns a tuple `(filename,line)` giving the location for the method that would be called for those arguments. | ||
It calls out to the `functionloc` function. | ||
""" | ||
:@functionloc | ||
|
||
""" | ||
@code_typed | ||
Evaluates the arguments to the function or macro call, determines their types, and calls | ||
[`code_typed`](:func:`code_typed`) on the resulting expression. | ||
""" | ||
:@code_typed | ||
|
||
""" | ||
@code_warntype | ||
Evaluates the arguments to the function or macro call, determines their types, and calls | ||
[`code_warntype`](:func:`code_warntype`) on the resulting expression. | ||
""" | ||
:@code_warntype | ||
|
||
""" | ||
@code_lowered | ||
Evaluates the arguments to the function or macro call, determines their types, and calls | ||
[`code_lowered`](:func:`code_lowered`) on the resulting expression. | ||
""" | ||
:@code_lowered | ||
|
||
""" | ||
@code_llvm | ||
Evaluates the arguments to the function or macro call, determines their types, and calls | ||
[`code_llvm`](:func:`code_llvm`) on the resulting expression. | ||
""" | ||
:@code_llvm | ||
|
||
""" | ||
@code_native | ||
Evaluates the arguments to the function or macro call, determines their types, and calls | ||
[`code_native`](:func:`code_native`) on the resulting expression. | ||
""" | ||
:@code_native | ||
|
||
function type_close_enough(x::ANY, t::ANY) | ||
x == t && return true | ||
|
@@ -306,6 +383,7 @@ function type_close_enough(x::ANY, t::ANY) | |
(isa(x,Union) && isa(t,DataType) && any(u -> is(u,t), x.types)) | ||
end | ||
|
||
# `methodswith` -- shows a list of methods using the type given | ||
function methodswith(t::Type, f::Function, showparents::Bool=false, meths = Method[]) | ||
mt = typeof(f).name.mt | ||
d = mt.defs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the special case needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The special case is needed because when the
expand
function called above expands the macro/insert its code into the expression, and hence theedit
call will call edit on the expanded code. The special casing makeswhich
,less
andedit
macro call the macrocall instead of the code the macro expands to. This enables the user to write:@edit @time 1+1
, this will go to the@time
macro's definition.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a case you don't want to use operate on the macro?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main concern is that this doesn't seem to be the way this should work. If it is somehow really necessary to have different behavior for different macros that use this function, it should be controlled explicitly with a optional/keyword argument to this function instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand your concern, but from my point of view it do not make sense to ask for
@edit @random_macro max(1,1)
and not point to the macro. As a macro can generate arbitrary code for you, and hence you do not know what you call@edit
on.