Menu

#872 broken boolean bitfield runtime assignments from unsigned values

closed
boolean (2)
compiler
2018-04-28
2017-12-19
dkl
No

It looks like saving a non-zero value from an unsigned variable/expression into a boolean bitfield will result in 0/false, while it should be -1/true.

type UDT
    a : 1 as boolean
    b : 1 as boolean
    c : 1 as boolean
end type

#macro test(assignment)
    scope
        dim x as UDT
        assignment
        print x.a, x.b, x.c
    end scope
#endmacro

scope
    test(x.a = 1u)
    test(x.b = 1u)
    test(x.c = 1u)
end scope

print "---"
scope
    dim value as uinteger = 1
    test(x.a = value)
    test(x.b = value)
    test(x.c = value)
end scope

Discussion

  • munair

    munair - 2017-12-21

    In your example, a, b and c are not booleans. They are integers. The following code works as expected:

    type UDT
      a as boolean = 1u
      b as boolean = 1
      c as boolean = -1
    end type
    
    dim x as UDT
    dim as uinteger value = 1
    
    print x.a, x.b, x.c
    x.a = value
    print x.a
    
     

    Last edit: munair 2017-12-21
  • munair

    munair - 2017-12-21

    At best, one could say that the demonstrated code "a: 1 as boolean" (sort of an implicit bitfied) results in unpredictable behaviour. It shouldn't be allowed in my opinion. When inserting integers in-between, the results are "correct":

    type UDT
      a: 1 as boolean
      aa as integer
      b: 1 as boolean
      bb as integer
      c: 1 as boolean
      cc as integer
    end type
    
    dim x as UDT
    dim value as uinteger = 1
    x.a = value
    print x.a
    x.b = value
    print x.b
    x.c = value
    print x.c
    

    Remark: tested with FBC 1.06

     

    Last edit: munair 2017-12-21
  • Jeff Marshall

    Jeff Marshall - 2018-03-07

    Internally, fbc handles the bitfields as FB_DATATYPE_UINT and conversion to BOOLEAN is getting optimized out prematurely.

    diff --git a/src/compiler/ast-node-misc.bas b/src/compiler/ast-node-misc.bas
    index 04a9321fa..ce030c973 100644
    --- a/src/compiler/ast-node-misc.bas
    +++ b/src/compiler/ast-node-misc.bas
    @@ -433,6 +433,10 @@ private function astSetBitfield _
        if( symbGetType( bitfield ) = FB_DATATYPE_BOOLEAN ) then
            if( r->class <> AST_NODECLASS_CONV ) then
                r = astNewCONV( FB_DATATYPE_BOOLEAN, NULL, r )
    
    +       else
    +           if( astGetFullType( r ) <> FB_DATATYPE_BOOLEAN ) then
    +               r = astNewCONV( FB_DATATYPE_BOOLEAN, NULL, r )
    +           end if
            end if
            r = astNewCONV( FB_DATATYPE_UINT, NULL, r )
            r = astNewBOP( AST_OP_AND, r, hMakeUintMask( bitfield->var_.bits, bitfield->var_.bitpos ) )
    

    What is method to handle bugs? branch, fix, update changelog, pull request? Then if accepted, close this ticket?

     
  • Jeff Marshall

    Jeff Marshall - 2018-04-28
    • labels: --> boolean
    • status: open --> closed
    • assigned_to: Jeff Marshall
     

Log in to post a comment.

MongoDB Logo MongoDB