#cannot infer error set

1 messages · Page 1 of 1 (latest)

sturdy abyss
#

So I am making a python formatter, a recursive one. I noticed at one point im duplicating effort for some stuff

.parameters, .argument_list, .tuple => {
                try self.output.append(allocator, '(');

                const child_count = node.namedChildCount();
                var i: u32 = 0;
                while (i < child_count) : (i += 1) {
                    const child = node.namedChild(i).?;
                    var child_cursor = child.walk();
                    try self.formatNode(allocator, &child_cursor, 0);

                    if (i + 1 < child_count) {
                        try self.output.appendSlice(allocator, ", ");
                    }
                }

                try self.output.append(allocator, ')');
            },

these were all the same for (), however i have some other similar stuff for lists which use {}, i thought to condense the splatting of the children with

fn splatChildren(self: *Fmt, allocator: std.mem.Allocator, node: ts.Node) !void {
        const child_count = node.namedChildCount();
        var i: u32 = 0;
        while (i < child_count) : (i += 1) {
            const elem = node.namedChild(i).?;
            var elem_cursor = elem.walk();
            try self.formatNode(allocator, &elem_cursor, 0);

            if (i + 1 < child_count) {
                try self.output.appendSlice(allocator, ", ");
            }
        }
    }```
however switching to the splat method cause the cannot infer error set, specifically at 
```bash
run
└─ run exe pyzfmt
   └─ compile exe pyzfmt Debug native 1 errors
src/fmt.zig:458:32: error: unable to resolve inferred error set
            try self.formatNode(allocator, &elem_cursor, 0);

(formatNode is the recursive function)

hexed mica
#

you probably just have to give a concrete error set here honestly, if formatNode calls splatChildren the compiler cant do much since to get the error set of one you need the error set of the other

#

It doesnt seem like you do much error handling though so you may even consider just not bothering with multiple errors or go a completely different route with a diagnostic out-pointer pattern

sturdy abyss
#

its mostly just OutOfMemory errors, since the only thing that can go wrong is my arraylist (which is overallocated already so shouldn't be a problem)

hexed mica
#

you should be using appendAssumeCapacity if its overallocated

#

But yeah then just make the error set OutOfMemory and see if the compiler tells you youre missing any other errors in the set

sturdy abyss
hexed mica
#

Oh so youre pre-allocating it but arent sure if its enough?

sturdy abyss
hexed mica
#

Makes sense

#

Then yeah just give it an explicit error set

sturdy abyss
hexed mica
#

error{OutOfMemory}!void but yes

sturdy abyss
#

@hexed mica worked thx

#

@hexed mica had to also do WriteFail