mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
commit
c8c798685f
64
src/Sema.zig
64
src/Sema.zig
@ -2869,7 +2869,16 @@ fn ensureResultUsed(
|
||||
};
|
||||
return sema.failWithOwnedErrorMsg(block, msg);
|
||||
},
|
||||
else => return sema.fail(block, src, "expression value is ignored", .{}),
|
||||
else => {
|
||||
const msg = msg: {
|
||||
const msg = try sema.errMsg(block, src, "value of type '{}' ignored", .{operand_ty.fmt(sema.mod)});
|
||||
errdefer msg.destroy(sema.gpa);
|
||||
try sema.errNote(block, src, msg, "all non-void values must be used", .{});
|
||||
try sema.errNote(block, src, msg, "this error can be suppressed by assigning the value to '_'", .{});
|
||||
break :msg msg;
|
||||
};
|
||||
return sema.failWithOwnedErrorMsg(block, msg);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -4508,6 +4517,7 @@ fn zirCompileLog(
|
||||
const arg = try sema.resolveInst(arg_ref);
|
||||
const arg_ty = sema.typeOf(arg);
|
||||
if (try sema.resolveMaybeUndefVal(block, src, arg)) |val| {
|
||||
try sema.resolveLazyValue(block, src, val);
|
||||
try writer.print("@as({}, {})", .{
|
||||
arg_ty.fmt(sema.mod), val.fmtValue(arg_ty, sema.mod),
|
||||
});
|
||||
@ -5498,7 +5508,7 @@ fn analyzeCall(
|
||||
// TODO add error note: declared here
|
||||
return sema.fail(
|
||||
block,
|
||||
func_src,
|
||||
call_src,
|
||||
"expected {d} argument(s), found {d}",
|
||||
.{ fn_params_len, uncasted_args.len },
|
||||
);
|
||||
@ -8918,6 +8928,10 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
|
||||
|
||||
if (special_prong == .@"else" and seen_errors.count() == operand_ty.errorSetNames().len) {
|
||||
|
||||
// TODO re-enable if defer implementation is improved
|
||||
// https://github.com/ziglang/zig/issues/11798
|
||||
if (true) break :else_validation;
|
||||
|
||||
// In order to enable common patterns for generic code allow simple else bodies
|
||||
// else => unreachable,
|
||||
// else => return,
|
||||
@ -20592,6 +20606,14 @@ fn tupleFieldPtr(
|
||||
.@"addrspace" = tuple_ptr_ty.ptrAddressSpace(),
|
||||
});
|
||||
|
||||
if (tuple_ty.structFieldValueComptime(field_index)) |default_val| {
|
||||
const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
|
||||
.field_ty = field_ty,
|
||||
.field_val = default_val,
|
||||
});
|
||||
return sema.addConstant(ptr_field_ty, val);
|
||||
}
|
||||
|
||||
if (try sema.resolveMaybeUndefVal(block, tuple_ptr_src, tuple_ptr)) |tuple_ptr_val| {
|
||||
return sema.addConstant(
|
||||
ptr_field_ty,
|
||||
@ -20603,14 +20625,6 @@ fn tupleFieldPtr(
|
||||
);
|
||||
}
|
||||
|
||||
if (tuple_ty.structFieldValueComptime(field_index)) |default_val| {
|
||||
const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
|
||||
.field_ty = field_ty,
|
||||
.field_val = default_val,
|
||||
});
|
||||
return sema.addConstant(ptr_field_ty, val);
|
||||
}
|
||||
|
||||
if (!init) {
|
||||
try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_ptr_src);
|
||||
}
|
||||
@ -21354,7 +21368,7 @@ fn coerceExtra(
|
||||
else => {},
|
||||
},
|
||||
.ErrorUnion => switch (inst_ty.zigTypeTag()) {
|
||||
.ErrorUnion => {
|
||||
.ErrorUnion => eu: {
|
||||
if (maybe_inst_val) |inst_val| {
|
||||
switch (inst_val.tag()) {
|
||||
.undef => return sema.addConstUndef(dest_ty),
|
||||
@ -21363,7 +21377,10 @@ fn coerceExtra(
|
||||
inst_ty.errorUnionPayload(),
|
||||
inst_val.castTag(.eu_payload).?.data,
|
||||
);
|
||||
return sema.wrapErrorUnionPayload(block, dest_ty, payload, inst_src);
|
||||
return sema.wrapErrorUnionPayload(block, dest_ty, payload, inst_src) catch |err| switch (err) {
|
||||
error.NotCoercible => break :eu,
|
||||
else => |e| return e,
|
||||
};
|
||||
},
|
||||
else => {
|
||||
const error_set = try sema.addConstant(
|
||||
@ -21382,9 +21399,12 @@ fn coerceExtra(
|
||||
.Undefined => {
|
||||
return sema.addConstUndef(dest_ty);
|
||||
},
|
||||
else => {
|
||||
else => eu: {
|
||||
// T to E!T
|
||||
return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src);
|
||||
return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src) catch |err| switch (err) {
|
||||
error.NotCoercible => break :eu,
|
||||
else => |e| return e,
|
||||
};
|
||||
},
|
||||
},
|
||||
.Union => switch (inst_ty.zigTypeTag()) {
|
||||
@ -23334,6 +23354,16 @@ fn beginComptimePtrLoad(
|
||||
break :blk deref;
|
||||
},
|
||||
|
||||
.comptime_field_ptr => blk: {
|
||||
const comptime_field_ptr = ptr_val.castTag(.comptime_field_ptr).?.data;
|
||||
break :blk ComptimePtrLoadKit{
|
||||
.parent = null,
|
||||
.pointee = .{ .ty = comptime_field_ptr.field_ty, .val = comptime_field_ptr.field_val },
|
||||
.is_mutable = false,
|
||||
.ty_without_well_defined_layout = comptime_field_ptr.field_ty,
|
||||
};
|
||||
},
|
||||
|
||||
.opt_payload_ptr,
|
||||
.eu_payload_ptr,
|
||||
=> blk: {
|
||||
@ -24864,7 +24894,7 @@ fn wrapErrorUnionPayload(
|
||||
inst_src: LazySrcLoc,
|
||||
) !Air.Inst.Ref {
|
||||
const dest_payload_ty = dest_ty.errorUnionPayload();
|
||||
const coerced = try sema.coerce(block, dest_payload_ty, inst, inst_src);
|
||||
const coerced = try sema.coerceExtra(block, dest_payload_ty, inst, inst_src, false, false);
|
||||
if (try sema.resolveMaybeUndefVal(block, inst_src, coerced)) |val| {
|
||||
return sema.addConstant(dest_ty, try Value.Tag.eu_payload.create(sema.arena, val));
|
||||
}
|
||||
@ -25523,6 +25553,10 @@ fn resolveLazyValue(
|
||||
const ty = val.castTag(.lazy_align).?.data;
|
||||
return sema.resolveTypeLayout(block, src, ty);
|
||||
},
|
||||
.lazy_size => {
|
||||
const ty = val.castTag(.lazy_size).?.data;
|
||||
return sema.resolveTypeLayout(block, src, ty);
|
||||
},
|
||||
else => return,
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,9 +6,14 @@ fn bar(a: i32, b: []const u8) void {
|
||||
@compileLog("a", a, "b", b);
|
||||
@compileLog("end",);
|
||||
}
|
||||
export fn baz() void {
|
||||
const S = struct { a: u32 };
|
||||
@compileLog(@sizeOf(S));
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=llvm
|
||||
// target=native
|
||||
//
|
||||
// :5:5: error: found compile log statement
|
||||
// :11:5: note: also here
|
||||
|
||||
@ -7,4 +7,6 @@ fn bar() anyerror!i32 { return 0; }
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:11: error: expression value is ignored
|
||||
// :2:11: error: value of type 'i32' ignored
|
||||
// :2:11: note: all non-void values must be used
|
||||
// :2:11: note: this error can be suppressed by assigning the value to '_'
|
||||
|
||||
@ -6,4 +6,6 @@ export fn foo() void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:15: error: expression value is ignored
|
||||
// :2:15: error: value of type 'comptime_int' ignored
|
||||
// :2:15: note: all non-void values must be used
|
||||
// :2:15: note: this error can be suppressed by assigning the value to '_'
|
||||
|
||||
@ -6,4 +6,6 @@ export fn foo() void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:5: error: expression value is ignored
|
||||
// :2:5: error: value of type 'comptime_int' ignored
|
||||
// :2:5: note: all non-void values must be used
|
||||
// :2:5: note: this error can be suppressed by assigning the value to '_'
|
||||
|
||||
@ -6,4 +6,6 @@ export fn foo() void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:12: error: expression value is ignored
|
||||
// :2:12: error: value of type 'comptime_int' ignored
|
||||
// :2:12: note: all non-void values must be used
|
||||
// :2:12: note: this error can be suppressed by assigning the value to '_'
|
||||
|
||||
@ -7,4 +7,6 @@ fn bar() i32 { return 0; }
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:8: error: expression value is ignored
|
||||
// :2:8: error: value of type 'i32' ignored
|
||||
// :2:8: note: all non-void values must be used
|
||||
// :2:8: note: this error can be suppressed by assigning the value to '_'
|
||||
|
||||
@ -6,4 +6,6 @@ export fn foo() void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:5: error: expression value is ignored
|
||||
// :2:5: error: value of type 'comptime_int' ignored
|
||||
// :2:5: note: all non-void values must be used
|
||||
// :2:5: note: this error can be suppressed by assigning the value to '_'
|
||||
|
||||
@ -40,6 +40,10 @@ pub export fn entry4() void {
|
||||
};
|
||||
_ = U.foo(.{ .foo = 2, .bar = 2 });
|
||||
}
|
||||
pub export fn entry5() void {
|
||||
comptime var y = .{ 1, 2};
|
||||
y = .{ 3, 4 };
|
||||
}
|
||||
// pub export fn entry5() void {
|
||||
// var x: u32 = 15;
|
||||
// const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x });
|
||||
@ -60,3 +64,4 @@ pub export fn entry4() void {
|
||||
// :31:19: error: value stored in comptime field does not match the default value of the field
|
||||
// :25:29: note: default value set here
|
||||
// :41:16: error: value stored in comptime field does not match the default value of the field
|
||||
// :45:12: error: value stored in comptime field does not match the default value of the field
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
export fn entry() void {
|
||||
var foo: u32 = @This(){};
|
||||
_ = foo;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:27: error: expected type 'u32', found 'tmp.tmp'
|
||||
// :1:1: note: struct declared here
|
||||
19
test/cases/compile_errors/nested_error_set_mismatch.zig
Normal file
19
test/cases/compile_errors/nested_error_set_mismatch.zig
Normal file
@ -0,0 +1,19 @@
|
||||
const NextError = error{NextError};
|
||||
const OtherError = error{OutOfMemory};
|
||||
|
||||
export fn entry() void {
|
||||
const a: ?NextError!i32 = foo();
|
||||
_ = a;
|
||||
}
|
||||
|
||||
fn foo() ?OtherError!i32 {
|
||||
return null;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=llvm
|
||||
// target=native
|
||||
//
|
||||
// :4:1: error: expected type '?error{NextError}!i32', found '?error{OutOfMemory}!i32'
|
||||
// :4:1: note: optional type child 'error{OutOfMemory}!i32' cannot cast into optional type child 'error{NextError}!i32'
|
||||
// :4:1: note: 'error.OutOfMemory' not a member of destination error set
|
||||
@ -5,4 +5,4 @@ test "example" { return 1; }
|
||||
// target=native
|
||||
// is_test=1
|
||||
//
|
||||
// :1:25: error: expected type 'void', found 'comptime_int'
|
||||
// :1:25: error: expected type '@typeInfo(@typeInfo(@TypeOf(tmp.test.example)).Fn.return_type.?).ErrorUnion.error_set!void', found 'comptime_int'
|
||||
@ -1,10 +0,0 @@
|
||||
export fn entry() void {
|
||||
var foo: u32 = @This(){};
|
||||
_ = foo;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:2:27: error: type 'u32' does not support array initialization
|
||||
@ -1,20 +0,0 @@
|
||||
const NextError = error{NextError};
|
||||
const OtherError = error{OutOfMemory};
|
||||
|
||||
export fn entry() void {
|
||||
const a: ?NextError!i32 = foo();
|
||||
_ = a;
|
||||
}
|
||||
|
||||
fn foo() ?OtherError!i32 {
|
||||
return null;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:5:34: error: expected type '?NextError!i32', found '?OtherError!i32'
|
||||
// tmp.zig:5:34: note: optional type child 'OtherError!i32' cannot cast into optional type child 'NextError!i32'
|
||||
// tmp.zig:5:34: note: error set 'OtherError' cannot cast into error set 'NextError'
|
||||
// tmp.zig:2:26: note: 'error.OutOfMemory' not a member of destination error set
|
||||
@ -3,7 +3,7 @@ export fn entry() void {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=llvm
|
||||
// target=native
|
||||
//
|
||||
// ?:?:?: error: 10 unused arguments in '{d} {d} {d} {d} {d}'
|
||||
// :?:?: error: 10 unused arguments in '{d} {d} {d} {d} {d}'
|
||||
@ -4,7 +4,7 @@ export fn a() void {
|
||||
fn c(d: i32, e: i32, f: i32) void { _ = d; _ = e; _ = f; }
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:2:6: error: expected 3 argument(s), found 1
|
||||
// :2:6: error: expected 3 argument(s), found 1
|
||||
@ -4,7 +4,8 @@ export fn entry() void {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:3:47: error: expected type 'std.builtin.AtomicOrder', found 'u32'
|
||||
// :3:47: error: expected type 'builtin.AtomicOrder', found 'u32'
|
||||
// :?:?: note: enum declared here
|
||||
@ -4,7 +4,8 @@ comptime {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:3:59: error: expected type 'std.builtin.GlobalLinkage', found 'comptime_int'
|
||||
// :3:50: error: expected type 'builtin.GlobalLinkage', found 'u32'
|
||||
// :?:?: note: enum declared here
|
||||
Loading…
x
Reference in New Issue
Block a user