| author | |
| committer | |
| log | 0db9e90e8f177c3a18f959e9067621e1dbc07c32 |
| tree | cc8bc1dc54fac9284a956b9051cf9f2ceebd9c9f |
| parent | 9b788b765c1557a414710872fa9c11fce1f8c504 |
4 files changed, 74 insertions(+), 40 deletions(-)
src/analyze.cpp+34-40| ... | @@ -1893,50 +1893,30 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc | ... | @@ -1893,50 +1893,30 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc |
| 1893 | } | 1893 | } |
| 1894 | } | 1894 | } |
| 1895 | 1895 | ||
| 1896 | switch (type_entry->id) { | 1896 | if(!is_valid_param_type(type_entry)){ |
| 1897 | case ZigTypeIdInvalid: | 1897 | if(type_entry->id == ZigTypeIdOpaque){ |
| 1898 | zig_unreachable(); | 1898 | add_node_error(g, param_node->data.param_decl.type, |
| 1899 | case ZigTypeIdUnreachable: | 1899 | buf_sprintf("parameter of opaque type '%s' not allowed", buf_ptr(&type_entry->name))); |
| 1900 | case ZigTypeIdUndefined: | 1900 | } else { |
| 1901 | case ZigTypeIdNull: | ||
| 1902 | case ZigTypeIdOpaque: | ||
| 1903 | add_node_error(g, param_node->data.param_decl.type, | 1901 | add_node_error(g, param_node->data.param_decl.type, |
| 1904 | buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name))); | 1902 | buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name))); |
| 1905 | return g->builtin_types.entry_invalid; | 1903 | } |
| 1906 | case ZigTypeIdComptimeFloat: | 1904 | |
| 1907 | case ZigTypeIdComptimeInt: | 1905 | return g->builtin_types.entry_invalid; |
| 1908 | case ZigTypeIdEnumLiteral: | 1906 | } |
| 1909 | case ZigTypeIdBoundFn: | 1907 | |
| 1910 | case ZigTypeIdMetaType: | 1908 | switch (type_requires_comptime(g, type_entry)) { |
| 1911 | case ZigTypeIdVoid: | 1909 | case ReqCompTimeNo: |
| 1912 | case ZigTypeIdBool: | ||
| 1913 | case ZigTypeIdInt: | ||
| 1914 | case ZigTypeIdFloat: | ||
| 1915 | case ZigTypeIdPointer: | ||
| 1916 | case ZigTypeIdArray: | ||
| 1917 | case ZigTypeIdStruct: | ||
| 1918 | case ZigTypeIdOptional: | ||
| 1919 | case ZigTypeIdErrorUnion: | ||
| 1920 | case ZigTypeIdErrorSet: | ||
| 1921 | case ZigTypeIdEnum: | ||
| 1922 | case ZigTypeIdUnion: | ||
| 1923 | case ZigTypeIdFn: | ||
| 1924 | case ZigTypeIdVector: | ||
| 1925 | case ZigTypeIdFnFrame: | ||
| 1926 | case ZigTypeIdAnyFrame: | ||
| 1927 | switch (type_requires_comptime(g, type_entry)) { | ||
| 1928 | case ReqCompTimeNo: | ||
| 1929 | break; | ||
| 1930 | case ReqCompTimeYes: | ||
| 1931 | add_node_error(g, param_node->data.param_decl.type, | ||
| 1932 | buf_sprintf("parameter of type '%s' must be declared comptime", | ||
| 1933 | buf_ptr(&type_entry->name))); | ||
| 1934 | return g->builtin_types.entry_invalid; | ||
| 1935 | case ReqCompTimeInvalid: | ||
| 1936 | return g->builtin_types.entry_invalid; | ||
| 1937 | } | ||
| 1938 | break; | 1910 | break; |
| 1911 | case ReqCompTimeYes: | ||
| 1912 | add_node_error(g, param_node->data.param_decl.type, | ||
| 1913 | buf_sprintf("parameter of type '%s' must be declared comptime", | ||
| 1914 | buf_ptr(&type_entry->name))); | ||
| 1915 | return g->builtin_types.entry_invalid; | ||
| 1916 | case ReqCompTimeInvalid: | ||
| 1917 | return g->builtin_types.entry_invalid; | ||
| 1939 | } | 1918 | } |
| 1919 | |||
| 1940 | FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index]; | 1920 | FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index]; |
| 1941 | param_info->type = type_entry; | 1921 | param_info->type = type_entry; |
| 1942 | param_info->is_noalias = param_node->data.param_decl.is_noalias; | 1922 | param_info->is_noalias = param_node->data.param_decl.is_noalias; |
| ... | @@ -2059,6 +2039,20 @@ bool is_valid_return_type(ZigType* type) { | ... | @@ -2059,6 +2039,20 @@ bool is_valid_return_type(ZigType* type) { |
| 2059 | zig_unreachable(); | 2039 | zig_unreachable(); |
| 2060 | } | 2040 | } |
| 2061 | 2041 | ||
| 2042 | bool is_valid_param_type(ZigType* type) { | ||
| 2043 | switch (type->id) { | ||
| 2044 | case ZigTypeIdInvalid: | ||
| 2045 | case ZigTypeIdUndefined: | ||
| 2046 | case ZigTypeIdNull: | ||
| 2047 | case ZigTypeIdOpaque: | ||
| 2048 | case ZigTypeIdUnreachable: | ||
| 2049 | return false; | ||
| 2050 | default: | ||
| 2051 | return true; | ||
| 2052 | } | ||
| 2053 | zig_unreachable(); | ||
| 2054 | } | ||
| 2055 | |||
| 2062 | bool type_is_invalid(ZigType *type_entry) { | 2056 | bool type_is_invalid(ZigType *type_entry) { |
| 2063 | switch (type_entry->id) { | 2057 | switch (type_entry->id) { |
| 2064 | case ZigTypeIdInvalid: | 2058 | case ZigTypeIdInvalid: |
src/analyze.hpp+1| ... | @@ -269,6 +269,7 @@ void resolve_llvm_types_fn(CodeGen *g, ZigFn *fn); | ... | @@ -269,6 +269,7 @@ void resolve_llvm_types_fn(CodeGen *g, ZigFn *fn); |
| 269 | bool fn_is_async(ZigFn *fn); | 269 | bool fn_is_async(ZigFn *fn); |
| 270 | CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto); | 270 | CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto); |
| 271 | bool is_valid_return_type(ZigType* type); | 271 | bool is_valid_return_type(ZigType* type); |
| 272 | bool is_valid_param_type(ZigType* type); | ||
| 272 | 273 | ||
| 273 | Error type_val_resolve_abi_align(CodeGen *g, AstNode *source_node, ZigValue *type_val, uint32_t *abi_align); | 274 | Error type_val_resolve_abi_align(CodeGen *g, AstNode *source_node, ZigValue *type_val, uint32_t *abi_align); |
| 274 | Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ZigValue *type_val, | 275 | Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ZigValue *type_val, |
src/ir.cpp+13| ... | @@ -31082,6 +31082,19 @@ static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, La | ... | @@ -31082,6 +31082,19 @@ static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, La |
| 31082 | ZigType *param_type = ir_resolve_type(ira, param_type_inst); | 31082 | ZigType *param_type = ir_resolve_type(ira, param_type_inst); |
| 31083 | if (type_is_invalid(param_type)) | 31083 | if (type_is_invalid(param_type)) |
| 31084 | return nullptr; | 31084 | return nullptr; |
| 31085 | |||
| 31086 | if(!is_valid_param_type(param_type)){ | ||
| 31087 | if(param_type->id == ZigTypeIdOpaque){ | ||
| 31088 | ir_add_error(ira, &param_type_inst->base, | ||
| 31089 | buf_sprintf("parameter of opaque type '%s' not allowed", buf_ptr(&param_type->name))); | ||
| 31090 | } else { | ||
| 31091 | ir_add_error(ira, &param_type_inst->base, | ||
| 31092 | buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&param_type->name))); | ||
| 31093 | } | ||
| 31094 | |||
| 31095 | return nullptr; | ||
| 31096 | } | ||
| 31097 | |||
| 31085 | switch (type_requires_comptime(ira->codegen, param_type)) { | 31098 | switch (type_requires_comptime(ira->codegen, param_type)) { |
| 31086 | case ReqCompTimeYes: | 31099 | case ReqCompTimeYes: |
| 31087 | if (!calling_convention_allows_zig_types(fn_type_id.cc)) { | 31100 | if (!calling_convention_allows_zig_types(fn_type_id.cc)) { |
test/compile_errors.zig+26| ... | @@ -6979,6 +6979,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { | ... | @@ -6979,6 +6979,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 6979 | "tmp.zig:2:1: note: function declared here", | 6979 | "tmp.zig:2:1: note: function declared here", |
| 6980 | }); | 6980 | }); |
| 6981 | 6981 | ||
| 6982 | cases.add("function parameter is opaque", | ||
| 6983 | \\const FooType = @OpaqueType(); | ||
| 6984 | \\export fn entry1() void { | ||
| 6985 | \\ const someFuncPtr: fn (FooType) void = undefined; | ||
| 6986 | \\} | ||
| 6987 | \\ | ||
| 6988 | \\export fn entry2() void { | ||
| 6989 | \\ const someFuncPtr: fn (@TypeOf(null)) void = undefined; | ||
| 6990 | \\} | ||
| 6991 | \\ | ||
| 6992 | \\fn foo(p: FooType) void {} | ||
| 6993 | \\export fn entry3() void { | ||
| 6994 | \\ _ = foo; | ||
| 6995 | \\} | ||
| 6996 | \\ | ||
| 6997 | \\fn bar(p: @TypeOf(null)) void {} | ||
| 6998 | \\export fn entry4() void { | ||
| 6999 | \\ _ = bar; | ||
| 7000 | \\} | ||
| 7001 | , &[_][]const u8{ | ||
| 7002 | "tmp.zig:3:28: error: parameter of opaque type 'FooType' not allowed", | ||
| 7003 | "tmp.zig:7:28: error: parameter of type '(null)' not allowed", | ||
| 7004 | "tmp.zig:10:11: error: parameter of opaque type 'FooType' not allowed", | ||
| 7005 | "tmp.zig:15:11: error: parameter of type '(null)' not allowed", | ||
| 7006 | }); | ||
| 7007 | |||
| 6982 | cases.add( // fixed bug #2032 | 7008 | cases.add( // fixed bug #2032 |
| 6983 | "compile diagnostic string for top level decl type", | 7009 | "compile diagnostic string for top level decl type", |
| 6984 | \\export fn entry() void { | 7010 | \\export fn entry() void { |