Skip to content

Commit

Permalink
compiler: preserve result type information through address-of operator
Browse files Browse the repository at this point in the history
This commit introduces the new `ref_coerced_ty` result type into AstGen.
This represents a expression which we want to treat as an lvalue, and
the pointer will be coerced to a given type.

This change gives known result types to many expressions, in particular
struct and array initializations. This allows certain casts to work
which previously required explicitly specifying types via `@as`. It also
eliminates our dependence on anonymous struct types for expressions of
the form `&.{ ... }` - this paves the way for ziglang#16865, and also results
in less Sema magic happening for such initializations, also leading to
potentially better runtime code.

As part of these changes, this commit also implements ziglang#17194 by
disallowing RLS on explicitly-typed struct and array initializations.
Apologies for linking these changes - it seemed rather pointless to try
and separate them, since they both make big changes to struct and array
initializations in AstGen. The rationale for this change can be found in
the proposal - in essence, performing RLS whilst maintaining the
semantics of the intermediary type is a very difficult problem to solve.

This allowed the problematic `coerce_result_ptr` ZIR instruction to be
completely eliminated, which in turn also simplified the logic for
inferred allocations in Sema - thanks to this, we almost break even on
line count!

In doing this, the ZIR instructions surrounding these initializations
have been restructured - some have been added and removed, and others
renamed for clarity (and their semantics changed slightly). In order to
optimize ZIR tag count, the `struct_init_anon_ref` and
`array_init_anon_ref` instructions have been removed in favour of using
`ref` on a standard anonymous value initialization, since these
instructions are now virtually never used.

Lastly, it's worth noting that this commit introduces a slightly strange
source of generic poison types: in the expression `@as(*anyopaque, &x)`,
the sub-expression `x` has a generic poison result type, despite no
generic code being involved. This turns out to be a logical choice,
because we don't know the result type for `x`, and the generic poison
type represents precisely this case, providing the semantics we need.

Resolves: ziglang#16512
Resolves: ziglang#17194
  • Loading branch information
mlugg committed Sep 23, 2023
1 parent b43a713 commit 1b50090
Show file tree
Hide file tree
Showing 34 changed files with 1,248 additions and 1,024 deletions.
7 changes: 6 additions & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,12 @@ pub const StackIterator = struct {

return StackIterator{
.first_address = first_address,
.fp = fp orelse @frameAddress(),
// TODO: this is a workaround for #16876
//.fp = fp orelse @frameAddress(),
.fp = fp orelse blk: {
const fa = @frameAddress();
break :blk fa;
},
};
}

Expand Down
623 changes: 318 additions & 305 deletions src/AstGen.zig

Large diffs are not rendered by default.

44 changes: 26 additions & 18 deletions src/AstRlAnnotate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,21 @@ fn expr(astrl: *AstRlAnnotate, node: Ast.Node.Index, block: ?*Block, ri: ResultI
=> {
var buf: [2]Ast.Node.Index = undefined;
const full = tree.fullArrayInit(&buf, node).?;
const have_type = if (full.ast.type_expr != 0) have_type: {

if (full.ast.type_expr != 0) {
// Explicitly typed init does not participate in RLS
_ = try astrl.expr(full.ast.type_expr, block, ResultInfo.none);
break :have_type true;
} else ri.have_type;
if (have_type) {
const elem_ri: ResultInfo = .{
.have_type = true,
.have_ptr = ri.have_ptr,
};
for (full.ast.elements) |elem_init| {
_ = try astrl.expr(elem_init, block, elem_ri);
_ = try astrl.expr(elem_init, block, ResultInfo.type_only);
}
return false;
}

if (ri.have_type) {
// Always forward type information
// If we have a result pointer, we use and forward it
for (full.ast.elements) |elem_init| {
_ = try astrl.expr(elem_init, block, ri);
}
return ri.have_ptr;
} else {
Expand All @@ -702,17 +706,21 @@ fn expr(astrl: *AstRlAnnotate, node: Ast.Node.Index, block: ?*Block, ri: ResultI
=> {
var buf: [2]Ast.Node.Index = undefined;
const full = tree.fullStructInit(&buf, node).?;
const have_type = if (full.ast.type_expr != 0) have_type: {

if (full.ast.type_expr != 0) {
// Explicitly typed init does not participate in RLS
_ = try astrl.expr(full.ast.type_expr, block, ResultInfo.none);
break :have_type true;
} else ri.have_type;
if (have_type) {
const elem_ri: ResultInfo = .{
.have_type = true,
.have_ptr = ri.have_ptr,
};
for (full.ast.fields) |field_init| {
_ = try astrl.expr(field_init, block, elem_ri);
_ = try astrl.expr(field_init, block, ResultInfo.type_only);
}
return false;
}

if (ri.have_type) {
// Always forward type information
// If we have a result pointer, we use and forward it
for (full.ast.fields) |field_init| {
_ = try astrl.expr(field_init, block, ri);
}
return ri.have_ptr;
} else {
Expand Down
37 changes: 3 additions & 34 deletions src/Autodoc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2391,34 +2391,6 @@ fn walkInstruction(
.expr = .{ .@"&" = expr_index },
};
},
.array_init_anon_ref => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.MultiOp, pl_node.payload_index);
const operands = file.zir.refSlice(extra.end, extra.data.operands_len);
const array_data = try self.arena.alloc(usize, operands.len);

for (operands, 0..) |op, idx| {
const wr = try self.walkRef(
file,
parent_scope,
parent_src,
op,
false,
call_ctx,
);
const expr_index = self.exprs.items.len;
try self.exprs.append(self.arena, wr.expr);
array_data[idx] = expr_index;
}

const expr_index = self.exprs.items.len;
try self.exprs.append(self.arena, .{ .array = array_data });

return DocData.WalkResult{
.typeRef = null,
.expr = .{ .@"&" = expr_index },
};
},
.float => {
const float = data[inst_index].float;
return DocData.WalkResult{
Expand Down Expand Up @@ -2709,9 +2681,7 @@ fn walkInstruction(
.expr = .{ .declRef = decl_status },
};
},
.field_val, .field_ptr, .field_type => {
// TODO: field type uses Zir.Inst.FieldType, it just happens to have the
// same layout as Zir.Inst.Field :^)
.field_val, .field_ptr => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Field, pl_node.payload_index);

Expand All @@ -2730,8 +2700,7 @@ fn walkInstruction(
};

if (tags[lhs] != .field_val and
tags[lhs] != .field_ptr and
tags[lhs] != .field_type) break :blk lhs_extra.data.lhs;
tags[lhs] != .field_ptr) break :blk lhs_extra.data.lhs;

lhs_extra = file.zir.extraData(
Zir.Inst.Field,
Expand Down Expand Up @@ -2870,7 +2839,7 @@ fn walkInstruction(

const field_name = blk: {
const field_inst_index = init_extra.data.field_type;
if (tags[field_inst_index] != .field_type) unreachable;
if (tags[field_inst_index] != .struct_init_field_type) unreachable;
const field_pl_node = data[field_inst_index].pl_node;
const field_extra = file.zir.extraData(
Zir.Inst.FieldType,
Expand Down
Loading

0 comments on commit 1b50090

Please sign in to comment.