Skip to content

Dyno error for variable without init/type, split-init error notes #27038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,4 @@ ERROR_CLASS(UserDiagnosticEmitError, UniqueString, ID)
WARNING_CLASS(UserDiagnosticEncounterWarning, UniqueString, ID)
WARNING_CLASS(UserDiagnosticEmitWarning, UniqueString, ID)
ERROR_CLASS(ValueUsedAsType, const uast::AstNode*, types::QualifiedType)
ERROR_CLASS(VariableWithoutInitOrType, const uast::AstNode*, ID, chpl::UniqueString)
22 changes: 17 additions & 5 deletions frontend/include/chpl/resolution/resolution-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2620,7 +2620,7 @@ class ResolvedExpression {
*/
class ResolutionResultByPostorderID {
private:
ID symbolId;
ID symbolId_;
// This map is generally accessed with operator[] to default-construct a new
// ResolvedExpression if none exists for an ID. at() is used instead only
// when const-ness is required.
Expand All @@ -2636,10 +2636,14 @@ class ResolutionResultByPostorderID {
/** prepare to resolve the body of a For loop */
void setupForParamLoop(const uast::For* loop, ResolutionResultByPostorderID& parent);

const ID& symbolId() const {
return symbolId_;
}

/* ID query functions */
bool hasId(const ID& id) const {
auto postorder = id.postOrderId();
if (id.symbolPath() == symbolId.symbolPath() &&
if (id.symbolPath() == symbolId_.symbolPath() &&
0 <= postorder && (map.count(postorder) > 0))
return true;

Expand Down Expand Up @@ -2677,20 +2681,20 @@ class ResolutionResultByPostorderID {
}

bool operator==(const ResolutionResultByPostorderID& other) const {
return symbolId == other.symbolId &&
return symbolId_ == other.symbolId_ &&
map == other.map;
}
bool operator!=(const ResolutionResultByPostorderID& other) const {
return !(*this == other);
}
void swap(ResolutionResultByPostorderID& other) {
symbolId.swap(other.symbolId);
symbolId_.swap(other.symbolId_);
map.swap(other.map);
}
static bool update(ResolutionResultByPostorderID& keep,
ResolutionResultByPostorderID& addin);
void mark(Context* context) const {
symbolId.mark(context);
symbolId_.mark(context);
for (auto const &elt : map) {
// mark ResolvedExpressions
elt.second.mark(context);
Expand All @@ -2699,6 +2703,14 @@ class ResolutionResultByPostorderID {

void stringify(std::ostream& ss, chpl::StringifyKind stringKind) const;

auto begin() const {
return map.begin();
}

auto end() const {
return map.end();
}

/// \cond DO_NOT_DOCUMENT
DECLARE_DUMP;
/// \endcond DO_NOT_DOCUMENT
Expand Down
10 changes: 9 additions & 1 deletion frontend/lib/resolution/call-init-deinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,15 @@ void CallInitDeinit::resolveDefaultInit(const VarLikeDecl* ast, RV& rv) {
return;
}
if (varType.isUnknownKindOrType()) {
context->error(ast, "cannot default initialize variable using generic or unknown type");
auto& po = rv.byPostorder();
chpl::ID symId;
for (auto& [intId, re] : po) {
if (re.toId() == ast->id()) {
auto symPath = po.symbolId().symbolPath();
symId = ID(symPath, intId, 0);
}
}
CHPL_REPORT(context, VariableWithoutInitOrType, ast, symId, ast->name());
return;
}
// check genericity
Expand Down
21 changes: 19 additions & 2 deletions frontend/lib/resolution/resolution-error-classes-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,10 @@ static void printRejectedCandidates(ErrorWriterBase& wr,
!offendingActual->typeExpression()) {
auto formalKind = badPass.formalType().kind();
auto actualName = "'" + actualExpr->toIdentifier()->name().str() + "'";
wr.message("The actual ", actualName,
wr.note(offendingActual->id(), "The actual ", actualName,
" expects to be split-initialized because it is declared without a type or initialization expression here:");
wr.codeForDef(offendingActual);
wr.message("The call to '", ci.name() ,"' occurs before any valid initialization points:");
wr.note(actualExpr, "The call to '", ci.name() ,"' occurs before any valid initialization points:");
wr.code(actualExpr, { actualExpr });
actualPrinted = true;
wr.message("The call to '", ci.name(), "' cannot initialize ",
Expand Down Expand Up @@ -2413,6 +2413,23 @@ void ErrorUseOfLaterVariable::write(ErrorWriterBase& wr) const {
wr.message("Variables cannot be referenced before they are defined.");
}


void ErrorVariableWithoutInitOrType::write(ErrorWriterBase& wr) const {
auto decl = std::get<const uast::AstNode*>(info_);
auto& useOf = std::get<ID>(info_);
auto name = std::get<UniqueString>(info_);
wr.heading(kind_, type_, decl,
"variable '", name, "' is declared without an initializer or type.");
if (!useOf.isEmpty()) {
wr.note(useOf, "cannot find initialization point to split-init this variable");
}
wr.codeForDef(decl);
if (!useOf.isEmpty()) {
wr.note(useOf, "the variable '", name, "' is used here");
wr.codeForLocation(useOf);
}
}

void ErrorUserDiagnosticEncounterError::write(ErrorWriterBase& wr) const {
auto msg = std::get<UniqueString>(info_);
auto& node = std::get<ID>(info_);
Expand Down
10 changes: 5 additions & 5 deletions frontend/lib/resolution/resolution-types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,14 +534,14 @@ CallInfo CallInfo::copyAndRename(const CallInfo &ci, UniqueString rename) {
void ResolutionResultByPostorderID::setupForSymbol(const AstNode* ast) {
CHPL_ASSERT(Builder::astTagIndicatesNewIdScope(ast->tag()));

symbolId = ast->id();
symbolId_ = ast->id();
}
void ResolutionResultByPostorderID::setupForSignature(const Function* func) {
symbolId = func->id();
symbolId_ = func->id();
}
void ResolutionResultByPostorderID::setupForParamLoop(
const For* loop, ResolutionResultByPostorderID& parent) {
this->symbolId = parent.symbolId;
this->symbolId_ = parent.symbolId_;
}
void ResolutionResultByPostorderID::setupForFunction(const Function* func) {
setupForSymbol(func);
Expand Down Expand Up @@ -1400,13 +1400,13 @@ ResolutionResultByPostorderID::stringify(std::ostream& ss,

size_t maxIdWidth = 0;
for (auto key : keys) {
auto id = ID(symbolId.symbolPath(), key, -1);
auto id = ID(symbolId_.symbolPath(), key, -1);
if (id.str().size() > maxIdWidth)
maxIdWidth = id.str().size();
}

for (auto key : keys) {
auto id = ID(symbolId.symbolPath(), key, -1);
auto id = ID(symbolId_.symbolPath(), key, -1);

// output the ID
std::cout << std::setw(maxIdWidth) << std::left << id.str();
Expand Down
18 changes: 18 additions & 0 deletions frontend/test/resolution/testResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,22 @@ static void testGenericSync() {
// guard should have no errors
}

static void testUseOfUninitializedVar() {
auto context = buildStdContext();
ErrorGuard guard(context);
auto qt = resolveTypeOfXInit(context,
R"""(
proc foo() {
var y;
return y;
}
var x = foo();
)""");
assert(qt.isUnknownKindOrType());
// expect uninitialized var y, can't establish type for call expression foo()
assert(guard.realizeErrors() == 2);
}

int main() {
test1();
test2();
Expand Down Expand Up @@ -2291,5 +2307,7 @@ int main() {

testGenericSync();

testUseOfUninitializedVar();

return 0;
}