From 718cb82c3468ad4fd70b1c79fb48bfe78863d19e Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 7 Sep 2018 17:18:46 -0400 Subject: [PATCH] fix #25955, hygiene of arg name of function defined by type (`(f::T)(...)`) (#29072) (cherry picked from commit bb7d0434d79c0ef8217706faec846862e3b2b805) --- src/macroexpand.scm | 10 ++++++++-- test/syntax.jl | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/macroexpand.scm b/src/macroexpand.scm index d95e8132ff21e..4349e3a5b67d6 100644 --- a/src/macroexpand.scm +++ b/src/macroexpand.scm @@ -60,7 +60,7 @@ ;; function definition (pattern-lambda (function (-$ (call name . argl) (|::| (call name . argl) _t)) body) - (cons 'varlist (safe-llist-positional-args (fix-arglist argl)))) + (cons 'varlist (safe-llist-positional-args (fix-arglist (append (self-argname name) argl))))) (pattern-lambda (function (where callspec . wheres) body) (let ((others (pattern-expand1 vars-introduced-by-patterns `(function ,callspec ,body)))) (cons 'varlist (append (if (and (pair? others) (eq? (car others) 'varlist)) @@ -75,7 +75,7 @@ (pattern-lambda (= (call (curly name . sparams) . argl) body) `(function (call (curly ,name . ,sparams) . ,argl) ,body)) (pattern-lambda (= (-$ (call name . argl) (|::| (call name . argl) _t)) body) - `(function (call ,name ,@argl) ,body)) + `(function ,(cadr __) ,body)) (pattern-lambda (= (where callspec . wheres) body) (cons 'function (cdr __))) @@ -257,6 +257,12 @@ ;; count escaped argument names as "keywords" to prevent renaming (safe-llist-positional-args lst #t)))) +;; argument name for the function itself given `function (f::T)(...)`, otherwise () +(define (self-argname name) + (if (and (length= name 3) (eq? (car name) '|::|)) + (list (cadr name)) + '())) + ;; resolve-expansion-vars-with-new-env, but turn on `inarg` once we get inside ;; the formal argument list. `e` in general might be e.g. `(f{T}(x)::T) where T`, ;; and we want `inarg` to be true for the `(x)` part. diff --git a/test/syntax.jl b/test/syntax.jl index 021d591d4e55a..6c6fc089b7203 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -1688,6 +1688,20 @@ end @test B28593.var.name === :S @test C28593.var.name === :S +# issue #25955 +macro noeffect25955(e) + return e +end + +struct foo25955 +end + +@noeffect25955 function (f::foo25955)() + 42 +end + +@test foo25955()() == 42 + # issue #28833 macro m28833(expr) esc(:(global a28833))