diff --git a/lib/std/zig.zig b/lib/std/zig.zig index 292e069820..7897c341b9 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -749,7 +749,6 @@ pub const SimpleComptimeReason = enum(u32) { atomic_order, array_mul_factor, slice_cat_operand, - comptime_call_target, inline_call_target, generic_call_target, wasm_memory_index, @@ -830,7 +829,6 @@ pub const SimpleComptimeReason = enum(u32) { .atomic_order => "atomic order must be comptime-known", .array_mul_factor => "array multiplication factor must be comptime-known", .slice_cat_operand => "slice being concatenated must be comptime-known", - .comptime_call_target => "function being called at comptime must be comptime-known", .inline_call_target => "function being called inline must be comptime-known", .generic_call_target => "generic function being called must be comptime-known", .wasm_memory_index => "wasm memory index must be comptime-known", diff --git a/src/Sema.zig b/src/Sema.zig index f38a82659c..2fe4aac581 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -8021,26 +8021,49 @@ fn analyzeCall( // This is an inline call. The function must be comptime-known. We will analyze its body directly using this `Sema`. - const call_type: []const u8 = if (block.isComptime()) "comptime" else "inline"; - - if (modifier == .never_inline) { - return sema.fail(block, call_src, "cannot perform {s} call with 'never_inline' modifier", .{call_type}); - } if (func_ty_info.is_noinline and !block.isComptime()) { - return sema.fail(block, call_src, "{s} call of noinline function", .{call_type}); + return sema.fail(block, call_src, "inline call of noinline function", .{}); + } + + const call_type: []const u8 = if (block.isComptime()) "comptime" else "inline"; + if (modifier == .never_inline) { + const msg, const fail_block = msg: { + const msg = try sema.errMsg(call_src, "cannot perform {s} call with 'never_inline' modifier", .{call_type}); + errdefer msg.destroy(gpa); + const fail_block = if (block.isComptime()) b: { + break :b try block.explainWhyBlockIsComptime(msg); + } else block; + break :msg .{ msg, fail_block }; + }; + return sema.failWithOwnedErrorMsg(fail_block, msg); } if (func_ty_info.is_var_args) { - return sema.fail(block, call_src, "{s} call of variadic function", .{call_type}); + const msg, const fail_block = msg: { + const msg = try sema.errMsg(call_src, "{s} call of variadic function", .{call_type}); + errdefer msg.destroy(gpa); + const fail_block = if (block.isComptime()) b: { + break :b try block.explainWhyBlockIsComptime(msg); + } else block; + break :msg .{ msg, fail_block }; + }; + return sema.failWithOwnedErrorMsg(fail_block, msg); } - if (func_val == null) { if (func_is_extern) { - return sema.fail(block, call_src, "{s} call of extern function", .{call_type}); + const msg, const fail_block = msg: { + const msg = try sema.errMsg(call_src, "{s} call of extern function", .{call_type}); + errdefer msg.destroy(gpa); + const fail_block = if (block.isComptime()) b: { + break :b try block.explainWhyBlockIsComptime(msg); + } else block; + break :msg .{ msg, fail_block }; + }; + return sema.failWithOwnedErrorMsg(fail_block, msg); } return sema.failWithNeededComptime( block, func_src, - .{ .simple = if (block.isComptime()) .comptime_call_target else .inline_call_target }, + if (block.isComptime()) null else .{ .simple = .inline_call_target }, ); } diff --git a/test/cases/compile_errors/complex_comptime_call_of_extern_function.zig b/test/cases/compile_errors/complex_comptime_call_of_extern_function.zig new file mode 100644 index 0000000000..fc0065cb24 --- /dev/null +++ b/test/cases/compile_errors/complex_comptime_call_of_extern_function.zig @@ -0,0 +1,40 @@ +extern fn next_id() u32; + +const Foo = struct { + bar: Bar, + + fn init() Foo { + return .{ .bar = .init() }; + } +}; +const Bar = struct { + qux: ?Qux, + id: u32, + + fn init() Bar { + return .{ + .qux = null, + .id = next_id(), + }; + } +}; +const Qux = struct { + handleThing: fn () void, +}; + +export fn entry() void { + const foo: Foo = .init(); + _ = foo; +} + +// error +// +// :17:26: error: comptime call of extern function +// :7:31: note: called at comptime from here +// :26:27: note: called at comptime from here +// :26:27: note: call to function with comptime-only return type 'tmp.Foo' is evaluated at comptime +// :6:15: note: return type declared here +// :4:10: note: struct requires comptime because of this field +// :11:10: note: struct requires comptime because of this field +// :22:18: note: struct requires comptime because of this field +// :22:18: note: use '*const fn () void' for a function pointer type diff --git a/test/cases/compile_errors/comptime_call_of_function_pointer.zig b/test/cases/compile_errors/comptime_call_of_function_pointer.zig index a73f541b5e..d419d5fe86 100644 --- a/test/cases/compile_errors/comptime_call_of_function_pointer.zig +++ b/test/cases/compile_errors/comptime_call_of_function_pointer.zig @@ -6,4 +6,4 @@ export fn entry() void { // error // // :3:14: error: unable to resolve comptime value -// :3:14: note: function being called at comptime must be comptime-known +// :3:5: note: 'comptime' keyword forces comptime evaluation diff --git a/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig b/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig index b06767111d..1f3ab93c96 100644 --- a/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig +++ b/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig @@ -7,3 +7,4 @@ export fn entry() i32 { // error // // :2:14: error: comptime call of extern function +// :2:14: note: initializer of container-level variable must be comptime-known diff --git a/test/cases/compile_errors/invalid_extern_function_call.zig b/test/cases/compile_errors/invalid_extern_function_call.zig index 36a8ac3fd7..887d2e1da0 100644 --- a/test/cases/compile_errors/invalid_extern_function_call.zig +++ b/test/cases/compile_errors/invalid_extern_function_call.zig @@ -11,4 +11,5 @@ export fn entry1() void { // error // // :4:15: error: comptime call of extern function +// :4:5: note: 'comptime' keyword forces comptime evaluation // :8:5: error: inline call of extern function diff --git a/test/cases/compile_errors/invalid_pointer_for_var_type.zig b/test/cases/compile_errors/invalid_pointer_for_var_type.zig index 2d80718b4f..01e049dcd0 100644 --- a/test/cases/compile_errors/invalid_pointer_for_var_type.zig +++ b/test/cases/compile_errors/invalid_pointer_for_var_type.zig @@ -9,3 +9,4 @@ export fn f() void { // error // // :2:16: error: comptime call of extern function +// :2:12: note: types must be comptime-known diff --git a/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig b/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig index 808f94d9bf..7750e325b8 100644 --- a/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig +++ b/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig @@ -11,3 +11,4 @@ export fn entry() usize { // error // // :4:27: error: comptime call of extern function +// :4:14: note: initializer of container-level variable must be comptime-known