macrolet.lsp 10 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Wed Oct  9 19:41:24 2002
;;;; Contains: Tests of MACROLET

(in-package :cl-test)

(deftest macrolet.1
  (let ((z (list 3 4)))
    (macrolet ((%m (x) `(car ,x)))
      (let ((y (list 1 2)))
sds's avatar
sds committed
12
        (values (%m y) (%m z)))))
13 14 15 16 17 18
  1 3)

(deftest macrolet.2
  (let ((z (list 3 4)))
    (macrolet ((%m (x) `(car ,x)))
      (let ((y (list 1 2)))
sds's avatar
sds committed
19 20 21 22
        (values
         (setf (%m y) 6)
         (setf (%m z) 'a)
         y z))))
23 24 25 26 27 28 29
  6 a (6 2) (a 4))

;;; Inner definitions shadow outer ones
(deftest macrolet.3
  (macrolet ((%m (w) `(cadr ,w)))
    (let ((z (list 3 4)))
      (macrolet ((%m (x) `(car ,x)))
sds's avatar
sds committed
30 31 32 33 34 35
        (let ((y (list 1 2)))
          (values
           (%m y) (%m z)
           (setf (%m y) 6)
           (setf (%m z) 'a)
           y z)))))
36 37 38 39 40 41
  1 3 6 a (6 2) (a 4))

;;; &whole parameter
(deftest macrolet.4
  (let ((x nil))
    (macrolet ((%m (&whole w arg)
sds's avatar
sds committed
42 43
                   `(progn (setq x (quote ,w))
                           ,arg)))
44 45 46 47 48 49 50
      (values (%m 1) x)))
  1 (%m 1))

;;; &whole parameter (nested, destructuring; see section 3.4.4)
(deftest macrolet.5
  (let ((x nil))
    (macrolet ((%m ((&whole w arg))
sds's avatar
sds committed
51 52
                   `(progn (setq x (quote ,w))
                           ,arg)))
53 54 55 56 57 58 59
      (values (%m (1)) x)))
  1 (1))

