Skip to content

Commit 04f033c

Browse files
committed
overload: Prefer passing literal 0 to ints instead of ptrs
hCalcTypesDiff() used to just return a FB_OVLPROC_HALFMATCH score when passing a literal zero with some integer type to a pointer parameter. This meant that such matches would have higher scores than any int-to-int matches (e.g. passing a literal zero of some integer type to some integer parameter), because those are scored via FB_OVLPROC_HALFMATCH - symb_dtypeMatchTB..., which will always produce lower values than FB_OVLPROC_HALFMATCH. However, I think that integer parameters should be preferred over pointer parameters when passing in an integer argument (even if it's a literal 0). Thus hCalcTypesDiff() now returns 1 instead of FB_OVLPROC_HALFMATCH when passing a literal 0 to a pointer. This case should still be allowed, but just rated lower than normal integer-to-integer matches. Thus it will only be the last resort, not the first choice.
1 parent dcf88c5 commit 04f033c

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Version 1.01.0
5858
- BLOAD was misreading bitfields in BMP files with undocumented BITMAPV3HEADER format (56-byte headers)
5959
- PRINT now disallows commas/newlines after SPC/TAB, instead of silently ignoring them
6060
- 0.90.0 regression: Compiler crash during error recovery for expressions like 'type<UDT>().field' where initializer values are missing
61+
- When passing a constant zero of some integer to an overloaded procedure, overloads with integer parameters will now be preferred over overloads with pointers (literal 0 can still be passed to pointer parameters in this case though, by casting it to the pointer type)
6162

6263

6364
Version 1.00.0 (former 0.91.0):

src/compiler/symb-proc.bas

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,13 @@ private function hCalcTypesDiff _
17181718
return 0
17191719
end if
17201720

1721-
return FB_OVLPROC_HALFMATCH
1721+
'' Allow passing a literal 0 integer to a pointer parameter,
1722+
'' but give it a very low score, such that we will prefer passing
1723+
'' the literal 0 integer to actual integer parameters (which are
1724+
'' scored based on FB_OVLPROC_HALFMATCH - symb_dtypeMatchTB...,
1725+
'' which is why we have to choose something that's hopefully lower
1726+
'' but still > 0 here).
1727+
return 1
17221728
end if
17231729

17241730
'' Both are pointers (but they're different,

tests/overload/integer_width.bas

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace fbc_tests.overloads.integer_width
1111
end function
1212

1313
sub zee_test cdecl( )
14-
#ifdef __FB_64BIT__
15-
CU_ASSERT_EQUAL( hexa(0ull), 1 )
16-
#else
1714
CU_ASSERT_EQUAL( hexa(0ull), 2 )
18-
#endif
15+
CU_ASSERT_EQUAL( hexa(1ull), 2 )
16+
CU_ASSERT_EQUAL( hexa(&hFFFFFFFFull), 2 )
17+
CU_ASSERT_EQUAL( hexa(&hFFFFFFFF00000000ull), 2 )
18+
CU_ASSERT_EQUAL( hexa(&hFFFFFFFFFFFFFFFFull), 2 )
1919
CU_ASSERT_EQUAL( hexa(1ull shl 32), 2 )
2020
end sub
2121

tests/overload/pointers.bas

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,72 @@ namespace integerPointers
791791
end namespace
792792
end namespace
793793

794+
namespace const0
795+
'' It's possible to pass constant zero to pointer parameters
796+
797+
sub a overload( byval p as any ptr ) : : end sub
798+
sub a overload( byref s as string ) : CU_FAIL( ) : end sub
799+
800+
#macro makeFunction1( suffix, inttype )
801+
function f1_##suffix overload( byval p as any ptr ) as string : function = "any ptr" : end function
802+
function f1_##suffix overload( byval i as inttype ) as string : function = #inttype : end function
803+
#endmacro
804+
805+
makeFunction1( b, byte )
806+
makeFunction1( ub, ubyte )
807+
makeFunction1( sh, short )
808+
makeFunction1( ush, ushort )
809+
makeFunction1( l, long )
810+
makeFunction1( ul, ulong )
811+
makeFunction1( ll, longint )
812+
makeFunction1( ull, ulongint )
813+
makeFunction1( i, integer )
814+
makeFunction1( ui, uinteger )
815+
816+
private sub test cdecl( )
817+
a( cptr( any ptr, 0 ) )
818+
a( 0 )
819+
a( cint( 0 ) )
820+
a( cuint( 0 ) )
821+
#ifdef __FB_64BIT__
822+
a( 0ll )
823+
a( 0ull )
824+
a( clngint( 0 ) )
825+
a( culngint( 0 ) )
826+
#else
827+
a( 0l )
828+
a( 0ul )
829+
a( clng( 0 ) )
830+
a( culng( 0 ) )
831+
#endif
832+
833+
#macro makeTest1( suffix, result )
834+
CU_ASSERT( f1_##suffix( cptr( any ptr, 0 ) ) = "any ptr" )
835+
CU_ASSERT( f1_##suffix( cbyte ( 0 ) ) = #result )
836+
CU_ASSERT( f1_##suffix( cubyte ( 0 ) ) = #result )
837+
CU_ASSERT( f1_##suffix( cshort ( 0 ) ) = #result )
838+
CU_ASSERT( f1_##suffix( cushort( 0 ) ) = #result )
839+
CU_ASSERT( f1_##suffix( 0l ) = #result )
840+
CU_ASSERT( f1_##suffix( 0ul ) = #result )
841+
CU_ASSERT( f1_##suffix( 0ll ) = #result )
842+
CU_ASSERT( f1_##suffix( 0ull ) = #result )
843+
CU_ASSERT( f1_##suffix( 0 ) = #result )
844+
CU_ASSERT( f1_##suffix( 0u ) = #result )
845+
#endmacro
846+
847+
makeTest1( b, byte )
848+
makeTest1( ub, ubyte )
849+
makeTest1( sh, short )
850+
makeTest1( ush, ushort )
851+
makeTest1( l, long )
852+
makeTest1( ul, ulong )
853+
makeTest1( ll, longint )
854+
makeTest1( ull, ulongint )
855+
makeTest1( i, integer )
856+
makeTest1( ui, uinteger )
857+
end sub
858+
end namespace
859+
794860
private sub ctor( ) constructor
795861
fbcu.add_suite( "tests/overload/pointers" )
796862
fbcu.add_test( "pointers", @pointers.test )
@@ -832,6 +898,8 @@ private sub ctor( ) constructor
832898
fbcu.add_test( "integerPointers.uinteger_longint" , @integerPointers.uinteger_longint.test )
833899
fbcu.add_test( "integerPointers.uinteger_ulongint", @integerPointers.uinteger_ulongint.test )
834900
fbcu.add_test( "integerPointers.uinteger_integer" , @integerPointers.uinteger_integer.test )
901+
902+
fbcu.add_test( "const0", @const0.test )
835903
end sub
836904

837905
end namespace

0 commit comments

Comments
 (0)