authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-17 12:48:56-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-17 12:48:56-04:00
log9a22c8b6ca98fd01795f8cd4f3e9d92311175f13
treed580bf0cd72ee38b1b2d9aa0d6255d0de9b4369e
parent03ed9e4173131ac5e6157be500e3c50f7e0b0947
parent887bf8ba93a9d060e08e3792afd6b5b9dabd9ccf
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5057 from xackus/opaque-param

stage1: fix assert fail on opaque fn ptr param

4 files changed, 80 insertions(+), 77 deletions(-)

src/analyze.cpp+40-77
...@@ -1891,50 +1891,30 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc...@@ -1891,50 +1891,30 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
1891 }1891 }
1892 }1892 }
18931893
1894 switch (type_entry->id) {1894 if(!is_valid_param_type(type_entry)){
1895 case ZigTypeIdInvalid:1895 if(type_entry->id == ZigTypeIdOpaque){
1896 zig_unreachable();1896 add_node_error(g, param_node->data.param_decl.type,
1897 case ZigTypeIdUnreachable:1897 buf_sprintf("parameter of opaque type '%s' not allowed", buf_ptr(&type_entry->name)));
1898 case ZigTypeIdUndefined:1898 } else {
1899 case ZigTypeIdNull:
1900 case ZigTypeIdOpaque:
1901 add_node_error(g, param_node->data.param_decl.type,1899 add_node_error(g, param_node->data.param_decl.type,
1902 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));1900 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
1903 return g->builtin_types.entry_invalid;1901 }
1904 case ZigTypeIdComptimeFloat:1902
1905 case ZigTypeIdComptimeInt:1903 return g->builtin_types.entry_invalid;
1906 case ZigTypeIdEnumLiteral:1904 }
1907 case ZigTypeIdBoundFn:1905
1908 case ZigTypeIdMetaType:1906 switch (type_requires_comptime(g, type_entry)) {
1909 case ZigTypeIdVoid:1907 case ReqCompTimeNo:
1910 case ZigTypeIdBool:
1911 case ZigTypeIdInt:
1912 case ZigTypeIdFloat:
1913 case ZigTypeIdPointer:
1914 case ZigTypeIdArray:
1915 case ZigTypeIdStruct:
1916 case ZigTypeIdOptional:
1917 case ZigTypeIdErrorUnion:
1918 case ZigTypeIdErrorSet:
1919 case ZigTypeIdEnum:
1920 case ZigTypeIdUnion:
1921 case ZigTypeIdFn:
1922 case ZigTypeIdVector:
1923 case ZigTypeIdFnFrame:
1924 case ZigTypeIdAnyFrame:
1925 switch (type_requires_comptime(g, type_entry)) {
1926 case ReqCompTimeNo:
1927 break;
1928 case ReqCompTimeYes:
1929 add_node_error(g, param_node->data.param_decl.type,
1930 buf_sprintf("parameter of type '%s' must be declared comptime",
1931 buf_ptr(&type_entry->name)));
1932 return g->builtin_types.entry_invalid;
1933 case ReqCompTimeInvalid:
1934 return g->builtin_types.entry_invalid;
1935 }
1936 break;1908 break;
1909 case ReqCompTimeYes:
1910 add_node_error(g, param_node->data.param_decl.type,
1911 buf_sprintf("parameter of type '%s' must be declared comptime",
1912 buf_ptr(&type_entry->name)));
1913 return g->builtin_types.entry_invalid;
1914 case ReqCompTimeInvalid:
1915 return g->builtin_types.entry_invalid;
1937 }1916 }
1917
1938 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];1918 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
1939 param_info->type = type_entry;1919 param_info->type = type_entry;
1940 param_info->is_noalias = param_node->data.param_decl.is_noalias;1920 param_info->is_noalias = param_node->data.param_decl.is_noalias;
...@@ -2001,43 +1981,12 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc...@@ -2001,43 +1981,12 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
2001 }1981 }
2002 }1982 }
20031983
2004 switch (fn_type_id.return_type->id) {1984 switch (type_requires_comptime(g, fn_type_id.return_type)) {
2005 case ZigTypeIdInvalid:1985 case ReqCompTimeInvalid:
2006 case ZigTypeIdUndefined:1986 return g->builtin_types.entry_invalid;
2007 case ZigTypeIdNull:1987 case ReqCompTimeYes:
2008 case ZigTypeIdOpaque:1988 return get_generic_fn_type(g, &fn_type_id);
2009 zig_unreachable();1989 case ReqCompTimeNo:
2010
2011 case ZigTypeIdComptimeFloat:
2012 case ZigTypeIdComptimeInt:
2013 case ZigTypeIdEnumLiteral:
2014 case ZigTypeIdBoundFn:
2015 case ZigTypeIdMetaType:
2016 case ZigTypeIdUnreachable:
2017 case ZigTypeIdVoid:
2018 case ZigTypeIdBool:
2019 case ZigTypeIdInt:
2020 case ZigTypeIdFloat:
2021 case ZigTypeIdPointer:
2022 case ZigTypeIdArray:
2023 case ZigTypeIdStruct:
2024 case ZigTypeIdOptional:
2025 case ZigTypeIdErrorUnion:
2026 case ZigTypeIdErrorSet:
2027 case ZigTypeIdEnum:
2028 case ZigTypeIdUnion:
2029 case ZigTypeIdFn:
2030 case ZigTypeIdVector:
2031 case ZigTypeIdFnFrame:
2032 case ZigTypeIdAnyFrame:
2033 switch (type_requires_comptime(g, fn_type_id.return_type)) {
2034 case ReqCompTimeInvalid:
2035 return g->builtin_types.entry_invalid;
2036 case ReqCompTimeYes:
2037 return get_generic_fn_type(g, &fn_type_id);
2038 case ReqCompTimeNo:
2039 break;
2040 }
2041 break;1990 break;
2042 }1991 }
20431992
...@@ -2057,6 +2006,20 @@ bool is_valid_return_type(ZigType* type) {...@@ -2057,6 +2006,20 @@ bool is_valid_return_type(ZigType* type) {
2057 zig_unreachable();2006 zig_unreachable();
2058}2007}
20592008
2009bool is_valid_param_type(ZigType* type) {
2010 switch (type->id) {
2011 case ZigTypeIdInvalid:
2012 case ZigTypeIdUndefined:
2013 case ZigTypeIdNull:
2014 case ZigTypeIdOpaque:
2015 case ZigTypeIdUnreachable:
2016 return false;
2017 default:
2018 return true;
2019 }
2020 zig_unreachable();
2021}
2022
2060bool type_is_invalid(ZigType *type_entry) {2023bool type_is_invalid(ZigType *type_entry) {
2061 switch (type_entry->id) {2024 switch (type_entry->id) {
2062 case ZigTypeIdInvalid:2025 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);
269bool fn_is_async(ZigFn *fn);269bool fn_is_async(ZigFn *fn);
270CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto);270CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto);
271bool is_valid_return_type(ZigType* type);271bool is_valid_return_type(ZigType* type);
272bool is_valid_param_type(ZigType* type);
272273
273Error type_val_resolve_abi_align(CodeGen *g, AstNode *source_node, ZigValue *type_val, uint32_t *abi_align);274Error type_val_resolve_abi_align(CodeGen *g, AstNode *source_node, ZigValue *type_val, uint32_t *abi_align);
274Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ZigValue *type_val,275Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ZigValue *type_val,
src/ir.cpp+13
...@@ -31109,6 +31109,19 @@ static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, La...@@ -31109,6 +31109,19 @@ static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, La
31109 ZigType *param_type = ir_resolve_type(ira, param_type_inst);31109 ZigType *param_type = ir_resolve_type(ira, param_type_inst);
31110 if (type_is_invalid(param_type))31110 if (type_is_invalid(param_type))
31111 return nullptr;31111 return nullptr;
31112
31113 if(!is_valid_param_type(param_type)){
31114 if(param_type->id == ZigTypeIdOpaque){
31115 ir_add_error(ira, &param_type_inst->base,
31116 buf_sprintf("parameter of opaque type '%s' not allowed", buf_ptr(&param_type->name)));
31117 } else {
31118 ir_add_error(ira, &param_type_inst->base,
31119 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&param_type->name)));
31120 }
31121
31122 return nullptr;
31123 }
31124
31112 switch (type_requires_comptime(ira->codegen, param_type)) {31125 switch (type_requires_comptime(ira->codegen, param_type)) {
31113 case ReqCompTimeYes:31126 case ReqCompTimeYes:
31114 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {31127 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
test/compile_errors.zig+26
...@@ -7046,6 +7046,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -7046,6 +7046,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
7046 "tmp.zig:2:1: note: function declared here",7046 "tmp.zig:2:1: note: function declared here",
7047 });7047 });
70487048
7049 cases.add("function parameter is opaque",
7050 \\const FooType = @OpaqueType();
7051 \\export fn entry1() void {
7052 \\ const someFuncPtr: fn (FooType) void = undefined;
7053 \\}
7054 \\
7055 \\export fn entry2() void {
7056 \\ const someFuncPtr: fn (@TypeOf(null)) void = undefined;
7057 \\}
7058 \\
7059 \\fn foo(p: FooType) void {}
7060 \\export fn entry3() void {
7061 \\ _ = foo;
7062 \\}
7063 \\
7064 \\fn bar(p: @TypeOf(null)) void {}
7065 \\export fn entry4() void {
7066 \\ _ = bar;
7067 \\}
7068 , &[_][]const u8{
7069 "tmp.zig:3:28: error: parameter of opaque type 'FooType' not allowed",
7070 "tmp.zig:7:28: error: parameter of type '(null)' not allowed",
7071 "tmp.zig:10:11: error: parameter of opaque type 'FooType' not allowed",
7072 "tmp.zig:15:11: error: parameter of type '(null)' not allowed",
7073 });
7074
7049 cases.add( // fixed bug #20327075 cases.add( // fixed bug #2032
7050 "compile diagnostic string for top level decl type",7076 "compile diagnostic string for top level decl type",
7051 \\export fn entry() void {7077 \\export fn entry() void {