-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/apply lint #269
Fix/apply lint #269
Conversation
Codecov Report
@@ Coverage Diff @@
## master #269 +/- ##
==========================================
- Coverage 88.85% 88.85% -0.01%
==========================================
Files 78 78
Lines 6444 6496 +52
Branches 454 456 +2
==========================================
+ Hits 5726 5772 +46
- Misses 264 268 +4
- Partials 454 456 +2
|
81f0ff5
to
2e74d09
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I checked that changes doesn't break the orignal codes, and it sounds good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this, @niyarin! I've added some comments.
I would like to take this chance to resolve boxed maths, too.
After playing around with the codes, I found that there are several varieties of this problem.
The biggest difference is that the warning is raised by clojure compiler or by eastwood. It seems like that there are cases that the compiler handles primitive types properly but eastwood can't recognize the types.
For example, cljam.io.util.bgzf/get-block-address
is reported as boxed math by lein eastwood '{:linters [:boxed-math] :namespaces [cljam.io.util.bgzf]}'
but not by lein update-in :global-vars assoc '*unchecked-math*' :warn-on-boxed -- check
.
This is because of ^:const
metadata attached to the vars. The compiler perform inlining of such vars to replace them with its values which are primitive numeric literals, obviously typed.
(set! *unchecked-math* :warn-on-boxed)
(def x 1)
(+ x 1)
;; Boxed math warning, call: public static java.lang.Number clojure.lang.Numbers.unchecked_add(java.lang.Object,long).
(def ^:const x 1)
(+ x 1)
So this is a bug in the linter and there is no practical problem at all, and they will be compiled and executed as a primitive-type arithmetic operation. This kind of reports can be ignored.
For cases that affect the actual bytecode, let's use this PR to correct the problem, checking with lein check
and not eastwood.
First, we will address function definitions as follows.
For functions that always return a non-null primitive value, prefix the return primitive hint with the parameter vector. (e.g. cljam.common/get-exec-n-threads
) This will make the return type primitive on direct caller via var.
Also, attach a primitive hint to a parameter that is always a primitive value. Note that the parameter symbol need to be top-level and must not be destructured. (e.g. cljam.io.bam-index.reader/read-bin-index*!
) This makes the type of parameters in function definitions primitive, and also allows function calls directly via var to be made with primitive arguments.
A reference to a dynamic var that is bound to a primitive value is not resolved to a primitive type as is, so we need to cast it. (e.g. cljam.common/*n-threads*
) Since other than long/double can be used for casts, select the appropriate size.
A var bound to a primitive value that can be ^:const
should be marked as ^:const
. (e.g. cljam.io.bam.encoder/fixed-tag-size
)
Non-primitive values that are ^:const
do not need a type hint. (e.g. cljam.io.csi/csi-magic
)
For destructuring of parameters, local bindings, and partial expressions, avoid primitive hints and cast instead. (e.g. cljam.io.bcf.reader/reader
)
However, the cast from Character
to byte
/short
fails when *unchecked-math*
is not nil
, so cast it toint
before casting to byte
/short
. (e.g. cljam.io.pileup/upper-table
)
(set! *unchecked-math* nil)
(byte \A) ;; => 65
(short \A) ;; => 65
(int \A) ;; => 65
(long \A) ;; => 65
(set! *unchecked-math* :warn-on-boxed)
(byte \A)
;; Execution error (ClassCastException)
;; class java.lang.Character cannot be cast to class java.lang.Number (java.lang.Character and java.lang.Number are in module java.base of loader 'bootstrap')
(short \A)
;; Execution error (ClassCastException)
;; class java.lang.Character cannot be cast to class java.lang.Number (java.lang.Character and java.lang.Number are in module java.base of loader 'bootstrap')
(int \A) ;; => 65
(long \A) ;; => 65
I have pushed an incomplete commit to spike/boxed-math
and hope it will be helpful.
89db329
to
78cd045
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@niyarin Thank you for updating the PR! I added a few extra comments, but basically the changes seem to be fine👍.
38e5c5e
to
e6424b3
Compare
@alumi |
I'll add
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for updating. There seems to be several points left that need to be fixed.
@alumi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @niyarin! LGTM 👍
Would you squash your commits as follows before merging?
- Replace
::set-output
with$GITHUB_OUTPUT
- Change linter options
- Fix return type hints
- Fix boxed math warnings
@niyarin Additionally, if you have time, would you consider whether some optional (default level is |
I fixed almost bad coding style, except Boxed-math, which I couldn't fix.
Clj-kondo
master
This PR
Eastwood (exclude boxed-math)
Master
This PR