What error is compiler complaining about in a simple test?

I have a module - a simple Lua wrapper. The (trimmed) code is as follows:

const clua = @cImport({
    // (trimmed)
});

pub const VM = struct {
  lua : ?*clua.lua_State
};

pub fn create() Error ! VM {
  var s = clua.luaL_newstate() orelse return Error.CreateFailed;
  clua.luaL_openlibs(s);
  var vm = VM { .lua = s };
  return vm;
}

Then, in my test I try to do as follows:

const lua = @import("../lua.zig"); // import the module

test "simplest" {
  var l = try lua.create();
  expect( l.lua != null );      // that's the problem line
}

The compiler result (trimmed from the output noise) is:

error: error is ignored
  expect( l.lua != null );
  ~~~~~~^~~~~~~~~~~~~~~~~
note: consider using `try`, `catch`, or `if`

I don’t get it. All I’m trying to do is:

  1. create a VM structure in Zig (the lua.create function),
  2. simply make sure, that in case no error has been returned (the try part, in the previous line), the structure has been created, and its only field is not null.

The field is of type ?*clua.lua_State, so it can be either null, or something else, and I’d like to simply check, whether it’s not null.

Which error is ignored here?

BTW, changing the test to:

test "simplest" {
  var l = try lua.create();
  try expect( l.lua != null );  // here the try was added
}

“fixes” the compilation problem, but I still don’t understand, what can be an error here (without this try)…

So I’m asking.

fn expect(ok: bool) !void

! sign means that this function can return an error.
Errors from functions in Zig (unlike C, for example) can not be ignored.
Hence the error message and the hints.

1 Like

Thx!

Right, all over the Zig docs, try expect(...); can be found.

I missed it.

1 Like