Subject: Problem with macros whose expansions define and use auxiliary macros

From: Mark H Weaver <[email-masked]>
Date: Mon, 18 Mar 2013 19:11:17 -0400

Consider the following module:

--8<---------------cut here---------------start------------->8---
(define-module (foo)
  #:export (foo))

(define-syntax-rule (foo bar)
  (begin
    (define-syntax-rule (blah x) x)
    (define (bar val) (blah val))))
--8<---------------cut here---------------end--------------->8---

and the following session with v2.0.7-204-g1ea3762:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> ,use (foo)
scheme@(guile-user)> (foo bar)
scheme@(guile-user)> (bar 5)
ERROR: In procedure #<syntax-transformer blah>:
ERROR: Wrong type to apply: #<syntax-transformer blah>

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> 
--8<---------------cut here---------------end--------------->8---

Note that it works when the same 'foo' macro is entered directly
at a fresh REPL:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> 
(define-syntax-rule (foo bar)
  (begin
    (define-syntax-rule (blah x) x)
    (define (bar val) (blah val))))
scheme@(guile-user)> (foo bar)
scheme@(guile-user)> (bar 5)
$1 = 5
scheme@(guile-user)> 
--8<---------------cut here---------------end--------------->8---

       Mark