[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-hackers] [PATCH] warn if binding to keyword
From: |
Felix |
Subject: |
Re: [Chicken-hackers] [PATCH] warn if binding to keyword |
Date: |
Mon, 24 Oct 2011 11:12:43 +0200 (CEST) |
From: Felix <address@hidden>
Subject: [PATCH] warn if binding to keyword
Date: Sun, 23 Oct 2011 01:35:22 +0200 (CEST)
> The attached patch adds a warning, if a keyword is used as a variable
> name in a binding construct (let, letrec, let-syntax, letrec-syntax),
> which can lead to nasty bugs that are hard to find.
The patch is incomplete - please consider this one instead.
cheers,
felix
>From 1fb122688fd4965b2e3d992e3933239937862cf0 Mon Sep 17 00:00:00 2001
From: felix <address@hidden>
Date: Mon, 24 Oct 2011 11:11:48 +0200
Subject: [PATCH] Squashed commit of the following:
commit f64d5be04131aaf0ab06b7e956d3bafa72a6b383
Author: felix <address@hidden>
Date: Mon Oct 24 11:04:08 2011 +0200
check not necessary for lambda
commit fc4601e3fc36f2a6ab4580fb803e8b1dc7d906c8
Author: felix <address@hidden>
Date: Sun Oct 23 01:31:09 2011 +0200
warn if using keyword as variable in binding form
---
compiler.scm | 20 ++++++++++++++------
eval.scm | 14 ++++++++++----
2 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/compiler.scm b/compiler.scm
index cb9b247..d907f63 100644
--- a/compiler.scm
+++ b/compiler.scm
@@ -501,6 +501,11 @@
(for-each pretty-print imps)
(print "\n;; END OF FILE"))))) ) )
+ (define (checkvar name form)
+ (when (keyword? name)
+ (warning "variable is keyword in binding form" `(,form (... (,name ...)
...) ...)))
+ name)
+
(define (walk x e se dest ldest h)
(cond ((symbol? x)
(cond ((keyword? x) `(quote ,x))
@@ -618,7 +623,7 @@
((##core#let)
(let* ((bindings (cadr x))
(vars (unzip1 bindings))
- (aliases (map gensym vars))
+ (aliases (map (o gensym (cut checkvar <> 'let))
vars))
(se2 (##sys#extend-se se vars aliases)))
(set-real-names! aliases vars)
`(let
@@ -636,7 +641,7 @@
(walk
`(##core#let
,(map (lambda (b)
- (list (car b) '(##core#undefined)))
+ (list (checkvar (car b) 'letrec)
'(##core#undefined)))
bindings)
,@(map (lambda (b)
`(##core#set! ,(car b) ,(cadr b)))
@@ -686,7 +691,7 @@
(let ((se2 (append
(map (lambda (b)
(list
- (car b)
+ (checkvar (car b) 'let-syntax)
se
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
@@ -701,7 +706,7 @@
((##core#letrec-syntax)
(let* ((ms (map (lambda (b)
(list
- (car b)
+ (checkvar (car b) 'letrec-syntax)
#f
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
@@ -776,8 +781,11 @@
((##core#let-compiler-syntax)
(let ((bs (map
(lambda (b)
- (##sys#check-syntax 'let-compiler-syntax b
'(symbol . #(_ 0 1)))
- (let ((name (lookup (car b) se)))
+ (##sys#check-syntax
+ 'let-compiler-syntax b '(symbol . #(_ 0
1)))
+ (let ((name (lookup
+ (checkvar (car b)
'let-compiler-syntax)
+ se)))
(list
name
(and (pair? (cdr b))
diff --git a/eval.scm b/eval.scm
index 0ad85b4..e66462c 100644
--- a/eval.scm
+++ b/eval.scm
@@ -231,6 +231,11 @@
(define (decorate p ll h cntr)
(##sys#eval-decorator p ll h cntr) )
+ (define (checkvar name form)
+ (when (keyword? name)
+ (warning "variable is keyword in binding form" `(,form (... (,name
...) ...) ...)))
+ name)
+
(define (compile x e h tf cntr se)
(cond ((keyword? x) (lambda v x))
((symbol? x)
@@ -372,7 +377,7 @@
[(##core#let)
(let* ([bindings (cadr x)]
[n (length bindings)]
- [vars (map (lambda (x) (car x)) bindings)]
+ [vars (map (lambda (x) (checkvar (car x)
'let)) bindings)]
(aliases (map gensym vars))
[e2 (cons aliases e)]
(se2 (##sys#extend-se se vars aliases))
@@ -424,7 +429,8 @@
(compile
`(##core#let
,(##sys#map (lambda (b)
- (list (car b)
'(##core#undefined)))
+ (list (checkvar (car b) 'letrec)
+ '(##core#undefined)))
bindings)
,@(##sys#map (lambda (b)
`(##core#set! ,(car b) ,(cadr
b)))
@@ -529,7 +535,7 @@
(let ((se2 (append
(map (lambda (b)
(list
- (car b)
+ (checkvar (car b) 'let-syntax)
se
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
@@ -543,7 +549,7 @@
((##core#letrec-syntax)
(let* ((ms (map (lambda (b)
(list
- (car b)
+ (checkvar (car b) 'letrec-syntax)
#f
(##sys#ensure-transformer
(##sys#eval/meta (cadr b))
--
1.6.0.4
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, (continued)
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Thomas Bushnell, BSG, 2011/10/22
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Felix, 2011/10/24
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Thomas Bushnell, BSG, 2011/10/24
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Felix, 2011/10/25
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Thomas Bushnell, BSG, 2011/10/25
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, John Cowan, 2011/10/25
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Thomas Bushnell, BSG, 2011/10/25
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, John Cowan, 2011/10/25
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Felix, 2011/10/26
- Re: [Chicken-hackers] [PATCH] warn if binding to keyword, Felix, 2011/10/26
Re: [Chicken-hackers] [PATCH] warn if binding to keyword,
Felix <=