kitlang / kit

Kit: a magical, high performance programming language, designed for game development.
https://www.kitlang.org
Other
1.02k stars 29 forks source link

Error when comparing CString literal to CString variable #80

Closed Gamerfiend closed 5 years ago

Gamerfiend commented 5 years ago

Describe the problem. What did you see? What did you expect to see?

When comparing a CString literal to a stored CString variable using the == operator, comparison is false even when value of CString is the same.

If this is a code issue, provide a minimal code example:

import kit.map;
import kit.array;

trait Component{
    function type(): CString;
}

struct PositionComponent{
    public var x: Int;
    public var y: Int;
    public var z: Int;
}

implement Component for PositionComponent{
    function type(): CString{
        return "Position";
    }
}

function main() {

    var myMap: Map[CString, Box[Component]] = Map.new(10);

    var myPosStruct = struct PositionComponent{
        x: 12,
        y: 12,
        z: 23
    };

    var myPosStructBox: Box[Component] = myPosStruct;
    myMap.put(myPosStructBox.type(), myPosStructBox);

    var keys = myMap.keys();

    printf("myMap has PositionComponent: %s\n", if myMap.exists(myPosStructBox.type()) then "true" else "false");
    printf("myMap has PositionComponent: %s\n", if myMap.exists(keys[0]) then "true" else "false");
    printf("myMap has PositionComponent: %s\n", if myMap.exists("Position") then "true" else "false");
}

Which outputs:

myMap has PositionComponent: true
myMap has PositionComponent: true
myMap has PositionComponent: false

However all three should be true.

Gamerfiend commented 5 years ago

Just throw another line of code to test something and this:

printf("Position literal == Position: %s\n", if(strcmp("Position", keys[0]) == 0) then "true" else "false");

Produces true, so I'm not sure where the mix up is happening.

bendmorris commented 5 years ago

I'm not seeing this, can you confirm that you're on the latest master?

ben ~/Dev/test$ kitc --run test.kit
[2018-12-18 16:00:06.045720] ===> parsing and building module graph
[2018-12-18 16:00:06.077737] ===> processing C includes
[2018-12-18 16:00:06.205697] ===> expanding macros
[2018-12-18 16:00:06.206061] ===> resolving module types
[2018-12-18 16:00:06.261386] ===> typing module content
[2018-12-18 16:00:06.864864] ===> generating intermediate representation
[2018-12-18 16:00:06.908372] ===> generating code
[2018-12-18 16:00:06.925372] ===> compiling
[2018-12-18 16:00:06.994895] ===> linking
[2018-12-18 16:00:07.012427] ===> finished; total time: 0.967077475s
[2018-12-18 16:00:07.012913] ===> running
myMap has PositionComponent: true
myMap has PositionComponent: true
myMap has PositionComponent: true
bendmorris commented 5 years ago

Fixed by https://github.com/kitlang/kit/commit/30683195021a30154ef9e6f702e7855cae646a5f