| author | |
| committer | |
| log | 31d8efc6b32a6b60b7ab292ac50e0f6c02bc140e |
| tree | 145498080181ec421bff99ef1c64a38de57694b9 |
| parent | 7c15c9428e9df26c8699f034704796b03cf839b9 |
| signature |
2 files changed, 48 insertions(+), 1 deletions(-)
src-self-hosted/type.zig+39| ... | ... | @@ -1062,6 +1062,45 @@ pub const Type = extern union { |
| 1062 | 1062 | } |
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 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 | ||
| 1065 | 1104 | /// Asserts the type is a pointer or array type. |
| 1066 | 1105 | pub fn elemType(self: Type) Type { |
| 1067 | 1106 | 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. |
| 364 | 364 | |
| 365 | 365 | fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst { |
| 366 | 366 | 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 | } | |
| 367 | 370 | const ptr_type = try mod.singlePtrType(scope, inst.base.src, true, var_type); |
| 368 | 371 | const b = try mod.requireRuntimeBlock(scope, inst.base.src); |
| 369 | 372 | 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 |
| 760 | 763 | const arena = scope.arena(); |
| 761 | 764 | const param_types = try arena.alloc(Type, fntype.positionals.param_types.len); |
| 762 | 765 | 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; | |
| 764 | 772 | } |
| 765 | 773 | |
| 766 | 774 | const payload = try arena.create(Type.Payload.Function); |