commit 17d5002892e86971926994ce99b02884fc0ea020
parent 56e37055a8d5f2f5458a0b2fa23645204651605a
Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
Date: Tue, 1 Oct 2024 18:03:23 +0200
cmd/hare: Move unwrap_module_error() logic to stdlib
Signed-off-by: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
Diffstat:
3 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/cmd/hare/build/gather.ha b/cmd/hare/build/gather.ha
@@ -28,7 +28,7 @@ export fn gather(ctx: *context, input: str) ([]module::module | error) = {
case let top: size =>
yield top;
case let e: module::error =>
- if (!(unwrap_module_error(e) is module::not_found)
+ if (!(module::unwrap_error(e) is module::not_found)
|| nsubmods == 0) {
return e;
};
@@ -64,7 +64,7 @@ fn gather_submodules(
case size =>
n += 1;
case let e: module::error =>
- if (!(unwrap_module_error(e) is module::not_found)) {
+ if (!(module::unwrap_error(e) is module::not_found)) {
return e;
};
};
diff --git a/cmd/hare/build/util.ha b/cmd/hare/build/util.ha
@@ -305,16 +305,3 @@ fn write_args(ctx: *context, out: str, args: []str, t: *task) (void | error) = {
};
fmt::fprintln(txt)?;
};
-
-// XXX: somewhat questionable, related to the hare::module context hackery, can
-// probably only be improved with language changes
-fn unwrap_module_error(err: module::error) module::error = {
- let unwrapped = err;
- for (true) match (unwrapped) {
- case let e: module::errcontext =>
- unwrapped = *e.1;
- case =>
- break;
- };
- return unwrapped;
-};
diff --git a/hare/module/types.ha b/hare/module/types.ha
@@ -72,6 +72,18 @@ export fn locstr(loc: location) str = {
// to carry context with errors
fn attach(ctx: str, e: error) errcontext = (ctx, alloc(e)): errcontext;
+// Returns the original [[error]] that might be hidden behind more context.
+export fn unwrap_error(err: error) error = {
+ // XXX: somewhat questionable context hackery, can probably only be
+ // improved with language changes
+ for (true) match (err) {
+ case let e: errcontext =>
+ err = *e.1;
+ case =>
+ return err;
+ };
+};
+
// Free the resources associated with an [[error]].
export fn finish_error(e: error) void = {
match (e) {