[SOLVED] Default UDT initializer behaves incorrectly with arrays

General FreeBASIC programming questions.
Post Reply
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

[SOLVED] Default UDT initializer behaves incorrectly with arrays

Post by shadow008 »

SOLVED: Bug possibly fixed in 1.10

I'm at a loss as to how to explain what's going on here, but I think this is a bug. Consider the following code.

Code: Select all

type firstType

    dim as uinteger<64> x
    
    'Remove to fix #1
    declare operator cast() as uinteger<64>

end type

'Also remove to fix #1
operator firstType.cast() as uinteger<64>
    return this.x
end operator

type mytype
    dim x as firstType
    dim y as integer
end type

'OK
dim comp as firstType
dim test as mytype = (comp, 1)

'NOT OK?
dim comp2(0) as firstType
'dim tempType as firstType = comp2(0) 'This can be used in place of comp2(0) below as a different fix #2
dim test2 as mytype = (comp2(0), 1) '<-- Too many expressions error
This code can be fixed in two ways:
1) Remove the cast to uinteger<64> in the type "firstType" (it could be any cast to any integer type and the bug still manifests btw)
2) First assign an intermediate variable of type "firstType" to "comp2(0), and use that variable instead

The default constructor of firstType is a (or any) simple cast to integer. The default constructo for mytype is simply (integer, integer). What is the compiler doing here that it sees more than 2 expressions? Is the access to an array counting as one expression and then a cast to integer using firstType.cast as a second? I'm confused here and the workarounds are terrible for my situation.
Last edited by shadow008 on Apr 03, 2023 17:37, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [BUG?] Default UDT initializer behaves incorrectly with arrays

Post by fxm »

This should now be fixed in fbc version 1.10.0.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: [BUG?] Default UDT initializer behaves incorrectly with arrays

Post by paul doe »

I get no errors compiling with 1.09.0 either. Which version are you using?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [BUG?] Default UDT initializer behaves incorrectly with arrays

Post by fxm »

With fbc 1.09.0, I get:
Compiler output:
C:\.....\FBIde0.4.6r4_fbc1.09.0\FBIDETEMP.bas(27) error 67: Too many expressions in 'dim test2 as mytype = (comp2(0), 1) '<-- Too many expressions error'

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc: FreeBASIC Compiler - Version 1.09.0 (2021-12-31), built for win32 (32bit)
OS: Windows NT 6.2 (build 9200)

Maybe fixed by this in fbc 1.10.0:
Merge pull request #396 from jayrm/typeini

fbc: Fix incorrect handling of initializers (typeini)

- fixes/changes how fbc handles pairing of a UDT's (internal) structure and the initializer list given by the user
- new: track the overall initializer size in bytes
- don't allow implicit casting to base types
- pass back the initializer to a parent field for full initialization instead of truncated initialization
- determine next field to initialize based on the overall bytes initialized instead of only the 'next field'
- pair the next initializer (in user source) with the next best matched field (in UDT's symbol structure)
- consider every field of the UDT including fields of hidden base types
- initializer elements for TYPE's should pair with and initialize every member of the TYPE sequentially
- initializer elements for UNION's should pair with only the first member of the UNION and skip the rest
- don't implicitly cast parent types to base types; instead pass the initializer back up the parser and try to pair the initializer with a parent type
- different handling (ideally better) of comma and parentheses initializer list delimeters so that next initializer is paired with correct UDT field.
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: [SOLVED] Default UDT initializer behaves incorrectly with arrays

Post by shadow008 »

I get no errors compiling with 1.09.0 either. Which version are you using?
I'm also compiling with 1.09 and get the error with or without any compiler arguments. Is your setup special in some way?
Maybe fixed by this in fbc 1.10.0
I will use a workaround until 1.10 is released in that case... or maybe I'll just pick up the unreleased version.
Post Reply