mirror of
https://github.com/ziglang/zig.git
synced 2026-01-21 06:45:24 +00:00
Sema: add error for dereferencing comptime value at runtime
This commit is contained in:
parent
71e2a56e3e
commit
b0e8bf15f5
@ -20961,6 +20961,9 @@ fn analyzeLoad(
|
||||
}
|
||||
}
|
||||
|
||||
const valid_rt = try sema.validateRunTimeType(block, src, elem_ty, false);
|
||||
if (!valid_rt) return sema.failWithNeededComptime(block, src);
|
||||
|
||||
try sema.requireRuntimeBlock(block, src);
|
||||
return block.addTyOp(.load, elem_ty, ptr);
|
||||
}
|
||||
|
||||
50
test/cases/compile_errors/dereference_anyopaque.zig
Normal file
50
test/cases/compile_errors/dereference_anyopaque.zig
Normal file
@ -0,0 +1,50 @@
|
||||
const std = @import("std");
|
||||
|
||||
const Error = error{Something};
|
||||
|
||||
fn next() Error!void {
|
||||
return;
|
||||
}
|
||||
|
||||
fn parse(comptime T: type, allocator: std.mem.Allocator) !void {
|
||||
parseFree(T, undefined, allocator);
|
||||
_ = (try next()) != null;
|
||||
}
|
||||
|
||||
fn parseFree(comptime T: type, value: T, allocator: std.mem.Allocator) void {
|
||||
switch (@typeInfo(T)) {
|
||||
.Struct => |structInfo| {
|
||||
inline for (structInfo.fields) |field| {
|
||||
if (!field.is_comptime)
|
||||
parseFree(field.field_type, undefined, allocator);
|
||||
}
|
||||
},
|
||||
.Pointer => |ptrInfo| {
|
||||
switch (ptrInfo.size) {
|
||||
.One => {
|
||||
parseFree(ptrInfo.child, value.*, allocator);
|
||||
},
|
||||
.Slice => {
|
||||
for (value) |v|
|
||||
parseFree(ptrInfo.child, v, allocator);
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
pub export fn entry() void {
|
||||
const allocator = std.testing.allocator_instance.allocator();
|
||||
_ = parse(std.StringArrayHashMap(bool), allocator) catch return;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=llvm
|
||||
//
|
||||
// :11:22: error: comparison of 'void' with null
|
||||
// :25:51: error: unable to resolve comptime value
|
||||
// :25:51: error: unable to resolve comptime value
|
||||
// :25:51: error: unable to resolve comptime value
|
||||
// :25:51: error: unable to resolve comptime value
|
||||
Loading…
x
Reference in New Issue
Block a user