Skip to content

Commit

Permalink
[Sema] add more late init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isuckatcs committed Jul 5, 2024
1 parent e1cfebe commit ca22d4f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
32 changes: 8 additions & 24 deletions src/sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,18 @@ bool Sema::checkReturnOnAllPaths(const ResolvedFunctionDecl &fn,

bool Sema::checkVariableInitialization(const ResolvedFunctionDecl &fn,
const CFG &cfg) {

enum class State { Bottom, Unassigned, Assigned, Top };

auto joinStates = [](State s1, State s2) {
if (s1 == State::Top)
if (s1 == s2)
return s1;

if (s2 == State::Top)
return s2;

if (s1 == State::Bottom)
return s2;

if (s2 == State::Bottom)
return s1;

if (s1 == s2)
return s1;

return State::Top;
};

Expand All @@ -97,14 +90,9 @@ bool Sema::checkVariableInitialization(const ResolvedFunctionDecl &fn,
const auto &[preds, succs, stmts] = cfg.basicBlocks[bb];

Lattice tmp;
for (auto &&pred : preds) {
for (auto &&[decl, state] : curLattices[pred.first]) {
if (tmp.count(decl))
tmp[decl] = joinStates(tmp[decl], state);
else
tmp[decl] = state;
}
}
for (auto &&pred : preds)
for (auto &&[decl, state] : curLattices[pred.first])
tmp[decl] = joinStates(tmp[decl], state);

auto stmtsReversed = stmts;
std::reverse(stmtsReversed.begin(), stmtsReversed.end());
Expand All @@ -120,11 +108,10 @@ bool Sema::checkVariableInitialization(const ResolvedFunctionDecl &fn,
const auto *varDecl =
dynamic_cast<const ResolvedVarDecl *>(assignment->variable->decl);

if (!varDecl)
continue;
assert(varDecl &&
"assignment to non-variables should have been caught by sema");

if (!varDecl->isMutable && tmp.count(varDecl) &&
tmp[varDecl] != State::Unassigned)
if (!varDecl->isMutable && tmp[varDecl] != State::Unassigned)
pendingErrors[&assignment->location] =
'\'' + varDecl->identifier + "' cannot be mutated";

Expand All @@ -134,10 +121,7 @@ bool Sema::checkVariableInitialization(const ResolvedFunctionDecl &fn,
const auto *varDecl =
dynamic_cast<const ResolvedVarDecl *>(declRefExpr->decl);

if (!varDecl || !tmp.count(varDecl))
continue;

if (tmp[varDecl] != State::Assigned)
if (varDecl && tmp[varDecl] != State::Assigned)
pendingErrors[&declRefExpr->location] =
'\'' + varDecl->identifier + "' is not initialized";
}
Expand Down
26 changes: 26 additions & 0 deletions test/sema/late_init.al
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,29 @@ fn shadowInitialized(): void {

x;
}

fn nestedInLoop(param: number): void {
var x: number;

if param {
while param {
if param {
x = param - 1.0;
}

// CHECK: [[# @LINE + 1 ]]:25: error: 'x' is not initialized
var y: number = x;
}
} else {
while param {
if param {
x = param - 1.0;
}

// CHECK: [[# @LINE + 1 ]]:25: error: 'x' is not initialized
var y: number = x;
}
}

x + x;
}

0 comments on commit ca22d4f

Please sign in to comment.