;;; key parameter
(deftest macrolet.6
  (let ((x nil))
    (macrolet ((%m (&key (a 'xxx) b)
sds's avatar
sds committed
60 61
                   `(setq x (quote ,a))))

62
      (values (%m :a foo) x
sds's avatar
sds committed
63
              (%m :b bar) x)))
64 65 66 67 68 69
  foo foo xxx xxx)

;;; nested key parameters
(deftest macrolet.7
  (let ((x nil))
    (macrolet ((%m ((&key a b))
sds's avatar
sds committed
70 71
                   `(setq x (quote ,a))))

72
      (values (%m (:a foo)) x
sds's avatar
sds committed
73
              (%m (:b bar)) x)))
74 75 76 77 78 79
  foo foo nil nil)

;;; nested key parameters
(deftest macrolet.8
  (let ((x nil))
    (macrolet ((%m ((&key (a 10) b))
sds's avatar
sds committed
80 81
                   `(setq x (quote ,a))))

82
      (values (%m (:a foo)) x
sds's avatar
sds committed
83
              (%m (:b bar)) x)))
84 85 86 87 88 89
  foo foo 10 10)

;;; keyword parameter with supplied-p parameter
(deftest macrolet.9
  (let ((x nil))
    (macrolet ((%m (&key (a 'xxx a-p) b)
sds's avatar
sds committed
90 91
                   `(setq x (quote ,(list a (not (not a-p)))))))

92
      (values (%m :a foo) x
sds's avatar
sds committed
93
              (%m :b bar) x)))
94 95 96 97 98 99 100
  (foo t) (foo t) (xxx nil) (xxx nil))


;;; rest parameter
(deftest macrolet.10
  (let ((x nil))
    (macrolet ((%m (b &rest a)
sds's avatar
sds committed
101
                   `(setq x (quote ,a))))
102 103 104 105 106 107 108
      (values (%m a1 a2) x)))
  (a2) (a2))

;;; rest parameter w. destructuring
(deftest macrolet.11
  (let ((x nil))
    (macrolet ((%m ((b &rest a))
sds's avatar
sds committed
109
                   `(setq x (quote ,a))))
110 111 112 113 114 115 116
      (values (%m (a1 a2)) x)))
  (a2) (a2))

;;; rest parameter w. whole
(deftest macrolet.12
  (let ((x nil))
    (macrolet ((%m (&whole w b &rest a)
sds's avatar
sds committed
117
                   `(setq x (quote ,(list a w)))))
118 119 120 121
      (values (%m a1 a2) x)))
  ((a2) (%m a1 a2))
  ((a2) (%m a1 a2)))

122 123 124 125 126
;;; Interaction with symbol-macrolet

(deftest macrolet.13
  (symbol-macrolet ((a b))
    (macrolet ((foo (x &environment env)
sds's avatar
sds committed
127 128
                    (let ((y (macroexpand x env)))
                      (if (eq y 'a) 1 2))))
129 130
      (foo a)))
  2)
131 132 133 134

(deftest macrolet.14
  (symbol-macrolet ((a b))
    (macrolet ((foo (x &environment env)
sds's avatar
sds committed
135 136
                    (let ((y (macroexpand-1 x env)))
                      (if (eq y 'a) 1 2))))
137 138 139
      (foo a)))
  2)

140 141 142 143 144 145
(deftest macrolet.15
  (macrolet ((nil () ''a))
    (nil))
  a)

(deftest macrolet.16
pfdietz's avatar
pfdietz committed
146
  (loop for s in *cl-non-function-macro-special-operator-symbols*
sds's avatar
sds committed
147 148 149
        for form = `(ignore-errors (macrolet ((,s () ''a)) (,s)))
        unless (eq (eval form) 'a)
        collect s)
150 151
  nil)

pfdietz's avatar
pfdietz committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
(deftest macrolet.17
  (macrolet ((%m (&key (a t)) `(quote ,a)))
    (%m :a nil))
  nil)

(deftest macrolet.18
  (macrolet ((%m (&key (a t a-p)) `(quote (,a ,(notnot a-p)))))
    (%m :a nil))
  (nil t))

(deftest macrolet.19
  (macrolet ((%m (x &optional y) `(quote (,x ,y))))
    (values (%m 1) (%m 2 3)))
  (1 nil)
  (2 3))

(deftest macrolet.20
  (macrolet ((%m (x &optional (y 'a)) `(quote (,x ,y))))
    (values (%m 1) (%m 2 3)))
  (1 a)
  (2 3))

;;; Note -- the supplied-p parameter in a macrolet &optional
;;; is required to be T (not just true) if the parameter is present.
;;; See section 3.4.4.1.2
(deftest macrolet.21
  (macrolet ((%m (x &optional (y 'a y-p)) `(quote (,x ,y ,y-p))))
    (values (%m 1) (%m 2 3)))
  (1 a nil)
  (2 3 t))

(deftest macrolet.22
  (macrolet ((%m (x &optional ((y z) '(2 3))) `(quote (,x ,y ,z))))
    (values
     (%m a)
     (%m a (b c))))
  (a 2 3)
  (a b c))

191
(deftest macrolet.22a
pfdietz's avatar
pfdietz committed
192
  (macrolet ((%m (x &optional ((y z) '(2 3) y-z-p))
sds's avatar
sds committed
193
                 `(quote (,x ,y ,z ,y-z-p))))
pfdietz's avatar
pfdietz committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    (values
     (%m a)
     (%m a (b c))))
  (a 2 3 nil)
  (a b c t))

(deftest macrolet.23
  (macrolet ((%m (&rest y) `(quote ,y)))
    (%m 1 2 3))
  (1 2 3))

;;; According to 3.4.4.1.2, the entity following &rest is
;;; 'a destructuring pattern that matches the rest of the list.'

(deftest macrolet.24
  (macrolet ((%m (&rest (x y z)) `(quote (,x ,y ,z))))
    (%m 1 2 3))
  (1 2 3))

(deftest macrolet.25
  (macrolet ((%m (&body (x y z)) `(quote (,x ,y ,z))))
    (%m 1 2 3))
  (1 2 3))

;;; More key parameters

(deftest macrolet.26
  (macrolet ((%m (&key ((:a b))) `(quote ,b)))
    (values (%m)
sds's avatar
sds committed
223
            (%m :a x)))
pfdietz's avatar
pfdietz committed
224 225 226 227 228 229 230 231 232 233 234
  nil
  x)

(deftest macrolet.27
  (macrolet ((%m (&key ((:a (b c)))) `(quote (,c ,b))))
    (%m :a (1 2)))
  (2 1))

(deftest macrolet.28
  (macrolet ((%m (&key ((:a (b c)) '(3 4))) `(quote (,c ,b))))
    (values (%m :a (1 2))
sds's avatar
sds committed
235 236
            (%m :a (1 2) :a (10 11))
            (%m)))
pfdietz's avatar
pfdietz committed
237 238 239 240 241 242 243
  (2 1)
  (2 1)
  (4 3))

(deftest macrolet.29
  (macrolet ((%m (&key a (b a)) `(quote (,a ,b))))
    (values (%m)
sds's avatar
sds committed
244 245 246 247 248 249 250
            (%m :a 1)
            (%m :b 2)
            (%m :a 3 :b 4)
            (%m :b 5 :a 6)
            (%m :a 7 :a 8)
            (%m :a 9 :b nil)
            (%m :a 10 :b nil :b 11)))
pfdietz's avatar
pfdietz committed
251 252 253 254 255 256 257 258 259 260 261 262
  (nil nil)
  (1 1)
  (nil 2)
  (3 4)
  (6 5)
  (7 7)
  (9 nil)
  (10 nil))

(deftest macrolet.30
  (macrolet ((%m ((&key a) &key (b a)) `(quote (,a ,b))))
    (values (%m ())
sds's avatar
sds committed
263 264 265 266 267 268
            (%m (:a 1))
            (%m () :b 2)
            (%m (:a 3) :b 4)
            (%m (:a 7 :a 8))
            (%m (:a 9) :b nil)
            (%m (:a 10) :b nil :b 11)))
pfdietz's avatar
pfdietz committed
269 270 271 272 273 274 275 276 277 278
  (nil nil)
  (1 1)
  (nil 2)
  (3 4)
  (7 7)
  (9 nil)
  (10 nil))

(deftest macrolet.31
  (macrolet ((%m (&key ((:a (b c)) '(3 4) a-p))
sds's avatar
sds committed
279
                 `(quote (,(notnot a-p) ,c ,b))))
pfdietz's avatar
pfdietz committed
280
    (values (%m :a (1 2))
sds's avatar
sds committed
281 282
            (%m :a (1 2) :a (10 11))
            (%m)))
pfdietz's avatar
pfdietz committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  (t 2 1)
  (t 2 1)
  (nil 4 3))

;;; Allow-other-keys tests

(deftest macrolet.32
  (macrolet ((%m (&key a b c) `(quote (,a ,b ,c))))
    (values
     (%m :allow-other-keys nil)
     (%m :a 1 :allow-other-keys nil)
     (%m :allow-other-keys t)
     (%m :allow-other-keys t :allow-other-keys nil :foo t)
     (%m :allow-other-keys t :c 1 :b 2 :a 3)
     (%m :allow-other-keys nil :c 1 :b 2 :a 3)))
  (nil nil nil)
  (1 nil nil)
  (nil nil nil)
  (nil nil nil)
  (3 2 1)
  (3 2 1))

(deftest macrolet.33
  (macrolet ((%m (&key allow-other-keys) `(quote ,allow-other-keys)))
    (values
     (%m)
     (%m :allow-other-keys nil)
     (%m :allow-other-keys t :foo t)))
  nil
  nil
  t)

(deftest macrolet.34
  (macrolet ((%m (&key &allow-other-keys) :good))
    (values
     (%m)
     (%m :foo t)
     (%m :allow-other-keys nil :foo t)))
  :good
  :good
  :good)

(deftest macrolet.35
  (macrolet ((%m (&key a b &allow-other-keys) `(quote (,a ,b))))
    (values
     (%m :a 1)
     (%m :foo t :b 2)
     (%m :allow-other-keys nil :a 1 :foo t :b 2)))
  (1 nil)
  (nil 2)
  (1 2))

335 336 337 338 339 340
;;; &whole is followed by a destructuring pattern (see 3.4.4.1.2)
(deftest macrolet.36
  (macrolet ((%m (&whole (m a b) c d) `(quote (,m ,a ,b ,c ,d))))
    (%m 1 2))
  (%m 1 2 1 2))

341 342 343 344 345 346 347 348
;;; Macro names are shadowed by local functions

(deftest macrolet.37
  (macrolet ((%f () :bad))
    (flet ((%f () :good))
      (%f)))
  :good)

349 350 351 352 353
;;; The &environment parameter is bound first

(deftest macrolet.38
  (macrolet ((foo () 1))
    (macrolet ((%f (&optional (x (macroexpand '(foo) env)) &environment env)
sds's avatar
sds committed
354
                   x))
355 356 357
      (%f)))
  1)

pfdietz's avatar
pfdietz committed
358 359 360 361 362 363
;;; Test for bug that showed up in sbcl

(deftest macrolet.39
  (macrolet ((%m (()) :good)) (%m ()))
  :good)

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
;;; Test that macrolets accept declarations

(deftest macrolet.40
  (macrolet ((%x () t))
    (declare (optimize)))
  nil)

(deftest macrolet.41
  (macrolet ((%x () t))
    (declare (optimize))
    (declare (notinline identity)))
  nil)

(deftest macrolet.42
  (macrolet ((%x () t))
    (declare (optimize))
    (%x))
pfdietz's avatar
pfdietz committed
381
  t)
382 383

(deftest macrolet.43
384 385 386
  (let ((*x-in-macrolet.43* nil))
    (declare (special *x-in-macrolet.43*))
    (let ((*f* #'(lambda () *x-in-macrolet.43*)))
387
      (declare (special *f*))
388
      (eval `(macrolet ((%m (*x-in-macrolet.43*)
sds's avatar
sds committed
389 390 391
                            (declare (special *f*))
                            (funcall *f*)))
               (%m t)))))
392 393 394
  nil)

(deftest macrolet.44
395 396 397
  (let ((*x-in-macrolet.44* nil))
    (declare (special *x-in-macrolet.44*))
    (let ((*f* #'(lambda () *x-in-macrolet.44*)))
398
      (declare (special *f*))
399
      (eval `(macrolet ((%m (*x-in-macrolet.44*)
sds's avatar
sds committed
400 401 402
                            (declare (special *f* *x-in-macrolet.44*))
                            (funcall *f*)))
               (%m t)))))
403 404 405
  t)

(deftest macrolet.45
406 407 408
  (let ((*x-in-macrolet.45* nil))
    (declare (special *x-in-macrolet.45*))
    (let ((*f* #'(lambda () *x-in-macrolet.45*)))
409
      (declare (special *f*))
410
      (eval `(macrolet ((%m ((*x-in-macrolet.45*))
sds's avatar
sds committed
411 412 413
                            (declare (special *f* *x-in-macrolet.45*))
                            (funcall *f*)))
               (%m (t))))))
414 415
  t)

416 417 418 419
;;; Macros are expanded in the appropriate environment

(deftest macrolet.46
  (macrolet ((%m (z) z))
sds's avatar
sds committed
420
            (macrolet () (expand-in-current-env (%m :good))))
421 422
  :good)

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
;;; Free declarations in macrolet

(deftest macrolet.47
  (let ((x :good))
    (declare (special x))
    (let ((x :bad))
      (macrolet () (declare (special x)) x)))
  :good)

(deftest macrolet.48
  (let ((x :good))
    (let ((y :bad))
      (macrolet () (declare (ignore y)) x)))
  :good)

(deftest macrolet.49
  (let ((x :good))
    (let ((y :bad))
      (macrolet () (declare (ignorable y)) x)))
  :good)

444

445
;;; TODO: more special declarations for other macrolet arguments