Skip to content

Commit

Permalink
lowering: Only try to define the method once (#57346)
Browse files Browse the repository at this point in the history
Before:
```
:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:thunk, CodeInfo(
1 ─     return $(Expr(:method, :(Main.f)))
)))
│         $(Expr(:method, :(Main.f)))
│   %3  = Main.f
│   %4  =   dynamic Core.Typeof(%3)
│   %5  =   builtin Core.svec(%4, Core.Any)
│   %6  =   builtin Core.svec()
│   %7  =   builtin Core.svec(%5, %6, $(QuoteNode(:(#= REPL[2]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%7), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %10 = Main.f
└──       return %10
))))
```

After:
```
julia> @Meta.lower f(x)=1
:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:method, :(Main.f)))
│         $(Expr(:latestworld))
│         Main.f
│         $(Expr(:latestworld))
│   %5  = Main.f
│   %6  =   dynamic Core.Typeof(%5)
│   %7  =   builtin Core.svec(%6, Core.Any)
│   %8  =   builtin Core.svec()
│   %9  =   builtin Core.svec(%7, %8, $(QuoteNode(:(#= REPL[1]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%9), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %12 = Main.f
└──       return %12
))))
```

This doesn't really make a semantic difference, but if `f` is a type, we
may now give a warning, so the prior definition would give the warning
twice
(#57311 (comment)).
We may want to consider rate-limiting the warning independently, but for
now at least give the correct number of warnings.
  • Loading branch information
Keno authored Feb 12, 2025
1 parent 3f2eed3 commit 512eb5e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
2 changes: 0 additions & 2 deletions doc/src/manual/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ julia> nice(::Cat) = "nice 😸"
ERROR: invalid method definition in Main: function NiceStuff.nice must be explicitly imported to be extended
Stacktrace:
[1] top-level scope
@ none:0
[2] top-level scope
@ none:1
```

Expand Down
3 changes: 2 additions & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4131,8 +4131,9 @@ f(x) = yt(x)
`(toplevel-butfirst
;; wrap in toplevel-butfirst so it gets moved higher along with
;; closure type definitions
(unnecessary ,(cadr e))
,e
(thunk (lambda () (() () 0 ()) (block (return ,e))))))))
(latestworld)))))
((null? cvs)
`(block
,@sp-inits
Expand Down

0 comments on commit 512eb5e

Please sign in to comment.