Skip to content

Commit e493ea4

Browse files
committed
Disallow duplicate method defs in proxy macro
1 parent dde1107 commit e493ea4

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/basilisp/core.lpy

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6998,7 +6998,6 @@
69986998

69996999
;; TODO: kwargs on supertypes
70007000
;; TODO: check interface/superclass method membership
7001-
;; TODO: check if duplicate methods
70027001
(defmacro proxy
70037002
"Create a new proxy class instance.
70047003

@@ -7034,21 +7033,30 @@
70347033
(let [formatted-single (fn [method-name [arg-vec & body]]
70357034
[(munge method-name)
70367035
(apply list 'fn method-name (vec (concat ['this] arg-vec)) body)])
7037-
formatted-multi (fn [method-name & arities]
7038-
[(munge method-name)
7039-
(apply list
7040-
'fn
7041-
method-name
7042-
(map (fn [[arg-vec & body]]
7043-
(apply list (vec (concat ['this] arg-vec)) body))
7044-
arities))])
7045-
methods (mapcat (fn [[method-name & body]]
7046-
(if (vector? (first body))
7047-
(formatted-single method-name body)
7048-
(apply formatted-multi method-name body)))
7049-
fs)]
7036+
formatted-multi (fn [method-name & arities]
7037+
[(munge method-name)
7038+
(apply list
7039+
'fn
7040+
method-name
7041+
(map (fn [[arg-vec & body]]
7042+
(apply list (vec (concat ['this] arg-vec)) body))
7043+
arities))])
7044+
methods (map (fn [[method-name & body]]
7045+
(if (vector? (first body))
7046+
(formatted-single method-name body)
7047+
(apply formatted-multi method-name body)))
7048+
fs)
7049+
method-map (reduce* (fn [m [method-name method]]
7050+
(if-let [existing-method (get m method-name)]
7051+
(throw
7052+
(ex-info "Cannot define proxy class with duplicate method"
7053+
{:method-name method-name
7054+
:impls [existing-method method]}))
7055+
(assoc m method-name method)))
7056+
{}
7057+
methods)]
70507058
#_(println methods)
7051-
`((get-proxy-class ~@class-and-interfaces) ~(apply hash-map methods) ~@args)))
7059+
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
70527060

70537061
(defmacro proxy-super
70547062
"Macro which expands to a call to the method named ``meth`` on the superclass

tests/basilisp/test_proxies.lpy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
(is (thrown? basilisp.lang.exception/ExceptionInfo
2121
(construct-proxy python/object)))))
2222

23+
(definterface Describable
24+
(describe-me []))
25+
2326
(definterface ToString
2427
(to-string [])
2528
(to-string [arg1])
@@ -33,6 +36,11 @@
3336
(to-string [this arg1 & rest] (str "rest" arg1 rest)))
3437

3538
(deftest proxy-test
39+
(testing "disallows duplicate method overrides"
40+
(proxy [Describable] []
41+
(describe-me [] "I'm a proxy")
42+
(describe-me [] "Proxy")))
43+
3644
(testing "multi-arity interface methods"
3745
(let [p (proxy [ConcreteToString] [1]
3846
(to-string

0 commit comments

Comments
 (0)