Skip to content

Commit be2448a

Browse files
committed
Disallow duplicate method defs in proxy macro
1 parent 368536f commit be2448a

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
@@ -7104,7 +7104,6 @@
71047104

71057105
;; TODO: kwargs on supertypes
71067106
;; TODO: check interface/superclass method membership
7107-
;; TODO: check if duplicate methods
71087107
(defmacro proxy
71097108
"Create a new proxy class instance.
71107109

@@ -7140,21 +7139,30 @@
71407139
(let [formatted-single (fn [method-name [arg-vec & body]]
71417140
[(munge method-name)
71427141
(apply list 'fn method-name (vec (concat ['this] arg-vec)) body)])
7143-
formatted-multi (fn [method-name & arities]
7144-
[(munge method-name)
7145-
(apply list
7146-
'fn
7147-
method-name
7148-
(map (fn [[arg-vec & body]]
7149-
(apply list (vec (concat ['this] arg-vec)) body))
7150-
arities))])
7151-
methods (mapcat (fn [[method-name & body]]
7152-
(if (vector? (first body))
7153-
(formatted-single method-name body)
7154-
(apply formatted-multi method-name body)))
7155-
fs)]
7142+
formatted-multi (fn [method-name & arities]
7143+
[(munge method-name)
7144+
(apply list
7145+
'fn
7146+
method-name
7147+
(map (fn [[arg-vec & body]]
7148+
(apply list (vec (concat ['this] arg-vec)) body))
7149+
arities))])
7150+
methods (map (fn [[method-name & body]]
7151+
(if (vector? (first body))
7152+
(formatted-single method-name body)
7153+
(apply formatted-multi method-name body)))
7154+
fs)
7155+
method-map (reduce* (fn [m [method-name method]]
7156+
(if-let [existing-method (get m method-name)]
7157+
(throw
7158+
(ex-info "Cannot define proxy class with duplicate method"
7159+
{:method-name method-name
7160+
:impls [existing-method method]}))
7161+
(assoc m method-name method)))
7162+
{}
7163+
methods)]
71567164
#_(println methods)
7157-
`((get-proxy-class ~@class-and-interfaces) ~(apply hash-map methods) ~@args)))
7165+
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
71587166

71597167
(defmacro proxy-super
71607168
"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)