Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stage2: support anon init through error unions and optionals #10925

Merged
merged 6 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Air.zig
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ pub const Inst = struct {
/// *(E!T) -> E. If the value is not an error, undefined behavior.
/// Uses the `ty_op` field.
unwrap_errunion_err_ptr,
/// *(E!T) => *T. Sets the value to non-error with an undefined payload value.
/// Uses the `ty_op` field.
errunion_payload_ptr_set,
/// wrap from T to E!T
/// Uses the `ty_op` field.
wrap_errunion_payload,
Expand Down Expand Up @@ -865,6 +868,7 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
.optional_payload,
.optional_payload_ptr,
.optional_payload_ptr_set,
.errunion_payload_ptr_set,
.wrap_optional,
.unwrap_errunion_payload,
.unwrap_errunion_err,
Expand Down
22 changes: 18 additions & 4 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ fn arrayInitExpr(
}
}
const array_type_inst = try typeExpr(gz, scope, array_init.ast.type_expr);
_ = try gz.addUnNode(.validate_array_init_ty, array_type_inst, node);
const elem_type = try gz.addUnNode(.elem_type, array_type_inst, array_init.ast.type_expr);
break :inst .{
.array = array_type_inst,
Expand Down Expand Up @@ -1384,7 +1385,8 @@ fn arrayInitExprRlPtr(
array_ty: Zir.Inst.Ref,
) InnerError!Zir.Inst.Ref {
if (array_ty == .none) {
return arrayInitExprRlPtrInner(gz, scope, node, result_ptr, elements);
const base_ptr = try gz.addUnNode(.array_base_ptr, result_ptr, node);
return arrayInitExprRlPtrInner(gz, scope, node, base_ptr, elements);
}

var as_scope = try gz.makeCoercionScope(scope, array_ty, result_ptr);
Expand Down Expand Up @@ -1493,8 +1495,11 @@ fn structInitExpr(

switch (rl) {
.discard => {
if (struct_init.ast.type_expr != 0)
_ = try typeExpr(gz, scope, struct_init.ast.type_expr);
// TODO if a type expr is given the fields should be validated for that type
if (struct_init.ast.type_expr != 0) {
const ty_inst = try typeExpr(gz, scope, struct_init.ast.type_expr);
_ = try gz.addUnNode(.validate_struct_init_ty, ty_inst, node);
}
for (struct_init.ast.fields) |field_init| {
_ = try expr(gz, scope, .discard, field_init);
}
Expand All @@ -1503,6 +1508,7 @@ fn structInitExpr(
.ref => {
if (struct_init.ast.type_expr != 0) {
const ty_inst = try typeExpr(gz, scope, struct_init.ast.type_expr);
_ = try gz.addUnNode(.validate_struct_init_ty, ty_inst, node);
return structInitExprRlTy(gz, scope, node, struct_init, ty_inst, .struct_init_ref);
} else {
return structInitExprRlNone(gz, scope, node, struct_init, .struct_init_anon_ref);
Expand All @@ -1511,6 +1517,7 @@ fn structInitExpr(
.none => {
if (struct_init.ast.type_expr != 0) {
const ty_inst = try typeExpr(gz, scope, struct_init.ast.type_expr);
_ = try gz.addUnNode(.validate_struct_init_ty, ty_inst, node);
return structInitExprRlTy(gz, scope, node, struct_init, ty_inst, .struct_init);
} else {
return structInitExprRlNone(gz, scope, node, struct_init, .struct_init_anon);
Expand All @@ -1521,6 +1528,7 @@ fn structInitExpr(
return structInitExprRlTy(gz, scope, node, struct_init, ty_inst, .struct_init);
}
const inner_ty_inst = try typeExpr(gz, scope, struct_init.ast.type_expr);
_ = try gz.addUnNode(.validate_struct_init_ty, inner_ty_inst, node);
const result = try structInitExprRlTy(gz, scope, node, struct_init, inner_ty_inst, .struct_init);
return rvalue(gz, rl, result, node);
},
Expand Down Expand Up @@ -1567,9 +1575,11 @@ fn structInitExprRlPtr(
result_ptr: Zir.Inst.Ref,
) InnerError!Zir.Inst.Ref {
if (struct_init.ast.type_expr == 0) {
return structInitExprRlPtrInner(gz, scope, node, struct_init, result_ptr);
const base_ptr = try gz.addUnNode(.field_base_ptr, result_ptr, node);
return structInitExprRlPtrInner(gz, scope, node, struct_init, base_ptr);
}
const ty_inst = try typeExpr(gz, scope, struct_init.ast.type_expr);
_ = try gz.addUnNode(.validate_struct_init_ty, ty_inst, node);

var as_scope = try gz.makeCoercionScope(scope, ty_inst, result_ptr);
defer as_scope.unstack();
Expand Down Expand Up @@ -2281,6 +2291,8 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
.ret_err_value_code,
.extended,
.closure_get,
.array_base_ptr,
.field_base_ptr,
=> break :b false,

// ZIR instructions that are always `noreturn`.
Expand Down Expand Up @@ -2329,6 +2341,8 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
.closure_capture,
.memcpy,
.memset,
.validate_array_init_ty,
.validate_struct_init_ty,
=> break :b true,
}
} else switch (maybe_unused_result) {
Expand Down
1 change: 1 addition & 0 deletions src/Liveness.zig
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ fn analyzeInst(
.optional_payload,
.optional_payload_ptr,
.optional_payload_ptr_set,
.errunion_payload_ptr_set,
.wrap_optional,
.unwrap_errunion_payload,
.unwrap_errunion_err,
Expand Down
Loading