Restrict signed(x) = reinterpret(...)
to BitIntegers.
#33459
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #30473
This is a minor PR, but it fixes an inconsistency in Base that was brought up in #30473 and was blocking the merge of #30445 (comment). :)
I also reordered the definitions to clean them up a bit. :)☺️ )
(Note that I also deleted the extra method for unsigned(x::Bool) because it's already covered by the default
unsigned(x) = convert(Unsigned, x)
(sinceBool <: Base.BitSigned) == false
), and it was a redundant copy/paste.This commit restricts the
reinterpret
implementation ofsigned(x)
to only apply to
Base.BitInteger
s. This brings its behavior in-linewith
unsigned(x)
.Previously, although
unsigned(x)
did restrict thereinterpret
implementation to only apply to
Base.BitInteger
s,signed(x)
bydefault called
reinterpret(Signed, x)
for any typeT <: Unsigned
.This of course wouldn't work for
BigInt
, which is presumably whyunsigned(x)
was restricted (sinceBigInt <: Signed
but notBigInt <: BitInteger
), but although Base contains no non-BitInteger types inUnsigned
(besidesBool
), of course users could create such types,so this should be restricted symmetrically.
Below is an example of the previous, unexpected broken behavior (curtesy @timholy):
Whereas after this commit, this works as expected, calling the
Signed
constructor:
This should be a non-breaking change. If there exist any
primitive type
implementations that were relying on the reinterpretimplementation, they may see a slight performance regression, as this
will by default now call
Signed(x::T)
, instead ofreinterpret(typeof(Signed(x)), x)
-- so they may want to manuallydefine the reinterpret implemention themselves.
CC: @JeffreySarnoff, @timholy