Hi all,

Megane found an interesting case where the macro-expander would
crash hard (at least in a debugbuild) when a macro expands to a
macro definition for an identifier that's already bound to a
macro. 

After some consideration I believe megane's patch is correct,
so attached is that fix, plus a test case.

This could also go to the chicken-4 branch.

Cheers,
Peter
From f2a8a18d6f4917545849242ad5b365379f0f2084 Mon Sep 17 00:00:00 2001
From: Peter Bex <pe...@more-magic.net>
Date: Sat, 4 Aug 2018 11:29:52 +0200
Subject: [PATCH] When looking up symbol aliases, ignore macros so we don't end
 up using macro definitions as identifiers

This fixes #1493, found by Megane
---
 core.scm               |  2 +-
 eval.scm               |  2 +-
 tests/syntax-tests.scm | 30 ++++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/core.scm b/core.scm
index 2bbed0b2..3ecdd817 100644
--- a/core.scm
+++ b/core.scm
@@ -517,7 +517,7 @@
 
   (define (lookup id)
     (cond ((find-id id (##sys#current-environment)))
-	  ((##sys#get id '##core#macro-alias))
+	  ((##sys#get id '##core#macro-alias) symbol? => values)
 	  (else id)))
 
   (define (macro-alias var)
diff --git a/eval.scm b/eval.scm
index f1c5bb6c..aeaf1732 100644
--- a/eval.scm
+++ b/eval.scm
@@ -92,7 +92,7 @@
 
       (define (rename var)
 	(cond ((find-id var (##sys#current-environment)))
-	      ((##sys#get var '##core#macro-alias))
+	      ((##sys#get var '##core#macro-alias) symbol? => values)
 	      (else var)))
 
       (define (lookup var0 e)
diff --git a/tests/syntax-tests.scm b/tests/syntax-tests.scm
index 38ae5978..58caf2c3 100644
--- a/tests/syntax-tests.scm
+++ b/tests/syntax-tests.scm
@@ -1285,3 +1285,33 @@ other-eval
 (t 1 (letrec* ((foo 1)
 	       (bar foo))
        bar))
+
+
+;; This would crash in nasty ways (see #1493, reported by megane)
+(module
+ self-redefinition
+ (foo)
+ (import scheme (chicken base))
+
+ (define-syntax foo
+   (ir-macro-transformer
+    (lambda (e i cmp)
+      (apply
+       (lambda (name)
+         `(begin
+	    (define-syntax ,(strip-syntax name)
+	      (syntax-rules () ((_ . _) 'new)))
+	    'old))
+       (cdr e)))))
+)
+
+(import (rename self-redefinition (foo imported-foo)))
+(import (rename self-redefinition (foo reimported-foo)))
+
+(t 'old (imported-foo imported-foo))
+(t 'new (imported-foo imported-foo))
+
+;; Like any normal redefinition, the underlying exported identifier
+;; changes, and any other imports are simply aliases.
+;;(t 'old (reimported-foo reimported-foo))
+(t 'new (reimported-foo reimported-foo))
-- 
2.11.0

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to