Skip to content

Commit

Permalink
Lock down erroneous types, too
Browse files Browse the repository at this point in the history
Signed-off-by: Danila Fedorin <[email protected]>
  • Loading branch information
DanilaFe committed Mar 7, 2024
1 parent 337c5d1 commit 4b67b7a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 5 deletions.
10 changes: 5 additions & 5 deletions frontend/include/chpl/types/UintType.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ class UintType final : public PrimitiveType {

static const owned<UintType>& getUintType(Context* context, int bitwidth);

/** what is stored in bitwidth_ for the default 'uint'? */
static int defaultBitwidth() {
return 64;
}

public:
~UintType() = default;

static const UintType* get(Context* context, int bitwidth);

/** what is stored in bitwidth_ for the default 'uint'? */
static int defaultBitwidth() {
return 64;
}

int bitwidth() const override {
return bitwidth_;
}
Expand Down
134 changes: 134 additions & 0 deletions frontend/test/resolution/testEnums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,136 @@ static void test11() {
assert(param1->toEnumParam()->value().postOrderId() == 5);
}

static void test12() {
Context ctx;
auto context = &ctx;
ErrorGuard guard(context);

// Production allows multiple constants to have the same numeric value.
// When casting backwards, the first matching constant is picked.
auto vars = resolveTypesOfVariables(context,
R"""(
enum color {
red = 0,
green = 1,
blue = 2,
gold = 3,
}
var redI: int = 0;
var greenU: uint = 1;
var blueI8: int(8) = 2;
var goldU8: uint(8) = 3;
var a = redI : color;
var b = greenU : color;
var c = blueI8 : color;
var d = goldU8 : color;
)""", {"a", "b", "c", "d"});

assert(vars.at("a").type()->isEnumType() && vars.at("a").type()->toEnumType()->name() == "color");
assert(vars.at("b").type()->isEnumType() && vars.at("b").type()->toEnumType()->name() == "color");
assert(vars.at("c").type()->isEnumType() && vars.at("c").type()->toEnumType()->name() == "color");
assert(vars.at("d").type()->isEnumType() && vars.at("d").type()->toEnumType()->name() == "color");
}

static void test13() {
Context ctx;
auto context = &ctx;
ErrorGuard guard(context);

// Production allows multiple constants to have the same numeric value.
// When casting backwards, the first matching constant is picked.
auto vars = resolveTypesOfVariables(context,
R"""(
enum color {
red = 0,
green = 1,
blue = 2,
gold = 3,
}
var red = color.red;
var a = red : int;
var b = red : uint;
var c = red : int(8);
var d = red : uint(8);
)""", {"red", "a", "b", "c", "d"});

assert(vars.at("red").type()->isEnumType());
assert(vars.at("a").type()->isIntType() && vars.at("a").type()->toIntType()->bitwidth() == IntType::defaultBitwidth());
assert(vars.at("b").type()->toUintType() && vars.at("b").type()->toUintType()->bitwidth() == UintType::defaultBitwidth());
assert(vars.at("c").type()->isIntType() && vars.at("c").type()->toIntType()->bitwidth() == 8);
assert(vars.at("d").type()->isUintType() && vars.at("d").type()->toUintType()->bitwidth() == 8);
}

static void test14() {
Context ctx;
auto context = &ctx;
ErrorGuard guard(context);

// Production allows multiple constants to have the same numeric value.
// When casting backwards, the first matching constant is picked.
auto vars = resolveTypesOfVariables(context,
R"""(
enum color {
red,
green,
blue,
gold,
}
var redI: int = 0;
var greenU: uint = 1;
var blueI8: int(8) = 2;
var goldU8: uint(8) = 3;
var a = redI : color;
var b = greenU : color;
var c = blueI8 : color;
var d = goldU8 : color;
)""", {"a", "b", "c", "d"});

for (auto pair : vars) {
assert(pair.second.isErroneousType());
}

assert(guard.numErrors() == 4);
for (int i = 0; i < 4; i ++) {
assert(guard.error(i)->type() == ErrorType::NoMatchingCandidates);
}
guard.realizeErrors();
}


static void test15() {
Context ctx;
auto context = &ctx;
ErrorGuard guard(context);

// Production allows multiple constants to have the same numeric value.
// When casting backwards, the first matching constant is picked.
auto vars = resolveTypesOfVariables(context,
R"""(
enum color {
red,
green,
blue,
gold,
}
var red = color.red;
var a = red : int;
var b = red : uint;
var c = red : int(8);
var d = red : uint(8);
)""", {"a", "b", "c", "d"});

for (auto pair : vars) {
assert(pair.second.isErroneousType());
}

assert(guard.numErrors() == 4);
for (int i = 0; i < 4; i ++) {
assert(guard.error(i)->type() == ErrorType::NoMatchingCandidates);
}
guard.realizeErrors();
}

int main() {
test1();
test2();
Expand All @@ -385,5 +515,9 @@ int main() {
test9();
test10();
test11();
test12();
test13();
test14();
test15();
return 0;
}

0 comments on commit 4b67b7a

Please sign in to comment.