Skip to content

Commit 406ee59

Browse files
committed
Disallow duplicate method defs in proxy macro
1 parent e9c3068 commit 406ee59

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
@@ -7055,7 +7055,6 @@
70557055

70567056
;; TODO: kwargs on supertypes
70577057
;; TODO: check interface/superclass method membership
7058-
;; TODO: check if duplicate methods
70597058
(defmacro proxy
70607059
"Create a new proxy class instance.
70617060

@@ -7091,21 +7090,30 @@
70917090
(let [formatted-single (fn [method-name [arg-vec & body]]
70927091
[(munge method-name)
70937092
(apply list 'fn method-name (vec (concat ['this] arg-vec)) body)])
7094-
formatted-multi (fn [method-name & arities]
7095-
[(munge method-name)
7096-
(apply list
7097-
'fn
7098-
method-name
7099-
(map (fn [[arg-vec & body]]
7100-
(apply list (vec (concat ['this] arg-vec)) body))
7101-
arities))])
7102-
methods (mapcat (fn [[method-name & body]]
7103-
(if (vector? (first body))
7104-
(formatted-single method-name body)
7105-
(apply formatted-multi method-name body)))
7106-
fs)]
7093+
formatted-multi (fn [method-name & arities]
7094+
[(munge method-name)
7095+
(apply list
7096+
'fn
7097+
method-name
7098+
(map (fn [[arg-vec & body]]
7099+
(apply list (vec (concat ['this] arg-vec)) body))
7100+
arities))])
7101+
methods (map (fn [[method-name & body]]
7102+
(if (vector? (first body))
7103+
(formatted-single method-name body)
7104+
(apply formatted-multi method-name body)))
7105+
fs)
7106+
method-map (reduce* (fn [m [method-name method]]
7107+
(if-let [existing-method (get m method-name)]
7108+
(throw
7109+
(ex-info "Cannot define proxy class with duplicate method"
7110+
{:method-name method-name
7111+
:impls [existing-method method]}))
7112+
(assoc m method-name method)))
7113+
{}
7114+
methods)]
71077115
#_(println methods)
7108-
`((get-proxy-class ~@class-and-interfaces) ~(apply hash-map methods) ~@args)))
7116+
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
71097117

71107118
(defmacro proxy-super
71117119
"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)