authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-18 13:57:09+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-18 13:57:09+03:00
log31d8efc6b32a6b60b7ab292ac50e0f6c02bc140e
tree145498080181ec421bff99ef1c64a38de57694b9
parent7c15c9428e9df26c8699f034704796b03cf839b9
signature Commit is signed but in an unrecognized format.

stage2: validate param and variable types


2 files changed, 48 insertions(+), 1 deletions(-)

src-self-hosted/type.zig+39
......@@ -1062,6 +1062,45 @@ pub const Type = extern union {
10621062 }
10631063 }
10641064
1065 /// Returns if type can be used for a runtime variable
1066 pub fn isValidVarType(self: Type) bool {
1067 var ty = self;
1068 while (true) switch (ty.zigTypeTag()) {
1069 .Bool,
1070 .Int,
1071 .Float,
1072 .ErrorSet,
1073 .Enum,
1074 .Frame,
1075 .AnyFrame,
1076 .Vector,
1077 => return true,
1078
1079 .BoundFn,
1080 .ComptimeFloat,
1081 .ComptimeInt,
1082 .EnumLiteral,
1083 .NoReturn,
1084 .Type,
1085 .Void,
1086 .Undefined,
1087 .Null,
1088 .Opaque,
1089 => return false,
1090
1091 .Optional => {
1092 var buf: Payload.Pointer = undefined;
1093 return ty.optionalChild(&buf).isValidVarType();
1094 },
1095 .Pointer, .Array => ty = ty.elemType(),
1096
1097 .ErrorUnion => @panic("TODO fn isValidVarType"),
1098 .Fn => @panic("TODO fn isValidVarType"),
1099 .Struct => @panic("TODO struct isValidVarType"),
1100 .Union => @panic("TODO union isValidVarType"),
1101 };
1102 }
1103
10651104 /// Asserts the type is a pointer or array type.
10661105 pub fn elemType(self: Type) Type {
10671106 return switch (self.tag()) {
src-self-hosted/zir_sema.zig+9-1
......@@ -364,6 +364,9 @@ fn analyzeInstEnsureResultNonError(mod: *Module, scope: *Scope, inst: *zir.Inst.
364364
365365fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
366366 const var_type = try resolveType(mod, scope, inst.positionals.operand);
367 if (!var_type.isValidVarType()) {
368 return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type});
369 }
367370 const ptr_type = try mod.singlePtrType(scope, inst.base.src, true, var_type);
368371 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
369372 return mod.addNoOp(b, inst.base.src, ptr_type, .alloc);
......@@ -760,7 +763,12 @@ fn analyzeInstFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) Inne
760763 const arena = scope.arena();
761764 const param_types = try arena.alloc(Type, fntype.positionals.param_types.len);
762765 for (fntype.positionals.param_types) |param_type, i| {
763 param_types[i] = try resolveType(mod, scope, param_type);
766 const resolved = try resolveType(mod, scope, param_type);
767 // TODO skip for comptime params
768 if (!resolved.isValidVarType()) {
769 return mod.fail(scope, param_type.src, "parameter of type '{}' must be declared comptime", .{resolved});
770 }
771 param_types[i] = resolved;
764772 }
765773
766774 const payload = try arena.create(Type.Payload.Function);