Skip to content

Commit

Permalink
raise an error for instance generators not covering class members
Browse files Browse the repository at this point in the history
  • Loading branch information
kthielen committed Apr 27, 2020
1 parent 8342e2d commit 924193d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/hobbes/eval/cmodule.C
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ void compile(const ModulePtr& m, cc* e, const InstanceDef* id) {
} else {
c->insert(TCInstanceFnPtr(new TCInstanceFn(id->className(), id->constraints(), targs, ms, id->la())));
}
} catch (annotated_error&) {
throw;
} catch (std::exception& ex) {
throw annotated_error(*id, ex.what());
}
Expand Down
24 changes: 24 additions & 0 deletions lib/hobbes/lang/preds/class.C
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ void TClass::insert(const TCInstanceFnPtr& ifp) {
ss << "Arity mismatch between instance generator definition (" << ifp->arity() << ") and type class definition (" << this->tvs << ").";
throw annotated_error(*ifp, ss.str());
} else {
// make sure that this instance generator covers all required members
auto mm = ifp->members(MonoTypeSubst());

std::ostringstream ss;
size_t errors = 0;
for (const auto& tcm : this->tcmembers) {
if (!mm.count(tcm.first)) {
ss << (errors?", ":"") << "expected definition of '" << tcm.first << "'";
++errors;
}
}
for (const auto& m : mm) {
if (!this->tcmembers.count(m.first)) {
ss << (errors?", ":"") << "unexpected definition of '" << m.first << "'";
++errors;
}
}
if (errors) {
std::ostringstream ess;
ess << "Can't introduce instance generator for '" << this->tcname << "': " << ss.str();
throw annotated_error(*ifp, ess.str());
}

// insert the instance generator
ifp->order = this->tcinstancefns.size();
this->tcinstancefns.push_back(ifp);

Expand Down

0 comments on commit 924193d

Please sign in to comment.