mirror of
https://github.com/ziglang/zig.git
synced 2026-02-15 22:09:49 +00:00
Sema: validate array element types
Fixes the compiler crash part of #15175
This commit is contained in:
parent
82a6acca93
commit
66520c8342
10
src/Sema.zig
10
src/Sema.zig
@ -7819,6 +7819,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
|
||||
const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
|
||||
const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, "array length must be comptime-known");
|
||||
const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
|
||||
try sema.validateArrayElemType(block, elem_type, elem_src);
|
||||
const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
|
||||
|
||||
return sema.addType(array_ty);
|
||||
@ -7835,6 +7836,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
|
||||
const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
|
||||
const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, "array length must be comptime-known");
|
||||
const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);
|
||||
try sema.validateArrayElemType(block, elem_type, elem_src);
|
||||
const uncasted_sentinel = try sema.resolveInst(extra.sentinel);
|
||||
const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src);
|
||||
const sentinel_val = try sema.resolveConstValue(block, sentinel_src, sentinel, "array sentinel value must be comptime-known");
|
||||
@ -7843,6 +7845,14 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
|
||||
return sema.addType(array_ty);
|
||||
}
|
||||
|
||||
fn validateArrayElemType(sema: *Sema, block: *Block, elem_type: Type, elem_src: LazySrcLoc) !void {
|
||||
if (elem_type.zigTypeTag() == .Opaque) {
|
||||
return sema.fail(block, elem_src, "array of opaque type '{}' not allowed", .{elem_type.fmt(sema.mod)});
|
||||
} else if (elem_type.zigTypeTag() == .NoReturn) {
|
||||
return sema.fail(block, elem_src, "array of 'noreturn' not allowed", .{});
|
||||
}
|
||||
}
|
||||
|
||||
fn zirAnyframeType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
13
test/cases/compile_errors/invalid_array_elem_type.zig
Normal file
13
test/cases/compile_errors/invalid_array_elem_type.zig
Normal file
@ -0,0 +1,13 @@
|
||||
comptime {
|
||||
_ = [1]anyopaque;
|
||||
}
|
||||
comptime {
|
||||
_ = [1]noreturn;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:12: error: array of opaque type 'anyopaque' not allowed
|
||||
// :5:12: error: array of 'noreturn' not allowed
|
||||
Loading…
x
Reference in New Issue
Block a user