aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-06-06 16:12:31 -0400
committerGitHub <noreply@github.com>2022-06-06 16:12:31 -0400
commit367e2b2fe43a2de09767ad8d5657866088b44678 (patch)
tree686b57ecfcfd472d70493c46c60a562b96d5ad31 /src/Sema.zig
parent41bf81dc3231eb763c93eb95b152e7ab8d3c5af8 (diff)
parent14685e59b26c8dc002ce6c25c6916cbad54e79d0 (diff)
downloadzig-367e2b2fe43a2de09767ad8d5657866088b44678.tar.gz
zig-367e2b2fe43a2de09767ad8d5657866088b44678.zip
Merge pull request #11800 from Vexu/stage2
`zig2 build test-std` progress
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig73
1 files changed, 56 insertions, 17 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 593b299833..381f922093 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -1739,9 +1739,9 @@ pub fn resolveAlign(
block: *Block,
src: LazySrcLoc,
zir_ref: Zir.Inst.Ref,
-) !u16 {
- const alignment_big = try sema.resolveInt(block, src, zir_ref, Type.initTag(.u16));
- const alignment = @intCast(u16, alignment_big); // We coerce to u16 in the prev line.
+) !u32 {
+ const alignment_big = try sema.resolveInt(block, src, zir_ref, Type.initTag(.u29));
+ const alignment = @intCast(u32, alignment_big); // We coerce to u16 in the prev line.
if (alignment == 0) return sema.fail(block, src, "alignment must be >= 1", .{});
if (!std.math.isPowerOfTwo(alignment)) {
return sema.fail(block, src, "alignment value {d} is not a power of two", .{
@@ -1875,7 +1875,7 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr));
const dummy_operand = try trash_block.addBitCast(pointee_ty, .void_value);
- try sema.storePtr(&trash_block, src, dummy_ptr, dummy_operand);
+ try sema.storePtr2(&trash_block, src, dummy_ptr, src, dummy_operand, src, .bitcast);
{
const air_tags = sema.air_instructions.items(.tag);
@@ -2526,7 +2526,7 @@ fn zirRetPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
const src: LazySrcLoc = .{ .node_offset = inst_data };
try sema.requireFunctionBlock(block, src);
- if (block.is_comptime) {
+ if (block.is_comptime or try sema.typeRequiresComptime(block, src, sema.fn_ret_ty)) {
const fn_ret_ty = try sema.resolveTypeFields(block, src, sema.fn_ret_ty);
return sema.analyzeComptimeAlloc(block, fn_ret_ty, 0, src);
}
@@ -2663,7 +2663,7 @@ fn zirAllocExtended(
break :blk try sema.resolveType(block, ty_src, type_ref);
} else undefined;
- const alignment: u16 = if (small.has_align) blk: {
+ const alignment: u32 = if (small.has_align) blk: {
const align_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
extra_index += 1;
const alignment = try sema.resolveAlign(block, align_src, align_ref);
@@ -3616,7 +3616,7 @@ fn zirValidateArrayInit(
const air_tags = sema.air_instructions.items(.tag);
const air_datas = sema.air_instructions.items(.data);
- for (instrs) |elem_ptr, i| {
+ outer: for (instrs) |elem_ptr, i| {
const elem_ptr_data = sema.code.instructions.items(.data)[elem_ptr].pl_node;
const elem_src: LazySrcLoc = .{ .node_offset = elem_ptr_data.src_node };
@@ -3630,6 +3630,10 @@ fn zirValidateArrayInit(
// of the for loop.
var block_index = block.instructions.items.len - 1;
while (block.instructions.items[block_index] != elem_ptr_air_inst) {
+ if (block_index == 0) {
+ array_is_comptime = true;
+ continue :outer;
+ }
block_index -= 1;
}
first_block_index = @minimum(first_block_index, block_index);
@@ -3672,6 +3676,13 @@ fn zirValidateArrayInit(
}
if (array_is_comptime) {
+ if (try sema.resolveDefinedValue(block, init_src, array_ptr)) |ptr_val| {
+ if (ptr_val.tag() == .comptime_field_ptr) {
+ // This store was validated by the individual elem ptrs.
+ return;
+ }
+ }
+
// Our task is to delete all the `elem_ptr` and `store` instructions, and insert
// instead a single `store` to the array_ptr with a comptime struct value.
// Also to populate the sentinel value, if any.
@@ -14199,7 +14210,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
.size = ptr_size,
.mutable = !is_const_val.toBool(),
.@"volatile" = is_volatile_val.toBool(),
- .@"align" = @intCast(u16, alignment_val.toUnsignedInt(target)), // TODO: Validate this value.
+ .@"align" = @intCast(u29, alignment_val.toUnsignedInt(target)), // TODO: Validate this value.
.@"addrspace" = address_space_val.toEnum(std.builtin.AddressSpace),
.pointee_type = try child_ty.copy(sema.arena),
.@"allowzero" = is_allowzero_val.toBool(),
@@ -16973,7 +16984,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
const body = sema.code.extra[extra_index..][0..body_len];
extra_index += body.len;
- const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u16);
+ const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u29);
if (val.tag() == .generic_poison) {
break :blk null;
}
@@ -18462,14 +18473,11 @@ fn structFieldPtrByIndex(
const ptr_field_ty = try Type.ptr(sema.arena, sema.mod, ptr_ty_data);
if (field.is_comptime) {
- var anon_decl = try block.startAnonDecl(field_src);
- defer anon_decl.deinit();
- const decl = try anon_decl.finish(
- try field.ty.copy(anon_decl.arena()),
- try field.default_val.copy(anon_decl.arena()),
- ptr_ty_data.@"align",
- );
- return sema.analyzeDeclRef(decl);
+ const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
+ .field_ty = try field.ty.copy(sema.arena),
+ .field_val = try field.default_val.copy(sema.arena),
+ });
+ return sema.addConstant(ptr_field_ty, val);
}
if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| {
@@ -20187,6 +20195,13 @@ fn storePtr2(
// TODO handle if the element type requires comptime
+ if (air_tag == .bitcast) {
+ // `air_tag == .bitcast` is used as a special case for `zirCoerceResultPtr`
+ // to avoid calling `requireRuntimeBlock` for the dummy block.
+ _ = try block.addBinOp(.store, ptr, operand);
+ return;
+ }
+
try sema.requireRuntimeBlock(block, runtime_src);
try sema.queueFullTypeResolution(elem_ty);
_ = try block.addBinOp(air_tag, ptr, operand);
@@ -20240,6 +20255,14 @@ fn storePtrVal(
const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, mut_kit.ty, 0);
+ if (mut_kit.decl_ref_mut.runtime_index == std.math.maxInt(u32)) {
+ // Special case for comptime field ptr.
+ if (!mut_kit.val.eql(bitcasted_val, mut_kit.ty, sema.mod)) {
+ return sema.fail(block, src, "value stored in comptime field does not match the default value of the field", .{});
+ }
+ return;
+ }
+
const arena = mut_kit.beginArena(sema.mod);
defer mut_kit.finishArena(sema.mod);
@@ -20289,6 +20312,19 @@ fn beginComptimePtrMutation(
.ty = decl.ty,
};
},
+ .comptime_field_ptr => {
+ const payload = ptr_val.castTag(.comptime_field_ptr).?.data;
+ const duped = try sema.arena.create(Value);
+ duped.* = payload.field_val;
+ return ComptimePtrMutationKit{
+ .decl_ref_mut = .{
+ .decl_index = @intToEnum(Module.Decl.Index, 0),
+ .runtime_index = std.math.maxInt(u32),
+ },
+ .val = duped,
+ .ty = payload.field_ty,
+ };
+ },
.elem_ptr => {
const elem_ptr = ptr_val.castTag(.elem_ptr).?.data;
var parent = try beginComptimePtrMutation(sema, block, src, elem_ptr.array_ptr);
@@ -23850,6 +23886,7 @@ pub fn typeHasOnePossibleValue(
.i8,
.u16,
.i16,
+ .u29,
.u32,
.i32,
.u64,
@@ -24143,6 +24180,7 @@ pub fn addType(sema: *Sema, ty: Type) !Air.Inst.Ref {
.u8 => return .u8_type,
.i8 => return .i8_type,
.u16 => return .u16_type,
+ .u29 => return .u29_type,
.i16 => return .i16_type,
.u32 => return .u32_type,
.i32 => return .i32_type,
@@ -24513,6 +24551,7 @@ pub fn typeRequiresComptime(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Typ
.i8,
.u16,
.i16,
+ .u29,
.u32,
.i32,
.u64,