Menu

#841 Inconsistency: negation may return different signedness for constants

closed
nobody
None
compiler
2017-11-20
2016-10-29
Matthew
No

The negation uop converts unsigned constants to signed, but leaves unsigned variables as unsigned. e.g (on 32-bit):

dim as uinteger v = 1
const as uinteger c = 1
print (-v) '4294967295
print (-c) '-1

It looks like it happens during constant folding at https://sourceforge.net/p/fbc/code/ci/master/tree/src/compiler/ast-node-uop.bas#l267, but we have no equivalent code for non-constant code.

For what it's worth, GCC seems to keep it as unsigned, but gives a warning on higher warning levels (http://stackoverflow.com/q/10216528/446106). Perhaps we should give a warning, if only because fixing this either way may break someone's code.

Discussion

  • dkl

    dkl - 2016-11-21

    Wow, I never noticed that... oh well, I think the non-constant case should do the same unsigned -> signed conversion.

    It's definitely an FB quirk (especially since C/C++ don't do it), but at least it makes negation useful on unsigned values (besides bit hacks, which will probably still work since the negation gives the same bit pattern either way).

    I don't like adding warnings though - better to make it an error then.

     
  • dkl

    dkl - 2017-11-20

    There now is a fix on master ([edeb71]) which makes the negation operator always return a signed result, so I'm guessing that this issue can be closed. See also: https://github.com/freebasic/fbc/pull/50

     

    Related

    Commit: [edeb71]

  • dkl

    dkl - 2017-11-20
    • status: open --> closed
     

Log in to post a comment.