From bb8761d3924a2f3974de0d1e162734406ae927a1 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Mon, 2 Nov 2026 21:41:19 +0800 Subject: [PATCH] =?UTF-8?q?nob=20=E5=AE=9E=E7=8E=B0=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E6=AD=A5=E7=BC=96=E8=AF=91=E6=A1=86=E6=9E=B6=20?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=20?= =?UTF-8?q?=E5=87=86=E5=A4=87for=E7=9A=84=E6=96=B0=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nob.c | 13 +++- nob/nob_data.h | 6 +- stage2/ast.spl | 2 + stage2/parser.spl | 13 +++- stage2/token.spl | 6 +- tests/stage3/006_with_fn.spl | 2 +- tests/stage3/011_generic_bind.spl | 23 +++++++ tests/stage3/012_generic_local.spl | 23 +++++++ ..._array_as_ptr.spl => 013_array_as_ptr.spl} | 0 tests/stage3/014_for_syntax.spl | 24 +++++++ tests/stage3/015_for_missing_in.spl | 9 +++ ..._call_too_few.spl => 016_call_too_few.spl} | 0 ...all_too_many.spl => 017_call_too_many.spl} | 0 ...all_arg_type.spl => 018_call_arg_type.spl} | 0 ...callable.spl => 019_call_not_callable.spl} | 0 ...le_arity.spl => 020_call_module_arity.spl} | 0 ...dule_type.spl => 021_call_module_type.spl} | 0 ...07_return_type.spl => 022_return_type.spl} | 0 ...et_init_type.spl => 023_let_init_type.spl} | 0 ...09_assign_type.spl => 024_assign_type.spl} | 0 ...dition_bool.spl => 025_condition_bool.spl} | 0 ...ield_missing.spl => 026_field_missing.spl} | 0 ..._ptr_field.spl => 027_array_ptr_field.spl} | 0 ...r_operand.spl => 028_operator_operand.spl} | 0 ...nary_operand.spl => 029_unary_operand.spl} | 0 ...global.spl => 030_call_unknown_global.spl} | 0 ...d_not_fn.spl => 031_call_field_not_fn.spl} | 0 ...all_arity.spl => 032_macro_call_arity.spl} | 0 ...g_type.spl => 033_macro_call_arg_type.spl} | 0 ...all_arity.spl => 034_shape_call_arity.spl} | 0 ...l_module_ok.spl => 035_call_module_ok.spl} | 0 ...s_ok.spl => 036_call_literal_types_ok.spl} | 0 ...ile_call_ptr.spl => 037_file_call_ptr.spl} | 0 ...issing_arg.spl => 038_fmt_missing_arg.spl} | 0 ..._fmt_bad_type.spl => 039_fmt_bad_type.spl} | 0 ..._nonliteral.spl => 040_fmt_nonliteral.spl} | 0 ...espace.spl => 041_duplicate_namespace.spl} | 0 ...icit_hook.spl => 042_no_implicit_hook.spl} | 0 ...k_duplicate.spl => 043_hook_duplicate.spl} | 0 ...cate_fn_fn.spl => 044_duplicate_fn_fn.spl} | 0 ..._type_fn.spl => 045_duplicate_type_fn.spl} | 0 ...onst_fn.spl => 046_duplicate_const_fn.spl} | 0 ...numeric.spl => 047_meta_error_numeric.spl} | 0 tests/stage3/108_reflect_codec.spl | 1 + tests/stage3/242_fmt_builder.spl | 12 ++-- ..._replace.spl => 301_decorator_replace.spl} | 0 ...or_append.spl => 302_decorator_append.spl} | 0 ..._missing.spl => 303_decorator_missing.spl} | 0 ...ator_order.spl => 304_decorator_order.spl} | 0 ...eturn.spl => 305_decorator_bad_return.spl} | 0 ...re.spl => 306_decorator_bad_signature.spl} | 0 ...or_import.spl => 307_decorator_import.spl} | 0 ...ator_const.spl => 308_decorator_const.spl} | 0 ...ecorator_var.spl => 309_decorator_var.spl} | 0 ...aram_replace.spl => 401_param_replace.spl} | 0 ...ependent.spl => 402_param_independent.spl} | 0 ...aram_missing.spl => 403_param_missing.spl} | 0 ...04_param_order.spl => 404_param_order.spl} | 0 ..._param_callee.spl => 405_param_callee.spl} | 0 ..._value.spl => 406_param_missing_value.spl} | 0 ...nature.spl => 407_param_bad_signature.spl} | 0 ..._param_import.spl => 408_param_import.spl} | 0 ...mpl_identity.spl => 501_impl_identity.spl} | 0 ...metadata.spl => 502_impl_tag_metadata.spl} | 0 ..._impl_missing.spl => 503_impl_missing.spl} | 0 ...l_not_macro.spl => 504_impl_not_macro.spl} | 0 ...gnature.spl => 505_impl_bad_signature.spl} | 0 ...bad_return.spl => 506_impl_bad_return.spl} | 0 ...08_impl_no_tag.spl => 507_impl_no_tag.spl} | 0 tests/stage3/601_errdefer_ok.spl | 56 +++++++++++++++++ tests/stage3/602_errdefer_lifo.spl | 59 ++++++++++++++++++ tests/stage3/603_try.spl | 55 ++++++++++++++++ tests/stage3/604_catch.spl | 57 +++++++++++++++++ tests/stage3/605_nested_cleanup.spl | 50 +++++++++++++++ tests/stage3/606_plain_enum.spl | 18 ++++++ tests/stage3/607_custom_names.spl | 55 ++++++++++++++++ tests/stage3/608_multi_error.spl | 53 ++++++++++++++++ tests/stage3/609_defer_order.spl | 62 +++++++++++++++++++ tests/stage3/610_try_payload_diff.spl | 57 +++++++++++++++++ tests/stage3/611_try_no_ok.spl | 17 +++++ tests/stage3/612_try_error_mismatch.spl | 36 +++++++++++ tests/stage3/613_catch_handler_type.spl | 26 ++++++++ tests/stage3/614_errdefer_no_error.spl | 10 +++ tests/stage3/615_catch_bad_pattern.spl | 26 ++++++++ tests/stage3/616_try_no_error.spl | 17 +++++ tests/stage3/617_duplicate_ok.spl | 18 ++++++ tests/stage3/801_error.spl | 3 - tests/stage3/802_borrow.spl | 3 - ...01_generic_vec.spl => 901_generic_vec.spl} | 12 ++-- tests/stage3/{702_fmt.spl => 902_fmt.spl} | 0 .../{703_receiver.spl => 903_receiver.spl} | 6 +- .../{704_default.spl => 904_default.spl} | 0 92 files changed, 803 insertions(+), 27 deletions(-) create mode 100644 tests/stage3/011_generic_bind.spl create mode 100644 tests/stage3/012_generic_local.spl rename tests/stage3/{901_array_as_ptr.spl => 013_array_as_ptr.spl} (100%) create mode 100644 tests/stage3/014_for_syntax.spl create mode 100644 tests/stage3/015_for_missing_in.spl rename tests/stage3/{301_call_too_few.spl => 016_call_too_few.spl} (100%) rename tests/stage3/{302_call_too_many.spl => 017_call_too_many.spl} (100%) rename tests/stage3/{303_call_arg_type.spl => 018_call_arg_type.spl} (100%) rename tests/stage3/{304_call_not_callable.spl => 019_call_not_callable.spl} (100%) rename tests/stage3/{305_call_module_arity.spl => 020_call_module_arity.spl} (100%) rename tests/stage3/{306_call_module_type.spl => 021_call_module_type.spl} (100%) rename tests/stage3/{307_return_type.spl => 022_return_type.spl} (100%) rename tests/stage3/{308_let_init_type.spl => 023_let_init_type.spl} (100%) rename tests/stage3/{309_assign_type.spl => 024_assign_type.spl} (100%) rename tests/stage3/{310_condition_bool.spl => 025_condition_bool.spl} (100%) rename tests/stage3/{311_field_missing.spl => 026_field_missing.spl} (100%) rename tests/stage3/{312_array_ptr_field.spl => 027_array_ptr_field.spl} (100%) rename tests/stage3/{313_operator_operand.spl => 028_operator_operand.spl} (100%) rename tests/stage3/{314_unary_operand.spl => 029_unary_operand.spl} (100%) rename tests/stage3/{315_call_unknown_global.spl => 030_call_unknown_global.spl} (100%) rename tests/stage3/{316_call_field_not_fn.spl => 031_call_field_not_fn.spl} (100%) rename tests/stage3/{317_macro_call_arity.spl => 032_macro_call_arity.spl} (100%) rename tests/stage3/{318_macro_call_arg_type.spl => 033_macro_call_arg_type.spl} (100%) rename tests/stage3/{319_shape_call_arity.spl => 034_shape_call_arity.spl} (100%) rename tests/stage3/{320_call_module_ok.spl => 035_call_module_ok.spl} (100%) rename tests/stage3/{321_call_literal_types_ok.spl => 036_call_literal_types_ok.spl} (100%) rename tests/stage3/{322_file_call_ptr.spl => 037_file_call_ptr.spl} (100%) rename tests/stage3/{323_fmt_missing_arg.spl => 038_fmt_missing_arg.spl} (100%) rename tests/stage3/{324_fmt_bad_type.spl => 039_fmt_bad_type.spl} (100%) rename tests/stage3/{325_fmt_nonliteral.spl => 040_fmt_nonliteral.spl} (100%) rename tests/stage3/{326_duplicate_namespace.spl => 041_duplicate_namespace.spl} (100%) rename tests/stage3/{327_no_implicit_hook.spl => 042_no_implicit_hook.spl} (100%) rename tests/stage3/{328_hook_duplicate.spl => 043_hook_duplicate.spl} (100%) rename tests/stage3/{329_duplicate_fn_fn.spl => 044_duplicate_fn_fn.spl} (100%) rename tests/stage3/{330_duplicate_type_fn.spl => 045_duplicate_type_fn.spl} (100%) rename tests/stage3/{331_duplicate_const_fn.spl => 046_duplicate_const_fn.spl} (100%) rename tests/stage3/{332_meta_error_numeric.spl => 047_meta_error_numeric.spl} (100%) rename tests/stage3/{401_decorator_replace.spl => 301_decorator_replace.spl} (100%) rename tests/stage3/{402_decorator_append.spl => 302_decorator_append.spl} (100%) rename tests/stage3/{403_decorator_missing.spl => 303_decorator_missing.spl} (100%) rename tests/stage3/{404_decorator_order.spl => 304_decorator_order.spl} (100%) rename tests/stage3/{405_decorator_bad_return.spl => 305_decorator_bad_return.spl} (100%) rename tests/stage3/{406_decorator_bad_signature.spl => 306_decorator_bad_signature.spl} (100%) rename tests/stage3/{407_decorator_import.spl => 307_decorator_import.spl} (100%) rename tests/stage3/{408_decorator_const.spl => 308_decorator_const.spl} (100%) rename tests/stage3/{409_decorator_var.spl => 309_decorator_var.spl} (100%) rename tests/stage3/{501_param_replace.spl => 401_param_replace.spl} (100%) rename tests/stage3/{502_param_independent.spl => 402_param_independent.spl} (100%) rename tests/stage3/{503_param_missing.spl => 403_param_missing.spl} (100%) rename tests/stage3/{504_param_order.spl => 404_param_order.spl} (100%) rename tests/stage3/{505_param_callee.spl => 405_param_callee.spl} (100%) rename tests/stage3/{506_param_missing_value.spl => 406_param_missing_value.spl} (100%) rename tests/stage3/{507_param_bad_signature.spl => 407_param_bad_signature.spl} (100%) rename tests/stage3/{508_param_import.spl => 408_param_import.spl} (100%) rename tests/stage3/{601_impl_identity.spl => 501_impl_identity.spl} (100%) rename tests/stage3/{603_impl_tag_metadata.spl => 502_impl_tag_metadata.spl} (100%) rename tests/stage3/{604_impl_missing.spl => 503_impl_missing.spl} (100%) rename tests/stage3/{605_impl_not_macro.spl => 504_impl_not_macro.spl} (100%) rename tests/stage3/{606_impl_bad_signature.spl => 505_impl_bad_signature.spl} (100%) rename tests/stage3/{607_impl_bad_return.spl => 506_impl_bad_return.spl} (100%) rename tests/stage3/{608_impl_no_tag.spl => 507_impl_no_tag.spl} (100%) create mode 100644 tests/stage3/601_errdefer_ok.spl create mode 100644 tests/stage3/602_errdefer_lifo.spl create mode 100644 tests/stage3/603_try.spl create mode 100644 tests/stage3/604_catch.spl create mode 100644 tests/stage3/605_nested_cleanup.spl create mode 100644 tests/stage3/606_plain_enum.spl create mode 100644 tests/stage3/607_custom_names.spl create mode 100644 tests/stage3/608_multi_error.spl create mode 100644 tests/stage3/609_defer_order.spl create mode 100644 tests/stage3/610_try_payload_diff.spl create mode 100644 tests/stage3/611_try_no_ok.spl create mode 100644 tests/stage3/612_try_error_mismatch.spl create mode 100644 tests/stage3/613_catch_handler_type.spl create mode 100644 tests/stage3/614_errdefer_no_error.spl create mode 100644 tests/stage3/615_catch_bad_pattern.spl create mode 100644 tests/stage3/616_try_no_error.spl create mode 100644 tests/stage3/617_duplicate_ok.spl delete mode 100644 tests/stage3/801_error.spl delete mode 100644 tests/stage3/802_borrow.spl rename tests/stage3/{701_generic_vec.spl => 901_generic_vec.spl} (90%) rename tests/stage3/{702_fmt.spl => 902_fmt.spl} (100%) rename tests/stage3/{703_receiver.spl => 903_receiver.spl} (76%) rename tests/stage3/{704_default.spl => 904_default.spl} (100%) diff --git a/nob.c b/nob.c index fbc52b1..619cef9 100644 --- a/nob.c +++ b/nob.c @@ -1,5 +1,5 @@ -#define COMMON_CFLAGS "-Wall", "-Wextra", "-D_CRT_SECURE_NO_WARNINGS" +#define COMMON_CFLAGS "-Wall", "-Wextra", "-D_CRT_SECURE_NO_WARNINGS", "-D_CRT_NONSTDC_NO_DEPRECATE" #define SPL_CFLAGS COMMON_CFLAGS, "-O0", "-g" #define SPL_OPTIMIZATION_FLAG COMMON_CFLAGS, "-O2" #define nob_cc_flags(cmd) nob_cmd_append(cmd, SPL_CFLAGS) @@ -288,6 +288,7 @@ defer: const char *build_target(const char *name) { if (!prepare_build()) return NULL; + const char *splc_libs = "./src/libs"; for (size_t i = 0; i < NOB_ARRAY_LEN(targets); ++i) { if (name != NULL && strcmp(targets[i].name, name) != 0) { continue; @@ -298,6 +299,7 @@ const char *build_target(const char *name) { case BUILD_EXE: { return build_exe(target); } + case BUILD_SPLC_SIR: case BUILD_SIR: { const char *target_sir = sir_path(name); // 编译器先建好:它的产物也要算进依赖,编译器变了就得重编 .sir。 @@ -321,6 +323,9 @@ const char *build_target(const char *name) { return target_sir; Nob_Cmd cmd = {0}; + if (target->kind == BUILD_SPLC_SIR) { + nob_cmd_append(&cmd, "-I", splc_libs); + } nob_cmd_append(&cmd, target->sir.src, target_sir); if (!run_target(target->sir.compiler, &cmd)) { nob_log(NOB_ERROR, "failed to build `%s`", name); @@ -330,6 +335,7 @@ const char *build_target(const char *name) { nob_cmd_free(cmd); return target_sir; } + case BUILD_SPLC_NATIVE: case BUILD_NATIVE: { Nob_Cmd cmd = {0}; const char *target_exe = exe_path(name); @@ -353,6 +359,9 @@ const char *build_target(const char *name) { return NULL; } if (needs != 0) { + if (target->kind == BUILD_SPLC_NATIVE) { + nob_cmd_append(&cmd, "-I", splc_libs); + } nob_cmd_append(&cmd, "--dump", "c", target->native.src, target_c); if (!run_target(target->native.compiler, &cmd)) { nob_log(NOB_ERROR, "failed to build `%s`", name); @@ -1057,10 +1066,12 @@ static int cli_list(void) { count_files(t->exe.srcs, t->exe.srcs_count), count_files(t->exe.headers, t->exe.headers_count)); break; + case BUILD_SPLC_SIR: case BUILD_SIR: printf(" %-12s %-9s %s <- %s deps=%zu\n", t->name, kind, t->sir.src, t->sir.compiler, count_files(t->sir.deps, t->sir.deps_count)); break; + case BUILD_SPLC_NATIVE: case BUILD_NATIVE: printf(" %-12s %-9s %s <- %s deps=%zu crt=%zu\n", t->name, kind, t->native.src, t->native.compiler, count_files(t->native.deps, t->native.deps_count), diff --git a/nob/nob_data.h b/nob/nob_data.h index a0685ba..8f05b57 100644 --- a/nob/nob_data.h +++ b/nob/nob_data.h @@ -50,6 +50,8 @@ typedef struct build { BUILD_EXE_WITH_OPTIMIZATION, BUILD_SIR, BUILD_NATIVE, + BUILD_SPLC_SIR, + BUILD_SPLC_NATIVE, } kind; union { struct { @@ -173,7 +175,7 @@ static build_t targets[] = { }, { "splc4", - BUILD_SIR, + BUILD_SPLC_SIR, .sir = { .src = "src/main.spl", @@ -184,7 +186,7 @@ static build_t targets[] = { }, { "splc4n", - BUILD_NATIVE, + BUILD_SPLC_NATIVE, .native = { .src = "src/main.spl", diff --git a/stage2/ast.spl b/stage2/ast.spl index df968aa..74fc0c8 100644 --- a/stage2/ast.spl +++ b/stage2/ast.spl @@ -96,6 +96,7 @@ type Body = enum { label: usize, iter_exprs: Vec, pattern: usize, + pattern_type: usize, body: usize, else_branch: usize, }, @@ -546,6 +547,7 @@ fn dump_node(ast: *Ast, buf: *ByteBuffer, stack: *Vec, ref: usize, last: bool): ok = dump_opt(ast, buf, stack, f.label, false) && dump_list(ast, buf, stack, f.iter_exprs) && dump_opt(ast, buf, stack, f.pattern, false) && + dump_opt(ast, buf, stack, f.pattern_type, true) && dump_opt(ast, buf, stack, f.body, false) && dump_opt(ast, buf, stack, f.else_branch, true); }, diff --git a/stage2/parser.spl b/stage2/parser.spl index 96f7e15..03a607e 100644 --- a/stage2/parser.spl +++ b/stage2/parser.spl @@ -2122,7 +2122,17 @@ fn parse_for_expr(self: *Parser): usize { advance(self); var pattern = parse_pattern(self); if pattern == 0 { ret 0; } - if !expect_punct(self, .Colon, "':' after for pattern") { ret 0; } + var pattern_type: usize = 0; + if at_punct(self, .Colon) { + advance(self); + pattern_type = parse_type_expr(self); + if pattern_type == 0 { ret 0; } + } + if !at_keyword(self, .In) { + parse_error(self, peek(self), "'in' after for pattern"); + ret 0; + } + advance(self); var iter_exprs = ref_vec(); self.stop_agg = self.stop_agg + 1; var e0 = parse_expr(self); @@ -2145,6 +2155,7 @@ fn parse_for_expr(self: *Parser): usize { .label = 0, .iter_exprs = iter_exprs, .pattern = pattern, + .pattern_type = pattern_type, .body = body, .else_branch = else_branch, }), kw); diff --git a/stage2/token.spl b/stage2/token.spl index 6347133..712a9aa 100644 --- a/stage2/token.spl +++ b/stage2/token.spl @@ -7,7 +7,7 @@ type .{ .ByteView } = @import("utils/byte_view.spl"); type Keyword = enum { Any, As, Bool, Break, Catch, Comptime, Const, Continue, Defer, Else, Enum, - Errdefer, False, Fn, For, If, Loop, Match, Null, Ret, Shape, Struct, True, + Errdefer, False, Fn, For, If, In, Loop, Match, Null, Ret, Shape, Struct, True, Try, Type, Undefined, Union, Var, While, Underscore, } @@ -75,6 +75,7 @@ fn keyword_of(text: ByteView): KeywordResult { if c0 == 'a' && is(text, "as") { ret .Yes(.As); } if c0 == 'f' && is(text, "fn") { ret .Yes(.Fn); } if c0 == 'i' && is(text, "if") { ret .Yes(.If); } + if c0 == 'i' && is(text, "in") { ret .Yes(.In); } ret .No; } if n == 3 { @@ -147,6 +148,7 @@ fn keyword_name(kw: Keyword): []u8 { .Fn => { ret "KW_FN"; }, .For => { ret "KW_FOR"; }, .If => { ret "KW_IF"; }, + .In => { ret "KW_IN"; }, .Loop => { ret "KW_LOOP"; }, .Match => { ret "KW_MATCH"; }, .Null => { ret "KW_NULL"; }, @@ -259,6 +261,7 @@ fn expect_keyword(result: KeywordResult, expected: Keyword) { fn test_keywords() { expect_keyword(keyword_of(view("fn")), .Fn); + expect_keyword(keyword_of(view("in")), .In); expect_keyword(keyword_of(view("while")), .While); expect_keyword(keyword_of(view("_")), .Underscore); match keyword_of(view("foo")) { @@ -337,6 +340,7 @@ fn test_kind_names() { expect_kind_name(.Keyword(.Fn), "KW_FN"); expect_kind_name(.Keyword(.For), "KW_FOR"); expect_kind_name(.Keyword(.If), "KW_IF"); + expect_kind_name(.Keyword(.In), "KW_IN"); expect_kind_name(.Keyword(.Loop), "KW_LOOP"); expect_kind_name(.Keyword(.Match), "KW_MATCH"); expect_kind_name(.Keyword(.Null), "KW_NULL"); diff --git a/tests/stage3/006_with_fn.spl b/tests/stage3/006_with_fn.spl index 040d011..f799c49 100644 --- a/tests/stage3/006_with_fn.spl +++ b/tests/stage3/006_with_fn.spl @@ -4,7 +4,7 @@ comptime fn Vec(t: type) type { data: []t, len: usize, - fn assert(v: *@Self): void { + fn assert(v: *@self): void { @assert(v.len == 1, "vec-field in assert"); v.len += 1; } diff --git a/tests/stage3/011_generic_bind.spl b/tests/stage3/011_generic_bind.spl new file mode 100644 index 0000000..5c9b9c0 --- /dev/null +++ b/tests/stage3/011_generic_bind.spl @@ -0,0 +1,23 @@ +// Expect retcode = 0 + +// 回归:泛型类型生成器的方法被复用后,参数/局部变量不能重复分配 def。 +// 这里只使用语言本身和一个最小 fixture,不依赖 src/libs。 +type meta = @import("meta"); +type .{ .Ref } = meta; +type .{ .Map } = @import("../support/generic_bind_fixture.spl"); + +#hook(param) +macro fn Self(callee: Ref, value: Ref): Ref { + ret @meta.construct(.{ .kind = .Addr(value) }); +} + +fn main() { + type M = @Map(8); + var map: M = M.init(); + + @assert(map.probe() == 8); + @assert(map.table.probe() == 8); + + map.put_first(42); + @assert(map.first() == 42); +} diff --git a/tests/stage3/012_generic_local.spl b/tests/stage3/012_generic_local.spl new file mode 100644 index 0000000..dc06b30 --- /dev/null +++ b/tests/stage3/012_generic_local.spl @@ -0,0 +1,23 @@ +// Expect retcode = 0 + +// 回归:生成方法中的局部变量必须写入方法体实际读取的 slot。 +// 以前 put_first 的参数和局部变量会出现两个 def,写入 0 而不是调用值。 +type meta = @import("meta"); +type .{ .Ref } = meta; +type .{ .Map } = @import("../support/generic_bind_fixture.spl"); + +#hook(param) +macro fn Self(callee: Ref, value: Ref): Ref { + ret @meta.construct(.{ .kind = .Addr(value) }); +} + +fn main() { + type M = @Map(8); + var map: M = M.init(); + + map.put_first(7); + @assert(map.first() == 7); + + map.put_first(-9); + @assert(map.first() == -9); +} diff --git a/tests/stage3/901_array_as_ptr.spl b/tests/stage3/013_array_as_ptr.spl similarity index 100% rename from tests/stage3/901_array_as_ptr.spl rename to tests/stage3/013_array_as_ptr.spl diff --git a/tests/stage3/014_for_syntax.spl b/tests/stage3/014_for_syntax.spl new file mode 100644 index 0000000..abc582b --- /dev/null +++ b/tests/stage3/014_for_syntax.spl @@ -0,0 +1,24 @@ +// Expect retcode = 0 + +// 只锁定 for 的新语法;迭代语义等 iterator shape 落地后再接 sema/lowering。 +fn typed(values: [3]i32) { + for value: i32 in values { + var copy = value; + } +} + +fn inferred(values: [3]i32) { + for value in values { + var copy = value; + } +} + +fn with_else(values: [3]i32) { + for value: i32 in values { + var copy = value; + } else { + var empty = true; + } +} + +fn main() {} diff --git a/tests/stage3/015_for_missing_in.spl b/tests/stage3/015_for_missing_in.spl new file mode 100644 index 0000000..7d3c6cc --- /dev/null +++ b/tests/stage3/015_for_missing_in.spl @@ -0,0 +1,9 @@ +// Expect compile_error E0002 + +fn test(values: [3]i32) { + for value: i32 values { + var copy = value; + } +} + +fn main() {} diff --git a/tests/stage3/301_call_too_few.spl b/tests/stage3/016_call_too_few.spl similarity index 100% rename from tests/stage3/301_call_too_few.spl rename to tests/stage3/016_call_too_few.spl diff --git a/tests/stage3/302_call_too_many.spl b/tests/stage3/017_call_too_many.spl similarity index 100% rename from tests/stage3/302_call_too_many.spl rename to tests/stage3/017_call_too_many.spl diff --git a/tests/stage3/303_call_arg_type.spl b/tests/stage3/018_call_arg_type.spl similarity index 100% rename from tests/stage3/303_call_arg_type.spl rename to tests/stage3/018_call_arg_type.spl diff --git a/tests/stage3/304_call_not_callable.spl b/tests/stage3/019_call_not_callable.spl similarity index 100% rename from tests/stage3/304_call_not_callable.spl rename to tests/stage3/019_call_not_callable.spl diff --git a/tests/stage3/305_call_module_arity.spl b/tests/stage3/020_call_module_arity.spl similarity index 100% rename from tests/stage3/305_call_module_arity.spl rename to tests/stage3/020_call_module_arity.spl diff --git a/tests/stage3/306_call_module_type.spl b/tests/stage3/021_call_module_type.spl similarity index 100% rename from tests/stage3/306_call_module_type.spl rename to tests/stage3/021_call_module_type.spl diff --git a/tests/stage3/307_return_type.spl b/tests/stage3/022_return_type.spl similarity index 100% rename from tests/stage3/307_return_type.spl rename to tests/stage3/022_return_type.spl diff --git a/tests/stage3/308_let_init_type.spl b/tests/stage3/023_let_init_type.spl similarity index 100% rename from tests/stage3/308_let_init_type.spl rename to tests/stage3/023_let_init_type.spl diff --git a/tests/stage3/309_assign_type.spl b/tests/stage3/024_assign_type.spl similarity index 100% rename from tests/stage3/309_assign_type.spl rename to tests/stage3/024_assign_type.spl diff --git a/tests/stage3/310_condition_bool.spl b/tests/stage3/025_condition_bool.spl similarity index 100% rename from tests/stage3/310_condition_bool.spl rename to tests/stage3/025_condition_bool.spl diff --git a/tests/stage3/311_field_missing.spl b/tests/stage3/026_field_missing.spl similarity index 100% rename from tests/stage3/311_field_missing.spl rename to tests/stage3/026_field_missing.spl diff --git a/tests/stage3/312_array_ptr_field.spl b/tests/stage3/027_array_ptr_field.spl similarity index 100% rename from tests/stage3/312_array_ptr_field.spl rename to tests/stage3/027_array_ptr_field.spl diff --git a/tests/stage3/313_operator_operand.spl b/tests/stage3/028_operator_operand.spl similarity index 100% rename from tests/stage3/313_operator_operand.spl rename to tests/stage3/028_operator_operand.spl diff --git a/tests/stage3/314_unary_operand.spl b/tests/stage3/029_unary_operand.spl similarity index 100% rename from tests/stage3/314_unary_operand.spl rename to tests/stage3/029_unary_operand.spl diff --git a/tests/stage3/315_call_unknown_global.spl b/tests/stage3/030_call_unknown_global.spl similarity index 100% rename from tests/stage3/315_call_unknown_global.spl rename to tests/stage3/030_call_unknown_global.spl diff --git a/tests/stage3/316_call_field_not_fn.spl b/tests/stage3/031_call_field_not_fn.spl similarity index 100% rename from tests/stage3/316_call_field_not_fn.spl rename to tests/stage3/031_call_field_not_fn.spl diff --git a/tests/stage3/317_macro_call_arity.spl b/tests/stage3/032_macro_call_arity.spl similarity index 100% rename from tests/stage3/317_macro_call_arity.spl rename to tests/stage3/032_macro_call_arity.spl diff --git a/tests/stage3/318_macro_call_arg_type.spl b/tests/stage3/033_macro_call_arg_type.spl similarity index 100% rename from tests/stage3/318_macro_call_arg_type.spl rename to tests/stage3/033_macro_call_arg_type.spl diff --git a/tests/stage3/319_shape_call_arity.spl b/tests/stage3/034_shape_call_arity.spl similarity index 100% rename from tests/stage3/319_shape_call_arity.spl rename to tests/stage3/034_shape_call_arity.spl diff --git a/tests/stage3/320_call_module_ok.spl b/tests/stage3/035_call_module_ok.spl similarity index 100% rename from tests/stage3/320_call_module_ok.spl rename to tests/stage3/035_call_module_ok.spl diff --git a/tests/stage3/321_call_literal_types_ok.spl b/tests/stage3/036_call_literal_types_ok.spl similarity index 100% rename from tests/stage3/321_call_literal_types_ok.spl rename to tests/stage3/036_call_literal_types_ok.spl diff --git a/tests/stage3/322_file_call_ptr.spl b/tests/stage3/037_file_call_ptr.spl similarity index 100% rename from tests/stage3/322_file_call_ptr.spl rename to tests/stage3/037_file_call_ptr.spl diff --git a/tests/stage3/323_fmt_missing_arg.spl b/tests/stage3/038_fmt_missing_arg.spl similarity index 100% rename from tests/stage3/323_fmt_missing_arg.spl rename to tests/stage3/038_fmt_missing_arg.spl diff --git a/tests/stage3/324_fmt_bad_type.spl b/tests/stage3/039_fmt_bad_type.spl similarity index 100% rename from tests/stage3/324_fmt_bad_type.spl rename to tests/stage3/039_fmt_bad_type.spl diff --git a/tests/stage3/325_fmt_nonliteral.spl b/tests/stage3/040_fmt_nonliteral.spl similarity index 100% rename from tests/stage3/325_fmt_nonliteral.spl rename to tests/stage3/040_fmt_nonliteral.spl diff --git a/tests/stage3/326_duplicate_namespace.spl b/tests/stage3/041_duplicate_namespace.spl similarity index 100% rename from tests/stage3/326_duplicate_namespace.spl rename to tests/stage3/041_duplicate_namespace.spl diff --git a/tests/stage3/327_no_implicit_hook.spl b/tests/stage3/042_no_implicit_hook.spl similarity index 100% rename from tests/stage3/327_no_implicit_hook.spl rename to tests/stage3/042_no_implicit_hook.spl diff --git a/tests/stage3/328_hook_duplicate.spl b/tests/stage3/043_hook_duplicate.spl similarity index 100% rename from tests/stage3/328_hook_duplicate.spl rename to tests/stage3/043_hook_duplicate.spl diff --git a/tests/stage3/329_duplicate_fn_fn.spl b/tests/stage3/044_duplicate_fn_fn.spl similarity index 100% rename from tests/stage3/329_duplicate_fn_fn.spl rename to tests/stage3/044_duplicate_fn_fn.spl diff --git a/tests/stage3/330_duplicate_type_fn.spl b/tests/stage3/045_duplicate_type_fn.spl similarity index 100% rename from tests/stage3/330_duplicate_type_fn.spl rename to tests/stage3/045_duplicate_type_fn.spl diff --git a/tests/stage3/331_duplicate_const_fn.spl b/tests/stage3/046_duplicate_const_fn.spl similarity index 100% rename from tests/stage3/331_duplicate_const_fn.spl rename to tests/stage3/046_duplicate_const_fn.spl diff --git a/tests/stage3/332_meta_error_numeric.spl b/tests/stage3/047_meta_error_numeric.spl similarity index 100% rename from tests/stage3/332_meta_error_numeric.spl rename to tests/stage3/047_meta_error_numeric.spl diff --git a/tests/stage3/108_reflect_codec.spl b/tests/stage3/108_reflect_codec.spl index b007bbe..e4e57a7 100644 --- a/tests/stage3/108_reflect_codec.spl +++ b/tests/stage3/108_reflect_codec.spl @@ -125,6 +125,7 @@ comptime fn codec_exprs(): usize { .label = lr, .inputs = args[0..2], .captures = args[0..1], + .pattern_type = 0, .body = br, .else_body = 0, }) }; diff --git a/tests/stage3/242_fmt_builder.spl b/tests/stage3/242_fmt_builder.spl index 7ecb17f..11043e2 100644 --- a/tests/stage3/242_fmt_builder.spl +++ b/tests/stage3/242_fmt_builder.spl @@ -11,7 +11,7 @@ type Builder = struct { ret .{ .buf = buf, .len = 0 }; } - fn append_bytes(b: *@Self, s: []u8) { + fn append_bytes(b: *@self, s: []u8) { var i: usize = 0; while i < s.len { b.buf[b.len] = s[i]; @@ -20,12 +20,12 @@ type Builder = struct { } } - fn append_byte(b: *@Self, c: u8) { + fn append_byte(b: *@self, c: u8) { b.buf[b.len] = c; b.len = b.len + 1; } - fn append_u64(b: *@Self, value: u64, base: u64) { + fn append_u64(b: *@self, value: u64, base: u64) { if value == 0 { b.append_byte(b, 48); ret; @@ -53,7 +53,7 @@ type Builder = struct { b.len = b.len + count; } - fn append_i64(b: *@Self, value: i64, base: u64) { + fn append_i64(b: *@self, value: i64, base: u64) { if value < 0 { b.append_byte(b, 45); b.append_u64(b, 0 - (value as u64), base); @@ -62,7 +62,7 @@ type Builder = struct { b.append_u64(b, value as u64, base); } - fn append_bool(b: *@Self, value: bool) { + fn append_bool(b: *@self, value: bool) { if value { b.append_bytes(b, "true"); } else { @@ -70,7 +70,7 @@ type Builder = struct { } } - fn text(b: *@Self): []u8 { + fn text(b: *@self): []u8 { ret b.buf[0..b.len]; } } diff --git a/tests/stage3/401_decorator_replace.spl b/tests/stage3/301_decorator_replace.spl similarity index 100% rename from tests/stage3/401_decorator_replace.spl rename to tests/stage3/301_decorator_replace.spl diff --git a/tests/stage3/402_decorator_append.spl b/tests/stage3/302_decorator_append.spl similarity index 100% rename from tests/stage3/402_decorator_append.spl rename to tests/stage3/302_decorator_append.spl diff --git a/tests/stage3/403_decorator_missing.spl b/tests/stage3/303_decorator_missing.spl similarity index 100% rename from tests/stage3/403_decorator_missing.spl rename to tests/stage3/303_decorator_missing.spl diff --git a/tests/stage3/404_decorator_order.spl b/tests/stage3/304_decorator_order.spl similarity index 100% rename from tests/stage3/404_decorator_order.spl rename to tests/stage3/304_decorator_order.spl diff --git a/tests/stage3/405_decorator_bad_return.spl b/tests/stage3/305_decorator_bad_return.spl similarity index 100% rename from tests/stage3/405_decorator_bad_return.spl rename to tests/stage3/305_decorator_bad_return.spl diff --git a/tests/stage3/406_decorator_bad_signature.spl b/tests/stage3/306_decorator_bad_signature.spl similarity index 100% rename from tests/stage3/406_decorator_bad_signature.spl rename to tests/stage3/306_decorator_bad_signature.spl diff --git a/tests/stage3/407_decorator_import.spl b/tests/stage3/307_decorator_import.spl similarity index 100% rename from tests/stage3/407_decorator_import.spl rename to tests/stage3/307_decorator_import.spl diff --git a/tests/stage3/408_decorator_const.spl b/tests/stage3/308_decorator_const.spl similarity index 100% rename from tests/stage3/408_decorator_const.spl rename to tests/stage3/308_decorator_const.spl diff --git a/tests/stage3/409_decorator_var.spl b/tests/stage3/309_decorator_var.spl similarity index 100% rename from tests/stage3/409_decorator_var.spl rename to tests/stage3/309_decorator_var.spl diff --git a/tests/stage3/501_param_replace.spl b/tests/stage3/401_param_replace.spl similarity index 100% rename from tests/stage3/501_param_replace.spl rename to tests/stage3/401_param_replace.spl diff --git a/tests/stage3/502_param_independent.spl b/tests/stage3/402_param_independent.spl similarity index 100% rename from tests/stage3/502_param_independent.spl rename to tests/stage3/402_param_independent.spl diff --git a/tests/stage3/503_param_missing.spl b/tests/stage3/403_param_missing.spl similarity index 100% rename from tests/stage3/503_param_missing.spl rename to tests/stage3/403_param_missing.spl diff --git a/tests/stage3/504_param_order.spl b/tests/stage3/404_param_order.spl similarity index 100% rename from tests/stage3/504_param_order.spl rename to tests/stage3/404_param_order.spl diff --git a/tests/stage3/505_param_callee.spl b/tests/stage3/405_param_callee.spl similarity index 100% rename from tests/stage3/505_param_callee.spl rename to tests/stage3/405_param_callee.spl diff --git a/tests/stage3/506_param_missing_value.spl b/tests/stage3/406_param_missing_value.spl similarity index 100% rename from tests/stage3/506_param_missing_value.spl rename to tests/stage3/406_param_missing_value.spl diff --git a/tests/stage3/507_param_bad_signature.spl b/tests/stage3/407_param_bad_signature.spl similarity index 100% rename from tests/stage3/507_param_bad_signature.spl rename to tests/stage3/407_param_bad_signature.spl diff --git a/tests/stage3/508_param_import.spl b/tests/stage3/408_param_import.spl similarity index 100% rename from tests/stage3/508_param_import.spl rename to tests/stage3/408_param_import.spl diff --git a/tests/stage3/601_impl_identity.spl b/tests/stage3/501_impl_identity.spl similarity index 100% rename from tests/stage3/601_impl_identity.spl rename to tests/stage3/501_impl_identity.spl diff --git a/tests/stage3/603_impl_tag_metadata.spl b/tests/stage3/502_impl_tag_metadata.spl similarity index 100% rename from tests/stage3/603_impl_tag_metadata.spl rename to tests/stage3/502_impl_tag_metadata.spl diff --git a/tests/stage3/604_impl_missing.spl b/tests/stage3/503_impl_missing.spl similarity index 100% rename from tests/stage3/604_impl_missing.spl rename to tests/stage3/503_impl_missing.spl diff --git a/tests/stage3/605_impl_not_macro.spl b/tests/stage3/504_impl_not_macro.spl similarity index 100% rename from tests/stage3/605_impl_not_macro.spl rename to tests/stage3/504_impl_not_macro.spl diff --git a/tests/stage3/606_impl_bad_signature.spl b/tests/stage3/505_impl_bad_signature.spl similarity index 100% rename from tests/stage3/606_impl_bad_signature.spl rename to tests/stage3/505_impl_bad_signature.spl diff --git a/tests/stage3/607_impl_bad_return.spl b/tests/stage3/506_impl_bad_return.spl similarity index 100% rename from tests/stage3/607_impl_bad_return.spl rename to tests/stage3/506_impl_bad_return.spl diff --git a/tests/stage3/608_impl_no_tag.spl b/tests/stage3/507_impl_no_tag.spl similarity index 100% rename from tests/stage3/608_impl_no_tag.spl rename to tests/stage3/507_impl_no_tag.spl diff --git a/tests/stage3/601_errdefer_ok.spl b/tests/stage3/601_errdefer_ok.spl new file mode 100644 index 0000000..3cbff5e --- /dev/null +++ b/tests/stage3/601_errdefer_ok.spl @@ -0,0 +1,56 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +type Trace = struct { + value: i32, +}; + +fn work(trace: *Trace, fail: bool): Result { + errdefer { + trace.value = trace.value + 10; + } + + if fail { + ret .Err(.Bad); + } + ret .Ok(7); +} + +fn main() { + var trace = Trace{ .value = 0 }; + + var ok = work(&trace, false); + match ok { + .Ok(value) => { + @assert(value == 7, "ok payload"); + }, + .Err(_) => { + @assert(false, "unexpected error"); + }, + } + @assert(trace.value == 0, "errdefer must not run on success"); + + var bad = work(&trace, true); + match bad { + .Ok(_) => { + @assert(false, "unexpected success"); + }, + .Err(error) => { + match error { + .Bad => {}, + } + }, + } + @assert(trace.value == 10, "errdefer runs on error"); +} diff --git a/tests/stage3/602_errdefer_lifo.spl b/tests/stage3/602_errdefer_lifo.spl new file mode 100644 index 0000000..7044117 --- /dev/null +++ b/tests/stage3/602_errdefer_lifo.spl @@ -0,0 +1,59 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +type Trace = struct { + value: i32, +}; + +fn work(trace: *Trace, fail: bool): Result { + errdefer { + trace.value = trace.value * 10 + 1; + } + errdefer { + trace.value = trace.value * 10 + 2; + } + + if fail { + ret .Err(.Bad); + } + ret .Ok(1); +} + +fn main() { + var trace = Trace{ .value = 0 }; + + var ok = work(&trace, false); + match ok { + .Ok(value) => { + @assert(value == 1, "ok payload"); + }, + .Err(_) => { + @assert(false, "unexpected error"); + }, + } + @assert(trace.value == 0, "errdefer skipped on success"); + + var bad = work(&trace, true); + match bad { + .Ok(_) => { + @assert(false, "unexpected success"); + }, + .Err(error) => { + match error { + .Bad => {}, + } + }, + } + @assert(trace.value == 21, "errdefer must run in reverse order"); +} diff --git a/tests/stage3/603_try.spl b/tests/stage3/603_try.spl new file mode 100644 index 0000000..1cd456c --- /dev/null +++ b/tests/stage3/603_try.spl @@ -0,0 +1,55 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +fn good(): Result { + ret .Ok(7); +} + +fn bad(): Result { + ret .Err(.Bad); +} + +fn good_chain(): Result { + var value: i32 = try good(); + ret .Ok(value + 1); +} + +fn bad_chain(): Result { + var value: i32 = try bad(); + ret .Ok(value); +} + +fn main() { + var ok = good_chain(); + match ok { + .Ok(value) => { + @assert(value == 8, "try unwraps #ok"); + }, + .Err(_) => { + @assert(false, "unexpected error"); + }, + } + + var bad = bad_chain(); + match bad { + .Ok(_) => { + @assert(false, "try must propagate #error"); + }, + .Err(error) => { + match error { + .Bad => {}, + } + }, + } +} diff --git a/tests/stage3/604_catch.spl b/tests/stage3/604_catch.spl new file mode 100644 index 0000000..7b0e5ce --- /dev/null +++ b/tests/stage3/604_catch.spl @@ -0,0 +1,57 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +fn good(): Result { + ret .Ok(7); +} + +fn bad(): Result { + ret .Err(.Bad); +} + +fn success(): Result { + var value: i32 = good() catch .Err(error) { + 0 + }; + ret .Ok(value); +} + +fn fallback(): Result { + var value: i32 = bad() catch .Err(error) { + 40 + 2 + }; + ret .Ok(value); +} + +fn main() { + var ok = success(); + match ok { + .Ok(value) => { + @assert(value == 7, "catch passes #ok payload"); + }, + .Err(_) => { + @assert(false, "unexpected error"); + }, + } + + var recovered = fallback(); + match recovered { + .Ok(value) => { + @assert(value == 42, "catch handles #error"); + }, + .Err(_) => { + @assert(false, "catch must consume error"); + }, + } +} diff --git a/tests/stage3/605_nested_cleanup.spl b/tests/stage3/605_nested_cleanup.spl new file mode 100644 index 0000000..5c7dec4 --- /dev/null +++ b/tests/stage3/605_nested_cleanup.spl @@ -0,0 +1,50 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +type Trace = struct { + value: i32, +}; + +fn inner(trace: *Trace): Result { + errdefer { + trace.value = trace.value + 1; + } + ret .Err(.Bad); +} + +fn outer(trace: *Trace): Result { + errdefer { + trace.value = trace.value + 10; + } + + var value: i32 = try inner(trace); + ret .Ok(value); +} + +fn main() { + var trace = Trace{ .value = 0 }; + var result = outer(&trace); + + match result { + .Ok(_) => { + @assert(false, "nested error must propagate"); + }, + .Err(error) => { + match error { + .Bad => {}, + } + }, + } + @assert(trace.value == 11, "inner and outer errdefer both run"); +} diff --git a/tests/stage3/606_plain_enum.spl b/tests/stage3/606_plain_enum.spl new file mode 100644 index 0000000..cb1fc26 --- /dev/null +++ b/tests/stage3/606_plain_enum.spl @@ -0,0 +1,18 @@ +// Expect retcode = 0 + +type Plain = enum { + A, + B, +}; + +fn main() { + var value: Plain = .A; + match value { + .A => { + @assert(true, "plain enum remains usable"); + }, + .B => { + @assert(false, "unexpected variant"); + }, + } +} diff --git a/tests/stage3/607_custom_names.spl b/tests/stage3/607_custom_names.spl new file mode 100644 index 0000000..dc38cd8 --- /dev/null +++ b/tests/stage3/607_custom_names.spl @@ -0,0 +1,55 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type CustomResult = enum { + #ok + Value: i32, + + #error + Problem: Failure, +}; + +fn good(): CustomResult { + ret .Value(9); +} + +fn bad(): CustomResult { + ret .Problem(.Bad); +} + +fn try_good(): CustomResult { + var value: i32 = try good(); + ret .Value(value + 1); +} + +fn catch_bad(): CustomResult { + var value: i32 = bad() catch .Problem(error) { + 5 + }; + ret .Value(value); +} + +fn main() { + var ok = try_good(); + match ok { + .Value(value) => { + @assert(value == 10, "custom #ok name"); + }, + .Problem(_) => { + @assert(false, "unexpected error"); + }, + } + + var recovered = catch_bad(); + match recovered { + .Value(value) => { + @assert(value == 5, "custom #error name"); + }, + .Problem(_) => { + @assert(false, "catch must consume error"); + }, + } +} diff --git a/tests/stage3/608_multi_error.spl b/tests/stage3/608_multi_error.spl new file mode 100644 index 0000000..882269f --- /dev/null +++ b/tests/stage3/608_multi_error.spl @@ -0,0 +1,53 @@ +// Expect retcode = 0 + +type Result = enum { + #ok + Ok: i32, + + #error + Bad, + + #error + Worse, +}; + +fn bad(): Result { + ret .Bad; +} + +fn worse(): Result { + ret .Worse; +} + +fn choose(kind: i32): Result { + if kind == 0 { + var value: i32 = try bad(); + ret .Ok(value); + } + var value: i32 = try worse(); + ret .Ok(value); +} + +fn main() { + var bad_result = choose(0); + match bad_result { + .Ok(_) => { + @assert(false, "unexpected success"); + }, + .Bad => {}, + .Worse => { + @assert(false, "wrong error variant"); + }, + } + + var worse_result = choose(1); + match worse_result { + .Ok(_) => { + @assert(false, "unexpected success"); + }, + .Bad => { + @assert(false, "wrong error variant"); + }, + .Worse => {}, + } +} diff --git a/tests/stage3/609_defer_order.spl b/tests/stage3/609_defer_order.spl new file mode 100644 index 0000000..3eae22f --- /dev/null +++ b/tests/stage3/609_defer_order.spl @@ -0,0 +1,62 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +type Trace = struct { + value: i32, +}; + +fn work(trace: *Trace, fail: bool): Result { + defer { + trace.value = trace.value * 10 + 3; + } + errdefer { + trace.value = trace.value * 10 + 1; + } + errdefer { + trace.value = trace.value * 10 + 2; + } + + if fail { + ret .Err(.Bad); + } + ret .Ok(1); +} + +fn main() { + var success_trace = Trace{ .value = 0 }; + var ok = work(&success_trace, false); + match ok { + .Ok(value) => { + @assert(value == 1, "ok payload"); + }, + .Err(_) => { + @assert(false, "unexpected error"); + }, + } + @assert(success_trace.value == 3, "only defer runs on success"); + + var error_trace = Trace{ .value = 0 }; + var bad = work(&error_trace, true); + match bad { + .Ok(_) => { + @assert(false, "unexpected success"); + }, + .Err(error) => { + match error { + .Bad => {}, + } + }, + } + @assert(error_trace.value == 213, "defer and errdefer run in LIFO order"); +} diff --git a/tests/stage3/610_try_payload_diff.spl b/tests/stage3/610_try_payload_diff.spl new file mode 100644 index 0000000..b44c63e --- /dev/null +++ b/tests/stage3/610_try_payload_diff.spl @@ -0,0 +1,57 @@ +// Expect retcode = 0 + +type Failure = enum { + Bad, +}; + +type LexResult = enum { + #ok + Token: i32, + + #error + Err: Failure, +}; + +type ParseResult = enum { + #ok + Ast: i32, + + #error + Err: Failure, +}; + +fn lex(fail: bool): LexResult { + if fail { + ret .Err(.Bad); + } + ret .Token(7); +} + +fn parse(fail: bool): ParseResult { + var token: i32 = try lex(fail); + ret .Ast(token + 1); +} + +fn main() { + var ok = parse(false); + match ok { + .Ast(value) => { + @assert(value == 8, "try allows different success payloads"); + }, + .Err(_) => { + @assert(false, "unexpected error"); + }, + } + + var bad = parse(true); + match bad { + .Ast(_) => { + @assert(false, "unexpected success"); + }, + .Err(error) => { + match error { + .Bad => {}, + } + }, + } +} diff --git a/tests/stage3/611_try_no_ok.spl b/tests/stage3/611_try_no_ok.spl new file mode 100644 index 0000000..2a9a9a8 --- /dev/null +++ b/tests/stage3/611_try_no_ok.spl @@ -0,0 +1,17 @@ +// Expect compile_error E0701 + +type BadResult = enum { + #error + Err: i32, +}; + +fn bad(): BadResult { + ret .Err(1); +} + +fn use_bad(): BadResult { + var value: i32 = try bad(); + ret .Err(value); +} + +fn main() {} diff --git a/tests/stage3/612_try_error_mismatch.spl b/tests/stage3/612_try_error_mismatch.spl new file mode 100644 index 0000000..f935ef2 --- /dev/null +++ b/tests/stage3/612_try_error_mismatch.spl @@ -0,0 +1,36 @@ +// Expect compile_error E0703 + +type LexError = enum { + Bad, +}; + +type ParseError = enum { + Bad, +}; + +type LexResult = enum { + #ok + Token: i32, + + #error + Err: LexError, +}; + +type ParseResult = enum { + #ok + Ast: i32, + + #error + Err: ParseError, +}; + +fn lex(): LexResult { + ret .Err(.Bad); +} + +fn parse(): ParseResult { + var token: i32 = try lex(); + ret .Ast(token); +} + +fn main() {} diff --git a/tests/stage3/613_catch_handler_type.spl b/tests/stage3/613_catch_handler_type.spl new file mode 100644 index 0000000..7a00016 --- /dev/null +++ b/tests/stage3/613_catch_handler_type.spl @@ -0,0 +1,26 @@ +// Expect compile_error E0702 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +fn bad(): Result { + ret .Err(.Bad); +} + +fn fallback(): Result { + var value: i32 = bad() catch .Err(error) { + true + }; + ret .Ok(value); +} + +fn main() {} diff --git a/tests/stage3/614_errdefer_no_error.spl b/tests/stage3/614_errdefer_no_error.spl new file mode 100644 index 0000000..39a23c3 --- /dev/null +++ b/tests/stage3/614_errdefer_no_error.spl @@ -0,0 +1,10 @@ +// Expect compile_error E0706 + +fn work(): i32 { + errdefer { + var ignored: i32 = 0; + } + ret 1; +} + +fn main() {} diff --git a/tests/stage3/615_catch_bad_pattern.spl b/tests/stage3/615_catch_bad_pattern.spl new file mode 100644 index 0000000..35c97b8 --- /dev/null +++ b/tests/stage3/615_catch_bad_pattern.spl @@ -0,0 +1,26 @@ +// Expect compile_error E0705 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #error + Err: Failure, +}; + +fn good(): Result { + ret .Ok(1); +} + +fn fallback(): Result { + var value: i32 = good() catch .Ok(payload) { + 2 + }; + ret .Ok(value); +} + +fn main() {} diff --git a/tests/stage3/616_try_no_error.spl b/tests/stage3/616_try_no_error.spl new file mode 100644 index 0000000..dd6248c --- /dev/null +++ b/tests/stage3/616_try_no_error.spl @@ -0,0 +1,17 @@ +// Expect compile_error E0708 + +type Result = enum { + #ok + Ok: i32, +}; + +fn good(): Result { + ret .Ok(1); +} + +fn propagate(): Result { + var value: i32 = try good(); + ret .Ok(value); +} + +fn main() {} diff --git a/tests/stage3/617_duplicate_ok.spl b/tests/stage3/617_duplicate_ok.spl new file mode 100644 index 0000000..52bb69f --- /dev/null +++ b/tests/stage3/617_duplicate_ok.spl @@ -0,0 +1,18 @@ +// Expect compile_error E0707 + +type Failure = enum { + Bad, +}; + +type Result = enum { + #ok + Ok: i32, + + #ok + Also: i32, + + #error + Err: Failure, +}; + +fn main() {} diff --git a/tests/stage3/801_error.spl b/tests/stage3/801_error.spl deleted file mode 100644 index daaf260..0000000 --- a/tests/stage3/801_error.spl +++ /dev/null @@ -1,3 +0,0 @@ - -fn main(): void { -} diff --git a/tests/stage3/802_borrow.spl b/tests/stage3/802_borrow.spl deleted file mode 100644 index daaf260..0000000 --- a/tests/stage3/802_borrow.spl +++ /dev/null @@ -1,3 +0,0 @@ - -fn main(): void { -} diff --git a/tests/stage3/701_generic_vec.spl b/tests/stage3/901_generic_vec.spl similarity index 90% rename from tests/stage3/701_generic_vec.spl rename to tests/stage3/901_generic_vec.spl index 024417d..a175006 100644 --- a/tests/stage3/701_generic_vec.spl +++ b/tests/stage3/901_generic_vec.spl @@ -20,7 +20,7 @@ macro fn alloc(called_type: type, shape_type: type) Ref { } #hook(param) -macro fn self(callee: Ref, value: Ref): Ref { +macro fn Self(callee: Ref, value: Ref): Ref { ret @meta.construct(.{ .kind = .Addr(value) }); } @@ -29,26 +29,26 @@ comptime fn Vec(t: type) type { data: []t, len: usize, cap: usize, - fn empty(): @Self { + fn empty(): @self { ret .{ .data = .[], .len = 0, .cap = 0 }; } - fn with_capacity(alloc: @alloc Allocator, n: usize): @Self { + fn with_capacity(alloc: @alloc Allocator, n: usize): @self { var es: usize = @sizeof(t); var p = alloc.alloc(n * es) as *t; ret .{ .data = .{ .ptr = p, .len = n }, .len = 0, .cap = n }; } - fn push(@self v: *@Self, x: t): void { + fn push(@Self v: *@self, x: t): void { v.data[v.len] = x; v.len = v.len + 1; } - fn get(@self v: *@Self, i: usize): t { + fn get(@Self v: *@self, i: usize): t { ret v.data[i]; } - fn as_slice(@self v: *@Self): []t { + fn as_slice(@Self v: *@self): []t { ret v.data[0..v.len]; } }; diff --git a/tests/stage3/702_fmt.spl b/tests/stage3/902_fmt.spl similarity index 100% rename from tests/stage3/702_fmt.spl rename to tests/stage3/902_fmt.spl diff --git a/tests/stage3/703_receiver.spl b/tests/stage3/903_receiver.spl similarity index 76% rename from tests/stage3/703_receiver.spl rename to tests/stage3/903_receiver.spl index f4441cf..ddfe7e2 100644 --- a/tests/stage3/703_receiver.spl +++ b/tests/stage3/903_receiver.spl @@ -4,18 +4,18 @@ type meta = @import("meta"); type .{ .Ref } = meta; #hook(param) -macro fn self(callee: Ref, value: Ref): Ref { +macro fn Self(callee: Ref, value: Ref): Ref { ret @meta.construct(.{ .kind = .Addr(value) }); } type Counter = struct { value: i32; - fn inc(@self c: *@Self): void { + fn inc(@Self c: *@self): void { c.value = c.value + 1; } - fn get(@self c: *@Self): i32 { + fn get(@Self c: *@self): i32 { ret c.value; } } diff --git a/tests/stage3/704_default.spl b/tests/stage3/904_default.spl similarity index 100% rename from tests/stage3/704_default.spl rename to tests/stage3/904_default.spl