Skip to content

Commit 616dc23

Browse files
committed
Fix emitting of CONST ref initializers (as opposed to OFFSETs)
The CONST code path in hFlushExprStatic() used the symbol type as-is, instead of converting to pointer type for references. Thus the type looked different and it attempted the astNewCONV() which could easily fail (because the init expression is a pointer, while the symbol could be anything, e.g. struct/string).
1 parent 6d3763b commit 616dc23

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/compiler/ast-node-typeini.bas

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,15 @@ private sub hFlushExprStatic( byval n as ASTNODE ptr, byval basesym as FBSYMBOL
522522
if( sym = NULL ) then
523523
sym = basesym
524524
end if
525+
525526
var sdtype = symbGetType( sym )
527+
var sfulldtype = symbGetType( sym )
528+
if( symbIsRef( sym ) ) then
529+
'' Initializers for references initialize the pointer,
530+
'' not an object of the symbol's type.
531+
sdtype = typeAddrOf( sdtype )
532+
sfulldtype = typeAddrOf( sfulldtype )
533+
end if
526534

527535
'' Get rhs expression
528536
var expr = n->l
@@ -543,7 +551,7 @@ private sub hFlushExprStatic( byval n as ASTNODE ptr, byval basesym as FBSYMBOL
543551
else
544552
'' different types?
545553
if( edtype <> sdtype ) then
546-
expr = astNewCONV( symbGetFullType( sym ), symbGetSubtype( sym ), expr, AST_CONVOPT_DONTCHKPTR )
554+
expr = astNewCONV( sfulldtype, symbGetSubtype( sym ), expr, AST_CONVOPT_DONTCHKPTR )
547555
assert( expr <> NULL )
548556
end if
549557

tests/dim/byref.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,25 @@ SUITE( fbc_tests.dim_.byref_ )
608608
externs.globalExterns_proc
609609
END_TEST
610610

611+
TEST_GROUP( globalConstantInitWithoutOffset )
612+
type UDT1
613+
dummy as integer
614+
end type
615+
616+
type UDT2
617+
dummy as integer
618+
static byref r1 as UDT1
619+
static byref r2 as UDT1
620+
end type
621+
622+
'' Initializer for global ref var using a constant that is not an offset
623+
dim byref UDT2.r1 as UDT1 = *cptr(UDT1 ptr, 0)
624+
dim byref UDT2.r2 as UDT1 = *cptr(UDT1 ptr, 123)
625+
626+
TEST( default )
627+
CU_ASSERT( @UDT2.r1 = 0 )
628+
CU_ASSERT( @UDT2.r2 = 123 )
629+
END_TEST
630+
END_TEST_GROUP
631+
611632
END_SUITE

0 commit comments

Comments
 (0)