Skip to content

C backend bug: ~ (not) operation #2054

@ronnuriel

Description

@ronnuriel

Hey, not op seems to be generated wrong:

def foo(grp: u16) -> u16:
    i: u16 = ~(u16(grp))

    return i


def foo2() -> u16:
    i: u16 = ~(u16(0xffff))

    return i

def foo3() -> u16:
    i: u16 = ~(u16(0xffff))

    return ~i

assert foo(u16(0)) == u16(0xffff), foo(u16(0))
assert foo(u16(0xffff)) == u16(0), foo(u16(0xffff))
assert foo2() == u16(0), foo2()
assert foo3() == u16(0xffff), foo3()

output:

#include <inttypes.h>

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>


// Implementations
uint16_t foo(uint16_t grp)
{
    uint16_t _lpython_return_variable;
    uint16_t i;
    i = grp;
    _lpython_return_variable = i;
    return _lpython_return_variable;
}

uint16_t foo2()
{
    uint16_t _lpython_return_variable;
    uint16_t i;
    i = (uint16_t)(65535);
    _lpython_return_variable = i;
    return _lpython_return_variable;
}

uint16_t foo3()
{
    uint16_t _lpython_return_variable;
    uint16_t i;
    i = (uint16_t)(65535);
    _lpython_return_variable = i;
    return _lpython_return_variable;
}

void _lpython_main_program()
{
    ASSERT_MSG(foo((uint16_t)(0)) == (uint16_t)(65535), foo((uint16_t)(0)));
    ASSERT_MSG(foo((uint16_t)(65535)) == (uint16_t)(0), foo((uint16_t)(65535)));
    ASSERT_MSG(foo2() == (uint16_t)(0), foo2());
    ASSERT_MSG(foo3() == (uint16_t)(65535), foo3());
}

int main(int argc, char* argv[])
{
    _lpython_set_argv(argc, argv);
    _lpython_main_program();
    return 0;
}

Please add them to Lpython tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    BLOCKERworkaround not found yetbugSomething isn't workingcLabel for C language related changeseasy to fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions