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

Use Abstract Interpretation to search for overridden property names #49199

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 21 additions & 1 deletion stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function complete_symbol(sym::String, @nospecialize(ffunc), context_module::Modu
lookup_module = true
t = Union{}
val = nothing
ex = :()
if something(findlast(in(non_identifier_chars), sym), 0) < something(findlast(isequal('.'), sym), 0)
# Find module
lookup_name, name = rsplit(sym, ".", limit=2)
Expand Down Expand Up @@ -213,8 +214,27 @@ function complete_symbol(sym::String, @nospecialize(ffunc), context_module::Modu
end
end
end

# Also, try abstract-interpreting propertynames
Copy link
Member

Choose a reason for hiding this comment

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

Does this lead to duplicate suggestions for, e.g., concrete types?

Copy link
Member

Choose a reason for hiding this comment

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

We should probably do this inference within get_type above.

thunk = context_module.eval(:(() -> propertynames($(ex))))
inf_params = Core.Compiler.InferenceParams(; assume_bindings_static=true)
frame = Core.Compiler.typeinf_frame(
Core.Compiler.NativeInterpreter(;inf_params),
first(methods(thunk)),
Tuple{typeof(thunk)},
Core.svec(),
true,
)
Comment on lines +219 to +227
Copy link
Member

Choose a reason for hiding this comment

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

There are some ways available to perform inference on a toplevel expression directly rather than wrapping it in a temporary function and performing inference on it.
This piece of code from JET.jl might be useful:
https://github.com/aviatesk/JET.jl/blob/9f8add4ce3d8ba528249ec02023711759abb00d3/src/toplevel/virtualprocess.jl#L1395-L1415
(ignore the abstract_global_symbols! stuff)

if frame !== nothing && Core.Compiler.is_inferred(frame) && isa(frame.result.result, Core.Compiler.Const)
for field in frame.result.result.val
s = string(field)
if startswith(s, name)
push!(suggestions, FieldCompletion(t, field))
end
end
end
end
suggestions
return suggestions
end

const sorted_keywords = [
Expand Down