@@ -539,6 +539,19 @@ function _resolve_scopes(ctx, ex::SyntaxTree)
539539 throw (LoweringError (ex, " Unknown syntax assertion" ))
540540 end
541541 makeleaf (ctx, ex, K " TOMBSTONE" )
542+ elseif k == K " function_decl"
543+ resolved = mapchildren (e-> _resolve_scopes (ctx, e), ctx, ex)
544+ name = resolved[1 ]
545+ if kind (name) == K " BindingId"
546+ bk = lookup_binding (ctx, name). kind
547+ if bk == :argument
548+ throw (LoweringError (name, " Cannot add method to a function argument" ))
549+ elseif bk == :global && ! ctx. scope_stack[end ]. in_toplevel_thunk
550+ throw (LoweringError (name,
551+ " Global method definition needs to be placed at the top level, or use `eval()`" ))
552+ end
553+ end
554+ resolved
542555 elseif k == K " const_if_global"
543556 id = _resolve_scopes (ctx, ex[1 ])
544557 if lookup_binding (ctx, id). kind == :global
@@ -630,9 +643,6 @@ function analyze_variables!(ctx, ex)
630643 analyze_variables! (ctx, ex[2 ])
631644 elseif k == K " function_decl"
632645 name = ex[1 ]
633- if kind (name) == K " BindingId" && lookup_binding (ctx, name). kind == :argument
634- throw (LoweringError (name, " Cannot add method to a function argument" ))
635- end
636646 if lookup_binding (ctx, name. var_id). kind === :local
637647 init_closure_bindings! (ctx, name)
638648 end
0 commit comments