Skip to content

Commit 9aba4a8

Browse files
authored
try improving check_top_bit error message (#53166)
Fixes #30247 @staticfloat this was your issue, so what do you think of this? I think the main problem was already solved (showing the wrong type), but I am wondering if this wording is still clearer, since it tries to align it with functions users might actually have encountered (trunc, convert, signbit)
1 parent d91a7fa commit 9aba4a8

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

base/boot.jl

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -744,12 +744,14 @@ function is_top_bit_set(x::Union{Int8,UInt8})
744744
eq_int(lshr_int(x, 7), trunc_int(typeof(x), 1))
745745
end
746746

747-
#TODO delete this function (but see #48097):
747+
# n.b. This function exists for CUDA to overload to configure error behavior (see #48097)
748748
throw_inexacterror(args...) = throw(InexactError(args...))
749749

750-
function check_top_bit(::Type{To}, x) where {To}
750+
function check_sign_bit(::Type{To}, x) where {To}
751751
@inline
752-
is_top_bit_set(x) && throw_inexacterror(:check_top_bit, To, x)
752+
# the top bit is the sign bit of x but "sign bit" sounds better in stacktraces
753+
# n.b. if x is signed, then sizeof(x) === sizeof(To), otherwise sizeof(x) >= sizeof(To)
754+
is_top_bit_set(x) && throw_inexacterror(sizeof(x) === sizeof(To) ? :convert : :trunc, To, x)
753755
x
754756
end
755757

@@ -774,22 +776,22 @@ toInt8(x::Int16) = checked_trunc_sint(Int8, x)
774776
toInt8(x::Int32) = checked_trunc_sint(Int8, x)
775777
toInt8(x::Int64) = checked_trunc_sint(Int8, x)
776778
toInt8(x::Int128) = checked_trunc_sint(Int8, x)
777-
toInt8(x::UInt8) = bitcast(Int8, check_top_bit(Int8, x))
778-
toInt8(x::UInt16) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
779-
toInt8(x::UInt32) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
780-
toInt8(x::UInt64) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
781-
toInt8(x::UInt128) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
779+
toInt8(x::UInt8) = bitcast(Int8, check_sign_bit(Int8, x))
780+
toInt8(x::UInt16) = checked_trunc_sint(Int8, check_sign_bit(Int8, x))
781+
toInt8(x::UInt32) = checked_trunc_sint(Int8, check_sign_bit(Int8, x))
782+
toInt8(x::UInt64) = checked_trunc_sint(Int8, check_sign_bit(Int8, x))
783+
toInt8(x::UInt128) = checked_trunc_sint(Int8, check_sign_bit(Int8, x))
782784
toInt8(x::Bool) = and_int(bitcast(Int8, x), Int8(1))
783785
toInt16(x::Int8) = sext_int(Int16, x)
784786
toInt16(x::Int16) = x
785787
toInt16(x::Int32) = checked_trunc_sint(Int16, x)
786788
toInt16(x::Int64) = checked_trunc_sint(Int16, x)
787789
toInt16(x::Int128) = checked_trunc_sint(Int16, x)
788790
toInt16(x::UInt8) = zext_int(Int16, x)
789-
toInt16(x::UInt16) = bitcast(Int16, check_top_bit(Int16, x))
790-
toInt16(x::UInt32) = checked_trunc_sint(Int16, check_top_bit(Int16, x))
791-
toInt16(x::UInt64) = checked_trunc_sint(Int16, check_top_bit(Int16, x))
792-
toInt16(x::UInt128) = checked_trunc_sint(Int16, check_top_bit(Int16, x))
791+
toInt16(x::UInt16) = bitcast(Int16, check_sign_bit(Int16, x))
792+
toInt16(x::UInt32) = checked_trunc_sint(Int16, check_sign_bit(Int16, x))
793+
toInt16(x::UInt64) = checked_trunc_sint(Int16, check_sign_bit(Int16, x))
794+
toInt16(x::UInt128) = checked_trunc_sint(Int16, check_sign_bit(Int16, x))
793795
toInt16(x::Bool) = and_int(zext_int(Int16, x), Int16(1))
794796
toInt32(x::Int8) = sext_int(Int32, x)
795797
toInt32(x::Int16) = sext_int(Int32, x)
@@ -798,9 +800,9 @@ toInt32(x::Int64) = checked_trunc_sint(Int32, x)
798800
toInt32(x::Int128) = checked_trunc_sint(Int32, x)
799801
toInt32(x::UInt8) = zext_int(Int32, x)
800802
toInt32(x::UInt16) = zext_int(Int32, x)
801-
toInt32(x::UInt32) = bitcast(Int32, check_top_bit(Int32, x))
802-
toInt32(x::UInt64) = checked_trunc_sint(Int32, check_top_bit(Int32, x))
803-
toInt32(x::UInt128) = checked_trunc_sint(Int32, check_top_bit(Int32, x))
803+
toInt32(x::UInt32) = bitcast(Int32, check_sign_bit(Int32, x))
804+
toInt32(x::UInt64) = checked_trunc_sint(Int32, check_sign_bit(Int32, x))
805+
toInt32(x::UInt128) = checked_trunc_sint(Int32, check_sign_bit(Int32, x))
804806
toInt32(x::Bool) = and_int(zext_int(Int32, x), Int32(1))
805807
toInt64(x::Int8) = sext_int(Int64, x)
806808
toInt64(x::Int16) = sext_int(Int64, x)
@@ -810,8 +812,8 @@ toInt64(x::Int128) = checked_trunc_sint(Int64, x)
810812
toInt64(x::UInt8) = zext_int(Int64, x)
811813
toInt64(x::UInt16) = zext_int(Int64, x)
812814
toInt64(x::UInt32) = zext_int(Int64, x)
813-
toInt64(x::UInt64) = bitcast(Int64, check_top_bit(Int64, x))
814-
toInt64(x::UInt128) = checked_trunc_sint(Int64, check_top_bit(Int64, x))
815+
toInt64(x::UInt64) = bitcast(Int64, check_sign_bit(Int64, x))
816+
toInt64(x::UInt128) = checked_trunc_sint(Int64, check_sign_bit(Int64, x))
815817
toInt64(x::Bool) = and_int(zext_int(Int64, x), Int64(1))
816818
toInt128(x::Int8) = sext_int(Int128, x)
817819
toInt128(x::Int16) = sext_int(Int128, x)
@@ -822,9 +824,9 @@ toInt128(x::UInt8) = zext_int(Int128, x)
822824
toInt128(x::UInt16) = zext_int(Int128, x)
823825
toInt128(x::UInt32) = zext_int(Int128, x)
824826
toInt128(x::UInt64) = zext_int(Int128, x)
825-
toInt128(x::UInt128) = bitcast(Int128, check_top_bit(Int128, x))
827+
toInt128(x::UInt128) = bitcast(Int128, check_sign_bit(Int128, x))
826828
toInt128(x::Bool) = and_int(zext_int(Int128, x), Int128(1))
827-
toUInt8(x::Int8) = bitcast(UInt8, check_top_bit(UInt8, x))
829+
toUInt8(x::Int8) = bitcast(UInt8, check_sign_bit(UInt8, x))
828830
toUInt8(x::Int16) = checked_trunc_uint(UInt8, x)
829831
toUInt8(x::Int32) = checked_trunc_uint(UInt8, x)
830832
toUInt8(x::Int64) = checked_trunc_uint(UInt8, x)
@@ -835,8 +837,8 @@ toUInt8(x::UInt32) = checked_trunc_uint(UInt8, x)
835837
toUInt8(x::UInt64) = checked_trunc_uint(UInt8, x)
836838
toUInt8(x::UInt128) = checked_trunc_uint(UInt8, x)
837839
toUInt8(x::Bool) = and_int(bitcast(UInt8, x), UInt8(1))
838-
toUInt16(x::Int8) = sext_int(UInt16, check_top_bit(UInt16, x))
839-
toUInt16(x::Int16) = bitcast(UInt16, check_top_bit(UInt16, x))
840+
toUInt16(x::Int8) = sext_int(UInt16, check_sign_bit(UInt16, x))
841+
toUInt16(x::Int16) = bitcast(UInt16, check_sign_bit(UInt16, x))
840842
toUInt16(x::Int32) = checked_trunc_uint(UInt16, x)
841843
toUInt16(x::Int64) = checked_trunc_uint(UInt16, x)
842844
toUInt16(x::Int128) = checked_trunc_uint(UInt16, x)
@@ -846,9 +848,9 @@ toUInt16(x::UInt32) = checked_trunc_uint(UInt16, x)
846848
toUInt16(x::UInt64) = checked_trunc_uint(UInt16, x)
847849
toUInt16(x::UInt128) = checked_trunc_uint(UInt16, x)
848850
toUInt16(x::Bool) = and_int(zext_int(UInt16, x), UInt16(1))
849-
toUInt32(x::Int8) = sext_int(UInt32, check_top_bit(UInt32, x))
850-
toUInt32(x::Int16) = sext_int(UInt32, check_top_bit(UInt32, x))
851-
toUInt32(x::Int32) = bitcast(UInt32, check_top_bit(UInt32, x))
851+
toUInt32(x::Int8) = sext_int(UInt32, check_sign_bit(UInt32, x))
852+
toUInt32(x::Int16) = sext_int(UInt32, check_sign_bit(UInt32, x))
853+
toUInt32(x::Int32) = bitcast(UInt32, check_sign_bit(UInt32, x))
852854
toUInt32(x::Int64) = checked_trunc_uint(UInt32, x)
853855
toUInt32(x::Int128) = checked_trunc_uint(UInt32, x)
854856
toUInt32(x::UInt8) = zext_int(UInt32, x)
@@ -857,22 +859,22 @@ toUInt32(x::UInt32) = x
857859
toUInt32(x::UInt64) = checked_trunc_uint(UInt32, x)
858860
toUInt32(x::UInt128) = checked_trunc_uint(UInt32, x)
859861
toUInt32(x::Bool) = and_int(zext_int(UInt32, x), UInt32(1))
860-
toUInt64(x::Int8) = sext_int(UInt64, check_top_bit(UInt64, x))
861-
toUInt64(x::Int16) = sext_int(UInt64, check_top_bit(UInt64, x))
862-
toUInt64(x::Int32) = sext_int(UInt64, check_top_bit(UInt64, x))
863-
toUInt64(x::Int64) = bitcast(UInt64, check_top_bit(UInt64, x))
862+
toUInt64(x::Int8) = sext_int(UInt64, check_sign_bit(UInt64, x))
863+
toUInt64(x::Int16) = sext_int(UInt64, check_sign_bit(UInt64, x))
864+
toUInt64(x::Int32) = sext_int(UInt64, check_sign_bit(UInt64, x))
865+
toUInt64(x::Int64) = bitcast(UInt64, check_sign_bit(UInt64, x))
864866
toUInt64(x::Int128) = checked_trunc_uint(UInt64, x)
865867
toUInt64(x::UInt8) = zext_int(UInt64, x)
866868
toUInt64(x::UInt16) = zext_int(UInt64, x)
867869
toUInt64(x::UInt32) = zext_int(UInt64, x)
868870
toUInt64(x::UInt64) = x
869871
toUInt64(x::UInt128) = checked_trunc_uint(UInt64, x)
870872
toUInt64(x::Bool) = and_int(zext_int(UInt64, x), UInt64(1))
871-
toUInt128(x::Int8) = sext_int(UInt128, check_top_bit(UInt128, x))
872-
toUInt128(x::Int16) = sext_int(UInt128, check_top_bit(UInt128, x))
873-
toUInt128(x::Int32) = sext_int(UInt128, check_top_bit(UInt128, x))
874-
toUInt128(x::Int64) = sext_int(UInt128, check_top_bit(UInt128, x))
875-
toUInt128(x::Int128) = bitcast(UInt128, check_top_bit(UInt128, x))
873+
toUInt128(x::Int8) = sext_int(UInt128, check_sign_bit(UInt128, x))
874+
toUInt128(x::Int16) = sext_int(UInt128, check_sign_bit(UInt128, x))
875+
toUInt128(x::Int32) = sext_int(UInt128, check_sign_bit(UInt128, x))
876+
toUInt128(x::Int64) = sext_int(UInt128, check_sign_bit(UInt128, x))
877+
toUInt128(x::Int128) = bitcast(UInt128, check_sign_bit(UInt128, x))
876878
toUInt128(x::UInt8) = zext_int(UInt128, x)
877879
toUInt128(x::UInt16) = zext_int(UInt128, x)
878880
toUInt128(x::UInt32) = zext_int(UInt128, x)

src/common_symbols2.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jl_symbol("structdiff"),
88
jl_symbol("UnitRange"),
99
jl_symbol("unitrange_last"),
1010
jl_symbol("sizeof"),
11-
jl_symbol("check_top_bit"),
11+
jl_symbol("check_sign_bit"),
1212
jl_symbol("is_top_bit_set"),
1313
jl_symbol("data"),
1414
jl_symbol("kwerr"),

0 commit comments

Comments
 (0)