Skip to content

Commit 3627031

Browse files
committed
[Heavy] Fix OpGen unwinding from LibrarySpec error
1 parent d515278 commit 3627031

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

heavy/include/heavy/OpGen.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ class OpGen : public ValueVisitor<OpGen, mlir::Value> {
151151
// Err - The stored error to indicate that the compiler is in an error state.
152152
// (Use CheckError())
153153
Value Err = nullptr;
154+
// Track depth of calls to RunSync to allow unwinding errors properly.
155+
size_t RunSyncDepth = 0;
154156

155157
bool IsLocalDefineAllowed = false;
156158
heavy::Symbol* ModulePrefix = nullptr;
@@ -330,26 +332,27 @@ class OpGen : public ValueVisitor<OpGen, mlir::Value> {
330332

331333
void createLoadModule(SourceLocation Loc, Symbol* MangledName);
332334

335+
mlir::Value SetError(heavy::Error* NewErr) {
336+
Err = NewErr;
337+
Context.setLoc(Err.getSourceLocation());
338+
if (RunSyncDepth == 0)
339+
Context.Raise(Err);
340+
else
341+
Context.Yield(Err);
342+
return Error();
343+
}
344+
333345
template <typename T>
334346
mlir::Value SetError(SourceLocation Loc, T Str, Value V = Undefined()) {
335-
Err = Context.CreateError(Loc, Str, Context.CreatePair(V));
336-
Context.setLoc(Loc);
337-
Context.Raise(Err);
338-
return Error();
347+
heavy::Error* E = Context.CreateError(Loc, Str, Context.CreatePair(V));
348+
return SetError(E);
339349
}
340350

341351
template <typename T>
342352
mlir::Value SetError(T Str, Value V = Undefined()) {
343353
return SetError(V.getSourceLocation(), Str, V);
344354
}
345355

346-
mlir::Value SetError(heavy::Error* NewErr) {
347-
Err = NewErr;
348-
Context.Raise(Err);
349-
return Error();
350-
}
351-
352-
353356
mlir::Value Error() {
354357
return createUndefined();
355358
}

heavy/lib/OpGen.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,11 @@ void OpGen::VisitLibrarySpec(Value LibSpec) {
259259
Context.PushCont([](heavy::Context& C, ValueRefs) {
260260
Value LibSpec = C.getCapture(0);
261261
bool DidCallSyntax = false;
262-
if (Pair* P = dyn_cast<Pair>(LibSpec))
262+
if (Pair* P = dyn_cast<Pair>(LibSpec)) {
263263
C.OpGen->MaybeCallSyntax(P, DidCallSyntax);
264+
if (C.OpGen->CheckError())
265+
return;
266+
}
264267

265268
if (!DidCallSyntax) {
266269
C.OpGen->SetError("expecting library spec", LibSpec);
@@ -1098,7 +1101,9 @@ mlir::Value OpGen::MaybeCallSyntax(Value Operator, Pair* P,
10981101
Value Input = P;
10991102
heavy::OpGen* OpGen = Context.OpGen;
11001103
assert(OpGen == this && "sanity check");
1104+
++RunSyncDepth;
11011105
Value Result = Context.RunSync(Operator, Input);
1106+
--RunSyncDepth;
11021107
assert(OpGen == Context.OpGen && "OpGen should not unwind itself");
11031108
if (auto ResultErr = dyn_cast_or_null<heavy::Error>(Result))
11041109
SetError(ResultErr);

0 commit comments

Comments
 (0)