authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-04-23 09:55:06+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-24 15:55:32-04:00
loga7a8c433d0153bf8d71d17f2cf54027ba8eff805
tree7337f6fe1320661e291930515061b2c11cdea6b2
parent7634e67ba503fdbdf75daff48a13f9d35e331cd4

stage1: Prevent the creation of illegal ptr types

Closes #5140

2 files changed, 14 insertions(+), 0 deletions(-)

src/ir.cpp+6
...@@ -29079,6 +29079,12 @@ static IrInstGen *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstSrcPtrTy...@@ -29079,6 +29079,12 @@ static IrInstGen *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstSrcPtrTy
29079 lazy_ptr_type->base.id = LazyValueIdPtrType;29079 lazy_ptr_type->base.id = LazyValueIdPtrType;
2908029080
29081 if (instruction->sentinel != nullptr) {29081 if (instruction->sentinel != nullptr) {
29082 if (instruction->ptr_len != PtrLenUnknown) {
29083 ir_add_error(ira, &instruction->base.base,
29084 buf_sprintf("sentinels are only allowed on unknown-length pointers"));
29085 return ira->codegen->invalid_inst_gen;
29086 }
29087
29082 lazy_ptr_type->sentinel = instruction->sentinel->child;29088 lazy_ptr_type->sentinel = instruction->sentinel->child;
29083 if (ir_resolve_const(ira, lazy_ptr_type->sentinel, LazyOk) == nullptr)29089 if (ir_resolve_const(ira, lazy_ptr_type->sentinel, LazyOk) == nullptr)
29084 return ira->codegen->invalid_inst_gen;29090 return ira->codegen->invalid_inst_gen;
test/compile_errors.zig+8
...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("invalid pointer syntax",
6 \\export fn foo() void {
7 \\ var guid: *:0 const u8 = undefined;
8 \\}
9 , &[_][]const u8{
10 "tmp.zig:2:15: error: sentinels are only allowed on unknown-length pointers",
11 });
12
5 cases.add("declaration between fields",13 cases.add("declaration between fields",
6 \\const S = struct {14 \\const S = struct {
7 \\ const foo = 2;15 \\ const foo = 2;