Skip to content

Commit cecb9af

Browse files
committed
CLJS-2247: Warn when overwriting protocol method
1 parent 4664996 commit cecb9af

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
:protocol-duped-method true
149149
:protocol-multiple-impls true
150150
:protocol-with-variadic-method true
151+
:protocol-with-overwriting-method true
151152
:protocol-impl-with-variadic-method true
152153
:protocol-impl-recur-with-target true
153154
:single-segment-namespace true
@@ -398,6 +399,14 @@
398399
(str "Protocol " (:protocol info) " declares method "
399400
(:name info) " with variadic signature (&)"))
400401

402+
(defmethod error-message :protocol-with-overwriting-method
403+
[warning-type info]
404+
(let [overwritten-protocol (-> info :existing :protocol)]
405+
(str "Protocol " (:protocol info) " is overwriting "
406+
(if overwritten-protocol "method" "function")
407+
" " (:name info)
408+
(when overwritten-protocol (str " of protocol " (name overwritten-protocol))))))
409+
401410
(defmethod error-message :protocol-impl-with-variadic-method
402411
[warning-type info]
403412
(str "Protocol " (:protocol info) " implements method "

src/main/clojure/cljs/core.cljc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,11 @@
21212121
(cljs.analyzer/warning
21222122
:protocol-with-variadic-method
21232123
&env {:protocol psym :name fname}))
2124+
_ (core/when-some [existing (core/get (-> &env :ns :defs) fname)]
2125+
(core/when-not (= p (:protocol existing))
2126+
(cljs.analyzer/warning
2127+
:protocol-with-overwriting-method
2128+
{} {:protocol psym :name fname :existing existing})))
21242129
slot (symbol (core/str prefix (munge (name fname))))
21252130
fname (vary-meta fname assoc
21262131
:protocol p

src/test/clojure/cljs/analyzer_tests.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,18 @@
18751875
:with-core? true})]
18761876
(is (zero? (count @ws)))))
18771877

1878+
(deftest test-cljs-2247
1879+
(let [ws (atom [])]
1880+
(try
1881+
(a/with-warning-handlers [(collecting-warning-handler ws)]
1882+
(e/with-compiler-env (assoc @test-cenv :repl-env {})
1883+
(a/analyze (ana/empty-env)
1884+
'(defn -foo []))
1885+
(a/analyze (ana/empty-env)
1886+
'(defprotocol IAlpha (-foo [this])))))
1887+
(catch Exception _))
1888+
(is (= ["Protocol IAlpha is overwriting function -foo"] @ws))))
1889+
18781890
(deftest test-cljs-2385-infer-priority
18791891
(let [ws (atom [])
18801892
res (infer-test-helper

0 commit comments

Comments
 (0)