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

6

7 8 9 10 11

(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
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)

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
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))))
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)))
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)))
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)))
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)))
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))))
280
    (values (%m :a (1 2))
sds's avatar
sds committed
281 282
            (%m :a (1 2) :a (10 11))
            (%m)))
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 354
;;; 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
355
                   x))
356 357 358
      (%f)))
  1)

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

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

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
;;; 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
382
  t)
383 384

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

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

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

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

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

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
;;; 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)

445

446
;;; TODO: more special declarations for other macrolet arguments
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472


;;; macrolet shadows global macro, function and compiler-macro
;;; definitions

(defmacro macrolet.50 () :bad)

(deftest macrolet.50
   (macrolet ((macrolet.50 () :good))
     (macrolet.50))
   :good)

(defun macrolet.51 () :bad)

(deftest macrolet.51
   (macrolet ((macrolet.51 () :good))
      (macrolet.51))
   :good)

(define-compiler-macro macrolet.52 (&whole form)
  :bad)

(deftest macrolet.52
   (macrolet ((macrolet.52 () :good))
      (macrolet.52))
   :good)