Skip to content

Commit 39addb7

Browse files
authored
Fix multiple small bugs in basilisp.core (#1232)
Fixes #1230 Fixes #1231
1 parent aa12514 commit 39addb7

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9-
* Added support for referring imported Python names as by `from ... import ...` (#1154)
9+
* Added support for referring imported Python names as by `from ... import ...` (#1154)
10+
11+
### Fixed
12+
* Fix a bug where protocols with methods with leading hyphens in the could not be defined (#1230)
13+
* Fix a bug where attempting to `:refer` a non-existent Var from another namespace would throw an unhelpful exception (#1231)
1014

1115
## [v0.3.8]
1216
### Added

src/basilisp/core.lpy

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5235,8 +5235,12 @@
52355235
(seq refer-opt)
52365236
(let [new-ns-interns (ns-interns new-ns)]
52375237
(doseq [var-sym refer-opt]
5238-
(let [var (get new-ns-interns var-sym)]
5239-
(.add-refer current-ns var-sym var))))
5238+
(if-let [var (get new-ns-interns var-sym)]
5239+
(.add-refer current-ns var-sym var)
5240+
(throw
5241+
(ex-info "Cannot find Var in the given namespace"
5242+
{:namespace new-ns
5243+
:var var-sym})))))
52405244

52415245
:else nil)))))
52425246
nil))
@@ -6463,18 +6467,28 @@
64636467
:method ~method-name
64646468
:object-type obj-type#})))))
64656469
arglists))))
6466-
(let [dotted-method-name (symbol (str "." (name method-name)))]
6467-
`(.register ~(vary-meta method-name assoc :no-warn-on-var-indirection true)
6468-
~interface-name
6469-
(fn ~method-name
6470-
~@(map (fn [[obj-sym & args]]
6471-
(let [has-varargs (some #(= '& %) args)
6472-
clean-args (filter #(not= '& %) args)]
6473-
(list `[~obj-sym ~@args]
6474-
(if has-varargs
6475-
`(apply-method ~obj-sym ~method-name ~@clean-args)
6476-
`(~dotted-method-name ~obj-sym ~@clean-args)))))
6477-
arglists))))]))
6470+
`(.register ~(vary-meta method-name assoc :no-warn-on-var-indirection true)
6471+
~interface-name
6472+
(fn ~method-name
6473+
~@(map (fn [[obj-sym & args]]
6474+
(let [has-varargs (some #(= '& %) args)
6475+
clean-args (filter #(not= '& %) args)]
6476+
(list `[~obj-sym ~@args]
6477+
(cond
6478+
has-varargs
6479+
`(apply-method ~obj-sym ~method-name ~@clean-args)
6480+
6481+
;; methods starting with "-" will be interpreted
6482+
;; by the compiler as a property access (which
6483+
;; prohibit arguments to be provided), so we have
6484+
;; to hack around that by accessing the method as
6485+
;; a property and then calling it
6486+
(.startswith (name method-name) "-")
6487+
`((. ~obj-sym ~method-name) ~@clean-args)
6488+
6489+
:else
6490+
`(. ~obj-sym ~method-name ~@clean-args)))))
6491+
arglists)))]))
64786492

64796493
;; For each protocol, the following keys are defined:
64806494
;;

tests/basilisp/test_core_fns.lpy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,9 @@
17781778
(ex-info "This is an exception" {})))))
17791779
(is (= :a redeffable))))
17801780

1781+
(deftest require-test
1782+
(is (thrown? basilisp.lang.exception/ExceptionInfo (require '[basilisp.set :refer [non-existent-var]]))))
1783+
17811784
(deftest requiring-resolve-test
17821785
(is (thrown? basilisp.lang.exception/ExceptionInfo (requiring-resolve 'a-symbol)))
17831786
(is (thrown? basilisp.lang.exception/ExceptionInfo (requiring-resolve :a-keyword)))

tests/basilisp/test_protocols.lpy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
[basilisp.string :as str]
55
[basilisp.test :refer [deftest is are testing]]))
66

7+
(defprotocol ProtocolWithLeadingHyphenMethod
8+
(-method [this] [this arg1] [this arg1 & args]))
9+
10+
(extend-protocol ProtocolWithLeadingHyphenMethod
11+
python/str
12+
(-method [this]
13+
"no args")
14+
(-method [this arg1]
15+
"1 arg")
16+
(-method [this arg1 & args]
17+
"varargs"))
18+
19+
(deftest protocol-method-name-test
20+
(is (= "no args" (-method "")))
21+
(is (= "1 arg" (-method "" "other")))
22+
(is (= "varargs" (-method "" "other" "rest"))))
23+
724
(defprotocol Shape
825
(area [this]))
926

0 commit comments

Comments
 (0)