#281 cast from pointer to non-(u)int fails

open
nobody
None
2014-08-17
2013-09-04
darkinsanity
No

In my kernel, I used the following line to calculate the offset of an array inside of a Task-State-Segment:
const TSS_IO_BITMAP_OFFSET as ushort = cushort(@(cast(task_state_segment ptr, 0)->io_bitmap(0)))

This worked nicely in version 0.24.0, but on 0.90.1 this produces an error. I was able to shrink down the problem to two simple lines of code:
dim x as ushort
cushort(@x)

This produces the following error:
error 20: Type mismatch, before ')' in 'cushort(@x)'

Using a cuint() or cint() works just fine, casting to any other type (byte, ubyte, short, ushort, longint, ulongint) fails. Working around this is possible by first casting to an (u)integer:
cushort(cuint(@x))

Maybe this is wanted to prevent people from doing crazy stuff with pointers but I wanted to report it because it also could be unwanted behavior.

Discussion

  • dkl
    dkl
    2013-09-04

    Casting pointers to integers of different size has always been disallowed, though it seems like the offsetof()-like case you've been using has still been accepted, perhaps by accident. 0.90.1 includes a fix ([6ad7fa]) which I guess is the reason why this case is now treated like the rest.

    I've always thought disallowing casting pointers to smaller integers was added to prevent loss-of-data problems, and it's useful to help porting code to 64bit, though I'm not sure it's really that useful. I'd personally prefer the compiler to show errors without casts, and accept code silently if casts were used.

     

    Related

    Commit: [6ad7fa]


    Last edit: dkl 2013-09-04
  • dkl
    dkl
    2013-10-05

    Ticket moved from /p/fbc/bugs/702/

     
    Last edit: dkl 2013-10-05