Skip to content

Handle function and macro expressions in explore#38

Open
satkarjuneja wants to merge 2 commits into
JuliaPluto:mainfrom
satkarjuneja:main
Open

Handle function and macro expressions in explore#38
satkarjuneja wants to merge 2 commits into
JuliaPluto:mainfrom
satkarjuneja:main

Conversation

@satkarjuneja

Copy link
Copy Markdown

This is an attempt to fix JuliaPluto/Pluto.jl#3545

The problem seems to be with the explore_local! function in src/explore.jl

in the function there was no handling to detect local function or macro

elseif isa(localisee, Expr) && (localisee.head === :function || localisee.head === :macro)
        old = scopestate.inglobalscope
        scopestate.inglobalscope = false
        result = explore!(localisee, scopestate)
        scopestate.inglobalscope = old
        return result::SymbolsState

this condition seems to fix said issue
I have ran the test suite it seems to work

the reason for old = scopestate.inglobalscope is that
ScopeState is declared as a mutable struct , the function receives a reference to the same object. So when explore_local! does scopestate.inglobalscope = false, it isn't creating some local, function-scoped version of that field. It's reaching into the one shared object and changing a field on it, visible to everyone else holding a reference to that same object.

So it should be set back after the inside of the function is explored

@fonsp

fonsp commented Jul 7, 2026

Copy link
Copy Markdown
Member

Nice, thank you for your PR! Could you also add a new test case for this?

If this syntax was added in a later Julia release, you can use Meta.parse like in some other test cases.

@satkarjuneja

Copy link
Copy Markdown
Author

Nice, thank you for your PR! Could you also add a new test case for this?

Done

@johroj

johroj commented Jul 7, 2026

Copy link
Copy Markdown

Thanks for having a look! Nice that you also cover macros. Have you checked whether other keywords that create variable bindings should work with the local prefix too? Struct, mutable struct, module, baremodule comes to mind.

@satkarjuneja

Copy link
Copy Markdown
Author
julia> begin
       local struct test
       x::Int
       end
       end
ERROR: syntax: invalid syntax in "local" declaration around REPL[1]:2
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

It seems julia itself wont allow local structs

@fonsp

fonsp commented Jul 7, 2026

Copy link
Copy Markdown
Member

Okay! In that case this PR is ready in its current form, right?

@satkarjuneja

satkarjuneja commented Jul 9, 2026

Copy link
Copy Markdown
Author

Any issues @fonsp ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

local function works, but leads to multiple definitions error

3 participants