From 90941b3b1f5513cef7f913bc54f1b5a2af0c8c7a Mon Sep 17 00:00:00 2001 From: JalonSolov Date: Mon, 9 Jan 2023 01:36:45 -0500 Subject: [PATCH] all: change optional to option (#16914) --- CHANGELOG.md | 12 +- ROADMAP.md | 2 +- cmd/tools/vast/vast.v | 4 +- doc/docs.md | 22 +- vlib/builtin/builtin.c.v | 8 +- vlib/builtin/builtin.v | 6 +- vlib/builtin/option.v | 4 +- vlib/builtin/utf8.v | 2 +- ...t.v => json_decode_with_option_arg_test.v} | 2 +- ...on_encode_struct_with_option_field_test.v} | 2 +- vlib/strconv/atoi.v | 2 +- vlib/toml/scanner/scanner.v | 2 +- vlib/v/ast/ast.v | 4 +- vlib/v/ast/table.v | 24 +- vlib/v/ast/types.v | 16 +- vlib/v/ast/types_test.v | 18 +- vlib/v/checker/check_types.v | 14 +- vlib/v/checker/checker.v | 136 +++++------ vlib/v/checker/comptime.v | 8 +- vlib/v/checker/containers.v | 6 +- vlib/v/checker/fn.v | 54 ++--- vlib/v/checker/for.v | 8 +- vlib/v/checker/if.v | 4 +- vlib/v/checker/infix.v | 18 +- vlib/v/checker/interface.v | 4 +- vlib/v/checker/match.v | 2 +- vlib/v/checker/return.v | 14 +- vlib/v/checker/str.v | 4 +- vlib/v/checker/struct.v | 6 +- .../tests/aliased_option_fn_call_err.out | 6 + ...l_err.vv => aliased_option_fn_call_err.vv} | 4 +- .../tests/aliased_optional_fn_call_err.out | 6 - .../array_filter_map_option_function_err.out | 21 ++ ...> array_filter_map_option_function_err.vv} | 10 +- ...array_filter_map_optional_function_err.out | 21 -- vlib/v/checker/tests/assert_option_err.out | 6 + vlib/v/checker/tests/assert_option_err.vv | 5 + vlib/v/checker/tests/assert_optional_err.out | 6 - vlib/v/checker/tests/assert_optional_err.vv | 5 - ...out => comptime_if_option_string_test.out} | 0 ...t.vv => comptime_if_option_string_test.vv} | 0 ...tom_comptime_define_error.mysymbol.run.out | 4 +- .../tests/custom_comptime_define_error.out | 14 +- .../tests/custom_comptime_define_error.vv | 16 +- vlib/v/checker/tests/defer_option.out | 7 + .../{defer_optional.vv => defer_option.vv} | 0 vlib/v/checker/tests/defer_optional.out | 7 - .../expression_should_return_an_option.out | 12 +- .../expression_should_return_an_option.vv | 12 +- vlib/v/checker/tests/fn_arg_of_option_err.out | 5 + vlib/v/checker/tests/fn_arg_of_option_err.vv | 7 + .../checker/tests/fn_arg_of_optional_err.out | 5 - .../v/checker/tests/fn_arg_of_optional_err.vv | 7 - ...k_for_matching_option_result_in_fields.out | 7 + ...k_for_matching_option_result_in_fields.vv} | 0 ...for_matching_optional_result_in_fields.out | 7 - vlib/v/checker/tests/fn_return_or_err.out | 6 +- ...ll.out => for_c_stmt_with_option_call.out} | 4 +- ...call.vv => for_c_stmt_with_option_call.vv} | 0 vlib/v/checker/tests/for_in_index_option.out | 7 + vlib/v/checker/tests/for_in_index_option.vv | 7 + .../v/checker/tests/for_in_index_optional.out | 7 - vlib/v/checker/tests/for_in_index_optional.vv | 6 - .../tests/go_append_option_to_threads_err.out | 7 + ....vv => go_append_option_to_threads_err.vv} | 2 +- .../go_append_optional_to_threads_err.out | 7 - vlib/v/checker/tests/go_wait_or.out | 16 +- vlib/v/checker/tests/if_expr_option_err.out | 7 + ..._optional_err.vv => if_expr_option_err.vv} | 2 +- vlib/v/checker/tests/if_expr_optional_err.out | 7 - vlib/v/checker/tests/index_of_option_err.out | 7 + ...optional_err.vv => index_of_option_err.vv} | 0 .../v/checker/tests/index_of_optional_err.out | 7 - .../tests/infix_compare_option_err.out | 7 + .../checker/tests/infix_compare_option_err.vv | 8 + .../tests/infix_compare_optional_err.out | 6 - .../tests/infix_compare_optional_err.vv | 7 - vlib/v/checker/tests/infix_err.out | 18 +- .../checker/tests/option_fields_addr_err.out | 13 + ..._addr_err.vv => option_fields_addr_err.vv} | 0 vlib/v/checker/tests/option_fn_err.out | 168 +++++++++++++ .../{optional_fn_err.vv => option_fn_err.vv} | 15 +- vlib/v/checker/tests/option_in_dump_err.out | 6 + ...l_in_dump_err.vv => option_in_dump_err.vv} | 0 .../tests/option_in_println_mismatch.out | 6 + ...match.vv => option_in_println_mismatch.vv} | 0 ...atch.out => option_interface_mismatch.out} | 4 +- ...smatch.vv => option_interface_mismatch.vv} | 0 .../tests/option_or_block_mismatch.out | 13 + ...ismatch.vv => option_or_block_mismatch.vv} | 0 .../tests/option_or_block_none_err.out | 7 + ...one_err.vv => option_or_block_none_err.vv} | 0 ...ock_returns_value_of_incompatible_type.out | 7 + ...lock_returns_value_of_incompatible_type.vv | 16 ++ .../checker/tests/option_propagate_nested.out | 28 +++ ...e_nested.vv => option_propagate_nested.vv} | 0 ..._call_err.out => option_type_call_err.out} | 4 +- ...pe_call_err.vv => option_type_call_err.vv} | 0 vlib/v/checker/tests/option_variable_err.out | 5 + ...variable_err.vv => option_variable_err.vv} | 0 .../tests/optional_fields_addr_err.out | 13 - vlib/v/checker/tests/optional_fn_err.out | 168 ------------- vlib/v/checker/tests/optional_in_dump_err.out | 6 - .../tests/optional_in_println_mismatch.out | 6 - .../tests/optional_or_block_mismatch.out | 13 - .../tests/optional_or_block_none_err.out | 7 - ...ock_returns_value_of_incompatible_type.out | 7 - ...lock_returns_value_of_incompatible_type.vv | 16 -- .../tests/optional_propagate_nested.out | 28 --- .../v/checker/tests/optional_variable_err.out | 5 - .../return_option_of_multiple_results.out | 6 + ...v => return_option_of_multiple_results.vv} | 2 +- .../return_optional_of_multiple_results.out | 6 - ...urn_method_can_be_used_instead_of_panic.vv | 4 +- .../tests/selector_expr_option_err.out | 6 + ...nal_err.vv => selector_expr_option_err.vv} | 0 .../tests/selector_expr_optional_err.out | 6 - .../checker/tests/struct_field_option_err.out | 41 ++++ ...onal_err.vv => struct_field_option_err.vv} | 0 .../tests/struct_field_optional_err.out | 41 ---- .../test_functions_wrong_signature_test.vv | 2 +- ...ional_err.out => type_cast_option_err.out} | 4 +- vlib/v/checker/tests/type_cast_option_err.vv | 3 + .../v/checker/tests/type_cast_optional_err.vv | 3 - vlib/v/checker/tests/unexpected_or.out | 6 +- .../checker/tests/unexpected_or_propagate.out | 4 +- .../checker/tests/unwrapped_option_infix.out | 5 + .../v/checker/tests/unwrapped_option_infix.vv | 5 + .../tests/unwrapped_optional_infix.out | 5 - .../checker/tests/unwrapped_optional_infix.vv | 5 - ...d_optional_err.out => void_option_err.out} | 4 +- ...oid_optional_err.vv => void_option_err.vv} | 0 .../tests/wrong_propagate_ret_type.out | 12 +- vlib/v/fmt/fmt.v | 2 +- ...vv => fntype_mut_args_with_option_keep.vv} | 0 ...l_keep.vv => fntype_return_option_keep.vv} | 0 vlib/v/fmt/tests/functions_expected.vv | 4 +- vlib/v/fmt/tests/functions_input.vv | 45 ++-- vlib/v/fmt/tests/language_prefixes_keep.vv | 2 +- ... map_value_with_option_result_expected.vv} | 0 ... => map_value_with_option_result_input.vv} | 0 .../{optional_keep.vv => option_keep.vv} | 0 ...agate_keep.vv => option_propagate_keep.vv} | 0 ...eep.vv => option_with_multi_stmts_keep.vv} | 0 vlib/v/fmt/tests/or_keep.vv | 4 +- ...d_optional_keep.vv => void_option_keep.vv} | 0 vlib/v/gen/c/assign.v | 28 +-- vlib/v/gen/c/auto_str_methods.v | 14 +- vlib/v/gen/c/cgen.v | 228 +++++++++--------- vlib/v/gen/c/comptime.v | 18 +- vlib/v/gen/c/dumpexpr.v | 6 +- vlib/v/gen/c/fn.v | 22 +- vlib/v/gen/c/for.v | 8 +- vlib/v/gen/c/if.v | 16 +- vlib/v/gen/c/index.v | 12 +- vlib/v/gen/c/infix.v | 2 +- vlib/v/gen/c/json.v | 6 +- vlib/v/gen/c/match.v | 10 +- vlib/v/gen/c/struct.v | 26 +- ...onal_call.out => comptime_option_call.out} | 0 ...tional_call.vv => comptime_option_call.vv} | 0 vlib/v/gen/golang/golang.v | 2 +- vlib/v/gen/js/auto_str_methods.v | 12 +- vlib/v/gen/js/comptime.v | 4 +- vlib/v/gen/js/deep_copy.v | 8 +- vlib/v/gen/js/fn.v | 32 +-- vlib/v/gen/js/js.v | 38 +-- vlib/v/gen/js/tests/{optional.v => option.v} | 0 vlib/v/gen/native/comptime.v | 4 +- vlib/v/markused/markused.v | 2 +- vlib/v/parser/if_match.v | 2 +- vlib/v/parser/parse_type.v | 24 +- vlib/v/parser/parser.v | 8 +- vlib/v/parser/struct.v | 10 +- vlib/v/parser/tests/if_guard_cond_err.out | 4 +- vlib/v/parser/tests/index_expr_option_err.out | 6 + ...tional_err.vv => index_expr_option_err.vv} | 0 .../parser/tests/index_expr_optional_err.out | 6 - .../tests/inline_sum_type_option_err.out | 10 + ...l_err.vv => inline_sum_type_option_err.vv} | 0 .../tests/inline_sum_type_optional_err.out | 10 - .../tests/option_sum_type_return_err.out | 4 +- .../struct_field_required_fn_option_type.out | 7 + ...> struct_field_required_fn_option_type.vv} | 2 +- ...struct_field_required_fn_optional_type.out | 7 - ...l_test.v => aliased_option_fn_call_test.v} | 2 +- ... => anon_fn_option_call_in_if_expr_test.v} | 6 +- .../v/tests/anon_fn_returning_question_test.v | 2 +- ...onal_test.v => anon_fn_with_option_test.v} | 2 +- ...st.v => array_elements_with_option_test.v} | 20 +- ...ional_test.v => array_index_option_test.v} | 4 +- vlib/v/tests/array_init_test.v | 2 +- vlib/v/tests/array_with_it_test.v | 2 +- ...n_option_of_multi_return_with_heap_test.v} | 2 +- ...ptional_args_test.v => option_args_test.v} | 0 ...test.v => cast_option_to_interface_test.v} | 2 +- vlib/v/tests/check_in_is_consistency_test.v | 12 +- .../comptime_for_in_fields_FieldData_test.v | 8 +- ...v => const_can_use_options_results_test.v} | 0 vlib/v/tests/const_test.v | 2 +- ...test.v => const_use_nested_options_test.v} | 2 +- vlib/v/tests/enum_test.v | 6 +- ...n_optional_test.v => for_in_option_test.v} | 2 +- ...l2_test.v => for_loop_with_option2_test.v} | 2 +- ...nal_test.v => for_loop_with_option_test.v} | 0 ...rics_interface_with_generic_sumtype_test.v | 29 ++- ...> generics_method_returning_option_test.v} | 2 +- vlib/v/tests/generics_test.v | 2 +- ...onal_test.v => if_cond_with_option_test.v} | 2 +- ... if_expr_nested_with_option_result_test.v} | 3 +- ...tional_test.v => if_expr_of_option_test.v} | 4 +- ...=> if_expr_with_method_call_option_test.v} | 2 +- vlib/v/tests/if_expression_test.v | 8 +- vlib/v/tests/init_multiple_branches_test.v | 4 +- vlib/v/tests/inout/comptime_bool_fields.out | 4 +- vlib/v/tests/inout/comptime_bool_fields.vv | 4 +- ...{optional_fn_arg.out => option_fn_arg.out} | 0 .../{optional_fn_arg.vv => option_fn_arg.vv} | 0 ...xpr.out => printing_option_in_if_expr.out} | 0 ..._expr.vv => printing_option_in_if_expr.vv} | 4 +- vlib/v/tests/inout/struct_field_option.out | 26 ++ ...eld_optional.vv => struct_field_option.vv} | 0 vlib/v/tests/inout/struct_field_optional.out | 26 -- ...=> interface_only_decl_with_option_test.v} | 2 +- ....v => map_value_with_option_result_test.v} | 2 +- ...ch_expr_nested_with_option_result1_test.v} | 3 +- ...ch_expr_nested_with_option_result2_test.v} | 3 +- ...t.v => match_expr_returning_option_test.v} | 6 +- vlib/v/tests/multi_line_with_optionals_test.v | 21 -- vlib/v/tests/multi_line_with_options_test.v | 21 ++ ...pes_test.v => option_compvar_types_test.v} | 0 ...r_val_test.v => option_compvar_val_test.v} | 2 +- vlib/v/tests/option_default_values_test.v | 18 +- ...turn_test.v => option_multi_return_test.v} | 2 +- vlib/v/tests/option_struct_init_test.v | 2 +- vlib/v/tests/option_test.v | 28 +-- vlib/v/tests/option_void_2_test.v | 2 +- vlib/v/tests/option_void_test.v | 8 +- .../{optional_call.repl => option_call.repl} | 0 ...n_optional_test.v => return_option_test.v} | 2 +- ...d_optional_test.v => shared_option_test.v} | 2 +- ...t_used_in_struct_default_init_recursive.vv | 2 +- ...s_for_options_and_results_in_main.run.out} | 0 ...s_and_results_in_main.skip_unused.run.out} | 0 ...thands_for_options_and_results_in_main.vv} | 0 ..._none_test.v => string_option_none_test.v} | 2 +- ...test.v => struct_option_field_zero_test.v} | 2 +- vlib/v/tests/struct_test.v | 38 +-- vlib/v/tests/typeof_type_test.v | 2 +- vlib/v/tests/valgrind/1.strings_and_arrays.v | 18 +- vlib/x/json2/decode_struct_test.v | 2 +- vlib/x/json2/decode_struct_todo_test.vv | 83 +++---- vlib/x/json2/encode_struct_test.v | 124 +++++----- vlib/x/json2/encode_struct_todo_test.vv | 116 ++++----- vlib/x/json2/encoder.v | 34 +-- ...t.v => json_decode_with_option_arg_test.v} | 2 +- 256 files changed, 1443 insertions(+), 1443 deletions(-) rename vlib/json/{json_decode_with_optional_arg_test.v => json_decode_with_option_arg_test.v} (85%) rename vlib/json/{json_encode_struct_with_optional_field_test.v => json_encode_struct_with_option_field_test.v} (84%) create mode 100644 vlib/v/checker/tests/aliased_option_fn_call_err.out rename vlib/v/checker/tests/{aliased_optional_fn_call_err.vv => aliased_option_fn_call_err.vv} (65%) delete mode 100644 vlib/v/checker/tests/aliased_optional_fn_call_err.out create mode 100644 vlib/v/checker/tests/array_filter_map_option_function_err.out rename vlib/v/checker/tests/{array_filter_map_optional_function_err.vv => array_filter_map_option_function_err.vv} (54%) delete mode 100644 vlib/v/checker/tests/array_filter_map_optional_function_err.out create mode 100644 vlib/v/checker/tests/assert_option_err.out create mode 100644 vlib/v/checker/tests/assert_option_err.vv delete mode 100644 vlib/v/checker/tests/assert_optional_err.out delete mode 100644 vlib/v/checker/tests/assert_optional_err.vv rename vlib/v/checker/tests/{comptime_if_optional_string_test.out => comptime_if_option_string_test.out} (100%) rename vlib/v/checker/tests/{comptime_if_optional_string_test.vv => comptime_if_option_string_test.vv} (100%) create mode 100644 vlib/v/checker/tests/defer_option.out rename vlib/v/checker/tests/{defer_optional.vv => defer_option.vv} (100%) delete mode 100644 vlib/v/checker/tests/defer_optional.out create mode 100644 vlib/v/checker/tests/fn_arg_of_option_err.out create mode 100644 vlib/v/checker/tests/fn_arg_of_option_err.vv delete mode 100644 vlib/v/checker/tests/fn_arg_of_optional_err.out delete mode 100644 vlib/v/checker/tests/fn_arg_of_optional_err.vv create mode 100644 vlib/v/checker/tests/fn_check_for_matching_option_result_in_fields.out rename vlib/v/checker/tests/{fn_check_for_matching_optional_result_in_fields.vv => fn_check_for_matching_option_result_in_fields.vv} (100%) delete mode 100644 vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out rename vlib/v/checker/tests/{for_c_stmt_with_optional_call.out => for_c_stmt_with_option_call.out} (61%) rename vlib/v/checker/tests/{for_c_stmt_with_optional_call.vv => for_c_stmt_with_option_call.vv} (100%) create mode 100644 vlib/v/checker/tests/for_in_index_option.out create mode 100644 vlib/v/checker/tests/for_in_index_option.vv delete mode 100644 vlib/v/checker/tests/for_in_index_optional.out delete mode 100644 vlib/v/checker/tests/for_in_index_optional.vv create mode 100644 vlib/v/checker/tests/go_append_option_to_threads_err.out rename vlib/v/checker/tests/{go_append_optional_to_threads_err.vv => go_append_option_to_threads_err.vv} (76%) delete mode 100644 vlib/v/checker/tests/go_append_optional_to_threads_err.out create mode 100644 vlib/v/checker/tests/if_expr_option_err.out rename vlib/v/checker/tests/{if_expr_optional_err.vv => if_expr_option_err.vv} (69%) delete mode 100644 vlib/v/checker/tests/if_expr_optional_err.out create mode 100644 vlib/v/checker/tests/index_of_option_err.out rename vlib/v/checker/tests/{index_of_optional_err.vv => index_of_option_err.vv} (100%) delete mode 100644 vlib/v/checker/tests/index_of_optional_err.out create mode 100644 vlib/v/checker/tests/infix_compare_option_err.out create mode 100644 vlib/v/checker/tests/infix_compare_option_err.vv delete mode 100644 vlib/v/checker/tests/infix_compare_optional_err.out delete mode 100644 vlib/v/checker/tests/infix_compare_optional_err.vv create mode 100644 vlib/v/checker/tests/option_fields_addr_err.out rename vlib/v/checker/tests/{optional_fields_addr_err.vv => option_fields_addr_err.vv} (100%) create mode 100644 vlib/v/checker/tests/option_fn_err.out rename vlib/v/checker/tests/{optional_fn_err.vv => option_fn_err.vv} (84%) create mode 100644 vlib/v/checker/tests/option_in_dump_err.out rename vlib/v/checker/tests/{optional_in_dump_err.vv => option_in_dump_err.vv} (100%) create mode 100644 vlib/v/checker/tests/option_in_println_mismatch.out rename vlib/v/checker/tests/{optional_in_println_mismatch.vv => option_in_println_mismatch.vv} (100%) rename vlib/v/checker/tests/{optional_interface_mismatch.out => option_interface_mismatch.out} (53%) rename vlib/v/checker/tests/{optional_interface_mismatch.vv => option_interface_mismatch.vv} (100%) create mode 100644 vlib/v/checker/tests/option_or_block_mismatch.out rename vlib/v/checker/tests/{optional_or_block_mismatch.vv => option_or_block_mismatch.vv} (100%) create mode 100644 vlib/v/checker/tests/option_or_block_none_err.out rename vlib/v/checker/tests/{optional_or_block_none_err.vv => option_or_block_none_err.vv} (100%) create mode 100644 vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.out create mode 100644 vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.vv create mode 100644 vlib/v/checker/tests/option_propagate_nested.out rename vlib/v/checker/tests/{optional_propagate_nested.vv => option_propagate_nested.vv} (100%) rename vlib/v/checker/tests/{optional_type_call_err.out => option_type_call_err.out} (51%) rename vlib/v/checker/tests/{optional_type_call_err.vv => option_type_call_err.vv} (100%) create mode 100644 vlib/v/checker/tests/option_variable_err.out rename vlib/v/checker/tests/{optional_variable_err.vv => option_variable_err.vv} (100%) delete mode 100644 vlib/v/checker/tests/optional_fields_addr_err.out delete mode 100644 vlib/v/checker/tests/optional_fn_err.out delete mode 100644 vlib/v/checker/tests/optional_in_dump_err.out delete mode 100644 vlib/v/checker/tests/optional_in_println_mismatch.out delete mode 100644 vlib/v/checker/tests/optional_or_block_mismatch.out delete mode 100644 vlib/v/checker/tests/optional_or_block_none_err.out delete mode 100644 vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.out delete mode 100644 vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.vv delete mode 100644 vlib/v/checker/tests/optional_propagate_nested.out delete mode 100644 vlib/v/checker/tests/optional_variable_err.out create mode 100644 vlib/v/checker/tests/return_option_of_multiple_results.out rename vlib/v/checker/tests/{return_optional_of_multiple_results.vv => return_option_of_multiple_results.vv} (71%) delete mode 100644 vlib/v/checker/tests/return_optional_of_multiple_results.out create mode 100644 vlib/v/checker/tests/selector_expr_option_err.out rename vlib/v/checker/tests/{selector_expr_optional_err.vv => selector_expr_option_err.vv} (100%) delete mode 100644 vlib/v/checker/tests/selector_expr_optional_err.out create mode 100644 vlib/v/checker/tests/struct_field_option_err.out rename vlib/v/checker/tests/{struct_field_optional_err.vv => struct_field_option_err.vv} (100%) delete mode 100644 vlib/v/checker/tests/struct_field_optional_err.out rename vlib/v/checker/tests/{type_cast_optional_err.out => type_cast_option_err.out} (53%) create mode 100644 vlib/v/checker/tests/type_cast_option_err.vv delete mode 100644 vlib/v/checker/tests/type_cast_optional_err.vv create mode 100644 vlib/v/checker/tests/unwrapped_option_infix.out create mode 100644 vlib/v/checker/tests/unwrapped_option_infix.vv delete mode 100644 vlib/v/checker/tests/unwrapped_optional_infix.out delete mode 100644 vlib/v/checker/tests/unwrapped_optional_infix.vv rename vlib/v/checker/tests/{void_optional_err.out => void_option_err.out} (50%) rename vlib/v/checker/tests/{void_optional_err.vv => void_option_err.vv} (100%) rename vlib/v/fmt/tests/{fntype_mut_args_with_optional_keep.vv => fntype_mut_args_with_option_keep.vv} (100%) rename vlib/v/fmt/tests/{fntype_return_optional_keep.vv => fntype_return_option_keep.vv} (100%) rename vlib/v/fmt/tests/{map_value_with_optional_result_expected.vv => map_value_with_option_result_expected.vv} (100%) rename vlib/v/fmt/tests/{map_value_with_optional_result_input.vv => map_value_with_option_result_input.vv} (100%) rename vlib/v/fmt/tests/{optional_keep.vv => option_keep.vv} (100%) rename vlib/v/fmt/tests/{optional_propagate_keep.vv => option_propagate_keep.vv} (100%) rename vlib/v/fmt/tests/{optional_with_multi_stmts_keep.vv => option_with_multi_stmts_keep.vv} (100%) rename vlib/v/fmt/tests/{void_optional_keep.vv => void_option_keep.vv} (100%) rename vlib/v/gen/c/testdata/{comptime_optional_call.out => comptime_option_call.out} (100%) rename vlib/v/gen/c/testdata/{comptime_optional_call.vv => comptime_option_call.vv} (100%) rename vlib/v/gen/js/tests/{optional.v => option.v} (100%) create mode 100644 vlib/v/parser/tests/index_expr_option_err.out rename vlib/v/parser/tests/{index_expr_optional_err.vv => index_expr_option_err.vv} (100%) delete mode 100644 vlib/v/parser/tests/index_expr_optional_err.out create mode 100644 vlib/v/parser/tests/inline_sum_type_option_err.out rename vlib/v/parser/tests/{inline_sum_type_optional_err.vv => inline_sum_type_option_err.vv} (100%) delete mode 100644 vlib/v/parser/tests/inline_sum_type_optional_err.out create mode 100644 vlib/v/parser/tests/struct_field_required_fn_option_type.out rename vlib/v/parser/tests/{struct_field_required_fn_optional_type.vv => struct_field_required_fn_option_type.vv} (69%) delete mode 100644 vlib/v/parser/tests/struct_field_required_fn_optional_type.out rename vlib/v/tests/{aliased_optional_fn_call_test.v => aliased_option_fn_call_test.v} (74%) rename vlib/v/tests/{anon_fn_optional_call_in_if_expr_test.v => anon_fn_option_call_in_if_expr_test.v} (69%) rename vlib/v/tests/{anon_fn_with_optional_test.v => anon_fn_with_option_test.v} (93%) rename vlib/v/tests/{array_elements_with_optional_test.v => array_elements_with_option_test.v} (50%) rename vlib/v/tests/{array_index_optional_test.v => array_index_option_test.v} (75%) rename vlib/v/tests/{assign_optional_of_multi_return_with_heap_test.v => assign_option_of_multi_return_with_heap_test.v} (93%) rename vlib/v/tests/c_function_mut_param/{optional_args_test.v => option_args_test.v} (100%) rename vlib/v/tests/{cast_optional_to_interface_test.v => cast_option_to_interface_test.v} (92%) rename vlib/v/tests/{const_can_use_optionals_results_test.v => const_can_use_options_results_test.v} (100%) rename vlib/v/tests/{const_use_nested_optionals_test.v => const_use_nested_options_test.v} (79%) rename vlib/v/tests/{for_in_optional_test.v => for_in_option_test.v} (78%) rename vlib/v/tests/{for_loop_with_optional2_test.v => for_loop_with_option2_test.v} (94%) rename vlib/v/tests/{for_loop_with_optional_test.v => for_loop_with_option_test.v} (100%) rename vlib/v/tests/{generics_method_returning_optional_test.v => generics_method_returning_option_test.v} (91%) rename vlib/v/tests/{if_cond_with_optional_test.v => if_cond_with_option_test.v} (94%) rename vlib/v/tests/{if_expr_nested_with_optional_result_test.v => if_expr_nested_with_option_result_test.v} (91%) rename vlib/v/tests/{if_expr_of_optional_test.v => if_expr_of_option_test.v} (91%) rename vlib/v/tests/{if_expr_with_method_call_optional_test.v => if_expr_with_method_call_option_test.v} (87%) rename vlib/v/tests/inout/{optional_fn_arg.out => option_fn_arg.out} (100%) rename vlib/v/tests/inout/{optional_fn_arg.vv => option_fn_arg.vv} (100%) rename vlib/v/tests/inout/{printing_optional_in_if_expr.out => printing_option_in_if_expr.out} (100%) rename vlib/v/tests/inout/{printing_optional_in_if_expr.vv => printing_option_in_if_expr.vv} (58%) create mode 100644 vlib/v/tests/inout/struct_field_option.out rename vlib/v/tests/inout/{struct_field_optional.vv => struct_field_option.vv} (100%) delete mode 100644 vlib/v/tests/inout/struct_field_optional.out rename vlib/v/tests/{interface_only_decl_with_optional_test.v => interface_only_decl_with_option_test.v} (64%) rename vlib/v/tests/{map_value_with_optional_result_test.v => map_value_with_option_result_test.v} (89%) rename vlib/v/tests/{match_expr_nested_with_optional_result1_test.v => match_expr_nested_with_option_result1_test.v} (90%) rename vlib/v/tests/{match_expr_nested_with_optional_result2_test.v => match_expr_nested_with_option_result2_test.v} (91%) rename vlib/v/tests/{match_expr_returning_optional_test.v => match_expr_returning_option_test.v} (77%) delete mode 100644 vlib/v/tests/multi_line_with_optionals_test.v create mode 100644 vlib/v/tests/multi_line_with_options_test.v rename vlib/v/tests/{optional_compvar_types_test.v => option_compvar_types_test.v} (100%) rename vlib/v/tests/{optional_compvar_val_test.v => option_compvar_val_test.v} (97%) rename vlib/v/tests/{optional_multi_return_test.v => option_multi_return_test.v} (76%) rename vlib/v/tests/repl/{optional_call.repl => option_call.repl} (100%) rename vlib/v/tests/{return_optional_test.v => return_option_test.v} (84%) rename vlib/v/tests/{shared_optional_test.v => shared_option_test.v} (89%) rename vlib/v/tests/skip_unused/{shorthands_for_optionals_and_results_in_main.run.out => shorthands_for_options_and_results_in_main.run.out} (100%) rename vlib/v/tests/skip_unused/{shorthands_for_optionals_and_results_in_main.skip_unused.run.out => shorthands_for_options_and_results_in_main.skip_unused.run.out} (100%) rename vlib/v/tests/skip_unused/{shorthands_for_optionals_and_results_in_main.vv => shorthands_for_options_and_results_in_main.vv} (100%) rename vlib/v/tests/{string_optional_none_test.v => string_option_none_test.v} (87%) rename vlib/v/tests/{struct_optional_field_zero_test.v => struct_option_field_zero_test.v} (84%) rename vlib/x/json2/json_module_compatibility_test/{json_decode_with_optional_arg_test.v => json_decode_with_option_arg_test.v} (86%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95ff86886..c41b79ba6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,7 +107,7 @@ - `Option` and `Result` are now separate types: `?Foo` and `!Foo` respectively. Old code will continue working for 1 year and will result in a warning/hint. - Hundreds of new checks in the type checker. - All V's backends have been split up into separate processes. As the result, building V got 26% faster. -- Maps and arrays can now return optionals: `m[bad_key] or { ... }`, `if x := arr[key] { ... }`. +- Maps and arrays can now return options: `m[bad_key] or { ... }`, `if x := arr[key] { ... }`. - `ustring` has been replaced with `[]rune` (works just like in Go). - Maps can now have non-string keys. - A new compiler pass for transforming the AST (doesn't slow the compiler too much, adds about 25ms to `v self`). It eliminates unreachable branches and performs other simple optimizations and transformations. @@ -447,14 +447,14 @@ used in some industries. - `fn init()` for module initialization. - `a in [1, 2, 3]` optimization: no array gets allocated. - Raw strings: `s := r'hello\nworld'`. -- `if a := func() { }` syntax for handling optionals. +- `if a := func() { }` syntax for handling options. - f32/f64 comparison now uses machine epsilon by default. ## V 0.1.21 *30 Sep 2019* -- `none` keyword for optionals. +- `none` keyword for options. - Solaris support. - All table lookup functions now use `none`. - varargs: `fn foo(bar int, params ...string) {`. @@ -535,7 +535,7 @@ this backend. - `libcurl` dependency was removed from the `http` module. - All function arguments are now immutable by default (previously they could be modifed inside the function). -- `http` functions now return optionals. +- `http` functions now return options. - `sync.WaitGroup`. - `vweb` static files serving. - `crypto.rand` module. @@ -629,8 +629,8 @@ this backend. ## V 0.1.12 *4 Jul 2019* - V can finally compile itself on Windows (https://github.com/vlang/v#mingw-w64). -- `os` module now uses optionals in all functions that return `File`. -- Lots of bugs with optionals were fixed. +- `os` module now uses options in all functions that return `File`. +- Lots of bugs with options were fixed. - `println` was optimized. It no longer results in allocations. Now it also works correctly with all integer types. - Lots of `vfmt` fixes, it will be enabled tomorrow. diff --git a/ROADMAP.md b/ROADMAP.md index 77fccfc35..5ba736de1 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -6,7 +6,7 @@ - [ ] Parallel C compilation - [ ] `recover()` from panics - [x] vfmt: add missing imports (like goimports) -- [ ] Recursive structs via optionals: `struct Node { next ?Node }` +- [ ] Recursive structs via options: `struct Node { next ?Node }` - [ ] Optional function struct fields - [ ] Handle function pointers safely, remove `if function == 0 {` - [x] Bundle OpenSSL like GC diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index f0fd4ff26..1b83ef744 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -614,7 +614,7 @@ fn (t Tree) struct_field(node ast.StructField) &Node { obj.add_terse('anon_struct_decl', t.struct_decl(node.anon_struct_decl)) obj.add_terse('unaliased_typ', t.type_node(node.unaliased_typ)) obj.add('type_pos', t.pos(node.type_pos)) - obj.add('optional_pos', t.pos(node.optional_pos)) + obj.add('option_pos', t.pos(node.option_pos)) obj.add_terse('has_default_expr', t.bool_node(node.has_default_expr)) obj.add_terse('default_expr_typ', t.type_node(node.default_expr_typ)) obj.add_terse('default_expr', t.expr(node.default_expr)) @@ -1474,7 +1474,7 @@ fn (t Tree) ident_var(node ast.IdentVar) &Node { obj.add_terse('is_mut', t.bool_node(node.is_mut)) obj.add_terse('is_static', t.bool_node(node.is_static)) obj.add_terse('is_volatile', t.bool_node(node.is_volatile)) - obj.add_terse('is_optional', t.bool_node(node.is_optional)) + obj.add_terse('is_option', t.bool_node(node.is_option)) obj.add_terse('share', t.enum_node(node.share)) return obj } diff --git a/doc/docs.md b/doc/docs.md index c14a18478..a43e0f3ec 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -125,7 +125,7 @@ In your terminal, you can: * [Interfaces](#interfaces) * [Sum types](#sum-types) * [Option/Result types & error handling](#optionresult-types-and-error-handling) - * [Handling optionals/results](#handling-optionalsresults) + * [Handling options/results](#handling-optionsresults) * [Custom error types](#custom-error-types) * [Generics](#generics) * [Concurrency](#concurrency) @@ -1344,7 +1344,7 @@ if v := m['abc'] { } ``` -The same optional check applies to arrays: +The same option check applies to arrays: ```v arr := [1, 2, 3] @@ -3595,7 +3595,7 @@ fn (r Repo) find_user_by_id(id int) !User { return error('User ${id} not found') } -// A version of the function using an optional +// A version of the function using an option fn (r Repo) find_user_by_id2(id int) ?User { for user in r.users { if user.id == id { @@ -3622,7 +3622,7 @@ fn main() { V used to combine `Option` and `Result` into one type, now they are separate. -The amount of work required to "upgrade" a function to an optional/result function is minimal; +The amount of work required to "upgrade" a function to an option/result function is minimal; you have to add a `?` or `!` to the return type and return an error when something goes wrong. This is the primary mechanism for error handling in V. They are still values, like in Go, @@ -3639,9 +3639,9 @@ user := repo.find_user_by_id(7) or { } ``` -#### Handling optionals/results +#### Handling options/results -There are four ways of handling an optional/result. The first method is to +There are four ways of handling an option/result. The first method is to propagate the error: ```v @@ -3655,8 +3655,8 @@ fn f(url string) !string { `http.get` returns `!http.Response`. Because `!` follows the call, the error will be propagated to the caller of `f`. When using `?` after a -function call producing an optional, the enclosing function must return -an optional as well. If error propagation is used in the `main()` +function call producing an option, the enclosing function must return +an option as well. If error propagation is used in the `main()` function it will `panic` instead, since the error cannot be propagated any further. @@ -3679,7 +3679,7 @@ entire program, or use a control flow statement (`return`, `break`, `continue`, to break from the current block. Note that `break` and `continue` can only be used inside a `for` loop. -V does not have a way to forcibly "unwrap" an optional (as other languages do, +V does not have a way to forcibly "unwrap" an option (as other languages do, for instance Rust's `unwrap()` or Swift's `!`). To do this, use `or { panic(err) }` instead. --- @@ -3708,7 +3708,7 @@ The fourth method is to use `if` unwrapping: import net.http if resp := http.get('https://google.com') { - println(resp.body) // resp is a http.Response, not an optional + println(resp.body) // resp is a http.Response, not an option } else { println(err) } @@ -3988,7 +3988,7 @@ A channel can be closed to indicate that no further objects can be pushed. Any a to do so will then result in a runtime panic (with the exception of `select` and `try_push()` - see below). Attempts to pop will return immediately if the associated channel has been closed and the buffer is empty. This situation can be -handled using an `or {}` block (see [Handling optionals/results](#handling-optionalsresults)). +handled using an `or {}` block (see [Handling options/results](#handling-optionsresults)). ```v wip ch := chan int{} diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index 7636a6080..3aca60437 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -83,14 +83,14 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) { vhalt() } -// panic_optional_not_set is called by V, when you use option error propagation in your main function. +// panic_option_not_set is called by V, when you use option error propagation in your main function. // It ends the program with a panic. [noreturn] -pub fn panic_optional_not_set(s string) { - panic('optional not set (${s})') +pub fn panic_option_not_set(s string) { + panic('option not set (${s})') } -// panic_optional_not_set is called by V, when you use result error propagation in your main function +// panic_result_not_set is called by V, when you use result error propagation in your main function // It ends the program with a panic. [noreturn] pub fn panic_result_not_set(s string) { diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 711beb461..4f700cbea 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -118,9 +118,9 @@ pub: is_pub bool // f is in a `pub:` section is_mut bool // f is in a `mut:` section // - is_shared bool // `f shared Abc` - is_atomic bool // `f atomic int` , TODO - is_optional bool // `f ?string` , TODO + is_shared bool // `f shared Abc` + is_atomic bool // `f atomic int` , TODO + is_option bool // `f ?string` , TODO // is_array bool // `f []string` , TODO is_map bool // `f map[string]int` , TODO diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index 22fb66592..551439290 100644 --- a/vlib/builtin/option.v +++ b/vlib/builtin/option.v @@ -107,7 +107,7 @@ pub fn error_with_code(message string, code int) IError { } } -// Option is the base of V's internal optional return system. +// Option is the base of V's internal option return system. struct Option { state u8 err IError = none__ @@ -116,7 +116,7 @@ struct Option { // derived Option_xxx types } -// option is the base of V's internal optional return system. +// option is the base of V's internal option return system. struct _option { state u8 err IError = none__ diff --git a/vlib/builtin/utf8.v b/vlib/builtin/utf8.v index c96f99049..847feb676 100644 --- a/vlib/builtin/utf8.v +++ b/vlib/builtin/utf8.v @@ -84,7 +84,7 @@ pub fn utf8_str_len(s string) int { // valid utf8 in the string, and could result in // values greater than the utf32 spec // it has been replaced by `utf8_to_utf32` which -// has an optional return type. +// has an option return type. // // this function is left for backward compatibility // it is used in vlib/builtin/string.v, diff --git a/vlib/json/json_decode_with_optional_arg_test.v b/vlib/json/json_decode_with_option_arg_test.v similarity index 85% rename from vlib/json/json_decode_with_optional_arg_test.v rename to vlib/json/json_decode_with_option_arg_test.v index bc84c92d1..f26e46c2a 100644 --- a/vlib/json/json_decode_with_optional_arg_test.v +++ b/vlib/json/json_decode_with_option_arg_test.v @@ -3,7 +3,7 @@ import os struct DbConfig {} -fn test_json_decode_with_optional_arg() { +fn test_json_decode_with_option_arg() { if ret := print_info() { println(ret) } else { diff --git a/vlib/json/json_encode_struct_with_optional_field_test.v b/vlib/json/json_encode_struct_with_option_field_test.v similarity index 84% rename from vlib/json/json_encode_struct_with_optional_field_test.v rename to vlib/json/json_encode_struct_with_option_field_test.v index 100eb89c2..4edeb15e3 100644 --- a/vlib/json/json_encode_struct_with_optional_field_test.v +++ b/vlib/json/json_encode_struct_with_option_field_test.v @@ -5,7 +5,7 @@ struct Foo { num ?int } -fn test_json_encode_struct_with_optional_field() { +fn test_json_encode_struct_with_option_field() { f1 := Foo{ name: 'hello' } diff --git a/vlib/strconv/atoi.v b/vlib/strconv/atoi.v index 3dfe47949..a95d0fe13 100644 --- a/vlib/strconv/atoi.v +++ b/vlib/strconv/atoi.v @@ -3,7 +3,7 @@ module strconv // Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. -// TODO: use optionals, or some way to return default with error. +// TODO: use options, or some way to return default with error. const ( // int_size is the size in bits of an int or uint value. // int_size = 32 << (~u32(0) >> 63) diff --git a/vlib/toml/scanner/scanner.v b/vlib/toml/scanner/scanner.v index ce64c180a..8abe0872f 100644 --- a/vlib/toml/scanner/scanner.v +++ b/vlib/toml/scanner/scanner.v @@ -662,7 +662,7 @@ fn (mut s Scanner) validate_and_skip_headers() ! { // NICE-TO-HAVE-TODO Check other types of (UTF-?) headers and yield an error. TOML is UTF-8 only. - // Skip optional UTF-8 heaser, if any. + // Skip optional UTF-8 header, if any. if s.at() == 0xEF && s.peek(1) == 0xBB && s.peek(2) == 0xBF { util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping UTF-8 byte order mark (BOM)') s.header_len = 3 diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index c77e91e0f..da2e30519 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -296,7 +296,7 @@ pub struct StructField { pub: pos token.Pos type_pos token.Pos - optional_pos token.Pos + option_pos token.Pos comments []Comment i int has_default_expr bool @@ -791,7 +791,7 @@ pub mut: is_mut bool is_static bool is_volatile bool - is_optional bool + is_option bool share ShareType } diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 81f820294..9c1a372d7 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -224,7 +224,7 @@ pub fn (t &Table) fn_type_signature(f &Fn) string { } if f.return_type != 0 && f.return_type != void_type { sym := t.sym(f.return_type) - opt := if f.return_type.has_flag(.optional) { 'option_' } else { '' } + opt := if f.return_type.has_flag(.option) { 'option_' } else { '' } res := if f.return_type.has_flag(.result) { 'result_' } else { '' } sig += '__${opt}${res}${sym.cname}' @@ -256,7 +256,7 @@ pub fn (t &Table) fn_type_source_signature(f &Fn) string { sig += ' !' } else if f.return_type != void_type { return_type_sym := t.sym(f.return_type) - if f.return_type.has_flag(.optional) { + if f.return_type.has_flag(.option) { sig += ' ?${return_type_sym.name}' } else if f.return_type.has_flag(.result) { sig += ' !${return_type_sym.name}' @@ -882,7 +882,7 @@ pub fn (t &Table) known_type_idx(typ Type) bool { pub fn (t &Table) array_name(elem_type Type) string { elem_type_sym := t.sym(elem_type) ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' } - opt := if elem_type.has_flag(.optional) { '?' } else { '' } + opt := if elem_type.has_flag(.option) { '?' } else { '' } res := if elem_type.has_flag(.result) { '!' } else { '' } return '[]${opt}${res}${ptr}${elem_type_sym.name}' } @@ -891,7 +891,7 @@ pub fn (t &Table) array_name(elem_type Type) string { pub fn (t &Table) array_cname(elem_type Type) string { elem_type_sym := t.sym(elem_type) suffix := if elem_type.is_ptr() { '_ptr'.repeat(elem_type.nr_muls()) } else { '' } - opt := if elem_type.has_flag(.optional) { '_option_' } else { '' } + opt := if elem_type.has_flag(.option) { '_option_' } else { '' } res := if elem_type.has_flag(.result) { '_result_' } else { '' } if elem_type_sym.cname.contains('[') { type_name := elem_type_sym.cname.replace_each(['[', '_T_', ', ', '_', ']', '']) @@ -907,7 +907,7 @@ pub fn (t &Table) array_cname(elem_type Type) string { pub fn (t &Table) array_fixed_name(elem_type Type, size int, size_expr Expr) string { elem_type_sym := t.sym(elem_type) ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' } - opt := if elem_type.has_flag(.optional) { '?' } else { '' } + opt := if elem_type.has_flag(.option) { '?' } else { '' } res := if elem_type.has_flag(.result) { '!' } else { '' } size_str := if size_expr is EmptyExpr || size != 987654321 { size.str() @@ -921,7 +921,7 @@ pub fn (t &Table) array_fixed_name(elem_type Type, size int, size_expr Expr) str pub fn (t &Table) array_fixed_cname(elem_type Type, size int) string { elem_type_sym := t.sym(elem_type) suffix := if elem_type.is_ptr() { '_ptr${elem_type.nr_muls()}' } else { '' } - opt := if elem_type.has_flag(.optional) { '_option_' } else { '' } + opt := if elem_type.has_flag(.option) { '_option_' } else { '' } res := if elem_type.has_flag(.result) { '_result_' } else { '' } if elem_type_sym.cname.contains('[') { type_name := elem_type_sym.cname.replace_each(['[', '_T_', ', ', '_', ']', '']) @@ -978,7 +978,7 @@ pub fn (t &Table) promise_cname(return_type Type) string { [inline] pub fn (t &Table) thread_name(return_type Type) string { if return_type.idx() == void_type_idx { - if return_type.has_flag(.optional) { + if return_type.has_flag(.option) { return 'thread ?' } else if return_type.has_flag(.result) { return 'thread !' @@ -988,7 +988,7 @@ pub fn (t &Table) thread_name(return_type Type) string { } return_type_sym := t.sym(return_type) ptr := if return_type.is_ptr() { '&' } else { '' } - opt := if return_type.has_flag(.optional) { '?' } else { '' } + opt := if return_type.has_flag(.option) { '?' } else { '' } res := if return_type.has_flag(.result) { '!' } else { '' } return 'thread ${opt}${res}${ptr}${return_type_sym.name}' } @@ -996,7 +996,7 @@ pub fn (t &Table) thread_name(return_type Type) string { [inline] pub fn (t &Table) thread_cname(return_type Type) string { if return_type == void_type { - if return_type.has_flag(.optional) { + if return_type.has_flag(.option) { return '__v_thread_Option_void' } else if return_type.has_flag(.result) { return '__v_thread_Result_void' @@ -1006,7 +1006,7 @@ pub fn (t &Table) thread_cname(return_type Type) string { } return_type_sym := t.sym(return_type) suffix := if return_type.is_ptr() { '_ptr' } else { '' } - opt := if return_type.has_flag(.optional) { '_option_' } else { '' } + opt := if return_type.has_flag(.option) { '_option_' } else { '' } res := if return_type.has_flag(.result) { '_result_' } else { '' } return '__v_thread_${opt}${res}${return_type_sym.cname}${suffix}' } @@ -1018,7 +1018,7 @@ pub fn (t &Table) map_name(key_type Type, value_type Type) string { key_type_sym := t.sym(key_type) value_type_sym := t.sym(value_type) ptr := if value_type.is_ptr() { '&'.repeat(value_type.nr_muls()) } else { '' } - opt := if value_type.has_flag(.optional) { '?' } else { '' } + opt := if value_type.has_flag(.option) { '?' } else { '' } res := if value_type.has_flag(.result) { '!' } else { '' } return 'map[${key_type_sym.name}]${opt}${res}${ptr}${value_type_sym.name}' } @@ -1028,7 +1028,7 @@ pub fn (t &Table) map_cname(key_type Type, value_type Type) string { key_type_sym := t.sym(key_type) value_type_sym := t.sym(value_type) suffix := if value_type.is_ptr() { '_ptr'.repeat(value_type.nr_muls()) } else { '' } - opt := if value_type.has_flag(.optional) { '_option_' } else { '' } + opt := if value_type.has_flag(.option) { '_option_' } else { '' } res := if value_type.has_flag(.result) { '_result_' } else { '' } if value_type_sym.cname.contains('[') { type_name := value_type_sym.cname.replace_each(['[', '_T_', ', ', '_', ']', '']) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index f6b27dc50..9a1d40436 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -105,7 +105,7 @@ pub mut: // max of 8 pub enum TypeFlag { - optional + option result variadic generic @@ -280,8 +280,8 @@ pub fn (t Type) debug() []string { res << 'idx: 0x${t.idx().hex():-8}' res << 'type: 0x${t.hex():-8}' res << 'nr_muls: ${t.nr_muls()}' - if t.has_flag(.optional) { - res << 'optional' + if t.has_flag(.option) { + res << 'option' } if t.has_flag(.result) { res << 'result' @@ -486,7 +486,7 @@ pub const ( pub const ( void_type = new_type(void_type_idx) - ovoid_type = new_type(void_type_idx).set_flag(.optional) // the return type of `fn ()?` + ovoid_type = new_type(void_type_idx).set_flag(.option) // the return type of `fn ()?` rvoid_type = new_type(void_type_idx).set_flag(.result) // the return type of `fn () !` voidptr_type = new_type(voidptr_type_idx) byteptr_type = new_type(byteptr_type_idx) @@ -569,7 +569,7 @@ pub mut: // returns TypeSymbol kind only if there are no type modifiers pub fn (t &Table) type_kind(typ Type) Kind { - if typ.nr_muls() > 0 || typ.has_flag(.optional) || typ.has_flag(.result) { + if typ.nr_muls() > 0 || typ.has_flag(.option) || typ.has_flag(.result) { return Kind.placeholder } return t.sym(typ).kind @@ -866,7 +866,7 @@ pub fn (t &TypeSymbol) is_builtin() bool { // type_size returns the size and alignment (in bytes) of `typ`, similarly to C's `sizeof()` and `alignof()`. pub fn (t &Table) type_size(typ Type) (int, int) { - if typ.has_flag(.optional) || typ.has_flag(.result) { + if typ.has_flag(.option) || typ.has_flag(.result) { return t.type_size(ast.error_type_idx) } if typ.nr_muls() > 0 { @@ -1328,7 +1328,7 @@ pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string] res += ']' } .void { - if typ.has_flag(.optional) { + if typ.has_flag(.option) { res = '?' return res } @@ -1369,7 +1369,7 @@ pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string] if nr_muls > 0 && !typ.has_flag(.variadic) { res = strings.repeat(`&`, nr_muls) + res } - if typ.has_flag(.optional) { + if typ.has_flag(.option) { res = '?${res}' } if typ.has_flag(.result) { diff --git a/vlib/v/ast/types_test.v b/vlib/v/ast/types_test.v index 6d0b60edd..ea024c797 100644 --- a/vlib/v/ast/types_test.v +++ b/vlib/v/ast/types_test.v @@ -37,30 +37,30 @@ fn test_flags() { mut t := ast.new_type(ast.void_type_idx) idx := t.idx() nr_muls := t.nr_muls() - t = t.set_flag(ast.TypeFlag.optional) - assert t.has_flag(ast.TypeFlag.optional) == true + t = t.set_flag(ast.TypeFlag.option) + assert t.has_flag(ast.TypeFlag.option) == true assert t.has_flag(ast.TypeFlag.variadic) == false assert t.has_flag(ast.TypeFlag.generic) == false t = t.set_flag(ast.TypeFlag.variadic) - assert t.has_flag(ast.TypeFlag.optional) == true + assert t.has_flag(ast.TypeFlag.option) == true assert t.has_flag(ast.TypeFlag.variadic) == true assert t.has_flag(ast.TypeFlag.generic) == false t = t.set_flag(ast.TypeFlag.generic) - assert t.has_flag(ast.TypeFlag.optional) == true + assert t.has_flag(ast.TypeFlag.option) == true assert t.has_flag(ast.TypeFlag.variadic) == true assert t.has_flag(ast.TypeFlag.generic) == true assert t.idx() == idx assert t.nr_muls() == nr_muls - t = t.clear_flag(ast.TypeFlag.optional) - assert t.has_flag(ast.TypeFlag.optional) == false + t = t.clear_flag(ast.TypeFlag.option) + assert t.has_flag(ast.TypeFlag.option) == false assert t.has_flag(ast.TypeFlag.variadic) == true assert t.has_flag(ast.TypeFlag.generic) == true t = t.clear_flag(ast.TypeFlag.variadic) - assert t.has_flag(ast.TypeFlag.optional) == false + assert t.has_flag(ast.TypeFlag.option) == false assert t.has_flag(ast.TypeFlag.variadic) == false assert t.has_flag(ast.TypeFlag.generic) == true t = t.clear_flag(ast.TypeFlag.generic) - assert t.has_flag(ast.TypeFlag.optional) == false + assert t.has_flag(ast.TypeFlag.option) == false assert t.has_flag(ast.TypeFlag.variadic) == false assert t.has_flag(ast.TypeFlag.generic) == false assert t.idx() == idx @@ -74,7 +74,7 @@ fn test_derive() { t = t.set_nr_muls(10) mut t2 := ast.new_type(ast.i16_type_idx) t2 = t2.derive(t) - assert t2.has_flag(ast.TypeFlag.optional) == false + assert t2.has_flag(ast.TypeFlag.option) == false assert t2.has_flag(ast.TypeFlag.variadic) == true assert t2.has_flag(ast.TypeFlag.generic) == true assert t2.nr_muls() == 10 diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 16658ac26..c2363644a 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -130,7 +130,7 @@ fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool { } } // allow direct int-literal assignment for pointers for now - // maybe in the future optionals should be used for that + // maybe in the future options should be used for that if expected.is_real_pointer() { if got == ast.int_literal_type { return true @@ -146,15 +146,15 @@ fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool { if expected == ast.charptr_type && got == ast.char_type.ref() { return true } - if expected.has_flag(.optional) || expected.has_flag(.result) { + if expected.has_flag(.option) || expected.has_flag(.result) { sym := c.table.sym(got) if ((sym.idx == ast.error_type_idx || got in [ast.none_type, ast.error_type]) - && expected.has_flag(.optional)) + && expected.has_flag(.option)) || ((sym.idx == ast.error_type_idx || got == ast.error_type) && expected.has_flag(.result)) { // IError return true - } else if !c.check_basic(got, expected.clear_flag(.optional).clear_flag(.result)) { + } else if !c.check_basic(got, expected.clear_flag(.option).clear_flag(.result)) { return false } } @@ -402,7 +402,7 @@ fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSymbol, if got_fn.params.len != exp_fn.params.len { return false } - if got_fn.return_type.has_flag(.optional) != exp_fn.return_type.has_flag(.optional) { + if got_fn.return_type.has_flag(.option) != exp_fn.return_type.has_flag(.option) { return false } if got_fn.return_type.has_flag(.result) != exp_fn.return_type.has_flag(.result) { @@ -581,7 +581,7 @@ fn (mut c Checker) promote(left_type ast.Type, right_type ast.Type) ast.Type { } if right_type.is_number() && left_type.is_number() { return c.promote_num(left_type, right_type) - } else if left_type.has_flag(.optional) != right_type.has_flag(.optional) { + } else if left_type.has_flag(.option) != right_type.has_flag(.option) { // incompatible return ast.void_type } else { @@ -645,7 +645,7 @@ fn (c &Checker) expected_msg(got ast.Type, expected ast.Type) string { fn (mut c Checker) symmetric_check(left ast.Type, right ast.Type) bool { // allow direct int-literal assignment for pointers for now - // maybe in the future optionals should be used for that + // maybe in the future options should be used for that if right.is_ptr() || right.is_pointer() { if left == ast.int_literal_type { return true diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1b7261305..be7061e02 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -920,23 +920,23 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to return true } -// return the actual type of the expression, once the optional is handled +// return the actual type of the expression, once the option is handled fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Type { if expr is ast.CallExpr { mut expr_ret_type := expr.return_type if expr_ret_type != 0 && c.table.sym(expr_ret_type).kind == .alias { unaliased_ret_type := c.table.unaliased_type(expr_ret_type) - if unaliased_ret_type.has_flag(.optional) || unaliased_ret_type.has_flag(.result) { + if unaliased_ret_type.has_flag(.option) || unaliased_ret_type.has_flag(.result) { expr_ret_type = unaliased_ret_type } } - if expr_ret_type.has_flag(.optional) || expr_ret_type.has_flag(.result) { - return_modifier_kind := if expr_ret_type.has_flag(.optional) { + if expr_ret_type.has_flag(.option) || expr_ret_type.has_flag(.result) { + return_modifier_kind := if expr_ret_type.has_flag(.option) { 'an option' } else { 'a result' } - return_modifier := if expr_ret_type.has_flag(.optional) { '?' } else { '!' } + return_modifier := if expr_ret_type.has_flag(.option) { '?' } else { '!' } if expr.or_block.kind == .absent { if c.inside_defer { c.error('${expr.name}() returns ${return_modifier_kind}, so it should have an `or {}` block at the end', @@ -948,25 +948,25 @@ fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Typ } else { c.check_or_expr(expr.or_block, ret_type, expr_ret_type) } - return ret_type.clear_flag(.optional).clear_flag(.result) + return ret_type.clear_flag(.option).clear_flag(.result) } else if expr.or_block.kind == .block { - c.error('unexpected `or` block, the function `${expr.name}` does neither return an optional nor a result', + c.error('unexpected `or` block, the function `${expr.name}` does not return an option or a result', expr.or_block.pos) } else if expr.or_block.kind == .propagate_option { - c.error('unexpected `?`, the function `${expr.name}` does not return an optional', + c.error('unexpected `?`, the function `${expr.name}` does not return an option', expr.or_block.pos) } else if expr.or_block.kind == .propagate_result { c.error('unexpected `!`, the function `${expr.name}` does not return a result', expr.or_block.pos) } } else if expr is ast.SelectorExpr && c.table.sym(ret_type).kind != .chan { - if expr.typ.has_flag(.optional) || expr.typ.has_flag(.result) { - with_modifier_kind := if expr.typ.has_flag(.optional) { + if expr.typ.has_flag(.option) || expr.typ.has_flag(.result) { + with_modifier_kind := if expr.typ.has_flag(.option) { 'an option' } else { 'a result' } - with_modifier := if expr.typ.has_flag(.optional) { '?' } else { '!' } + with_modifier := if expr.typ.has_flag(.option) { '?' } else { '!' } if expr.or_block.kind == .absent { if c.inside_defer { c.error('field `${expr.field_name}` is ${with_modifier_kind}, so it should have an `or {}` block at the end', @@ -978,12 +978,12 @@ fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Typ } else { c.check_or_expr(expr.or_block, ret_type, expr.typ) } - return ret_type.clear_flag(.optional).clear_flag(.result) + return ret_type.clear_flag(.option).clear_flag(.result) } else if expr.or_block.kind == .block { - c.error('unexpected `or` block, the field `${expr.field_name}` is neither an optional, nor a result', + c.error('unexpected `or` block, the field `${expr.field_name}` is neither an option, nor a result', expr.or_block.pos) } else if expr.or_block.kind == .propagate_option { - c.error('unexpected `?`, the field `${expr.field_name}` is not an optional', + c.error('unexpected `?`, the field `${expr.field_name}` is not an option', expr.or_block.pos) } else if expr.or_block.kind == .propagate_result { c.error('unexpected `!`, result fields are not supported', expr.or_block.pos) @@ -998,18 +998,18 @@ fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Typ fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_return_type ast.Type) { if node.kind == .propagate_option { - if c.table.cur_fn != unsafe { nil } && !c.table.cur_fn.return_type.has_flag(.optional) + if c.table.cur_fn != unsafe { nil } && !c.table.cur_fn.return_type.has_flag(.option) && !c.table.cur_fn.is_main && !c.table.cur_fn.is_test && !c.inside_const { - c.add_instruction_for_optional_type() - c.error('to propagate the call, `${c.table.cur_fn.name}` must return an optional type', + c.add_instruction_for_option_type() + c.error('to propagate the call, `${c.table.cur_fn.name}` must return an option type', node.pos) } - if !expr_return_type.has_flag(.optional) { + if !expr_return_type.has_flag(.option) { if expr_return_type.has_flag(.result) { c.warn('propagating a result like an option is deprecated, use `foo()!` instead of `foo()?`', node.pos) } else { - c.error('to propagate an option, the call must also return an optional type', + c.error('to propagate an option, the call must also return an option type', node.pos) } } @@ -1038,7 +1038,7 @@ fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_return return } last_stmt := node.stmts.last() - c.check_or_last_stmt(last_stmt, ret_type, expr_return_type.clear_flag(.optional).clear_flag(.result)) + c.check_or_last_stmt(last_stmt, ret_type, expr_return_type.clear_flag(.option).clear_flag(.result)) } fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_return_type ast.Type) { @@ -1046,7 +1046,7 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret match stmt { ast.ExprStmt { c.expected_type = ret_type - c.expected_or_type = ret_type.clear_flag(.optional).clear_flag(.result) + c.expected_or_type = ret_type.clear_flag(.option).clear_flag(.result) last_stmt_typ := c.expr(stmt.expr) c.expected_or_type = ast.void_type type_fits := c.check_types(last_stmt_typ, ret_type) @@ -1071,7 +1071,7 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret } return } - expected_type_name := c.table.type_to_str(ret_type.clear_flag(.optional).clear_flag(.result)) + expected_type_name := c.table.type_to_str(ret_type.clear_flag(.option).clear_flag(.result)) c.error('`or` block must provide a default value of type `${expected_type_name}`, or return/continue/break or call a [noreturn] function like panic(err) or exit(1)', stmt.expr.pos()) } else { @@ -1080,7 +1080,7 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret return } type_name := c.table.type_to_str(last_stmt_typ) - expected_type_name := c.table.type_to_str(ret_type.clear_flag(.optional).clear_flag(.result)) + expected_type_name := c.table.type_to_str(ret_type.clear_flag(.option).clear_flag(.result)) c.error('wrong return type `${type_name}` in the `or {}` block, expected `${expected_type_name}`', stmt.expr.pos()) } @@ -1094,7 +1094,7 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret } ast.Return {} else { - expected_type_name := c.table.type_to_str(ret_type.clear_flag(.optional).clear_flag(.result)) + expected_type_name := c.table.type_to_str(ret_type.clear_flag(.option).clear_flag(.result)) c.error('last statement in the `or {}` block should be an expression of type `${expected_type_name}` or exit parent scope', stmt.pos) } @@ -1219,8 +1219,8 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type { } node.expr_type = typ if !(node.expr is ast.Ident && (node.expr as ast.Ident).kind == .constant) { - if node.expr_type.has_flag(.optional) { - c.error('cannot access fields of an optional, handle the error with `or {...}` or propagate it with `?`', + if node.expr_type.has_flag(.option) { + c.error('cannot access fields of an option, handle the error with `or {...}` or propagate it with `?`', node.pos) } else if node.expr_type.has_flag(.result) { c.error('cannot access fields of a result, handle the error with `or {...}` or propagate it with `!`', @@ -1337,7 +1337,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type { } node.typ = field.typ if node.or_block.kind == .block { - c.expected_or_type = node.typ.clear_flag(.optional).clear_flag(.result) + c.expected_or_type = node.typ.clear_flag(.option).clear_flag(.result) c.stmts_ending_with_expression(node.or_block.stmts) c.expected_or_type = ast.void_type } @@ -2338,25 +2338,25 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type { mut ret_type := c.call_expr(mut node) if ret_type != 0 && c.table.sym(ret_type).kind == .alias { unaliased_type := c.table.unaliased_type(ret_type) - if unaliased_type.has_flag(.optional) || unaliased_type.has_flag(.result) { + if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) { ret_type = unaliased_type } } - if !ret_type.has_flag(.optional) && !ret_type.has_flag(.result) { + if !ret_type.has_flag(.option) && !ret_type.has_flag(.result) { if node.or_block.kind == .block { - c.error('unexpected `or` block, the function `${node.name}` does neither return an optional nor a result', + c.error('unexpected `or` block, the function `${node.name}` does not return an option or a result', node.or_block.pos) } else if node.or_block.kind == .propagate_option { - c.error('unexpected `?`, the function `${node.name}` does neither return an optional nor a result', + c.error('unexpected `?`, the function `${node.name}` does not return an option or a result', node.or_block.pos) } else if node.or_block.kind == .propagate_result { - c.error('unexpected `!`, the function `${node.name}` does neither return an optional nor a result', + c.error('unexpected `!`, the function `${node.name}` does not return an option or a result', node.or_block.pos) } } if node.or_block.kind != .absent { - if ret_type.has_flag(.optional) { - ret_type = ret_type.clear_flag(.optional) + if ret_type.has_flag(.option) { + ret_type = ret_type.clear_flag(.option) } if ret_type.has_flag(.result) { ret_type = ret_type.clear_flag(.result) @@ -2401,7 +2401,7 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type { unwrapped_expr_type := c.unwrap_generic(node.expr_type) tsym := c.table.sym(unwrapped_expr_type) - c.table.dumps[int(unwrapped_expr_type.clear_flag(.optional).clear_flag(.result))] = tsym.cname + c.table.dumps[int(unwrapped_expr_type.clear_flag(.option).clear_flag(.result))] = tsym.cname node.cname = tsym.cname return node.expr_type } @@ -2425,18 +2425,18 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type { c.inside_if_guard = true node.expr_type = c.expr(node.expr) c.inside_if_guard = old_inside_if_guard - if !node.expr_type.has_flag(.optional) && !node.expr_type.has_flag(.result) { + if !node.expr_type.has_flag(.option) && !node.expr_type.has_flag(.result) { mut no_opt_or_res := true match mut node.expr { ast.IndexExpr { no_opt_or_res = false - node.expr_type = node.expr_type.set_flag(.optional) + node.expr_type = node.expr_type.set_flag(.option) node.expr.is_option = true } ast.PrefixExpr { if node.expr.op == .arrow { no_opt_or_res = false - node.expr_type = node.expr_type.set_flag(.optional) + node.expr_type = node.expr_type.set_flag(.option) node.expr.is_option = true } } @@ -2519,21 +2519,21 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type { return ret_type } - if !ret_type.has_flag(.optional) && !ret_type.has_flag(.result) { + if !ret_type.has_flag(.option) && !ret_type.has_flag(.result) { if node.or_block.kind == .block { - c.error('unexpected `or` block, the field `${node.field_name}` is neither an optional, nor a result', + c.error('unexpected `or` block, the field `${node.field_name}` is neither an option, nor a result', node.or_block.pos) } else if node.or_block.kind == .propagate_option { - c.error('unexpected `?`, the field `${node.field_name}` is neither an optional, nor a result', + c.error('unexpected `?`, the field `${node.field_name}` is neither an option, nor a result', node.or_block.pos) } else if node.or_block.kind == .propagate_result { - c.error('unexpected `!`, the field `${node.field_name}` is neither an optional, nor a result', + c.error('unexpected `!`, the field `${node.field_name}` is neither an option, nor a result', node.or_block.pos) } } if node.or_block.kind != .absent { - if ret_type.has_flag(.optional) { - ret_type = ret_type.clear_flag(.optional) + if ret_type.has_flag(.option) { + ret_type = ret_type.clear_flag(.option) } if ret_type.has_flag(.result) { ret_type = ret_type.clear_flag(.result) @@ -2641,8 +2641,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { mut to_sym := c.table.sym(to_type) // type to be used as cast mut final_to_sym := c.table.final_sym(to_type) - if to_type.has_flag(.optional) { - c.error('casting to optional type is forbidden', node.pos) + if to_type.has_flag(.option) { + c.error('casting to option type is forbidden', node.pos) } else if to_type.has_flag(.result) { c.error('casting to result type is forbidden', node.pos) } @@ -2674,7 +2674,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { node.expr_type = c.promote_num(node.expr_type, xx) from_type = node.expr_type } - if !c.table.sumtype_has_variant(to_type, from_type, false) && !to_type.has_flag(.optional) + if !c.table.sumtype_has_variant(to_type, from_type, false) && !to_type.has_flag(.option) && !to_type.has_flag(.result) { ft := c.table.type_to_str(from_type) tt := c.table.type_to_str(to_type) @@ -2736,8 +2736,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { } else if to_type == ast.bool_type && from_type != ast.bool_type && !c.inside_unsafe && !c.pref.translated && !c.file.is_translated { c.error('cannot cast to bool - use e.g. `some_int != 0` instead', node.pos) - } else if from_type == ast.none_type && !to_type.has_flag(.optional) - && !to_type.has_flag(.result) { + } else if from_type == ast.none_type && !to_type.has_flag(.option) && !to_type.has_flag(.result) { type_name := c.table.type_to_str(to_type) c.error('cannot cast `none` to `${type_name}`', node.pos) } else if from_sym.kind == .struct_ && !from_type.is_ptr() { @@ -2751,11 +2750,11 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { ft := c.table.type_to_str(from_type) tt := c.table.type_to_str(to_type) c.error('cannot cast type `${ft}` to `${tt}`', node.pos) - } else if from_type.has_flag(.optional) || from_type.has_flag(.result) + } else if from_type.has_flag(.option) || from_type.has_flag(.result) || from_type.has_flag(.variadic) { // variadic case can happen when arrays are converted into variadic - msg := if from_type.has_flag(.optional) { - 'an optional' + msg := if from_type.has_flag(.option) { + 'an option' } else if from_type.has_flag(.result) { 'a result' } else { @@ -3083,7 +3082,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { } } if mut obj.expr is ast.IfGuardExpr { - // new variable from if guard shouldn't have the optional flag for further use + // new variable from if guard shouldn't have the option flag for further use // a temp variable will be generated which unwraps it sym := c.table.sym(obj.expr.expr_type) if sym.kind == .multi_return { @@ -3096,7 +3095,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { } } } else { - typ = obj.expr.expr_type.clear_flag(.optional).clear_flag(.result) + typ = obj.expr.expr_type.clear_flag(.option).clear_flag(.result) } } else if obj.expr is ast.EmptyExpr { c.error('invalid variable `${node.name}`', node.pos) @@ -3105,19 +3104,19 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { typ = c.expr(obj.expr) } } - is_optional := typ.has_flag(.optional) || typ.has_flag(.result) + is_option := typ.has_flag(.option) || typ.has_flag(.result) node.kind = .variable node.info = ast.IdentVar{ typ: typ - is_optional: is_optional + is_option: is_option } if !is_sum_type_cast { obj.typ = typ } node.obj = obj - // unwrap optional (`println(x)`) - if is_optional { - return typ.clear_flag(.optional).clear_flag(.result) + // unwrap option (`println(x)`) + if is_option { + return typ.clear_flag(.option).clear_flag(.result) } return typ } @@ -3158,7 +3157,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { if mut obj.expr is ast.CallExpr { if obj.expr.or_block.kind != .absent { - typ = typ.clear_flag(.optional).clear_flag(.result) + typ = typ.clear_flag(.option).clear_flag(.result) } } } @@ -3613,8 +3612,8 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type { node.pos.extend(node.right.pos)) } - if node.right.typ.has_flag(.optional) { - c.error('cannot take the address of an optional field', node.pos.extend(node.right.pos)) + if node.right.typ.has_flag(.option) { + c.error('cannot take the address of an option field', node.pos.extend(node.right.pos)) } } } @@ -3742,13 +3741,13 @@ fn (mut c Checker) check_index(typ_sym &ast.TypeSymbol, index ast.Expr, index_ty } } } - if index_type.has_flag(.optional) || index_type.has_flag(.result) { + if index_type.has_flag(.option) || index_type.has_flag(.result) { type_str := if typ_sym.kind == .string { '(type `${typ_sym.name}`)' } else { '(array type `${typ_sym.name}`)' } - c.error('cannot use optional or result as index ${type_str}', pos) + c.error('cannot use option or result as index ${type_str}', pos) } } } @@ -3789,10 +3788,11 @@ fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type { && typ !in [ast.byteptr_type, ast.charptr_type] && !typ.has_flag(.variadic) { c.error('type `${typ_sym.name}` does not support indexing', node.pos) } - if typ.has_flag(.optional) { - c.error('type `?${typ_sym.name}` is optional, it does not support indexing', node.left.pos()) + if typ.has_flag(.option) { + c.error('type `?${typ_sym.name}` is an option, it does not support indexing', + node.left.pos()) } else if typ.has_flag(.result) { - c.error('type `!${typ_sym.name}` is result, it does not support indexing', node.left.pos()) + c.error('type `!${typ_sym.name}` is a result, it does not support indexing', node.left.pos()) } if typ_sym.kind == .string && !typ.is_ptr() && node.is_setter { c.error('cannot assign to s[i] since V strings are immutable\n' + @@ -4004,7 +4004,7 @@ fn (mut c Checker) add_error_detail_with_pos(msg string, pos token.Pos) { c.add_error_detail(util.formatted_error('details:', msg, c.file.path, pos)) } -fn (mut c Checker) add_instruction_for_optional_type() { +fn (mut c Checker) add_instruction_for_option_type() { c.add_error_detail_with_pos('prepend ? before the declaration of the return type of `${c.table.cur_fn.name}`', c.table.cur_fn.return_type_pos) } diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 597fc1954..a7a7c128d 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -188,7 +188,7 @@ fn (mut c Checker) comptime_for(node ast.ComptimeFor) { unwrapped_expr_type := c.unwrap_generic(field.typ) tsym := c.table.sym(unwrapped_expr_type) - c.table.dumps[int(unwrapped_expr_type.clear_flag(.optional).clear_flag(.result).clear_flag(.atomic_f))] = tsym.cname + c.table.dumps[int(unwrapped_expr_type.clear_flag(.option).clear_flag(.result).clear_flag(.atomic_f))] = tsym.cname } c.comptime_for_field_var = '' c.inside_comptime_for_field = false @@ -445,7 +445,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool { if node.ct_expr is ast.Ident { if node.ct_opt { if node.ct_expr.name in constants.valid_comptime_not_user_defined { - c.error('optional `[if expression ?]` tags, can be used only for user defined identifiers', + c.error('option `[if expression ?]` tags, can be used only for user defined identifiers', node.pos) node.ct_skip = true } else { @@ -765,7 +765,7 @@ fn (mut c Checker) check_comptime_is_field_selector(node ast.SelectorExpr) bool [inline] fn (mut c Checker) check_comptime_is_field_selector_bool(node ast.SelectorExpr) bool { if c.check_comptime_is_field_selector(node) { - return node.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_optional', + return node.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias'] } return false @@ -781,7 +781,7 @@ fn (mut c Checker) get_comptime_selector_bool_field(field_name string) bool { 'is_mut' { return field.is_mut } 'is_shared' { return field_typ.has_flag(.shared_f) } 'is_atomic' { return field_typ.has_flag(.atomic_f) } - 'is_optional' { return field.typ.has_flag(.optional) } + 'is_option' { return field.typ.has_flag(.option) } 'is_array' { return field_sym.kind in [.array, .array_fixed] } 'is_map' { return field_sym.kind == .map } 'is_chan' { return field_sym.kind == .chan } diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index 814390dad..8d866664f 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -116,12 +116,12 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { // } array_info := type_sym.array_info() node.elem_type = array_info.elem_type - // clear optional flag incase of: `fn opt_arr() ?[]int { return [] }` + // clear option flag incase of: `fn opt_arr() ?[]int { return [] }` return if c.expected_type.has_flag(.shared_f) { c.expected_type.clear_flag(.shared_f).deref() } else { c.expected_type - }.clear_flag(.optional).clear_flag(.result) + }.clear_flag(.option).clear_flag(.result) } // [1,2,3] if node.exprs.len > 0 && node.elem_type == ast.void_type { @@ -282,7 +282,7 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type { sym := c.table.sym(c.expected_type) if sym.kind == .map { info := sym.map_info() - node.typ = c.expected_type.clear_flag(.optional).clear_flag(.result) + node.typ = c.expected_type.clear_flag(.option).clear_flag(.result) node.key_type = info.key_type node.value_type = info.value_type return node.typ diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 059a664ac..8b1958278 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -117,7 +117,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { if multi_type == ast.error_type { c.error('type `IError` cannot be used in multi-return, return an option instead', node.return_type_pos) - } else if multi_type.has_flag(.optional) { + } else if multi_type.has_flag(.option) { c.error('option cannot be used in multi-return, return an option instead', node.return_type_pos) } else if multi_type.has_flag(.result) { @@ -211,9 +211,8 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.error('invalid use of reserved type `${param.name}` as a parameter name', param.pos) } - if param.typ.has_flag(.optional) || param.typ.has_flag(.result) { - c.error('optional or result type argument is not supported currently', - param.type_pos) + if param.typ.has_flag(.option) || param.typ.has_flag(.result) { + c.error('option or result type argument is not supported currently', param.type_pos) } arg_typ_sym := c.table.sym(param.typ) if arg_typ_sym.info is ast.Struct { @@ -333,7 +332,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { } if node.return_type != ast.void_type_idx - && node.return_type.clear_flag(.optional) != ast.void_type_idx + && node.return_type.clear_flag(.option) != ast.void_type_idx && node.return_type.clear_flag(.result) != ast.void_type_idx { c.error('test functions should either return nothing at all, or be marked to return `?` or `!`', node.pos) @@ -343,7 +342,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.table.cur_fn = unsafe { node } // c.table.cur_fn = node // Add return if `fn(...) ? {...}` have no return at end - if node.return_type != ast.void_type && node.return_type.has_flag(.optional) + if node.return_type != ast.void_type && node.return_type.has_flag(.option) && (node.stmts.len == 0 || node.stmts.last() !is ast.Return) { sym := c.table.sym(node.return_type) if sym.kind == .void { @@ -476,7 +475,7 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type { c.inside_fn_arg = old_inside_fn_arg // autofree: mark args that have to be freed (after saving them in tmp exprs) free_tmp_arg_vars := c.pref.autofree && !c.is_builtin_mod && node.args.len > 0 - && !node.args[0].typ.has_flag(.optional) && !node.args[0].typ.has_flag(.result) + && !node.args[0].typ.has_flag(.option) && !node.args[0].typ.has_flag(.result) if free_tmp_arg_vars && !c.inside_const { for i, arg in node.args { if arg.typ != ast.string_type { @@ -497,7 +496,7 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type { node.free_receiver = true } } - c.expected_or_type = node.return_type.clear_flag(.optional).clear_flag(.result) + c.expected_or_type = node.return_type.clear_flag(.option).clear_flag(.result) c.stmts_ending_with_expression(node.or_block.stmts) c.expected_or_type = ast.void_type @@ -505,15 +504,14 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type { && !c.table.cur_fn.is_test { // TODO: use just `if node.or_block.kind == .propagate_result && !c.table.cur_fn.return_type.has_flag(.result) {` after the deprecation for ?!Type if node.or_block.kind == .propagate_result && !c.table.cur_fn.return_type.has_flag(.result) - && !c.table.cur_fn.return_type.has_flag(.optional) { + && !c.table.cur_fn.return_type.has_flag(.option) { c.add_instruction_for_result_type() c.error('to propagate the result call, `${c.table.cur_fn.name}` must return a result', node.or_block.pos) } - if node.or_block.kind == .propagate_option - && !c.table.cur_fn.return_type.has_flag(.optional) { - c.add_instruction_for_optional_type() - c.error('to propagate the optional call, `${c.table.cur_fn.name}` must return an optional', + if node.or_block.kind == .propagate_option && !c.table.cur_fn.return_type.has_flag(.option) { + c.add_instruction_for_option_type() + c.error('to propagate the option call, `${c.table.cur_fn.name}` must return an option', node.or_block.pos) } } @@ -611,7 +609,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. match tsym.info { ast.Struct { mut ret_type := tsym.info.concrete_types[0] - ret_type = ret_type.set_flag(.optional) + ret_type = ret_type.set_flag(.option) node.return_type = ret_type return ret_type } @@ -1328,8 +1326,8 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { } else { 'unknown method or field: `${left_sym.name}.${method_name}`' } - if left_type.has_flag(.optional) { - c.error('optional type cannot be called directly', node.left.pos()) + if left_type.has_flag(.option) { + c.error('option type cannot be called directly', node.left.pos()) return ast.void_type } else if left_type.has_flag(.result) { c.error('result type cannot be called directly', node.left.pos()) @@ -1401,7 +1399,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { c.table.cur_fn.has_await = true } node.return_type = info.concrete_types[0] - node.return_type.set_flag(.optional) + node.return_type.set_flag(.option) return node.return_type } else if left_sym.kind == .thread && method_name == 'wait' { info := left_sym.info as ast.Thread @@ -1960,7 +1958,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { fn (mut c Checker) go_expr(mut node ast.GoExpr) ast.Type { ret_type := c.call_expr(mut node.call_expr) if node.call_expr.or_block.kind != .absent { - c.error('optional handling cannot be done in `go` call. Do it when calling `.wait()`', + c.error('option handling cannot be done in `go` call. Do it when calling `.wait()`', node.call_expr.or_block.pos) } // Make sure there are no mutable arguments @@ -2072,8 +2070,8 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast arg_expr := node.args[0].expr match arg_expr { ast.AnonFn { - if arg_expr.decl.return_type.has_flag(.optional) { - c.error('optional needs to be unwrapped before using it in map/filter', + if arg_expr.decl.return_type.has_flag(.option) { + c.error('option needs to be unwrapped before using it in map/filter', node.args[0].pos) } if arg_expr.decl.params.len > 1 { @@ -2094,8 +2092,8 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast c.error('${arg_expr.name} does not exist', arg_expr.pos) return } - if func.return_type.has_flag(.optional) { - c.error('optional needs to be unwrapped before using it in map/filter', + if func.return_type.has_flag(.option) { + c.error('option needs to be unwrapped before using it in map/filter', node.pos) } if func.params.len > 1 { @@ -2114,8 +2112,8 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast expr := arg_expr.obj.expr if expr is ast.AnonFn { // copied from above - if expr.decl.return_type.has_flag(.optional) { - c.error('optional needs to be unwrapped before using it in map/filter', + if expr.decl.return_type.has_flag(.option) { + c.error('option needs to be unwrapped before using it in map/filter', arg_expr.pos) } if expr.decl.params.len > 1 { @@ -2145,9 +2143,9 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast c.error('type mismatch, `${arg_expr.name}` does not return anything', arg_expr.pos) } else if !is_map && arg_expr.return_type != ast.bool_type { - if arg_expr.or_block.kind != .absent && (arg_expr.return_type.has_flag(.optional) + if arg_expr.or_block.kind != .absent && (arg_expr.return_type.has_flag(.option) || arg_expr.return_type.has_flag(.result)) - && arg_expr.return_type.clear_flag(.optional).clear_flag(.result) == ast.bool_type { + && arg_expr.return_type.clear_flag(.option).clear_flag(.result) == ast.bool_type { return } c.error('type mismatch, `${arg_expr.name}` must return a bool', arg_expr.pos) @@ -2283,8 +2281,8 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as c.error('`.wait()` does not have any arguments', node.args[0].pos) } thread_ret_type := elem_sym.thread_info().return_type - if thread_ret_type.has_flag(.optional) { - c.error('`.wait()` cannot be called for an array when thread functions return optionals. Iterate over the arrays elements instead and handle each returned optional with `or`.', + if thread_ret_type.has_flag(.option) { + c.error('`.wait()` cannot be called for an array when thread functions return options. Iterate over the arrays elements instead and handle each returned option with `or`.', node.pos) } else if thread_ret_type.has_flag(.result) { c.error('`.wait()` cannot be called for an array when thread functions return results. Iterate over the arrays elements instead and handle each returned result with `or`.', diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index 2145db736..49958996b 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -17,7 +17,7 @@ fn (mut c Checker) for_c_stmt(node ast.ForCStmt) { for right in node.inc.right { if right is ast.CallExpr { if right.or_block.stmts.len > 0 { - c.error('optionals are not allowed in `for statement increment` (yet)', + c.error('options are not allowed in `for statement increment` (yet)', right.pos) } } @@ -98,8 +98,8 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { c.error('a struct must have a `next()` method to be an iterator', node.cond.pos()) return } - if !next_fn.return_type.has_flag(.optional) { - c.error('iterator method `next()` must return an optional', node.cond.pos()) + if !next_fn.return_type.has_flag(.option) { + c.error('iterator method `next()` must return an option', node.cond.pos()) } return_sym := c.table.sym(next_fn.return_type) if return_sym.kind == .multi_return { @@ -109,7 +109,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { if next_fn.params.len != 1 { c.error('iterator method `next()` must have 0 parameters', node.cond.pos()) } - mut val_type := next_fn.return_type.clear_flag(.optional).clear_flag(.result) + mut val_type := next_fn.return_type.clear_flag(.option).clear_flag(.result) if node.val_is_mut { val_type = val_type.ref() } diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 25af24245..2fc65f35d 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -47,7 +47,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { // check condition type is boolean c.expected_type = ast.bool_type cond_typ := c.unwrap_generic(c.expr(branch.cond)) - if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional) + if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.option) || cond_typ.has_flag(.result)) && !c.pref.translated && !c.file.is_translated { c.error('non-bool type `${c.table.type_to_str(cond_typ)}` used as if condition', branch.cond.pos()) @@ -202,7 +202,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { node.is_expr = true node.typ = c.expected_type } - if c.expected_type.has_flag(.optional) || c.expected_type.has_flag(.result) { + if c.expected_type.has_flag(.option) || c.expected_type.has_flag(.result) { if node.typ == ast.void_type { node.is_expr = true node.typ = c.expected_type diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 44846add7..9566cb44b 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -330,7 +330,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { left_name := c.table.type_to_str(unwrapped_left_type) right_name := c.table.type_to_str(unwrapped_right_type) c.error('mismatched types `${left_name}` and `${right_name}`', left_right_pos) - } else if promoted_type.has_flag(.optional) || promoted_type.has_flag(.result) { + } else if promoted_type.has_flag(.option) || promoted_type.has_flag(.result) { s := c.table.type_to_str(promoted_type) c.error('`${node.op}` cannot be used with `${s}`', node.pos) } else if promoted_type.is_float() { @@ -429,9 +429,9 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { } } } - } else if left_type.has_flag(.optional) || right_type.has_flag(.optional) { - opt_comp_pos := if left_type.has_flag(.optional) { left_pos } else { right_pos } - c.error('unwrapped optional cannot be compared in an infix expression', + } else if left_type.has_flag(.option) || right_type.has_flag(.option) { + opt_comp_pos := if left_type.has_flag(.option) { left_pos } else { right_pos } + c.error('unwrapped option cannot be compared in an infix expression', opt_comp_pos) } } @@ -652,11 +652,11 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { c.error('cannot use operator `${node.op}` with `${right_sym.name}`', node.pos) } // TODO move this to symmetric_check? Right now it would break `return 0` for `fn()?int ` - left_is_optional := left_type.has_flag(.optional) - right_is_optional := right_type.has_flag(.optional) - if left_is_optional || right_is_optional { - opt_infix_pos := if left_is_optional { left_pos } else { right_pos } - c.error('unwrapped optional cannot be used in an infix expression', opt_infix_pos) + left_is_option := left_type.has_flag(.option) + right_is_option := right_type.has_flag(.option) + if left_is_option || right_is_option { + opt_infix_pos := if left_is_option { left_pos } else { right_pos } + c.error('unwrapped option cannot be used in an infix expression', opt_infix_pos) } left_is_result := left_type.has_flag(.result) diff --git a/vlib/v/checker/interface.v b/vlib/v/checker/interface.v index 3eb318e7c..31e3538ae 100644 --- a/vlib/v/checker/interface.v +++ b/vlib/v/checker/interface.v @@ -268,8 +268,8 @@ fn (mut c Checker) resolve_generic_interface(typ ast.Type, interface_type ast.Ty } } else if c.table.get_type_name(imethod.return_type) == gt_name { mut ret_typ := method.return_type - if imethod.return_type.has_flag(.optional) { - ret_typ = ret_typ.clear_flag(.optional) + if imethod.return_type.has_flag(.option) { + ret_typ = ret_typ.clear_flag(.option) } else if imethod.return_type.has_flag(.result) { ret_typ = ret_typ.clear_flag(.result) } diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index f22bc0925..266b1425e 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -61,7 +61,7 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { expr_type := c.expr(stmt.expr) stmt.typ = expr_type if first_iteration { - if node.is_expr && (node.expected_type.has_flag(.optional) + if node.is_expr && (node.expected_type.has_flag(.option) || node.expected_type.has_flag(.result) || c.table.type_kind(node.expected_type) in [.sum_type, .multi_return]) { c.check_match_branch_last_stmt(stmt, node.expected_type, expr_type) diff --git a/vlib/v/checker/return.v b/vlib/v/checker/return.v index c096dca4a..2a1933813 100644 --- a/vlib/v/checker/return.v +++ b/vlib/v/checker/return.v @@ -14,7 +14,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { mut expected_type := c.unwrap_generic(c.expected_type) if expected_type != 0 && c.table.sym(expected_type).kind == .alias { unaliased_type := c.table.unaliased_type(expected_type) - if unaliased_type.has_flag(.optional) || unaliased_type.has_flag(.result) { + if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) { expected_type = unaliased_type } } @@ -32,7 +32,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { if node.exprs.len == 0 { return } - exp_is_optional := expected_type.has_flag(.optional) + exp_is_option := expected_type.has_flag(.option) exp_is_result := expected_type.has_flag(.result) mut expected_types := [expected_type] if expected_type_sym.info is ast.MultiReturn { @@ -92,18 +92,18 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { } } } - // allow `none` & `error` return types for function that returns optional + // allow `none` & `error` return types for function that returns option option_type_idx := c.table.type_idxs['_option'] result_type_idx := c.table.type_idxs['_result'] got_types_0_idx := got_types[0].idx() - if (exp_is_optional + if (exp_is_option && got_types_0_idx in [ast.none_type_idx, ast.error_type_idx, option_type_idx]) || (exp_is_result && got_types_0_idx in [ast.error_type_idx, result_type_idx]) { return } if expected_types.len > 0 && expected_types.len != got_types.len { // `fn foo() !(int, string) { return Err{} }` - if (exp_is_optional || exp_is_result) && node.exprs.len == 1 { + if (exp_is_option || exp_is_result) && node.exprs.len == 1 { got_typ := c.expr(node.exprs[0]) got_typ_sym := c.table.sym(got_typ) if got_typ_sym.kind == .struct_ && c.type_implements(got_typ, ast.error_type, node.pos) { @@ -126,7 +126,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { } for i, exp_type in expected_types { got_typ := c.unwrap_generic(got_types[i]) - if got_typ.has_flag(.optional) && (!exp_type.has_flag(.optional) + if got_typ.has_flag(.option) && (!exp_type.has_flag(.option) || c.table.type_to_str(got_typ) != c.table.type_to_str(exp_type)) { pos := node.exprs[expr_idxs[i]].pos() c.error('cannot use `${c.table.type_to_str(got_typ)}` as type `${c.table.type_to_str(exp_type)}` in return argument', @@ -228,7 +228,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { } } } - if exp_is_optional && node.exprs.len > 0 { + if exp_is_option && node.exprs.len > 0 { expr0 := node.exprs[0] if expr0 is ast.CallExpr { if expr0.or_block.kind == .propagate_option && node.exprs.len == 1 { diff --git a/vlib/v/checker/str.v b/vlib/v/checker/str.v index f5d99d5f8..8f394db2f 100644 --- a/vlib/v/checker/str.v +++ b/vlib/v/checker/str.v @@ -7,7 +7,7 @@ import v.ast import v.token fn (mut c Checker) get_default_fmt(ftyp ast.Type, typ ast.Type) u8 { - if ftyp.has_flag(.optional) || ftyp.has_flag(.result) { + if ftyp.has_flag(.option) || ftyp.has_flag(.result) { return `s` } else if typ.is_float() { return `g` @@ -32,7 +32,7 @@ fn (mut c Checker) get_default_fmt(ftyp ast.Type, typ ast.Type) u8 { } if ftyp in [ast.string_type, ast.bool_type] || sym.kind in [.enum_, .array, .array_fixed, .struct_, .map, .multi_return, .sum_type, .interface_, .none_] - || ftyp.has_flag(.optional) || ftyp.has_flag(.result) || sym.has_method('str') { + || ftyp.has_flag(.option) || ftyp.has_flag(.result) || sym.has_method('str') { return `s` } else { return `_` diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 979d41323..6111b8ed1 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -72,7 +72,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) { for i, field in node.fields { if field.typ.has_flag(.result) { - c.error('struct field does not support storing result', field.optional_pos) + c.error('struct field does not support storing result', field.option_pos) } c.ensure_type_exists(field.typ, field.type_pos) or { return } c.ensure_generic_type_specify_type_names(field.typ, field.type_pos) or { return } @@ -119,7 +119,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) { if field.has_default_expr { c.expected_type = field.typ default_expr_type := c.expr(field.default_expr) - if !field.typ.has_flag(.optional) && !field.typ.has_flag(.result) { + if !field.typ.has_flag(.option) && !field.typ.has_flag(.result) { c.check_expr_opt_call(field.default_expr, default_expr_type) } struct_sym.info.fields[i].default_expr_typ = default_expr_type @@ -463,7 +463,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type { if expr_type == ast.void_type { c.error('`${field.expr}` (no value) used as value', field.pos) } - if !field_info.typ.has_flag(.optional) && !field.typ.has_flag(.result) { + if !field_info.typ.has_flag(.option) && !field.typ.has_flag(.result) { expr_type = c.check_expr_opt_call(field.expr, expr_type) } expr_type_sym := c.table.sym(expr_type) diff --git a/vlib/v/checker/tests/aliased_option_fn_call_err.out b/vlib/v/checker/tests/aliased_option_fn_call_err.out new file mode 100644 index 000000000..73187c065 --- /dev/null +++ b/vlib/v/checker/tests/aliased_option_fn_call_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/aliased_option_fn_call_err.vv:8:10: error: foo() returns an option, so it should have either an `or {}` block, or `?` at the end + 6 | + 7 | fn main() { + 8 | println(foo()) + | ~~~~~ + 9 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/aliased_optional_fn_call_err.vv b/vlib/v/checker/tests/aliased_option_fn_call_err.vv similarity index 65% rename from vlib/v/checker/tests/aliased_optional_fn_call_err.vv rename to vlib/v/checker/tests/aliased_option_fn_call_err.vv index 7631dbbc9..c7de9d4db 100644 --- a/vlib/v/checker/tests/aliased_optional_fn_call_err.vv +++ b/vlib/v/checker/tests/aliased_option_fn_call_err.vv @@ -1,9 +1,9 @@ type OptStr = ?string -fn foo() OptStr{ +fn foo() OptStr { return 'abc' } -fn main(){ +fn main() { println(foo()) } diff --git a/vlib/v/checker/tests/aliased_optional_fn_call_err.out b/vlib/v/checker/tests/aliased_optional_fn_call_err.out deleted file mode 100644 index 9da634a23..000000000 --- a/vlib/v/checker/tests/aliased_optional_fn_call_err.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/aliased_optional_fn_call_err.vv:8:10: error: foo() returns an option, so it should have either an `or {}` block, or `?` at the end - 6 | - 7 | fn main(){ - 8 | println(foo()) - | ~~~~~ - 9 | } diff --git a/vlib/v/checker/tests/array_filter_map_option_function_err.out b/vlib/v/checker/tests/array_filter_map_option_function_err.out new file mode 100644 index 000000000..eb4e34805 --- /dev/null +++ b/vlib/v/checker/tests/array_filter_map_option_function_err.out @@ -0,0 +1,21 @@ +vlib/v/checker/tests/array_filter_map_option_function_err.vv:14:25: error: option needs to be unwrapped before using it in map/filter + 12 | } + 13 | + 14 | arr_struct2 := arr.map(option_mapping_struct) + | ~~~~~~~~~~~~~~~~~~~~~ + 15 | println(arr_struct2) + 16 | +vlib/v/checker/tests/array_filter_map_option_function_err.vv:17:18: error: option needs to be unwrapped before using it in map/filter + 15 | println(arr_struct2) + 16 | + 17 | arr_int2 := arr.map(option_mapping_int) + | ~~~~~~~~~~~~~~~~~~~~~~~ + 18 | println(arr_int2) + 19 | +vlib/v/checker/tests/array_filter_map_option_function_err.vv:20:20: error: option needs to be unwrapped before using it in map/filter + 18 | println(arr_int2) + 19 | + 20 | arr_filter := arr.filter(option_mapping_int) + | ~~~~~~~~~~~~~~~~~~~~~~~~~~ + 21 | println(arr_filter) + 22 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/array_filter_map_optional_function_err.vv b/vlib/v/checker/tests/array_filter_map_option_function_err.vv similarity index 54% rename from vlib/v/checker/tests/array_filter_map_optional_function_err.vv rename to vlib/v/checker/tests/array_filter_map_option_function_err.vv index 89eb5627c..d5c6e0255 100644 --- a/vlib/v/checker/tests/array_filter_map_optional_function_err.vv +++ b/vlib/v/checker/tests/array_filter_map_option_function_err.vv @@ -1,7 +1,7 @@ fn main() { arr := [1, 2, 3, 4, 5] - optional_mapping_struct := fn (n int) ?MyStruct { + option_mapping_struct := fn (n int) ?MyStruct { if n < 0 { return none } else { @@ -11,13 +11,13 @@ fn main() { } } - arr_struct2 := arr.map(optional_mapping_struct) + arr_struct2 := arr.map(option_mapping_struct) println(arr_struct2) - arr_int2 := arr.map(optional_mapping_int) + arr_int2 := arr.map(option_mapping_int) println(arr_int2) - arr_filter := arr.filter(optional_mapping_int) + arr_filter := arr.filter(option_mapping_int) println(arr_filter) } @@ -25,7 +25,7 @@ struct MyStruct { my_field int } -fn optional_mapping_int(n int) ?int { +fn option_mapping_int(n int) ?int { if n < 0 { return none } else { diff --git a/vlib/v/checker/tests/array_filter_map_optional_function_err.out b/vlib/v/checker/tests/array_filter_map_optional_function_err.out deleted file mode 100644 index f2e35e920..000000000 --- a/vlib/v/checker/tests/array_filter_map_optional_function_err.out +++ /dev/null @@ -1,21 +0,0 @@ -vlib/v/checker/tests/array_filter_map_optional_function_err.vv:14:25: error: optional needs to be unwrapped before using it in map/filter - 12 | } - 13 | - 14 | arr_struct2 := arr.map(optional_mapping_struct) - | ~~~~~~~~~~~~~~~~~~~~~~~ - 15 | println(arr_struct2) - 16 | -vlib/v/checker/tests/array_filter_map_optional_function_err.vv:17:18: error: optional needs to be unwrapped before using it in map/filter - 15 | println(arr_struct2) - 16 | - 17 | arr_int2 := arr.map(optional_mapping_int) - | ~~~~~~~~~~~~~~~~~~~~~~~~~ - 18 | println(arr_int2) - 19 | -vlib/v/checker/tests/array_filter_map_optional_function_err.vv:20:20: error: optional needs to be unwrapped before using it in map/filter - 18 | println(arr_int2) - 19 | - 20 | arr_filter := arr.filter(optional_mapping_int) - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 21 | println(arr_filter) - 22 | } diff --git a/vlib/v/checker/tests/assert_option_err.out b/vlib/v/checker/tests/assert_option_err.out new file mode 100644 index 000000000..cf1cdae0a --- /dev/null +++ b/vlib/v/checker/tests/assert_option_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/assert_option_err.vv:4:9: error: assert can be used only with `bool` expressions, but found `void` instead + 2 | + 3 | fn main() { + 4 | assert os.truncate('testfile.txt', 6666) or { panic(err) } + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 5 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/assert_option_err.vv b/vlib/v/checker/tests/assert_option_err.vv new file mode 100644 index 000000000..45340a75b --- /dev/null +++ b/vlib/v/checker/tests/assert_option_err.vv @@ -0,0 +1,5 @@ +import os + +fn main() { + assert os.truncate('testfile.txt', 6666) or { panic(err) } +} diff --git a/vlib/v/checker/tests/assert_optional_err.out b/vlib/v/checker/tests/assert_optional_err.out deleted file mode 100644 index 9d2f49a44..000000000 --- a/vlib/v/checker/tests/assert_optional_err.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/assert_optional_err.vv:4:9: error: assert can be used only with `bool` expressions, but found `void` instead - 2 | - 3 | fn main(){ - 4 | assert os.truncate("testfile.txt", 6666) or { panic(err) } - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 5 | } diff --git a/vlib/v/checker/tests/assert_optional_err.vv b/vlib/v/checker/tests/assert_optional_err.vv deleted file mode 100644 index 703661618..000000000 --- a/vlib/v/checker/tests/assert_optional_err.vv +++ /dev/null @@ -1,5 +0,0 @@ -import os - -fn main(){ - assert os.truncate("testfile.txt", 6666) or { panic(err) } -} diff --git a/vlib/v/checker/tests/comptime_if_optional_string_test.out b/vlib/v/checker/tests/comptime_if_option_string_test.out similarity index 100% rename from vlib/v/checker/tests/comptime_if_optional_string_test.out rename to vlib/v/checker/tests/comptime_if_option_string_test.out diff --git a/vlib/v/checker/tests/comptime_if_optional_string_test.vv b/vlib/v/checker/tests/comptime_if_option_string_test.vv similarity index 100% rename from vlib/v/checker/tests/comptime_if_optional_string_test.vv rename to vlib/v/checker/tests/comptime_if_option_string_test.vv diff --git a/vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out b/vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out index 97a3e70c2..a56f5230e 100644 --- a/vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out +++ b/vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out @@ -1,2 +1,2 @@ -optional compitme define works -non optional comptime define works +comptime option define works +comptime non-option define works \ No newline at end of file diff --git a/vlib/v/checker/tests/custom_comptime_define_error.out b/vlib/v/checker/tests/custom_comptime_define_error.out index b916d1d3d..6d48ace84 100644 --- a/vlib/v/checker/tests/custom_comptime_define_error.out +++ b/vlib/v/checker/tests/custom_comptime_define_error.out @@ -1,7 +1,7 @@ -vlib/v/checker/tests/custom_comptime_define_error.vv:6:13: error: undefined ident: `mysymbol` - 4 | println('optional compitme define works') - 5 | } - 6 | $if mysymbol { - | ~~~~~~~~ - 7 | // this will produce a checker error when `-d mysymbol` is not given on the CLI - 8 | println('non optional comptime define works') +vlib/v/checker/tests/custom_comptime_define_error.vv:6:6: error: undefined ident: `mysymbol` + 4 | println('comptime option define works') + 5 | } + 6 | $if mysymbol { + | ~~~~~~~~ + 7 | // this will produce a checker error when `-d mysymbol` is not given on the CLI + 8 | println('comptime non-option define works') \ No newline at end of file diff --git a/vlib/v/checker/tests/custom_comptime_define_error.vv b/vlib/v/checker/tests/custom_comptime_define_error.vv index 34f99676a..887f27f5c 100644 --- a/vlib/v/checker/tests/custom_comptime_define_error.vv +++ b/vlib/v/checker/tests/custom_comptime_define_error.vv @@ -1,10 +1,10 @@ fn main() { - $if mysymbol? { - // this should not produce checker errors, but will print only with `-d mysymbol` - println('optional compitme define works') - } - $if mysymbol { - // this will produce a checker error when `-d mysymbol` is not given on the CLI - println('non optional comptime define works') - } + $if mysymbol ? { + // this should not produce checker errors, but will print only with `-d mysymbol` + println('comptime option define works') + } + $if mysymbol { + // this will produce a checker error when `-d mysymbol` is not given on the CLI + println('comptime non-option define works') + } } diff --git a/vlib/v/checker/tests/defer_option.out b/vlib/v/checker/tests/defer_option.out new file mode 100644 index 000000000..f4cf35306 --- /dev/null +++ b/vlib/v/checker/tests/defer_option.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/defer_option.vv:5:3: error: opt() returns an option, so it should have an `or {}` block at the end + 3 | fn thing() ?string { + 4 | defer { + 5 | opt() + | ~~~~~ + 6 | } + 7 | return 'ok' \ No newline at end of file diff --git a/vlib/v/checker/tests/defer_optional.vv b/vlib/v/checker/tests/defer_option.vv similarity index 100% rename from vlib/v/checker/tests/defer_optional.vv rename to vlib/v/checker/tests/defer_option.vv diff --git a/vlib/v/checker/tests/defer_optional.out b/vlib/v/checker/tests/defer_optional.out deleted file mode 100644 index 184e35e64..000000000 --- a/vlib/v/checker/tests/defer_optional.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/defer_optional.vv:5:3: error: opt() returns an option, so it should have an `or {}` block at the end - 3 | fn thing() ?string { - 4 | defer { - 5 | opt() - | ~~~~~ - 6 | } - 7 | return 'ok' \ No newline at end of file diff --git a/vlib/v/checker/tests/expression_should_return_an_option.out b/vlib/v/checker/tests/expression_should_return_an_option.out index 3337fc113..eb615f09a 100644 --- a/vlib/v/checker/tests/expression_should_return_an_option.out +++ b/vlib/v/checker/tests/expression_should_return_an_option.out @@ -1,7 +1,7 @@ -vlib/v/checker/tests/expression_should_return_an_option.vv:28:10: error: expression should either return an option or a result - 26 | } - 27 | // should be an checker error: - 28 | if x := return_string() { +vlib/v/checker/tests/expression_should_return_an_option.vv:26:10: error: expression should either return an option or a result + 24 | return_option(true) or { println(err) } + 25 | // should be an checker error: + 26 | if x := return_string() { | ~~~~~~~~~~~~~~~ - 29 | println('x: $x') - 30 | } \ No newline at end of file + 27 | println('x: ${x}') + 28 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/expression_should_return_an_option.vv b/vlib/v/checker/tests/expression_should_return_an_option.vv index 6b25dbc2f..05ad45a39 100644 --- a/vlib/v/checker/tests/expression_should_return_an_option.vv +++ b/vlib/v/checker/tests/expression_should_return_an_option.vv @@ -1,4 +1,4 @@ -fn return_optional(fail bool) ?string { +fn return_option(fail bool) ?string { if fail { return error('nope') } @@ -11,21 +11,19 @@ fn return_string() string { fn main() { // works - if r := return_optional(false) { + if r := return_option(false) { println(r) } // works - if r := return_optional(false) { + if r := return_option(false) { println(r) } else { println(err) } // works - return_optional(true) or { - println(err) - } + return_option(true) or { println(err) } // should be an checker error: if x := return_string() { - println('x: $x') + println('x: ${x}') } } diff --git a/vlib/v/checker/tests/fn_arg_of_option_err.out b/vlib/v/checker/tests/fn_arg_of_option_err.out new file mode 100644 index 000000000..b6e19ee0d --- /dev/null +++ b/vlib/v/checker/tests/fn_arg_of_option_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/fn_arg_of_option_err.vv:1:17: error: option or result type argument is not supported currently + 1 | fn option_arg(x ?int) { + | ~~~~ + 2 | println('int type: ${x}') + 3 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/fn_arg_of_option_err.vv b/vlib/v/checker/tests/fn_arg_of_option_err.vv new file mode 100644 index 000000000..0a1fe42a0 --- /dev/null +++ b/vlib/v/checker/tests/fn_arg_of_option_err.vv @@ -0,0 +1,7 @@ +fn option_arg(x ?int) { + println('int type: ${x}') +} + +fn main() { + option_arg(1) +} diff --git a/vlib/v/checker/tests/fn_arg_of_optional_err.out b/vlib/v/checker/tests/fn_arg_of_optional_err.out deleted file mode 100644 index 5d9cd1bb8..000000000 --- a/vlib/v/checker/tests/fn_arg_of_optional_err.out +++ /dev/null @@ -1,5 +0,0 @@ -vlib/v/checker/tests/fn_arg_of_optional_err.vv:1:19: error: optional or result type argument is not supported currently - 1 | fn optional_arg(x ?int) { - | ~~~~ - 2 | println('int type: $x') - 3 | } diff --git a/vlib/v/checker/tests/fn_arg_of_optional_err.vv b/vlib/v/checker/tests/fn_arg_of_optional_err.vv deleted file mode 100644 index 5c7c250bc..000000000 --- a/vlib/v/checker/tests/fn_arg_of_optional_err.vv +++ /dev/null @@ -1,7 +0,0 @@ -fn optional_arg(x ?int) { - println('int type: $x') -} - -fn main() { - optional_arg(1) -} diff --git a/vlib/v/checker/tests/fn_check_for_matching_option_result_in_fields.out b/vlib/v/checker/tests/fn_check_for_matching_option_result_in_fields.out new file mode 100644 index 000000000..e16a4346a --- /dev/null +++ b/vlib/v/checker/tests/fn_check_for_matching_option_result_in_fields.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/fn_check_for_matching_option_result_in_fields.vv:7:3: error: cannot assign to field `f`: expected `fn (voidptr)`, not `fn (voidptr) ?` + 5 | fn main() { + 6 | a := Abc{ + 7 | f: fn (data voidptr) ? {} + | ~~~~~~~~~~~~~~~~~~~~~~~~~ + 8 | } + 9 | println(a) \ No newline at end of file diff --git a/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv b/vlib/v/checker/tests/fn_check_for_matching_option_result_in_fields.vv similarity index 100% rename from vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv rename to vlib/v/checker/tests/fn_check_for_matching_option_result_in_fields.vv diff --git a/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out b/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out deleted file mode 100644 index c41095f57..000000000 --- a/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv:7:3: error: cannot assign to field `f`: expected `fn (voidptr)`, not `fn (voidptr) ?` - 5 | fn main() { - 6 | a := Abc{ - 7 | f: fn (data voidptr) ? {} - | ~~~~~~~~~~~~~~~~~~~~~~~~~ - 8 | } - 9 | println(a) diff --git a/vlib/v/checker/tests/fn_return_or_err.out b/vlib/v/checker/tests/fn_return_or_err.out index 3da817947..3aa6f5cb9 100644 --- a/vlib/v/checker/tests/fn_return_or_err.out +++ b/vlib/v/checker/tests/fn_return_or_err.out @@ -1,7 +1,7 @@ -vlib/v/checker/tests/fn_return_or_err.vv:6:17: error: unexpected `or` block, the function `pop` does neither return an optional nor a result - 4 | +vlib/v/checker/tests/fn_return_or_err.vv:6:17: error: unexpected `or` block, the function `pop` does not return an option or a result + 4 | 5 | pub fn next(mut v []Typ) Typ { 6 | return v.pop() or { Typ{} } | ~~~~~~~~~~~~ 7 | } - 8 | + 8 | \ No newline at end of file diff --git a/vlib/v/checker/tests/for_c_stmt_with_optional_call.out b/vlib/v/checker/tests/for_c_stmt_with_option_call.out similarity index 61% rename from vlib/v/checker/tests/for_c_stmt_with_optional_call.out rename to vlib/v/checker/tests/for_c_stmt_with_option_call.out index 90a424bf9..e3f46f01a 100644 --- a/vlib/v/checker/tests/for_c_stmt_with_optional_call.out +++ b/vlib/v/checker/tests/for_c_stmt_with_option_call.out @@ -1,7 +1,7 @@ -vlib/v/checker/tests/for_c_stmt_with_optional_call.vv:15:31: error: optionals are not allowed in `for statement increment` (yet) +vlib/v/checker/tests/for_c_stmt_with_option_call.vv:15:31: error: options are not allowed in `for statement increment` (yet) 13 | for t := 0; t < tests; t++ { 14 | mut v := []bool{len: nmax, init: false} 15 | for x := 0; !v[x]; x = rand.intn(n) or { 0 } { | ~~~~~~~ 16 | v[x] = true - 17 | sum++ + 17 | sum++ \ No newline at end of file diff --git a/vlib/v/checker/tests/for_c_stmt_with_optional_call.vv b/vlib/v/checker/tests/for_c_stmt_with_option_call.vv similarity index 100% rename from vlib/v/checker/tests/for_c_stmt_with_optional_call.vv rename to vlib/v/checker/tests/for_c_stmt_with_option_call.vv diff --git a/vlib/v/checker/tests/for_in_index_option.out b/vlib/v/checker/tests/for_in_index_option.out new file mode 100644 index 000000000..26ec76b77 --- /dev/null +++ b/vlib/v/checker/tests/for_in_index_option.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/for_in_index_option.vv:4:17: error: for in: cannot index `![]string` + 2 | + 3 | fn main() { + 4 | for file in os.ls('.') { + | ~~~~~~~ + 5 | println(file) + 6 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/for_in_index_option.vv b/vlib/v/checker/tests/for_in_index_option.vv new file mode 100644 index 000000000..51e605495 --- /dev/null +++ b/vlib/v/checker/tests/for_in_index_option.vv @@ -0,0 +1,7 @@ +import os + +fn main() { + for file in os.ls('.') { + println(file) + } +} diff --git a/vlib/v/checker/tests/for_in_index_optional.out b/vlib/v/checker/tests/for_in_index_optional.out deleted file mode 100644 index ad2939c15..000000000 --- a/vlib/v/checker/tests/for_in_index_optional.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/for_in_index_optional.vv:3:18: error: for in: cannot index `![]string` - 1 | import os - 2 | fn main() { - 3 | for file in os.ls('.') { - | ~~~~~~~ - 4 | println(file) - 5 | } diff --git a/vlib/v/checker/tests/for_in_index_optional.vv b/vlib/v/checker/tests/for_in_index_optional.vv deleted file mode 100644 index c384c790f..000000000 --- a/vlib/v/checker/tests/for_in_index_optional.vv +++ /dev/null @@ -1,6 +0,0 @@ -import os -fn main() { - for file in os.ls('.') { - println(file) - } -} diff --git a/vlib/v/checker/tests/go_append_option_to_threads_err.out b/vlib/v/checker/tests/go_append_option_to_threads_err.out new file mode 100644 index 000000000..6bb655dca --- /dev/null +++ b/vlib/v/checker/tests/go_append_option_to_threads_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/go_append_option_to_threads_err.vv:5:15: error: cannot append `thread !` to `[]thread` + 3 | fn main() { + 4 | mut ths := []thread{} + 5 | ths << spawn foo() + | ~~~~~ + 6 | ths.wait() + 7 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/go_append_optional_to_threads_err.vv b/vlib/v/checker/tests/go_append_option_to_threads_err.vv similarity index 76% rename from vlib/v/checker/tests/go_append_optional_to_threads_err.vv rename to vlib/v/checker/tests/go_append_option_to_threads_err.vv index 3cf48ceab..9aec870a3 100644 --- a/vlib/v/checker/tests/go_append_optional_to_threads_err.vv +++ b/vlib/v/checker/tests/go_append_option_to_threads_err.vv @@ -2,6 +2,6 @@ fn foo() ! {} fn main() { mut ths := []thread{} - ths << go foo() + ths << spawn foo() ths.wait() } diff --git a/vlib/v/checker/tests/go_append_optional_to_threads_err.out b/vlib/v/checker/tests/go_append_optional_to_threads_err.out deleted file mode 100644 index e8e7afcc1..000000000 --- a/vlib/v/checker/tests/go_append_optional_to_threads_err.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/go_append_optional_to_threads_err.vv:5:12: error: cannot append `thread !` to `[]thread` - 3 | fn main() { - 4 | mut ths := []thread{} - 5 | ths << go foo() - | ~~~~~ - 6 | ths.wait() - 7 | } diff --git a/vlib/v/checker/tests/go_wait_or.out b/vlib/v/checker/tests/go_wait_or.out index 92242c6ab..0389ca6d6 100644 --- a/vlib/v/checker/tests/go_wait_or.out +++ b/vlib/v/checker/tests/go_wait_or.out @@ -1,32 +1,32 @@ -vlib/v/checker/tests/go_wait_or.vv:11:16: error: unexpected `?`, the function `wait` does neither return an optional nor a result +vlib/v/checker/tests/go_wait_or.vv:11:16: error: unexpected `?`, the function `wait` does not return an option or a result 9 | go d(1) 10 | ] 11 | r := tg.wait()? | ^ 12 | println(r) 13 | s := tg[0].wait() or { panic('problem') } -vlib/v/checker/tests/go_wait_or.vv:13:20: error: unexpected `or` block, the function `wait` does neither return an optional nor a result +vlib/v/checker/tests/go_wait_or.vv:13:20: error: unexpected `or` block, the function `wait` does not return an option or a result 11 | r := tg.wait()? 12 | println(r) 13 | s := tg[0].wait() or { panic('problem') } | ~~~~~~~~~~~~~~~~~~~~~~~ 14 | println(s) 15 | tg2 := [ -vlib/v/checker/tests/go_wait_or.vv:19:13: error: unexpected `or` block, the function `wait` does neither return an optional nor a result +vlib/v/checker/tests/go_wait_or.vv:19:13: error: unexpected `or` block, the function `wait` does not return an option or a result 17 | go e(1) 18 | ] 19 | tg2.wait() or { panic('problem') } | ~~~~~~~~~~~~~~~~~~~~~~~ 20 | tg2[0].wait()? 21 | tg3 := [ -vlib/v/checker/tests/go_wait_or.vv:20:15: error: unexpected `?`, the function `wait` does neither return an optional nor a result +vlib/v/checker/tests/go_wait_or.vv:20:15: error: unexpected `?`, the function `wait` does not return an option or a result 18 | ] 19 | tg2.wait() or { panic('problem') } 20 | tg2[0].wait()? | ^ 21 | tg3 := [ 22 | go f(0) -vlib/v/checker/tests/go_wait_or.vv:25:6: error: `.wait()` cannot be called for an array when thread functions return optionals. Iterate over the arrays elements instead and handle each returned optional with `or`. +vlib/v/checker/tests/go_wait_or.vv:25:6: error: `.wait()` cannot be called for an array when thread functions return options. Iterate over the arrays elements instead and handle each returned option with `or`. 23 | go f(1) 24 | ] 25 | tg3.wait() or { panic('problem') } @@ -47,7 +47,7 @@ vlib/v/checker/tests/go_wait_or.vv:31:15: error: wait() returns an option, so it | ~~~~~~ 32 | println(a) 33 | } -vlib/v/checker/tests/go_wait_or.vv:38:6: error: `.wait()` cannot be called for an array when thread functions return optionals. Iterate over the arrays elements instead and handle each returned optional with `or`. +vlib/v/checker/tests/go_wait_or.vv:38:6: error: `.wait()` cannot be called for an array when thread functions return options. Iterate over the arrays elements instead and handle each returned option with `or`. 36 | go g(1) 37 | ] 38 | tg4.wait() @@ -61,9 +61,9 @@ vlib/v/checker/tests/go_wait_or.vv:39:9: error: wait() returns an option, so it | ~~~~~~ 40 | go g(3) or { panic('problem') } 41 | } -vlib/v/checker/tests/go_wait_or.vv:40:10: error: optional handling cannot be done in `go` call. Do it when calling `.wait()` +vlib/v/checker/tests/go_wait_or.vv:40:10: error: option handling cannot be done in `go` call. Do it when calling `.wait()` 38 | tg4.wait() 39 | tg4[0].wait() 40 | go g(3) or { panic('problem') } | ~~~~~~~~~~~~~~~~~~~~~~~ - 41 | } + 41 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/if_expr_option_err.out b/vlib/v/checker/tests/if_expr_option_err.out new file mode 100644 index 000000000..aad47a8c8 --- /dev/null +++ b/vlib/v/checker/tests/if_expr_option_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/if_expr_option_err.vv:6:5: error: non-bool type `?bool` used as if condition + 4 | + 5 | fn main() { + 6 | if get_bool() { + | ~~~~~~~~~~ + 7 | println('Using plain lists') + 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/if_expr_optional_err.vv b/vlib/v/checker/tests/if_expr_option_err.vv similarity index 69% rename from vlib/v/checker/tests/if_expr_optional_err.vv rename to vlib/v/checker/tests/if_expr_option_err.vv index 432e4bb7b..1d62ca4d3 100644 --- a/vlib/v/checker/tests/if_expr_optional_err.vv +++ b/vlib/v/checker/tests/if_expr_option_err.vv @@ -4,6 +4,6 @@ fn get_bool() ?bool { fn main() { if get_bool() { - println("Using plain lists") + println('Using plain lists') } } diff --git a/vlib/v/checker/tests/if_expr_optional_err.out b/vlib/v/checker/tests/if_expr_optional_err.out deleted file mode 100644 index 9df929bfc..000000000 --- a/vlib/v/checker/tests/if_expr_optional_err.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/if_expr_optional_err.vv:6:5: error: non-bool type `?bool` used as if condition - 4 | - 5 | fn main() { - 6 | if get_bool() { - | ~~~~~~~~~~ - 7 | println("Using plain lists") - 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/index_of_option_err.out b/vlib/v/checker/tests/index_of_option_err.out new file mode 100644 index 000000000..f4c0af085 --- /dev/null +++ b/vlib/v/checker/tests/index_of_option_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/index_of_option_err.vv:6:7: error: type `?[]int` is an option, it does not support indexing + 4 | + 5 | fn main() { + 6 | a := abc()[0] or { 5 } + | ~~~~~ + 7 | dump(a) + 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/index_of_optional_err.vv b/vlib/v/checker/tests/index_of_option_err.vv similarity index 100% rename from vlib/v/checker/tests/index_of_optional_err.vv rename to vlib/v/checker/tests/index_of_option_err.vv diff --git a/vlib/v/checker/tests/index_of_optional_err.out b/vlib/v/checker/tests/index_of_optional_err.out deleted file mode 100644 index a3628501d..000000000 --- a/vlib/v/checker/tests/index_of_optional_err.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/index_of_optional_err.vv:6:7: error: type `?[]int` is optional, it does not support indexing - 4 | - 5 | fn main() { - 6 | a := abc()[0] or { 5 } - | ~~~~~ - 7 | dump(a) - 8 | } diff --git a/vlib/v/checker/tests/infix_compare_option_err.out b/vlib/v/checker/tests/infix_compare_option_err.out new file mode 100644 index 000000000..ec5c4be16 --- /dev/null +++ b/vlib/v/checker/tests/infix_compare_option_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/infix_compare_option_err.vv:6:5: error: unwrapped option cannot be compared in an infix expression + 4 | + 5 | fn main() { + 6 | if foo() > foo() { + | ~~~~~ + 7 | } + 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/infix_compare_option_err.vv b/vlib/v/checker/tests/infix_compare_option_err.vv new file mode 100644 index 000000000..e7a8cbdc5 --- /dev/null +++ b/vlib/v/checker/tests/infix_compare_option_err.vv @@ -0,0 +1,8 @@ +fn foo() ?int { + return 0 +} + +fn main() { + if foo() > foo() { + } +} diff --git a/vlib/v/checker/tests/infix_compare_optional_err.out b/vlib/v/checker/tests/infix_compare_optional_err.out deleted file mode 100644 index 8a8d27a3d..000000000 --- a/vlib/v/checker/tests/infix_compare_optional_err.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/infix_compare_optional_err.vv:6:5: error: unwrapped optional cannot be compared in an infix expression - 4 | - 5 | fn main(){ - 6 | if foo() > foo() {} - | ~~~~~ - 7 | } diff --git a/vlib/v/checker/tests/infix_compare_optional_err.vv b/vlib/v/checker/tests/infix_compare_optional_err.vv deleted file mode 100644 index 6d75f3176..000000000 --- a/vlib/v/checker/tests/infix_compare_optional_err.vv +++ /dev/null @@ -1,7 +0,0 @@ -fn foo() ?int{ - return 0 -} - -fn main(){ - if foo() > foo() {} -} diff --git a/vlib/v/checker/tests/infix_err.out b/vlib/v/checker/tests/infix_err.out index ff9049ebd..f3573fe03 100644 --- a/vlib/v/checker/tests/infix_err.out +++ b/vlib/v/checker/tests/infix_err.out @@ -17,17 +17,17 @@ vlib/v/checker/tests/infix_err.vv:9:9: error: `+` cannot be used with `?string` 8 | _ = f() + '' 9 | _ = f() + f() | ^ - 10 | + 10 | 11 | _ = 4 + g() vlib/v/checker/tests/infix_err.vv:11:7: error: `+` cannot be used with `?int` 9 | _ = f() + f() - 10 | + 10 | 11 | _ = 4 + g() | ^ 12 | _ = int(0) + g() // FIXME not detected 13 | _ = g() + int(3) -vlib/v/checker/tests/infix_err.vv:12:14: error: unwrapped optional cannot be used in an infix expression - 10 | +vlib/v/checker/tests/infix_err.vv:12:14: error: unwrapped option cannot be used in an infix expression + 10 | 11 | _ = 4 + g() 12 | _ = int(0) + g() // FIXME not detected | ~~~ @@ -45,10 +45,10 @@ vlib/v/checker/tests/infix_err.vv:14:9: error: `+` cannot be used with `?int` 13 | _ = g() + int(3) 14 | _ = g() + 3 | ^ - 15 | + 15 | 16 | // binary operands vlib/v/checker/tests/infix_err.vv:17:5: error: left operand for `&&` is not a boolean - 15 | + 15 | 16 | // binary operands 17 | _ = 1 && 2 | ^ @@ -59,10 +59,10 @@ vlib/v/checker/tests/infix_err.vv:18:13: error: right operand for `||` is not a 17 | _ = 1 && 2 18 | _ = true || 2 | ^ - 19 | + 19 | 20 | // boolean expressions vlib/v/checker/tests/infix_err.vv:21:22: error: ambiguous boolean expression. use `()` to ensure correct order of operations - 19 | + 19 | 20 | // boolean expressions 21 | _ = 1 == 1 && 2 == 2 || 3 == 3 | ~~ @@ -78,4 +78,4 @@ vlib/v/checker/tests/infix_err.vv:24:2: error: ambiguous boolean expression. use 22 | _ = 1 == 1 23 | && 2 == 2 || 3 == 3 24 | && 4 == 4 - | ~~ + | ~~ \ No newline at end of file diff --git a/vlib/v/checker/tests/option_fields_addr_err.out b/vlib/v/checker/tests/option_fields_addr_err.out new file mode 100644 index 000000000..7c608d7c0 --- /dev/null +++ b/vlib/v/checker/tests/option_fields_addr_err.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/option_fields_addr_err.vv:9:10: error: cannot take the address of an option field + 7 | fn main() { + 8 | x := Wrapper{} + 9 | if _ := &x.value { + | ~~~~~~~~ + 10 | } + 11 | +vlib/v/checker/tests/option_fields_addr_err.vv:12:7: error: cannot take the address of an option field + 10 | } + 11 | + 12 | _ := &x.value + | ~~~~~~~~ + 13 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_fields_addr_err.vv b/vlib/v/checker/tests/option_fields_addr_err.vv similarity index 100% rename from vlib/v/checker/tests/optional_fields_addr_err.vv rename to vlib/v/checker/tests/option_fields_addr_err.vv diff --git a/vlib/v/checker/tests/option_fn_err.out b/vlib/v/checker/tests/option_fn_err.out new file mode 100644 index 000000000..789972da7 --- /dev/null +++ b/vlib/v/checker/tests/option_fn_err.out @@ -0,0 +1,168 @@ +vlib/v/checker/tests/option_fn_err.vv:12:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 10 | + 11 | const ( + 12 | const_value = bar(0) + | ~~~~~~ + 13 | ) + 14 | +vlib/v/checker/tests/option_fn_err.vv:18:15: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 16 | f fn (int) + 17 | mut: + 18 | value int = bar(0) + | ~~~~~~ + 19 | opt ?int = bar(0) + 20 | } +vlib/v/checker/tests/option_fn_err.vv:32:2: error: foo() returns an option, so it should have either an `or {}` block, or `?` at the end + 30 | fn main() { + 31 | // call fn + 32 | foo() + | ~~~~~ + 33 | _ := bar(0) + 34 | println(twice(bar(0))) +vlib/v/checker/tests/option_fn_err.vv:33:7: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 31 | // call fn + 32 | foo() + 33 | _ := bar(0) + | ~~~~~~ + 34 | println(twice(bar(0))) + 35 | +vlib/v/checker/tests/option_fn_err.vv:34:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 32 | foo() + 33 | _ := bar(0) + 34 | println(twice(bar(0))) + | ~~~~~~ + 35 | + 36 | // anon fn +vlib/v/checker/tests/option_fn_err.vv:37:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 35 | + 36 | // anon fn + 37 | fn (_ int) {}(bar(0)) + | ~~~~~~ + 38 | + 39 | // assert +vlib/v/checker/tests/option_fn_err.vv:40:9: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 38 | + 39 | // assert + 40 | assert bar(true) + | ~~~~~~~~~ + 41 | + 42 | // struct +vlib/v/checker/tests/option_fn_err.vv:45:10: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 43 | mut v := Data{ + 44 | f: fn (_ int) {} + 45 | value: bar(0) + | ~~~~~~ + 46 | opt: bar(0) + 47 | } +vlib/v/checker/tests/option_fn_err.vv:48:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 46 | opt: bar(0) + 47 | } + 48 | v.add(bar(0)) // call method + | ~~~~~~ + 49 | v.f(bar(0)) // call fn field + 50 | +vlib/v/checker/tests/option_fn_err.vv:49:6: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 47 | } + 48 | v.add(bar(0)) // call method + 49 | v.f(bar(0)) // call fn field + | ~~~~~~ + 50 | + 51 | // array +vlib/v/checker/tests/option_fn_err.vv:53:9: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 51 | // array + 52 | mut arr := [1, 2] + 53 | arr << bar(0) + | ~~~~~~ + 54 | // init + 55 | _ := [bar(0)] +vlib/v/checker/tests/option_fn_err.vv:55:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 53 | arr << bar(0) + 54 | // init + 55 | _ := [bar(0)] + | ~~~~~~ + 56 | _ := []int{len: 1, init: bar(0)} + 57 | _ := [bar(0)]! +vlib/v/checker/tests/option_fn_err.vv:56:27: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 54 | // init + 55 | _ := [bar(0)] + 56 | _ := []int{len: 1, init: bar(0)} + | ~~~~~~ + 57 | _ := [bar(0)]! + 58 | _ := [1]int{init: bar(0)} +vlib/v/checker/tests/option_fn_err.vv:57:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 55 | _ := [bar(0)] + 56 | _ := []int{len: 1, init: bar(0)} + 57 | _ := [bar(0)]! + | ~~~~~~ + 58 | _ := [1]int{init: bar(0)} + 59 | // index +vlib/v/checker/tests/option_fn_err.vv:60:13: error: cannot use option or result as index (array type `[]int`) + 58 | _ := [1]int{init: bar(0)} + 59 | // index + 60 | println(arr[bar(0)]) + | ~~~~~~~~ + 61 | // array builtin methods + 62 | arr.insert(0, bar(0)) +vlib/v/checker/tests/option_fn_err.vv:62:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 60 | println(arr[bar(0)]) + 61 | // array builtin methods + 62 | arr.insert(0, bar(0)) + | ~~~~~~ + 63 | arr.prepend(bar(0)) + 64 | arr.contains(bar(0)) +vlib/v/checker/tests/option_fn_err.vv:63:14: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 61 | // array builtin methods + 62 | arr.insert(0, bar(0)) + 63 | arr.prepend(bar(0)) + | ~~~~~~ + 64 | arr.contains(bar(0)) + 65 | arr.index(bar(0)) +vlib/v/checker/tests/option_fn_err.vv:64:15: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 62 | arr.insert(0, bar(0)) + 63 | arr.prepend(bar(0)) + 64 | arr.contains(bar(0)) + | ~~~~~~ + 65 | arr.index(bar(0)) + 66 | println(arr.map(bar(0))) +vlib/v/checker/tests/option_fn_err.vv:65:12: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 63 | arr.prepend(bar(0)) + 64 | arr.contains(bar(0)) + 65 | arr.index(bar(0)) + | ~~~~~~ + 66 | println(arr.map(bar(0))) + 67 | println(arr.filter(bar(true))) +vlib/v/checker/tests/option_fn_err.vv:66:18: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 64 | arr.contains(bar(0)) + 65 | arr.index(bar(0)) + 66 | println(arr.map(bar(0))) + | ~~~~~~ + 67 | println(arr.filter(bar(true))) + 68 | println(arr.any(bar(true))) +vlib/v/checker/tests/option_fn_err.vv:67:21: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 65 | arr.index(bar(0)) + 66 | println(arr.map(bar(0))) + 67 | println(arr.filter(bar(true))) + | ~~~~~~~~~ + 68 | println(arr.any(bar(true))) + 69 | println(arr.all(bar(true))) +vlib/v/checker/tests/option_fn_err.vv:68:18: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 66 | println(arr.map(bar(0))) + 67 | println(arr.filter(bar(true))) + 68 | println(arr.any(bar(true))) + | ~~~~~~~~~ + 69 | println(arr.all(bar(true))) + 70 | +vlib/v/checker/tests/option_fn_err.vv:69:18: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 67 | println(arr.filter(bar(true))) + 68 | println(arr.any(bar(true))) + 69 | println(arr.all(bar(true))) + | ~~~~~~~~~ + 70 | + 71 | match bar(0) { +vlib/v/checker/tests/option_fn_err.vv:71:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 69 | println(arr.all(bar(true))) + 70 | + 71 | match bar(0) { + | ~~~~~~ + 72 | 0 {} + 73 | else {} \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_fn_err.vv b/vlib/v/checker/tests/option_fn_err.vv similarity index 84% rename from vlib/v/checker/tests/optional_fn_err.vv rename to vlib/v/checker/tests/option_fn_err.vv index 1727144c0..0319c83b3 100644 --- a/vlib/v/checker/tests/optional_fn_err.vv +++ b/vlib/v/checker/tests/option_fn_err.vv @@ -1,5 +1,4 @@ - -// use optional without ? or an or block in places where it is not allowed +// use option without ? or an or block in places where it is not allowed fn foo() ? { println('foo is called') @@ -16,7 +15,7 @@ const ( struct Data { f fn (int) mut: - value int = bar(0) + value int = bar(0) opt ?int = bar(0) } @@ -42,9 +41,9 @@ fn main() { // struct mut v := Data{ - f: fn (_ int) {}, - value: bar(0), - opt: bar(0), + f: fn (_ int) {} + value: bar(0) + opt: bar(0) } v.add(bar(0)) // call method v.f(bar(0)) // call fn field @@ -70,7 +69,7 @@ fn main() { println(arr.all(bar(true))) match bar(0) { - 0 { } - else { } + 0 {} + else {} } } diff --git a/vlib/v/checker/tests/option_in_dump_err.out b/vlib/v/checker/tests/option_in_dump_err.out new file mode 100644 index 000000000..b13d4b128 --- /dev/null +++ b/vlib/v/checker/tests/option_in_dump_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/option_in_dump_err.vv:10:7: error: create() returns an option, so it should have either an `or {}` block, or `?` at the end + 8 | + 9 | fn main() { + 10 | dump(create()) + | ~~~~~~~~ + 11 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_in_dump_err.vv b/vlib/v/checker/tests/option_in_dump_err.vv similarity index 100% rename from vlib/v/checker/tests/optional_in_dump_err.vv rename to vlib/v/checker/tests/option_in_dump_err.vv diff --git a/vlib/v/checker/tests/option_in_println_mismatch.out b/vlib/v/checker/tests/option_in_println_mismatch.out new file mode 100644 index 000000000..d2900cdb3 --- /dev/null +++ b/vlib/v/checker/tests/option_in_println_mismatch.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/option_in_println_mismatch.vv:6:23: error: wrong return type `string` in the `or {}` block, expected `int` + 4 | + 5 | fn main() { + 6 | println(funcy() or { '' }) + | ~~ + 7 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_in_println_mismatch.vv b/vlib/v/checker/tests/option_in_println_mismatch.vv similarity index 100% rename from vlib/v/checker/tests/optional_in_println_mismatch.vv rename to vlib/v/checker/tests/option_in_println_mismatch.vv diff --git a/vlib/v/checker/tests/optional_interface_mismatch.out b/vlib/v/checker/tests/option_interface_mismatch.out similarity index 53% rename from vlib/v/checker/tests/optional_interface_mismatch.out rename to vlib/v/checker/tests/option_interface_mismatch.out index 64fe585ba..af522fdb2 100644 --- a/vlib/v/checker/tests/optional_interface_mismatch.out +++ b/vlib/v/checker/tests/option_interface_mismatch.out @@ -1,6 +1,6 @@ -vlib/v/checker/tests/optional_interface_mismatch.vv:11:9: error: mismatched types `?MObject` and `string` +vlib/v/checker/tests/option_interface_mismatch.vv:11:9: error: mismatched types `?MObject` and `string` 9 | 10 | fn give_string(line string) ?MObject { 11 | return if true { 'string' } else { 'string' } | ~~ - 12 | } + 12 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_interface_mismatch.vv b/vlib/v/checker/tests/option_interface_mismatch.vv similarity index 100% rename from vlib/v/checker/tests/optional_interface_mismatch.vv rename to vlib/v/checker/tests/option_interface_mismatch.vv diff --git a/vlib/v/checker/tests/option_or_block_mismatch.out b/vlib/v/checker/tests/option_or_block_mismatch.out new file mode 100644 index 000000000..9fa8c497d --- /dev/null +++ b/vlib/v/checker/tests/option_or_block_mismatch.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/option_or_block_mismatch.vv:10:18: error: wrong return type `Bar` in the `or {}` block, expected `&Bar` + 8 | + 9 | fn main() { + 10 | x := foo() or { Bar{} } + | ~~~~~ + 11 | println(x) + 12 | +vlib/v/checker/tests/option_or_block_mismatch.vv:13:13: error: the default expression type in the `or` block should be `&Bar`, instead you gave a value of type `Bar` + 11 | println(x) + 12 | + 13 | foo() or { Bar{} } + | ~~~~~ + 14 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_or_block_mismatch.vv b/vlib/v/checker/tests/option_or_block_mismatch.vv similarity index 100% rename from vlib/v/checker/tests/optional_or_block_mismatch.vv rename to vlib/v/checker/tests/option_or_block_mismatch.vv diff --git a/vlib/v/checker/tests/option_or_block_none_err.out b/vlib/v/checker/tests/option_or_block_none_err.out new file mode 100644 index 000000000..8fdb5fcbc --- /dev/null +++ b/vlib/v/checker/tests/option_or_block_none_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/option_or_block_none_err.vv:19:32: error: wrong return type `none` in the `or {}` block, expected `Animal` + 17 | + 18 | fn main() { + 19 | mut dog := new_animal(9) or { none } + | ~~~~ + 20 | + 21 | println(dog) \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_or_block_none_err.vv b/vlib/v/checker/tests/option_or_block_none_err.vv similarity index 100% rename from vlib/v/checker/tests/optional_or_block_none_err.vv rename to vlib/v/checker/tests/option_or_block_none_err.vv diff --git a/vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.out b/vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.out new file mode 100644 index 000000000..89d945c97 --- /dev/null +++ b/vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.vv:13:3: error: the default expression type in the `or` block should be `string`, instead you gave a value of type `int literal` + 11 | // must be of the same type of the return + 12 | // type of the `test_option` function + 13 | 123 + | ~~~ + 14 | // 'I break things' + 15 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.vv b/vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.vv new file mode 100644 index 000000000..ec75da47f --- /dev/null +++ b/vlib/v/checker/tests/option_or_block_returns_value_of_incompatible_type.vv @@ -0,0 +1,16 @@ +fn test_option(fail bool) ?string { + if fail { + return error('false') + } + return 'fff' +} + +fn main() { + // a := test_option(false) or { println(err) } + test_option(true) or { + // must be of the same type of the return + // type of the `test_option` function + 123 + // 'I break things' + } +} diff --git a/vlib/v/checker/tests/option_propagate_nested.out b/vlib/v/checker/tests/option_propagate_nested.out new file mode 100644 index 000000000..3174c43a1 --- /dev/null +++ b/vlib/v/checker/tests/option_propagate_nested.out @@ -0,0 +1,28 @@ +vlib/v/checker/tests/option_propagate_nested.vv:10:18: error: to propagate the option call, `xx_prop` must return an option + 8 | + 9 | fn xx_prop() string { + 10 | s := ret(raise()?) + | ^ + 11 | return s + 12 | } +Details: vlib/v/checker/tests/option_propagate_nested.vv:9:14: details: prepend ? before the declaration of the return type of `xx_prop` + 7 | } + 8 | + 9 | fn xx_prop() string { + | ~~~~~~ + 10 | s := ret(raise()?) + 11 | return s +vlib/v/checker/tests/option_propagate_nested.vv:28:21: error: to propagate the option call, `aa_propagate` must return an option + 26 | + 27 | fn (mut s St) aa_propagate() { + 28 | f := retf(s.raise()?) + | ^ + 29 | s.z = 7.5 + 30 | println(f) +Details: vlib/v/checker/tests/option_propagate_nested.vv:27:30: details: prepend ? before the declaration of the return type of `aa_propagate` + 25 | } + 26 | + 27 | fn (mut s St) aa_propagate() { + | ^ + 28 | f := retf(s.raise()?) + 29 | s.z = 7.5 \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_propagate_nested.vv b/vlib/v/checker/tests/option_propagate_nested.vv similarity index 100% rename from vlib/v/checker/tests/optional_propagate_nested.vv rename to vlib/v/checker/tests/option_propagate_nested.vv diff --git a/vlib/v/checker/tests/optional_type_call_err.out b/vlib/v/checker/tests/option_type_call_err.out similarity index 51% rename from vlib/v/checker/tests/optional_type_call_err.out rename to vlib/v/checker/tests/option_type_call_err.out index b4002ca53..400c497db 100644 --- a/vlib/v/checker/tests/optional_type_call_err.out +++ b/vlib/v/checker/tests/option_type_call_err.out @@ -1,6 +1,6 @@ -vlib/v/checker/tests/optional_type_call_err.vv:4:5: error: result type cannot be called directly +vlib/v/checker/tests/option_type_call_err.vv:4:5: error: result type cannot be called directly 2 | 3 | fn main() { 4 | os.ls('.').filter(it.ends_with('.v')) or { return } | ~~~~~~~ - 5 | } + 5 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_type_call_err.vv b/vlib/v/checker/tests/option_type_call_err.vv similarity index 100% rename from vlib/v/checker/tests/optional_type_call_err.vv rename to vlib/v/checker/tests/option_type_call_err.vv diff --git a/vlib/v/checker/tests/option_variable_err.out b/vlib/v/checker/tests/option_variable_err.out new file mode 100644 index 000000000..e45a818e7 --- /dev/null +++ b/vlib/v/checker/tests/option_variable_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/option_variable_err.vv:2:7: error: casting to option type is forbidden + 1 | fn main() { + 2 | _ := ?bool(false) + | ~~~~~~~~~~~~ + 3 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/optional_variable_err.vv b/vlib/v/checker/tests/option_variable_err.vv similarity index 100% rename from vlib/v/checker/tests/optional_variable_err.vv rename to vlib/v/checker/tests/option_variable_err.vv diff --git a/vlib/v/checker/tests/optional_fields_addr_err.out b/vlib/v/checker/tests/optional_fields_addr_err.out deleted file mode 100644 index f41665ffe..000000000 --- a/vlib/v/checker/tests/optional_fields_addr_err.out +++ /dev/null @@ -1,13 +0,0 @@ -vlib/v/checker/tests/optional_fields_addr_err.vv:9:10: error: cannot take the address of an optional field - 7 | fn main() { - 8 | x := Wrapper{} - 9 | if _ := &x.value { - | ~~~~~~~~ - 10 | } - 11 | -vlib/v/checker/tests/optional_fields_addr_err.vv:12:7: error: cannot take the address of an optional field - 10 | } - 11 | - 12 | _ := &x.value - | ~~~~~~~~ - 13 | } diff --git a/vlib/v/checker/tests/optional_fn_err.out b/vlib/v/checker/tests/optional_fn_err.out deleted file mode 100644 index 105498e3e..000000000 --- a/vlib/v/checker/tests/optional_fn_err.out +++ /dev/null @@ -1,168 +0,0 @@ -vlib/v/checker/tests/optional_fn_err.vv:13:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 11 | - 12 | const ( - 13 | const_value = bar(0) - | ~~~~~~ - 14 | ) - 15 | -vlib/v/checker/tests/optional_fn_err.vv:19:14: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 17 | f fn (int) - 18 | mut: - 19 | value int = bar(0) - | ~~~~~~ - 20 | opt ?int = bar(0) - 21 | } -vlib/v/checker/tests/optional_fn_err.vv:33:2: error: foo() returns an option, so it should have either an `or {}` block, or `?` at the end - 31 | fn main() { - 32 | // call fn - 33 | foo() - | ~~~~~ - 34 | _ := bar(0) - 35 | println(twice(bar(0))) -vlib/v/checker/tests/optional_fn_err.vv:34:7: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 32 | // call fn - 33 | foo() - 34 | _ := bar(0) - | ~~~~~~ - 35 | println(twice(bar(0))) - 36 | -vlib/v/checker/tests/optional_fn_err.vv:35:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 33 | foo() - 34 | _ := bar(0) - 35 | println(twice(bar(0))) - | ~~~~~~ - 36 | - 37 | // anon fn -vlib/v/checker/tests/optional_fn_err.vv:38:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 36 | - 37 | // anon fn - 38 | fn (_ int) {}(bar(0)) - | ~~~~~~ - 39 | - 40 | // assert -vlib/v/checker/tests/optional_fn_err.vv:41:9: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 39 | - 40 | // assert - 41 | assert bar(true) - | ~~~~~~~~~ - 42 | - 43 | // struct -vlib/v/checker/tests/optional_fn_err.vv:46:10: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 44 | mut v := Data{ - 45 | f: fn (_ int) {}, - 46 | value: bar(0), - | ~~~~~~ - 47 | opt: bar(0), - 48 | } -vlib/v/checker/tests/optional_fn_err.vv:49:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 47 | opt: bar(0), - 48 | } - 49 | v.add(bar(0)) // call method - | ~~~~~~ - 50 | v.f(bar(0)) // call fn field - 51 | -vlib/v/checker/tests/optional_fn_err.vv:50:6: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 48 | } - 49 | v.add(bar(0)) // call method - 50 | v.f(bar(0)) // call fn field - | ~~~~~~ - 51 | - 52 | // array -vlib/v/checker/tests/optional_fn_err.vv:54:9: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 52 | // array - 53 | mut arr := [1, 2] - 54 | arr << bar(0) - | ~~~~~~ - 55 | // init - 56 | _ := [bar(0)] -vlib/v/checker/tests/optional_fn_err.vv:56:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 54 | arr << bar(0) - 55 | // init - 56 | _ := [bar(0)] - | ~~~~~~ - 57 | _ := []int{len: 1, init: bar(0)} - 58 | _ := [bar(0)]! -vlib/v/checker/tests/optional_fn_err.vv:57:27: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 55 | // init - 56 | _ := [bar(0)] - 57 | _ := []int{len: 1, init: bar(0)} - | ~~~~~~ - 58 | _ := [bar(0)]! - 59 | _ := [1]int{init: bar(0)} -vlib/v/checker/tests/optional_fn_err.vv:58:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 56 | _ := [bar(0)] - 57 | _ := []int{len: 1, init: bar(0)} - 58 | _ := [bar(0)]! - | ~~~~~~ - 59 | _ := [1]int{init: bar(0)} - 60 | // index -vlib/v/checker/tests/optional_fn_err.vv:61:13: error: cannot use optional or result as index (array type `[]int`) - 59 | _ := [1]int{init: bar(0)} - 60 | // index - 61 | println(arr[bar(0)]) - | ~~~~~~~~ - 62 | // array builtin methods - 63 | arr.insert(0, bar(0)) -vlib/v/checker/tests/optional_fn_err.vv:63:16: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 61 | println(arr[bar(0)]) - 62 | // array builtin methods - 63 | arr.insert(0, bar(0)) - | ~~~~~~ - 64 | arr.prepend(bar(0)) - 65 | arr.contains(bar(0)) -vlib/v/checker/tests/optional_fn_err.vv:64:14: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 62 | // array builtin methods - 63 | arr.insert(0, bar(0)) - 64 | arr.prepend(bar(0)) - | ~~~~~~ - 65 | arr.contains(bar(0)) - 66 | arr.index(bar(0)) -vlib/v/checker/tests/optional_fn_err.vv:65:15: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 63 | arr.insert(0, bar(0)) - 64 | arr.prepend(bar(0)) - 65 | arr.contains(bar(0)) - | ~~~~~~ - 66 | arr.index(bar(0)) - 67 | println(arr.map(bar(0))) -vlib/v/checker/tests/optional_fn_err.vv:66:12: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 64 | arr.prepend(bar(0)) - 65 | arr.contains(bar(0)) - 66 | arr.index(bar(0)) - | ~~~~~~ - 67 | println(arr.map(bar(0))) - 68 | println(arr.filter(bar(true))) -vlib/v/checker/tests/optional_fn_err.vv:67:18: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 65 | arr.contains(bar(0)) - 66 | arr.index(bar(0)) - 67 | println(arr.map(bar(0))) - | ~~~~~~ - 68 | println(arr.filter(bar(true))) - 69 | println(arr.any(bar(true))) -vlib/v/checker/tests/optional_fn_err.vv:68:21: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 66 | arr.index(bar(0)) - 67 | println(arr.map(bar(0))) - 68 | println(arr.filter(bar(true))) - | ~~~~~~~~~ - 69 | println(arr.any(bar(true))) - 70 | println(arr.all(bar(true))) -vlib/v/checker/tests/optional_fn_err.vv:69:18: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 67 | println(arr.map(bar(0))) - 68 | println(arr.filter(bar(true))) - 69 | println(arr.any(bar(true))) - | ~~~~~~~~~ - 70 | println(arr.all(bar(true))) - 71 | -vlib/v/checker/tests/optional_fn_err.vv:70:18: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 68 | println(arr.filter(bar(true))) - 69 | println(arr.any(bar(true))) - 70 | println(arr.all(bar(true))) - | ~~~~~~~~~ - 71 | - 72 | match bar(0) { -vlib/v/checker/tests/optional_fn_err.vv:72:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end - 70 | println(arr.all(bar(true))) - 71 | - 72 | match bar(0) { - | ~~~~~~ - 73 | 0 { } - 74 | else { } diff --git a/vlib/v/checker/tests/optional_in_dump_err.out b/vlib/v/checker/tests/optional_in_dump_err.out deleted file mode 100644 index 0d63178d4..000000000 --- a/vlib/v/checker/tests/optional_in_dump_err.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/optional_in_dump_err.vv:10:7: error: create() returns an option, so it should have either an `or {}` block, or `?` at the end - 8 | - 9 | fn main() { - 10 | dump(create()) - | ~~~~~~~~ - 11 | } diff --git a/vlib/v/checker/tests/optional_in_println_mismatch.out b/vlib/v/checker/tests/optional_in_println_mismatch.out deleted file mode 100644 index 8f1358660..000000000 --- a/vlib/v/checker/tests/optional_in_println_mismatch.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/optional_in_println_mismatch.vv:6:23: error: wrong return type `string` in the `or {}` block, expected `int` - 4 | - 5 | fn main() { - 6 | println(funcy() or { '' }) - | ~~ - 7 | } diff --git a/vlib/v/checker/tests/optional_or_block_mismatch.out b/vlib/v/checker/tests/optional_or_block_mismatch.out deleted file mode 100644 index 28f19c7b2..000000000 --- a/vlib/v/checker/tests/optional_or_block_mismatch.out +++ /dev/null @@ -1,13 +0,0 @@ -vlib/v/checker/tests/optional_or_block_mismatch.vv:10:18: error: wrong return type `Bar` in the `or {}` block, expected `&Bar` - 8 | - 9 | fn main() { - 10 | x := foo() or { Bar{} } - | ~~~~~ - 11 | println(x) - 12 | -vlib/v/checker/tests/optional_or_block_mismatch.vv:13:13: error: the default expression type in the `or` block should be `&Bar`, instead you gave a value of type `Bar` - 11 | println(x) - 12 | - 13 | foo() or { Bar{} } - | ~~~~~ - 14 | } diff --git a/vlib/v/checker/tests/optional_or_block_none_err.out b/vlib/v/checker/tests/optional_or_block_none_err.out deleted file mode 100644 index ea420af19..000000000 --- a/vlib/v/checker/tests/optional_or_block_none_err.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/optional_or_block_none_err.vv:19:32: error: wrong return type `none` in the `or {}` block, expected `Animal` - 17 | - 18 | fn main() { - 19 | mut dog := new_animal(9) or { none } - | ~~~~ - 20 | - 21 | println(dog) diff --git a/vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.out b/vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.out deleted file mode 100644 index 0cd1b389a..000000000 --- a/vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.vv:13:3: error: the default expression type in the `or` block should be `string`, instead you gave a value of type `int literal` - 11 | // must be of the same type of the return - 12 | // type of the `test_optional` function - 13 | 123 - | ~~~ - 14 | // 'I break things' - 15 | } diff --git a/vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.vv b/vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.vv deleted file mode 100644 index 9935bb7a5..000000000 --- a/vlib/v/checker/tests/optional_or_block_returns_value_of_incompatible_type.vv +++ /dev/null @@ -1,16 +0,0 @@ -fn test_optional(fail bool) ?string { - if fail { - return error('false') - } - return 'fff' -} - -fn main() { - // a := test_optional(false) or { println(err) } - test_optional(true) or { - // must be of the same type of the return - // type of the `test_optional` function - 123 - // 'I break things' - } -} diff --git a/vlib/v/checker/tests/optional_propagate_nested.out b/vlib/v/checker/tests/optional_propagate_nested.out deleted file mode 100644 index 375873eed..000000000 --- a/vlib/v/checker/tests/optional_propagate_nested.out +++ /dev/null @@ -1,28 +0,0 @@ -vlib/v/checker/tests/optional_propagate_nested.vv:10:18: error: to propagate the optional call, `xx_prop` must return an optional - 8 | - 9 | fn xx_prop() string { - 10 | s := ret(raise()?) - | ^ - 11 | return s - 12 | } -Details: vlib/v/checker/tests/optional_propagate_nested.vv:9:14: details: prepend ? before the declaration of the return type of `xx_prop` - 7 | } - 8 | - 9 | fn xx_prop() string { - | ~~~~~~ - 10 | s := ret(raise()?) - 11 | return s -vlib/v/checker/tests/optional_propagate_nested.vv:28:21: error: to propagate the optional call, `aa_propagate` must return an optional - 26 | - 27 | fn (mut s St) aa_propagate() { - 28 | f := retf(s.raise()?) - | ^ - 29 | s.z = 7.5 - 30 | println(f) -Details: vlib/v/checker/tests/optional_propagate_nested.vv:27:30: details: prepend ? before the declaration of the return type of `aa_propagate` - 25 | } - 26 | - 27 | fn (mut s St) aa_propagate() { - | ^ - 28 | f := retf(s.raise()?) - 29 | s.z = 7.5 diff --git a/vlib/v/checker/tests/optional_variable_err.out b/vlib/v/checker/tests/optional_variable_err.out deleted file mode 100644 index 997774b02..000000000 --- a/vlib/v/checker/tests/optional_variable_err.out +++ /dev/null @@ -1,5 +0,0 @@ -vlib/v/checker/tests/optional_variable_err.vv:2:7: error: casting to optional type is forbidden - 1 | fn main() { - 2 | _ := ?bool(false) - | ~~~~~~~~~~~~ - 3 | } diff --git a/vlib/v/checker/tests/return_option_of_multiple_results.out b/vlib/v/checker/tests/return_option_of_multiple_results.out new file mode 100644 index 000000000..64dad71db --- /dev/null +++ b/vlib/v/checker/tests/return_option_of_multiple_results.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/return_option_of_multiple_results.vv:2:9: error: `?` is not needed, use `return test()` + 1 | fn test() ?(&int, &int) { + 2 | return test()? + | ~~~~~~ + 3 | } + 4 | \ No newline at end of file diff --git a/vlib/v/checker/tests/return_optional_of_multiple_results.vv b/vlib/v/checker/tests/return_option_of_multiple_results.vv similarity index 71% rename from vlib/v/checker/tests/return_optional_of_multiple_results.vv rename to vlib/v/checker/tests/return_option_of_multiple_results.vv index fc89b13df..ebf65280a 100644 --- a/vlib/v/checker/tests/return_optional_of_multiple_results.vv +++ b/vlib/v/checker/tests/return_option_of_multiple_results.vv @@ -1,5 +1,5 @@ fn test() ?(&int, &int) { - return test() ? + return test()? } fn main() { diff --git a/vlib/v/checker/tests/return_optional_of_multiple_results.out b/vlib/v/checker/tests/return_optional_of_multiple_results.out deleted file mode 100644 index 8a7d97208..000000000 --- a/vlib/v/checker/tests/return_optional_of_multiple_results.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/return_optional_of_multiple_results.vv:2:9: error: `?` is not needed, use `return test()` - 1 | fn test() ?(&int, &int) { - 2 | return test() ? - | ~~~~~~ - 3 | } - 4 | diff --git a/vlib/v/checker/tests/run/noreturn_method_can_be_used_instead_of_panic.vv b/vlib/v/checker/tests/run/noreturn_method_can_be_used_instead_of_panic.vv index 581b5e645..9b6b7bcab 100644 --- a/vlib/v/checker/tests/run/noreturn_method_can_be_used_instead_of_panic.vv +++ b/vlib/v/checker/tests/run/noreturn_method_can_be_used_instead_of_panic.vv @@ -7,13 +7,13 @@ fn (mut t Test) zz_exit() { exit(0) } -fn optional() ?int { +fn option() ?int { return error('oh no') } fn main() { mut test := Test{} - a := optional() or { test.zz_exit() } + a := option() or { test.zz_exit() } println(a) exit(1) } diff --git a/vlib/v/checker/tests/selector_expr_option_err.out b/vlib/v/checker/tests/selector_expr_option_err.out new file mode 100644 index 000000000..a5f6b49dd --- /dev/null +++ b/vlib/v/checker/tests/selector_expr_option_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/selector_expr_option_err.vv:10:16: error: cannot access fields of an option, handle the error with `or {...}` or propagate it with `?` + 8 | + 9 | fn main() { + 10 | println(abc().status_code) + | ~~~~~~~~~~~ + 11 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/selector_expr_optional_err.vv b/vlib/v/checker/tests/selector_expr_option_err.vv similarity index 100% rename from vlib/v/checker/tests/selector_expr_optional_err.vv rename to vlib/v/checker/tests/selector_expr_option_err.vv diff --git a/vlib/v/checker/tests/selector_expr_optional_err.out b/vlib/v/checker/tests/selector_expr_optional_err.out deleted file mode 100644 index f79a9fb5a..000000000 --- a/vlib/v/checker/tests/selector_expr_optional_err.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/selector_expr_optional_err.vv:10:16: error: cannot access fields of an optional, handle the error with `or {...}` or propagate it with `?` - 8 | - 9 | fn main() { - 10 | println(abc().status_code) - | ~~~~~~~~~~~ - 11 | } diff --git a/vlib/v/checker/tests/struct_field_option_err.out b/vlib/v/checker/tests/struct_field_option_err.out new file mode 100644 index 000000000..30c6f2d48 --- /dev/null +++ b/vlib/v/checker/tests/struct_field_option_err.out @@ -0,0 +1,41 @@ +vlib/v/checker/tests/struct_field_option_err.vv:3:6: error: struct field does not support storing result + 1 | struct Foo { + 2 | mut: + 3 | foo !int + | ^ + 4 | bar ?int = 1 + 5 | baz int = 1 +vlib/v/checker/tests/struct_field_option_err.vv:11:8: error: field `bar` is an option, so it should have either an `or {}` block, or `?` at the end + 9 | mut f := Foo{} + 10 | + 11 | _ = f.bar + | ~~~ + 12 | _ = f.bar + 1 + 13 | +vlib/v/checker/tests/struct_field_option_err.vv:12:12: error: `+` cannot be used with `?int` + 10 | + 11 | _ = f.bar + 12 | _ = f.bar + 1 + | ^ + 13 | + 14 | _ = f.bar! +vlib/v/checker/tests/struct_field_option_err.vv:14:11: error: to propagate a result, the call must also return a result type + 12 | _ = f.bar + 1 + 13 | + 14 | _ = f.bar! + | ^ + 15 | + 16 | _ = f.bar or { _ = 1 } +vlib/v/checker/tests/struct_field_option_err.vv:16:19: error: last statement in the `or {}` block should be an expression of type `int` or exit parent scope + 14 | _ = f.bar! + 15 | + 16 | _ = f.bar or { _ = 1 } + | ^ + 17 | + 18 | _ = f.baz? +vlib/v/checker/tests/struct_field_option_err.vv:18:11: error: unexpected `?`, the field `baz` is neither an option, nor a result + 16 | _ = f.bar or { _ = 1 } + 17 | + 18 | _ = f.baz? + | ^ + 19 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/struct_field_optional_err.vv b/vlib/v/checker/tests/struct_field_option_err.vv similarity index 100% rename from vlib/v/checker/tests/struct_field_optional_err.vv rename to vlib/v/checker/tests/struct_field_option_err.vv diff --git a/vlib/v/checker/tests/struct_field_optional_err.out b/vlib/v/checker/tests/struct_field_optional_err.out deleted file mode 100644 index 9c3cbd445..000000000 --- a/vlib/v/checker/tests/struct_field_optional_err.out +++ /dev/null @@ -1,41 +0,0 @@ -vlib/v/checker/tests/struct_field_optional_err.vv:3:6: error: struct field does not support storing result - 1 | struct Foo { - 2 | mut: - 3 | foo !int - | ^ - 4 | bar ?int = 1 - 5 | baz int = 1 -vlib/v/checker/tests/struct_field_optional_err.vv:11:8: error: field `bar` is an option, so it should have either an `or {}` block, or `?` at the end - 9 | mut f := Foo{} - 10 | - 11 | _ = f.bar - | ~~~ - 12 | _ = f.bar + 1 - 13 | -vlib/v/checker/tests/struct_field_optional_err.vv:12:12: error: `+` cannot be used with `?int` - 10 | - 11 | _ = f.bar - 12 | _ = f.bar + 1 - | ^ - 13 | - 14 | _ = f.bar! -vlib/v/checker/tests/struct_field_optional_err.vv:14:11: error: to propagate a result, the call must also return a result type - 12 | _ = f.bar + 1 - 13 | - 14 | _ = f.bar! - | ^ - 15 | - 16 | _ = f.bar or { _ = 1 } -vlib/v/checker/tests/struct_field_optional_err.vv:16:19: error: last statement in the `or {}` block should be an expression of type `int` or exit parent scope - 14 | _ = f.bar! - 15 | - 16 | _ = f.bar or { _ = 1 } - | ^ - 17 | - 18 | _ = f.baz? -vlib/v/checker/tests/struct_field_optional_err.vv:18:11: error: unexpected `?`, the field `baz` is neither an optional, nor a result - 16 | _ = f.bar or { _ = 1 } - 17 | - 18 | _ = f.baz? - | ^ - 19 | } diff --git a/vlib/v/checker/tests/test_functions_wrong_signature_test.vv b/vlib/v/checker/tests/test_functions_wrong_signature_test.vv index 1fc4a5615..b5913d943 100644 --- a/vlib/v/checker/tests/test_functions_wrong_signature_test.vv +++ b/vlib/v/checker/tests/test_functions_wrong_signature_test.vv @@ -10,7 +10,7 @@ fn test_returning_int() int { } // NB: this is allowed explicitly now, to allow for shorter tests -// of functions returning optionals. +// of functions returning options. fn test_returning_opt() { assert true } diff --git a/vlib/v/checker/tests/type_cast_optional_err.out b/vlib/v/checker/tests/type_cast_option_err.out similarity index 53% rename from vlib/v/checker/tests/type_cast_optional_err.out rename to vlib/v/checker/tests/type_cast_option_err.out index bb52bad66..9a3276f9e 100644 --- a/vlib/v/checker/tests/type_cast_optional_err.out +++ b/vlib/v/checker/tests/type_cast_option_err.out @@ -1,5 +1,5 @@ -vlib/v/checker/tests/type_cast_optional_err.vv:2:13: error: cannot type cast an optional +vlib/v/checker/tests/type_cast_option_err.vv:2:10: error: cannot type cast an option 1 | fn main() { 2 | println(int('hi'.last_index('i'))) | ~~~~~~~~~~~~~~~~~~~~~~~~~ - 3 | } + 3 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/type_cast_option_err.vv b/vlib/v/checker/tests/type_cast_option_err.vv new file mode 100644 index 000000000..5c9cc124e --- /dev/null +++ b/vlib/v/checker/tests/type_cast_option_err.vv @@ -0,0 +1,3 @@ +fn main() { + println(int('hi'.last_index('i'))) +} diff --git a/vlib/v/checker/tests/type_cast_optional_err.vv b/vlib/v/checker/tests/type_cast_optional_err.vv deleted file mode 100644 index 982559cbd..000000000 --- a/vlib/v/checker/tests/type_cast_optional_err.vv +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println(int('hi'.last_index('i'))) -} diff --git a/vlib/v/checker/tests/unexpected_or.out b/vlib/v/checker/tests/unexpected_or.out index 03eeaea33..4c5e42664 100644 --- a/vlib/v/checker/tests/unexpected_or.out +++ b/vlib/v/checker/tests/unexpected_or.out @@ -1,6 +1,6 @@ -vlib/v/checker/tests/unexpected_or.vv:6:17: error: unexpected `or` block, the function `ret_zero` does neither return an optional nor a result - 4 | +vlib/v/checker/tests/unexpected_or.vv:6:17: error: unexpected `or` block, the function `ret_zero` does not return an option or a result + 4 | 5 | fn main() { 6 | _ = ret_zero() or { 1 } | ~~~~~~~~ - 7 | } + 7 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/unexpected_or_propagate.out b/vlib/v/checker/tests/unexpected_or_propagate.out index 7470c57b5..d4ac1c447 100644 --- a/vlib/v/checker/tests/unexpected_or_propagate.out +++ b/vlib/v/checker/tests/unexpected_or_propagate.out @@ -1,7 +1,7 @@ -vlib/v/checker/tests/unexpected_or_propagate.vv:6:17: error: unexpected `?`, the function `ret_zero` does neither return an optional nor a result +vlib/v/checker/tests/unexpected_or_propagate.vv:6:17: error: unexpected `?`, the function `ret_zero` does not return an option or a result 4 | 5 | fn opt_fn() ?int { 6 | a := ret_zero()? | ^ 7 | return a - 8 | } + 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/unwrapped_option_infix.out b/vlib/v/checker/tests/unwrapped_option_infix.out new file mode 100644 index 000000000..ece51059b --- /dev/null +++ b/vlib/v/checker/tests/unwrapped_option_infix.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/unwrapped_option_infix.vv:5:9: error: unwrapped option cannot be used in an infix expression + 3 | } + 4 | + 5 | println(test() == '') + | ~~~~~~ \ No newline at end of file diff --git a/vlib/v/checker/tests/unwrapped_option_infix.vv b/vlib/v/checker/tests/unwrapped_option_infix.vv new file mode 100644 index 000000000..0c863f510 --- /dev/null +++ b/vlib/v/checker/tests/unwrapped_option_infix.vv @@ -0,0 +1,5 @@ +fn test() ?string { + return '' +} + +println(test() == '') diff --git a/vlib/v/checker/tests/unwrapped_optional_infix.out b/vlib/v/checker/tests/unwrapped_optional_infix.out deleted file mode 100644 index 96ab9cf70..000000000 --- a/vlib/v/checker/tests/unwrapped_optional_infix.out +++ /dev/null @@ -1,5 +0,0 @@ -vlib/v/checker/tests/unwrapped_optional_infix.vv:5:9: error: unwrapped optional cannot be used in an infix expression - 3 | } - 4 | - 5 | println(test() == "") - | ~~~~~~ diff --git a/vlib/v/checker/tests/unwrapped_optional_infix.vv b/vlib/v/checker/tests/unwrapped_optional_infix.vv deleted file mode 100644 index 432b0cc5b..000000000 --- a/vlib/v/checker/tests/unwrapped_optional_infix.vv +++ /dev/null @@ -1,5 +0,0 @@ -fn test() ?string { - return "" -} - -println(test() == "") diff --git a/vlib/v/checker/tests/void_optional_err.out b/vlib/v/checker/tests/void_option_err.out similarity index 50% rename from vlib/v/checker/tests/void_optional_err.out rename to vlib/v/checker/tests/void_option_err.out index c9efcb72b..115edbc20 100644 --- a/vlib/v/checker/tests/void_optional_err.out +++ b/vlib/v/checker/tests/void_option_err.out @@ -1,5 +1,5 @@ -vlib/v/checker/tests/void_optional_err.vv:1:16: error: use `?` instead of `?void` +vlib/v/checker/tests/void_option_err.vv:1:16: error: use `?` instead of `?void` 1 | fn ret_void() ?void { | ~~~~ 2 | return error('error') - 3 | } + 3 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/void_optional_err.vv b/vlib/v/checker/tests/void_option_err.vv similarity index 100% rename from vlib/v/checker/tests/void_optional_err.vv rename to vlib/v/checker/tests/void_option_err.vv diff --git a/vlib/v/checker/tests/wrong_propagate_ret_type.out b/vlib/v/checker/tests/wrong_propagate_ret_type.out index 53f55a187..c122f0227 100644 --- a/vlib/v/checker/tests/wrong_propagate_ret_type.out +++ b/vlib/v/checker/tests/wrong_propagate_ret_type.out @@ -1,5 +1,5 @@ -vlib/v/checker/tests/wrong_propagate_ret_type.vv:10:17: error: to propagate the optional call, `opt_call` must return an optional - 8 | +vlib/v/checker/tests/wrong_propagate_ret_type.vv:10:17: error: to propagate the option call, `opt_call` must return an option + 8 | 9 | fn opt_call() int { 10 | a := ret_none()? | ^ @@ -7,13 +7,13 @@ vlib/v/checker/tests/wrong_propagate_ret_type.vv:10:17: error: to propagate the 12 | } Details: vlib/v/checker/tests/wrong_propagate_ret_type.vv:9:15: details: prepend ? before the declaration of the return type of `opt_call` 7 | } - 8 | + 8 | 9 | fn opt_call() int { | ~~~ 10 | a := ret_none()? 11 | return a vlib/v/checker/tests/wrong_propagate_ret_type.vv:15:17: error: to propagate the result call, `res_call` must return a result - 13 | + 13 | 14 | fn res_call() bool { 15 | a := ret_bool()! | ^ @@ -21,8 +21,8 @@ vlib/v/checker/tests/wrong_propagate_ret_type.vv:15:17: error: to propagate the 17 | } Details: vlib/v/checker/tests/wrong_propagate_ret_type.vv:14:15: details: prepend ! before the declaration of the return type of `res_call` 12 | } - 13 | + 13 | 14 | fn res_call() bool { | ~~~~ 15 | a := ret_bool()! - 16 | return a + 16 | return a \ No newline at end of file diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6bc44b364..fbf839efd 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1419,7 +1419,7 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) { ret_str := f.no_cur_mod(f.table.type_to_str_using_aliases(fn_info.return_type, f.mod2alias)) f.write(' ${ret_str}') - } else if fn_info.return_type.has_flag(.optional) { + } else if fn_info.return_type.has_flag(.option) { f.write(' ?') } else if fn_info.return_type.has_flag(.result) { f.write(' !') diff --git a/vlib/v/fmt/tests/fntype_mut_args_with_optional_keep.vv b/vlib/v/fmt/tests/fntype_mut_args_with_option_keep.vv similarity index 100% rename from vlib/v/fmt/tests/fntype_mut_args_with_optional_keep.vv rename to vlib/v/fmt/tests/fntype_mut_args_with_option_keep.vv diff --git a/vlib/v/fmt/tests/fntype_return_optional_keep.vv b/vlib/v/fmt/tests/fntype_return_option_keep.vv similarity index 100% rename from vlib/v/fmt/tests/fntype_return_optional_keep.vv rename to vlib/v/fmt/tests/fntype_return_option_keep.vv diff --git a/vlib/v/fmt/tests/functions_expected.vv b/vlib/v/fmt/tests/functions_expected.vv index 1d88e63d3..20cf59bf1 100644 --- a/vlib/v/fmt/tests/functions_expected.vv +++ b/vlib/v/fmt/tests/functions_expected.vv @@ -40,14 +40,14 @@ fn (this User) fn_with_receiver() { println('') } -fn fn_with_optional() ?int { +fn fn_with_option() ?int { if true { return error('true') } return 30 } -fn (f Foo) fn_with_optional() ?int { +fn (f Foo) fn_with_option() ?int { if true { return error('true') } diff --git a/vlib/v/fmt/tests/functions_input.vv b/vlib/v/fmt/tests/functions_input.vv index cbd98656c..20cf59bf1 100644 --- a/vlib/v/fmt/tests/functions_input.vv +++ b/vlib/v/fmt/tests/functions_input.vv @@ -1,52 +1,56 @@ fn C.func(arg int) int -fn fn_variadic(arg int, args... string) { +fn fn_variadic(arg int, args ...string) { println('Do nothing') } fn fn_with_assign_stmts() { - _,_ := fn_with_multi_return() + _, _ := fn_with_multi_return() } -fn fn_with_multi_return() (int,string) { - return 0,'test' +fn fn_with_multi_return() (int, string) { + return 0, 'test' } -fn voidfn(){ - println('this is a function that does not return anything') - } +fn voidfn() { + println('this is a function that does not return anything') +} fn fn_with_1_arg(arg int) int { -return 0 + return 0 } fn fn_with_2a_args(arg1 int, arg2 int) int { -return 0 + return 0 } fn fn_with_2_args_to_be_shorten(arg1 int, arg2 int) int { -return 0 + return 0 } fn fn_with_2b_args(arg1 string, arg2 int) int { -return 0 + return 0 } fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int { -return 0 + return 0 } -fn (this User) fn_with_receiver() { -println('') +fn (this User) fn_with_receiver() { + println('') } -fn fn_with_optional() ?int { - if true { return error('true') } +fn fn_with_option() ?int { + if true { + return error('true') + } return 30 } -fn (f Foo) fn_with_optional() ?int { - if true { return error('true') } +fn (f Foo) fn_with_option() ?int { + if true { + return error('true') + } return 40 } @@ -58,6 +62,7 @@ fn fn_with_ref_return() &Foo { return &Foo{} } -[inline] fn fn_with_flag() { -println('flag') +[inline] +fn fn_with_flag() { + println('flag') } diff --git a/vlib/v/fmt/tests/language_prefixes_keep.vv b/vlib/v/fmt/tests/language_prefixes_keep.vv index a0a25c599..eb1842cdd 100644 --- a/vlib/v/fmt/tests/language_prefixes_keep.vv +++ b/vlib/v/fmt/tests/language_prefixes_keep.vv @@ -8,6 +8,6 @@ fn main() { JS.Math.abs(0) } -fn object_ref_optional() ?&C.File { +fn object_ref_option() ?&C.File { return error('') } diff --git a/vlib/v/fmt/tests/map_value_with_optional_result_expected.vv b/vlib/v/fmt/tests/map_value_with_option_result_expected.vv similarity index 100% rename from vlib/v/fmt/tests/map_value_with_optional_result_expected.vv rename to vlib/v/fmt/tests/map_value_with_option_result_expected.vv diff --git a/vlib/v/fmt/tests/map_value_with_optional_result_input.vv b/vlib/v/fmt/tests/map_value_with_option_result_input.vv similarity index 100% rename from vlib/v/fmt/tests/map_value_with_optional_result_input.vv rename to vlib/v/fmt/tests/map_value_with_option_result_input.vv diff --git a/vlib/v/fmt/tests/optional_keep.vv b/vlib/v/fmt/tests/option_keep.vv similarity index 100% rename from vlib/v/fmt/tests/optional_keep.vv rename to vlib/v/fmt/tests/option_keep.vv diff --git a/vlib/v/fmt/tests/optional_propagate_keep.vv b/vlib/v/fmt/tests/option_propagate_keep.vv similarity index 100% rename from vlib/v/fmt/tests/optional_propagate_keep.vv rename to vlib/v/fmt/tests/option_propagate_keep.vv diff --git a/vlib/v/fmt/tests/optional_with_multi_stmts_keep.vv b/vlib/v/fmt/tests/option_with_multi_stmts_keep.vv similarity index 100% rename from vlib/v/fmt/tests/optional_with_multi_stmts_keep.vv rename to vlib/v/fmt/tests/option_with_multi_stmts_keep.vv diff --git a/vlib/v/fmt/tests/or_keep.vv b/vlib/v/fmt/tests/or_keep.vv index ca2f26ba6..3e4263f9a 100644 --- a/vlib/v/fmt/tests/or_keep.vv +++ b/vlib/v/fmt/tests/or_keep.vv @@ -5,12 +5,12 @@ fn main() { } fn fn_with_or() int { - fn_with_optional() or { return 10 } + fn_with_option() or { return 10 } return 20 } fn (f Foo) method_with_or() int { - f.fn_with_optional() or { return 10 } + f.fn_with_option() or { return 10 } return 20 } diff --git a/vlib/v/fmt/tests/void_optional_keep.vv b/vlib/v/fmt/tests/void_option_keep.vv similarity index 100% rename from vlib/v/fmt/tests/void_optional_keep.vv rename to vlib/v/fmt/tests/void_option_keep.vv diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 2f917a0bb..7d339b52c 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -195,7 +195,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { && !g.pref.translated g.is_assign_lhs = true g.assign_op = node.op - if val_type.has_flag(.optional) || val_type.has_flag(.result) { + if val_type.has_flag(.option) || val_type.has_flag(.result) { g.right_is_opt = true } if blank_assign { @@ -418,14 +418,14 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { cloned = true } } - unwrap_optional := !var_type.has_flag(.optional) && val_type.has_flag(.optional) - if unwrap_optional { - // Unwrap the optional now that the testing code has been prepended. + unwrap_option := !var_type.has_flag(.option) && val_type.has_flag(.option) + if unwrap_option { + // Unwrap the option now that the testing code has been prepended. // `pos := s.index(... // `int pos = *(int)_t10.data;` // if g.is_autofree { /* - if is_optional { + if is_option { g.write('*($styp*)') g.write(tmp_opt + '.data/*FFz*/') g.right_is_opt = false @@ -438,7 +438,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { } if !cloned { if !g.inside_comptime_for_field - && ((var_type.has_flag(.optional) && !val_type.has_flag(.optional)) + && ((var_type.has_flag(.option) && !val_type.has_flag(.option)) || (var_type.has_flag(.result) && !val_type.has_flag(.result))) { tmp_var := g.new_tmp_var() g.expr_with_tmp_var(val, val_type, var_type, tmp_var) @@ -521,9 +521,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { fn (mut g Gen) gen_multi_return_assign(node &ast.AssignStmt, return_type ast.Type) { // multi return // TODO Handle in if_expr - is_opt := return_type.has_flag(.optional) || return_type.has_flag(.result) + is_opt := return_type.has_flag(.option) || return_type.has_flag(.result) mr_var_name := 'mr_${node.pos.pos}' - mr_styp := g.typ(return_type.clear_flag(.optional).clear_flag(.result)) + mr_styp := g.typ(return_type.clear_flag(.option).clear_flag(.result)) g.write('${mr_styp} ${mr_var_name} = ') g.expr(node.right[0]) g.writeln(';') @@ -596,7 +596,7 @@ fn (mut g Gen) gen_assign_vars_autofree(node &ast.AssignStmt) { // } // // - // Handle optionals. We need to declare a temp variable for them, that's why they are handled + // Handle options. We need to declare a temp variable for them, that's why they are handled // here, not in call_expr(). // `pos := s.index('x') or { return }` // ==========> @@ -608,19 +608,19 @@ fn (mut g Gen) gen_assign_vars_autofree(node &ast.AssignStmt) { // int pos = *(int*)_t190.data; // mut tmp_opt := '' /* - is_optional := false && g.is_autofree && (node.op in [.decl_assign, .assign]) + is_option := false && g.is_autofree && (node.op in [.decl_assign, .assign]) && node.left_types.len == 1 && node.right[0] is ast.CallExpr - if is_optional { - // g.write('/* optional assignment */') + if is_option { + // g.write('/* option assignment */') call_expr := node.right[0] as ast.CallExpr if call_expr.or_block.kind != .absent { - styp := g.typ(call_expr.return_type.set_flag(.optional)) + styp := g.typ(call_expr.return_type.set_flag(.option)) tmp_opt = g.new_tmp_var() g.write('/*AF opt*/$styp $tmp_opt = ') g.expr(node.right[0]) g.or_block(tmp_opt, call_expr.or_block, call_expr.return_type) g.writeln('/*=============ret*/') - // if af && is_optional { + // if af && is_option { // g.autofree_call_postgen() // } // return diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 916113789..5e067c14a 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -64,8 +64,8 @@ fn (mut g Gen) get_str_fn(typ ast.Type) string { unwrapped = ast.u64_type } } - if typ.has_flag(.optional) { - unwrapped.set_flag(.optional) + if typ.has_flag(.option) { + unwrapped.set_flag(.option) } if typ.has_flag(.result) { unwrapped.set_flag(.result) @@ -103,7 +103,7 @@ fn (mut g Gen) final_gen_str(typ StrType) { } g.generated_str_fns << typ sym := g.table.sym(typ.typ) - if sym.has_method_with_generic_parent('str') && !(typ.typ.has_flag(.optional) + if sym.has_method_with_generic_parent('str') && !(typ.typ.has_flag(.option) || typ.typ.has_flag(.result)) { return } @@ -113,7 +113,7 @@ fn (mut g Gen) final_gen_str(typ StrType) { return } g.str_fn_names << str_fn_name - if typ.typ.has_flag(.optional) { + if typ.typ.has_flag(.option) { g.gen_str_for_option(typ.typ, styp, str_fn_name) return } @@ -175,7 +175,7 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string) $if trace_autostr ? { eprintln('> gen_str_for_option: ${typ.debug()} | ${styp} | ${str_fn_name}') } - parent_type := typ.clear_flag(.optional) + parent_type := typ.clear_flag(.option) sym := g.table.sym(parent_type) sym_has_str_method, _, _ := sym.str_method_info() parent_str_fn_name := g.get_str_fn(parent_type) @@ -473,7 +473,7 @@ fn (mut g Gen) fn_decl_str(info ast.FnType) string { fn_str += ' !' } else if info.func.return_type != ast.void_type { x := util.strip_main_name(g.table.get_type_name(g.unwrap_generic(info.func.return_type))) - if info.func.return_type.has_flag(.optional) { + if info.func.return_type.has_flag(.option) { fn_str += ' ?${x}' } else { fn_str += ' ${x}' @@ -987,7 +987,7 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, _field_type ast.Type, fn_name strin return '${fn_name}(${deref}it.${c_name(field_name)}${sufix})', true } else { mut method_str := '' - if !field_type.is_ptr() && (field_type.has_flag(.optional) || field_type.has_flag(.result)) { + if !field_type.is_ptr() && (field_type.has_flag(.option) || field_type.has_flag(.result)) { method_str = '(*(${sym.name}*)it.${c_name(field_name)}.data)' } else { method_str = 'it.${c_name(field_name)}' diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 4ea3d1ad9..a6732f99d 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -104,15 +104,15 @@ mut: is_fn_index_call bool is_cc_msvc bool // g.pref.ccompiler == 'msvc' vlines_path string // set to the proper path for generating #line directives - optionals_pos_forward int // insertion point to forward + options_pos_forward int // insertion point to forward options_forward []string // to forward - optionals map[string]string // to avoid duplicates + options map[string]string // to avoid duplicates results_forward []string // to forward results map[string]string // to avoid duplicates - done_optionals shared []string // to avoid duplicates + done_options shared []string // to avoid duplicates done_results shared []string // to avoid duplicates - chan_pop_optionals map[string]string // types for `x := <-ch or {...}` - chan_push_optionals map[string]string // types for `ch <- x or {...}` + chan_pop_options map[string]string // types for `x := <-ch or {...}` + chan_push_options map[string]string // types for `ch <- x or {...}` mtxs string // array of mutexes if the `lock` has multiple variables labeled_loops map[string]&ast.Stmt inner_loop &ast.Stmt = unsafe { nil } @@ -123,9 +123,9 @@ mut: inside_map_index bool inside_opt_or_res bool inside_opt_data bool - inside_if_optional bool + inside_if_option bool inside_if_result bool - inside_match_optional bool + inside_match_option bool inside_match_result bool inside_vweb_tmpl bool inside_return bool @@ -367,14 +367,14 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) (string, for k, v in g.shareds { global_g.shareds[k] = v } - for k, v in g.chan_pop_optionals { - global_g.chan_pop_optionals[k] = v + for k, v in g.chan_pop_options { + global_g.chan_pop_options[k] = v } - for k, v in g.chan_push_optionals { - global_g.chan_push_optionals[k] = v + for k, v in g.chan_push_options { + global_g.chan_push_options[k] = v } - for k, v in g.optionals { - global_g.optionals[k] = v + for k, v in g.options { + global_g.options[k] = v } for k, v in g.results { global_g.results[k] = v @@ -441,14 +441,14 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) (string, global_g.write_sumtype_casting_fn(sumtype_casting_fn) } global_g.write_shareds() - global_g.write_chan_pop_optional_fns() - global_g.write_chan_push_optional_fns() + global_g.write_chan_pop_option_fns() + global_g.write_chan_push_option_fns() global_g.gen_array_contains_methods() global_g.gen_array_index_methods() global_g.gen_equality_fns() global_g.gen_free_methods() global_g.write_results() - global_g.write_optionals() + global_g.write_options() global_g.sort_globals_consts() util.timing_measure('cgen unification') @@ -478,9 +478,9 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) (string, g.write_init_function() } - // insert for optionals forward + // insert for options forward if g.out_options_forward.len > 0 || g.out_results_forward.len > 0 { - tail := g.type_definitions.cut_to(g.optionals_pos_forward) + tail := g.type_definitions.cut_to(g.options_pos_forward) if g.out_options_forward.len > 0 { g.type_definitions.writeln('// #start V forward option_xxx definitions:') g.type_definitions.writeln(g.out_options_forward.str()) @@ -657,7 +657,7 @@ fn cgen_process_one_file_cb(mut p pool.PoolProcessor, idx int, wid int) &Gen { threaded_fns: global_g.threaded_fns options_forward: global_g.options_forward results_forward: global_g.results_forward - done_optionals: global_g.done_optionals + done_options: global_g.done_options done_results: global_g.done_results is_autofree: global_g.pref.autofree referenced_fns: global_g.referenced_fns @@ -779,7 +779,7 @@ pub fn (mut g Gen) init() { g.cheaders.writeln('#include ') } g.write_builtin_types() - g.optionals_pos_forward = g.type_definitions.len + g.options_pos_forward = g.type_definitions.len g.write_typedef_types() g.write_typeof_functions() g.write_sorted_types() @@ -964,9 +964,9 @@ pub fn (mut g Gen) write_typeof_functions() { // V type to C typecc fn (mut g Gen) typ(t ast.Type) string { - if t.has_flag(.optional) { + if t.has_flag(.option) { // Register an optional if it's not registered yet - return g.register_optional(t) + return g.register_option(t) } else if t.has_flag(.result) { return g.register_result(t) } else { @@ -1045,7 +1045,7 @@ fn (mut g Gen) expr_string_surround(prepend string, expr ast.Expr, append string // but I(emily) would rather have this generation // all unified in one place so that it doesnt break // if one location changes -fn (mut g Gen) optional_type_name(t ast.Type) (string, string) { +fn (mut g Gen) option_type_name(t ast.Type) (string, string) { base := g.base_type(t) mut styp := '' sym := g.table.sym(t) @@ -1062,8 +1062,8 @@ fn (mut g Gen) optional_type_name(t ast.Type) (string, string) { fn (mut g Gen) result_type_name(t ast.Type) (string, string) { mut base := g.base_type(t) - if t.has_flag(.optional) { - g.register_optional(t) + if t.has_flag(.option) { + g.register_option(t) base = '_option_' + base } mut styp := '' @@ -1079,7 +1079,7 @@ fn (mut g Gen) result_type_name(t ast.Type) (string, string) { return styp, base } -fn (g Gen) optional_type_text(styp string, base string) string { +fn (g Gen) option_type_text(styp string, base string) string { // replace void with something else size := if base == 'void' { 'u8' @@ -1113,9 +1113,9 @@ fn (g Gen) result_type_text(styp string, base string) string { return ret } -fn (mut g Gen) register_optional(t ast.Type) string { - styp, base := g.optional_type_name(t) - g.optionals[base] = styp +fn (mut g Gen) register_option(t ast.Type) string { + styp, base := g.option_type_name(t) + g.options[base] = styp return styp } @@ -1125,21 +1125,21 @@ fn (mut g Gen) register_result(t ast.Type) string { return styp } -fn (mut g Gen) write_optionals() { +fn (mut g Gen) write_options() { mut done := []string{} - rlock g.done_optionals { - done = g.done_optionals.clone() + rlock g.done_options { + done = g.done_options.clone() } - for base, styp in g.optionals { + for base, styp in g.options { if base in done { continue } done << base g.typedefs.writeln('typedef struct ${styp} ${styp};') if base in g.options_forward { - g.out_options_forward.write_string(g.optional_type_text(styp, base) + ';\n\n') + g.out_options_forward.write_string(g.option_type_text(styp, base) + ';\n\n') } else { - g.out_options.write_string(g.optional_type_text(styp, base) + ';\n\n') + g.out_options.write_string(g.option_type_text(styp, base) + ';\n\n') } } } @@ -1258,13 +1258,13 @@ ${ret_typ} ${fn_name}(${thread_arr_typ} a) { return fn_name } -fn (mut g Gen) register_chan_pop_optional_call(opt_el_type string, styp string) { - g.chan_pop_optionals[opt_el_type] = styp +fn (mut g Gen) register_chan_pop_option_call(opt_el_type string, styp string) { + g.chan_pop_options[opt_el_type] = styp } -fn (mut g Gen) write_chan_pop_optional_fns() { +fn (mut g Gen) write_chan_pop_option_fns() { mut done := []string{} - for opt_el_type, styp in g.chan_pop_optionals { + for opt_el_type, styp in g.chan_pop_options { if opt_el_type in done { continue } @@ -1280,18 +1280,18 @@ static inline ${opt_el_type} __Option_${styp}_popval(${styp} ch) { } } -fn (mut g Gen) register_chan_push_optional_fn(el_type string, styp string) { - g.chan_push_optionals[styp] = el_type +fn (mut g Gen) register_chan_push_option_fn(el_type string, styp string) { + g.chan_push_options[styp] = el_type } -fn (mut g Gen) write_chan_push_optional_fns() { +fn (mut g Gen) write_chan_push_option_fns() { mut done := []string{} - for styp, el_type in g.chan_push_optionals { + for styp, el_type in g.chan_push_options { if styp in done { continue } done << styp - g.register_optional(ast.void_type.set_flag(.optional)) + g.register_option(ast.void_type.set_flag(.option)) g.channel_definitions.writeln(' static inline ${c.option_name}_void __Option_${styp}_pushval(${styp} ch, ${el_type} e) { if (sync__Channel_try_push_priv(ch, &e, false)) { @@ -1375,8 +1375,8 @@ pub fn (mut g Gen) write_typedef_types() { g.type_definitions.writeln(def_str) } else { g.type_definitions.writeln('typedef ${fixed} ${styp} [${len}];') - base := g.typ(info.elem_type.clear_flag(.optional).clear_flag(.result)) - if info.elem_type.has_flag(.optional) && base !in g.options_forward { + base := g.typ(info.elem_type.clear_flag(.option).clear_flag(.result)) + if info.elem_type.has_flag(.option) && base !in g.options_forward { g.options_forward << base } else if info.elem_type.has_flag(.result) && base !in g.results_forward { g.results_forward << base @@ -1575,18 +1575,18 @@ pub fn (mut g Gen) write_multi_return_types() { g.type_definitions.writeln('struct ${sym.cname} {') for i, mr_typ in info.types { type_name := g.typ(mr_typ) - if mr_typ.has_flag(.optional) { - // optional in multi_return + if mr_typ.has_flag(.option) { + // option in multi_return // Dont use g.typ() here because it will register - // optional and we dont want that - styp, base := g.optional_type_name(mr_typ) - lock g.done_optionals { - if base !in g.done_optionals { - g.done_optionals << base + // option and we dont want that + styp, base := g.option_type_name(mr_typ) + lock g.done_options { + if base !in g.done_options { + g.done_options << base last_text := g.type_definitions.after(start_pos).clone() g.type_definitions.go_back_to(start_pos) g.typedefs.writeln('typedef struct ${styp} ${styp};') - g.type_definitions.writeln('${g.optional_type_text(styp, base)};') + g.type_definitions.writeln('${g.option_type_text(styp, base)};') g.type_definitions.write_string(last_text) } } @@ -1680,7 +1680,7 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { } if i == stmts.len - 1 && tmp_var != '' { // Handle if expressions, set the value of the last expression to the temp var. - if g.inside_if_optional || g.inside_match_optional { + if g.inside_if_option || g.inside_match_option { g.set_current_pos_as_last_stmt_pos() g.skip_stmt_pos = true if stmt is ast.ExprStmt { @@ -1698,13 +1698,13 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { styp = 'f64' } } - if stmt.typ.has_flag(.optional) { + if stmt.typ.has_flag(.option) { g.writeln('') g.write('${tmp_var} = ') g.expr(stmt.expr) g.writeln(';') } else { - ret_typ := g.fn_decl.return_type.clear_flag(.optional) + ret_typ := g.fn_decl.return_type.clear_flag(.option) styp = g.base_type(ret_typ) g.write('_option_ok(&(${styp}[]) { ') g.expr_with_cast(stmt.expr, stmt.typ, ret_typ) @@ -1763,7 +1763,7 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { } } else { g.stmt(stmt) - if (g.inside_if_optional || g.inside_if_result || g.inside_match_optional + if (g.inside_if_option || g.inside_if_result || g.inside_match_option || g.inside_match_result) && stmt is ast.ExprStmt { g.writeln(';') } @@ -1817,8 +1817,8 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { // applicable to situations where the expr_typ does not have `optinal` and `result`, // e.g. field default: "foo ?int = 1", field assign: "foo = 1", field init: "foo: 1" fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.Type, tmp_var string) { - if !ret_typ.has_flag(.optional) && !ret_typ.has_flag(.result) { - panic('cgen: parameter `ret_typ` of function `expr_with_tmp_var()` must have optional or result') + if !ret_typ.has_flag(.option) && !ret_typ.has_flag(.result) { + panic('cgen: parameter `ret_typ` of function `expr_with_tmp_var()` must be an option or result') } stmt_str := g.go_before_stmt(0).trim_space() @@ -1827,17 +1827,17 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T if g.table.sym(expr_typ).kind == .none_ { g.write('${g.typ(ret_typ)} ${tmp_var} = ') - g.gen_optional_error(ret_typ, expr) + g.gen_option_error(ret_typ, expr) g.writeln(';') } else { g.writeln('${g.typ(ret_typ)} ${tmp_var};') - if ret_typ.has_flag(.optional) { + if ret_typ.has_flag(.option) { g.write('_option_ok(&(${styp}[]) { ') } else { g.write('_result_ok(&(${styp}[]) { ') } g.expr_with_cast(expr, expr_typ, ret_typ) - if ret_typ.has_flag(.optional) { + if ret_typ.has_flag(.option) { g.writeln(' }, (${c.option_name}*)(&${tmp_var}), sizeof(${styp}));') } else { g.writeln(' }, (${c.result_name}*)(&${tmp_var}), sizeof(${styp}));') @@ -1987,7 +1987,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { // if af { // g.autofree_call_postgen() // } - if g.inside_ternary == 0 && !g.inside_if_optional && !g.inside_match_optional + if g.inside_ternary == 0 && !g.inside_if_option && !g.inside_match_option && !g.inside_if_result && !g.inside_match_result && !node.is_expr && node.expr !is ast.IfExpr { if node.expr is ast.MatchExpr { @@ -2170,9 +2170,9 @@ fn (mut g Gen) stmt(node ast.Stmt) { ts := g.table.sym(node.typ) if !(ts.info as ast.Interface).is_generic { for method in node.methods { - if method.return_type.has_flag(.optional) { - // Register an optional if it's not registered yet - g.register_optional(method.return_type) + if method.return_type.has_flag(.option) { + // Register an option if it's not registered yet + g.register_option(method.return_type) } if method.return_type.has_flag(.result) { // Register a result if it's not registered yet @@ -2378,7 +2378,7 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr g.write('&') } } - if got_styp == 'none' && !g.cur_fn.return_type.has_flag(.optional) { + if got_styp == 'none' && !g.cur_fn.return_type.has_flag(.option) { g.write('(none){EMPTY_STRUCT_INITIALIZATION}') } else { g.expr(expr) @@ -2406,7 +2406,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ return } if got_sym.info !is ast.Interface && exp_sym.info is ast.Interface - && got_type.idx() != expected_type.idx() && !expected_type.has_flag(.optional) + && got_type.idx() != expected_type.idx() && !expected_type.has_flag(.option) && !expected_type.has_flag(.result) { if expr is ast.StructInit && !got_type.is_ptr() { g.inside_cast_in_heap++ @@ -2513,7 +2513,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ neither_void := ast.voidptr_type !in [got_type, expected_type] && ast.nil_type !in [got_type, expected_type] if expected_type.has_flag(.shared_f) && !got_type_raw.has_flag(.shared_f) - && !expected_type.has_flag(.optional) && !expected_type.has_flag(.result) { + && !expected_type.has_flag(.option) && !expected_type.has_flag(.result) { shared_styp := exp_styp[0..exp_styp.len - 1] // `shared` implies ptr, so eat one `*` if got_type_raw.is_ptr() { g.error('cannot convert reference to `shared`', expr.pos()) @@ -2544,7 +2544,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ got_deref_type := got_type.deref() deref_sym := g.table.sym(got_deref_type) deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx] - got_is_opt_or_res := got_type.has_flag(.optional) || got_type.has_flag(.result) + got_is_opt_or_res := got_type.has_flag(.option) || got_type.has_flag(.result) if deref_will_match || got_is_opt_or_res || expr.is_auto_deref_var() { g.write('*') } @@ -2951,9 +2951,9 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int // Do not free vars that were declared after this scope continue } - is_optional := obj.typ.has_flag(.optional) - if is_optional { - // TODO: free optionals + is_option := obj.typ.has_flag(.option) + if is_option { + // TODO: free options continue } g.autofree_variable(obj) @@ -3185,7 +3185,7 @@ fn (mut g Gen) expr(node_ ast.Expr) { ret_type := if node.or_block.kind == .absent { node.return_type } else { - node.return_type.clear_flag(.optional).clear_flag(.result) + node.return_type.clear_flag(.option).clear_flag(.result) } mut shared_styp := '' if g.is_shared && !ret_type.has_flag(.shared_f) && !g.inside_or_block { @@ -3409,8 +3409,8 @@ fn (mut g Gen) expr(node_ ast.Expr) { } tmp_opt := if gen_or { g.new_tmp_var() } else { '' } if gen_or { - opt_elem_type := g.typ(elem_type.set_flag(.optional)) - g.register_chan_pop_optional_call(opt_elem_type, styp) + opt_elem_type := g.typ(elem_type.set_flag(.option)) + g.register_chan_pop_option_call(opt_elem_type, styp) g.write('${opt_elem_type} ${tmp_opt} = __Option_${styp}_popval(') } else { g.write('__${styp}_popval(') @@ -3626,7 +3626,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { g.or_block(tmp_var, node.or_block, node.typ) g.write(stmt_str) g.write(' ') - unwrapped_typ := node.typ.clear_flag(.optional).clear_flag(.result) + unwrapped_typ := node.typ.clear_flag(.option).clear_flag(.result) unwrapped_styp := g.typ(unwrapped_typ) g.write('(*(${unwrapped_styp}*)${tmp_var}.data)') return @@ -3635,7 +3635,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { sym := g.table.sym(g.unwrap_generic(node.expr_type)) // if node expr is a root ident and an optional mut is_opt_or_res := node.expr is ast.Ident - && (node.expr_type.has_flag(.optional) || node.expr_type.has_flag(.result)) + && (node.expr_type.has_flag(.option) || node.expr_type.has_flag(.result)) if is_opt_or_res { opt_base_typ := g.base_type(node.expr_type) g.writeln('(*(${opt_base_typ}*)') @@ -3987,7 +3987,7 @@ fn (mut g Gen) unlock_locks() { fn (mut g Gen) map_init(node ast.MapInit) { unwrap_key_typ := g.unwrap_generic(node.key_type) - unwrap_val_typ := g.unwrap_generic(node.value_type).clear_flag(.optional).clear_flag(.result) + unwrap_val_typ := g.unwrap_generic(node.value_type).clear_flag(.option).clear_flag(.result) key_typ_str := g.typ(unwrap_key_typ) value_typ_str := g.typ(unwrap_val_typ) value_sym := g.table.sym(unwrap_val_typ) @@ -4252,7 +4252,7 @@ fn (mut g Gen) ident(node ast.Ident) { if node.info is ast.IdentVar { if node.obj is ast.Var { if !g.is_assign_lhs && node.obj.is_comptime_field { - if g.comptime_for_field_type.has_flag(.optional) { + if g.comptime_for_field_type.has_flag(.option) { if g.inside_opt_or_res { g.write('${name}') } else { @@ -4270,7 +4270,7 @@ fn (mut g Gen) ident(node ast.Ident) { // `x = 10` => `x.data = 10` (g.right_is_opt == false) // `x = new_opt()` => `x = new_opt()` (g.right_is_opt == true) // `println(x)` => `println(*(int*)x.data)` - if node.info.is_optional && !(g.is_assign_lhs && g.right_is_opt) { + if node.info.is_option && !(g.is_assign_lhs && g.right_is_opt) { if g.inside_opt_or_res { g.write('${name}') } else { @@ -4394,8 +4394,8 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { || (sym.info as ast.Alias).parent_type !in [node.expr_type, ast.string_type] { cast_label = '(${styp})' } - if node.typ.has_flag(.optional) && node.expr is ast.None { - g.gen_optional_error(node.typ, node.expr) + if node.typ.has_flag(.option) && node.expr is ast.None { + g.gen_option_error(node.typ, node.expr) } else { g.write('(${cast_label}(') if sym.kind == .alias && g.table.final_sym(node.typ).kind == .string { @@ -4418,11 +4418,11 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { } fn (mut g Gen) concat_expr(node ast.ConcatExpr) { - mut styp := g.typ(node.return_type.clear_flag(.optional).clear_flag(.result)) + mut styp := g.typ(node.return_type.clear_flag(.option).clear_flag(.result)) if g.inside_return { - styp = g.typ(g.fn_decl.return_type.clear_flag(.optional).clear_flag(.result)) + styp = g.typ(g.fn_decl.return_type.clear_flag(.option).clear_flag(.result)) } else if g.inside_or_block { - styp = g.typ(g.or_expr_return_type.clear_flag(.optional).clear_flag(.result)) + styp = g.typ(g.or_expr_return_type.clear_flag(.option).clear_flag(.result)) } sym := g.table.sym(node.return_type) is_multi := sym.kind == .multi_return @@ -4456,8 +4456,8 @@ fn (mut g Gen) gen_result_error(target_type ast.Type, expr ast.Expr) { g.write(', .data={EMPTY_STRUCT_INITIALIZATION} }') } -// NB: remove this when optional has no errors anymore -fn (mut g Gen) gen_optional_error(target_type ast.Type, expr ast.Expr) { +// NB: remove this when option has no errors anymore +fn (mut g Gen) gen_option_error(target_type ast.Type, expr ast.Expr) { styp := g.typ(g.unwrap_generic(target_type)) g.write('(${styp}){ .state=2, .err=') g.expr(expr) @@ -4488,17 +4488,17 @@ fn (mut g Gen) return_stmt(node ast.Return) { mut fn_ret_type := g.fn_decl.return_type if sym.kind == .alias { unaliased_type := g.table.unaliased_type(fn_ret_type) - if unaliased_type.has_flag(.optional) || unaliased_type.has_flag(.result) { + if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) { fn_ret_type = unaliased_type } } fn_return_is_multi := sym.kind == .multi_return - fn_return_is_optional := fn_ret_type.has_flag(.optional) + fn_return_is_option := fn_ret_type.has_flag(.option) fn_return_is_result := fn_ret_type.has_flag(.result) mut has_semicolon := false if node.exprs.len == 0 { g.write_defer_stmts_when_needed() - if fn_return_is_optional || fn_return_is_result { + if fn_return_is_option || fn_return_is_result { styp := g.typ(fn_ret_type) g.writeln('return (${styp}){0};') } else { @@ -4515,15 +4515,15 @@ fn (mut g Gen) return_stmt(node ast.Return) { mut use_tmp_var := g.defer_stmts.len > 0 || g.defer_profile_code.len > 0 || g.cur_lock.lockeds.len > 0 // handle promoting none/error/function returning _option' - if fn_return_is_optional { - optional_none := node.exprs[0] is ast.None + if fn_return_is_option { + option_none := node.exprs[0] is ast.None ftyp := g.typ(node.types[0]) mut is_regular_option := ftyp == '_option' - if optional_none || is_regular_option || node.types[0] == ast.error_type_idx { + if option_none || is_regular_option || node.types[0] == ast.error_type_idx { if g.fn_decl != unsafe { nil } && g.fn_decl.is_test { test_error_var := g.new_tmp_var() g.write('${ret_typ} ${test_error_var} = ') - g.gen_optional_error(fn_ret_type, node.exprs[0]) + g.gen_option_error(fn_ret_type, node.exprs[0]) g.writeln(';') g.write_defer_stmts_when_needed() g.gen_failing_return_error_for_test_fn(node, test_error_var) @@ -4534,7 +4534,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { } else { g.write('return ') } - g.gen_optional_error(fn_ret_type, node.exprs[0]) + g.gen_option_error(fn_ret_type, node.exprs[0]) g.writeln(';') if use_tmp_var { g.write_defer_stmts_when_needed() @@ -4585,7 +4585,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { typ_sym := g.table.sym(g.fn_decl.return_type) mr_info := typ_sym.info as ast.MultiReturn mut styp := '' - if fn_return_is_optional { + if fn_return_is_option { g.writeln('${ret_typ} ${tmpvar};') styp = g.base_type(g.fn_decl.return_type) g.write('_option_ok(&(${styp}[]) { ') @@ -4611,7 +4611,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { call_expr := expr as ast.CallExpr expr_sym := g.table.sym(call_expr.return_type) mut tmp := g.new_tmp_var() - if !call_expr.return_type.has_flag(.optional) + if !call_expr.return_type.has_flag(.option) && !call_expr.return_type.has_flag(.result) { line := g.go_before_stmt(0) expr_styp := g.typ(call_expr.return_type) @@ -4654,7 +4654,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { } } g.write('}') - if fn_return_is_optional { + if fn_return_is_option { g.writeln(' }, (${c.option_name}*)(&${tmpvar}), sizeof(${styp}));') g.write_defer_stmts_when_needed() g.write('return ${tmpvar}') @@ -4667,7 +4667,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { if multi_unpack.len > 0 { g.insert_before_stmt(multi_unpack) } - if use_tmp_var && !fn_return_is_optional && !fn_return_is_result { + if use_tmp_var && !fn_return_is_option && !fn_return_is_result { if !has_semicolon { g.writeln(';') } @@ -4683,16 +4683,16 @@ fn (mut g Gen) return_stmt(node ast.Return) { // normal return return_sym := g.table.sym(node.types[0]) expr0 := node.exprs[0] - // `return opt_ok(expr)` for functions that expect an optional + // `return opt_ok(expr)` for functions that expect an option expr_type_is_opt := match expr0 { ast.CallExpr { - expr0.return_type.has_flag(.optional) && expr0.or_block.kind == .absent + expr0.return_type.has_flag(.option) && expr0.or_block.kind == .absent } else { - node.types[0].has_flag(.optional) + node.types[0].has_flag(.option) } } - if fn_return_is_optional && !expr_type_is_opt && return_sym.name != c.option_name { + if fn_return_is_option && !expr_type_is_opt && return_sym.name != c.option_name { styp := g.base_type(fn_ret_type) g.writeln('${ret_typ} ${tmpvar};') g.write('_option_ok(&(${styp}[]) { ') @@ -4702,7 +4702,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { } } for i, expr in node.exprs { - g.expr_with_cast(expr, node.types[i], fn_ret_type.clear_flag(.optional).clear_flag(.result)) + g.expr_with_cast(expr, node.types[i], fn_ret_type.clear_flag(.option).clear_flag(.result)) if i < node.exprs.len - 1 { g.write(', ') } @@ -4852,7 +4852,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) { } } ast.CallExpr { - if field.expr.return_type.has_flag(.optional) + if field.expr.return_type.has_flag(.option) || field.expr.return_type.has_flag(.result) { g.inside_const_opt_or_res = true unwrap_opt_res := field.expr.or_block.kind != .absent @@ -5675,7 +5675,7 @@ fn (mut g Gen) sort_structs(typesa []&ast.TypeSymbol) []&ast.TypeSymbol { // fn (mut g Gen) start_tmp() { // } // If user is accessing the return value eg. in assigment, pass the variable name. -// If the user is not using the optional return value. We need to pass a temp var +// If the user is not using the option return value. We need to pass a temp var // to access its fields (`.ok`, `.error` etc) // `os.cp(...)` => `Option bool tmp = os__cp(...); if (tmp.state != 0) { ... }` // Returns the type of the last stmt @@ -5700,7 +5700,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty } } if or_block.kind == .block { - g.or_expr_return_type = return_type.clear_flag(.optional).clear_flag(.result) + g.or_expr_return_type = return_type.clear_flag(.option).clear_flag(.result) if g.inside_or_block { g.writeln('\terr = ${cvar_name}.err;') } else { @@ -5726,9 +5726,9 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty g.write('return ') g.gen_result_error(g.cur_fn.return_type, expr_stmt.expr) g.writeln(';') - } else if g.cur_fn.return_type.has_flag(.optional) { + } else if g.cur_fn.return_type.has_flag(.option) { g.write('return ') - g.gen_optional_error(g.cur_fn.return_type, expr_stmt.expr) + g.gen_option_error(g.cur_fn.return_type, expr_stmt.expr) g.writeln(';') } } @@ -5736,7 +5736,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty g.write('*(${mr_styp}*) ${cvar_name}.data = ') old_inside_opt_data := g.inside_opt_data g.inside_opt_data = true - g.expr_with_cast(expr_stmt.expr, expr_stmt.typ, return_type.clear_flag(.optional).clear_flag(.result)) + g.expr_with_cast(expr_stmt.expr, expr_stmt.typ, return_type.clear_flag(.option).clear_flag(.result)) g.inside_opt_data = old_inside_opt_data g.writeln(';') g.stmt_path_pos.delete_last() @@ -5791,7 +5791,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty paline, pafile, pamod, pafn := g.panic_debug_info(or_block.pos) g.writeln('panic_debug(${paline}, tos3("${pafile}"), tos3("${pamod}"), tos3("${pafn}"), ${err_msg} );') } else { - g.writeln('\tpanic_optional_not_set( ${err_msg} );') + g.writeln('\tpanic_option_not_set( ${err_msg} );') } } else if g.fn_decl != unsafe { nil } && g.fn_decl.is_test { g.gen_failing_error_propagation_for_test_fn(or_block, cvar_name) @@ -5828,7 +5828,7 @@ fn c_name(name_ string) string { fn (mut g Gen) type_default(typ_ ast.Type) string { typ := g.unwrap_generic(typ_) - if typ.has_flag(.optional) || typ.has_flag(.result) { + if typ.has_flag(.option) || typ.has_flag(.result) { return '{0}' } // Always set pointers to 0 diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 421c81a29..94f17c351 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -36,7 +36,7 @@ fn (mut g Gen) get_comptime_selector_bool_field(field_name string) bool { 'is_mut' { return field.is_mut } 'is_shared' { return field_typ.has_flag(.shared_f) } 'is_atomic' { return field_typ.has_flag(.atomic_f) } - 'is_optional' { return field.typ.has_flag(.optional) } + 'is_option' { return field.typ.has_flag(.option) } 'is_array' { return field_sym.kind in [.array, .array_fixed] } 'is_map' { return field_sym.kind == .map } 'is_chan' { return field_sym.kind == .chan } @@ -156,7 +156,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { return } - if !g.inside_call && (m.return_type.has_flag(.optional) || m.return_type.has_flag(.result)) { + if !g.inside_call && (m.return_type.has_flag(.option) || m.return_type.has_flag(.result)) { g.write('(*(${g.base_type(m.return_type)}*)') } // TODO: check argument types @@ -203,7 +203,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { } } g.write(')') - if !g.inside_call && (m.return_type.has_flag(.optional) || m.return_type.has_flag(.result)) { + if !g.inside_call && (m.return_type.has_flag(.option) || m.return_type.has_flag(.result)) { g.write('.data)') } return @@ -486,7 +486,7 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) { } if cond.op == .key_is { - g.write('${exp_type.idx()} == ${got_type.idx()} && ${exp_type.has_flag(.optional)} == ${got_type.has_flag(.optional)}') + g.write('${exp_type.idx()} == ${got_type.idx()} && ${exp_type.has_flag(.option)} == ${got_type.has_flag(.option)}') return exp_type == got_type, true } else { g.write('${exp_type.idx()} != ${got_type.idx()}') @@ -533,7 +533,7 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) { } else if expr is ast.TypeNode { got_type := g.unwrap_generic(expr.typ) is_true := checked_type.idx() == got_type.idx() - && checked_type.has_flag(.optional) == got_type.has_flag(.optional) + && checked_type.has_flag(.option) == got_type.has_flag(.option) if is_true { if cond.op == .key_in { g.write('1') @@ -571,7 +571,7 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) { } ast.SelectorExpr { if g.inside_comptime_for_field && cond.expr is ast.Ident - && (cond.expr as ast.Ident).name == g.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_optional', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias'] { + && (cond.expr as ast.Ident).name == g.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias'] { ret_bool := g.get_comptime_selector_bool_field(cond.field_name) g.write(ret_bool.str()) return ret_bool, true @@ -737,7 +737,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { // g.writeln('\t${node.val_var}.is_shared = ${field.typ.has_flag(.shared_f)};') g.writeln('\t${node.val_var}.is_atomic = ${field.typ.has_flag(.atomic_f)};') - g.writeln('\t${node.val_var}.is_optional = ${field.typ.has_flag(.optional)};') + g.writeln('\t${node.val_var}.is_option = ${field.typ.has_flag(.option)};') // g.writeln('\t${node.val_var}.is_array = ${field_sym.kind in [.array, .array_fixed]};') g.writeln('\t${node.val_var}.is_map = ${field_sym.kind == .map};') @@ -774,7 +774,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { g.writeln('}// \$for') } -fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_optional bool) ?string { +fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_option bool) ?string { match name { // platforms/os-es: 'windows' { @@ -923,7 +923,7 @@ fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_optional bool) ?str return 'TARGET_ORDER_IS_BIG' } else { - if is_comptime_optional + if is_comptime_option || (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) { return 'CUSTOM_DEFINE_${name}' } diff --git a/vlib/v/gen/c/dumpexpr.v b/vlib/v/gen/c/dumpexpr.v index ba2a1f3df..d7ca7cd59 100644 --- a/vlib/v/gen/c/dumpexpr.v +++ b/vlib/v/gen/c/dumpexpr.v @@ -34,7 +34,7 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) { ident_expr := selector_expr.expr if ident_expr.name == g.comptime_for_field_var && selector_expr.field_name == 'name' { field, _ := g.get_comptime_selector_var_type(selector) - name = g.typ(g.unwrap_generic(field.typ.clear_flag(.shared_f).clear_flag(.optional).clear_flag(.result))) + name = g.typ(g.unwrap_generic(field.typ.clear_flag(.shared_f).clear_flag(.option).clear_flag(.result))) expr_type = field.typ } } @@ -51,7 +51,7 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) { g.write('&') g.expr(node.expr) g.write('->val') - } else if expr_type.has_flag(.optional) || expr_type.has_flag(.result) { + } else if expr_type.has_flag(.option) || expr_type.has_flag(.result) { old_inside_opt_or_res := g.inside_opt_or_res g.inside_opt_or_res = true g.write('(*(${name}*)') @@ -78,7 +78,7 @@ fn (mut g Gen) dump_expr_definitions() { typ := ast.Type(dump_type) is_ptr := typ.is_ptr() deref, _ := deref_kind(str_method_expects_ptr, is_ptr, dump_type) - to_string_fn_name := g.get_str_fn(typ.clear_flag(.shared_f).clear_flag(.optional).clear_flag(.result)) + to_string_fn_name := g.get_str_fn(typ.clear_flag(.shared_f).clear_flag(.option).clear_flag(.result)) ptr_asterisk := if is_ptr { '*'.repeat(typ.nr_muls()) } else { '' } mut str_dumparg_type := '' if dump_sym.kind == .none_ { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 976fba091..1eef5e180 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -673,7 +673,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result mut cur_line := if is_gen_or_and_assign_rhs || gen_keep_alive { // && !g.is_autofree { // `x := foo() or { ...}` - // cut everything that has been generated to prepend optional variable creation + // cut everything that has been generated to prepend option variable creation line := g.go_before_stmt(0) g.out.write_string(util.tabs(g.indent)) line @@ -686,7 +686,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { mut ret_typ := node.return_type if g.table.sym(ret_typ).kind == .alias { unaliased_type := g.table.unaliased_type(ret_typ) - if unaliased_type.has_flag(.optional) || unaliased_type.has_flag(.result) { + if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) { ret_typ = unaliased_type } } @@ -719,11 +719,11 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { } if gen_or { g.or_block(tmp_opt, node.or_block, node.return_type) - mut unwrapped_typ := node.return_type.clear_flag(.optional).clear_flag(.result) + mut unwrapped_typ := node.return_type.clear_flag(.option).clear_flag(.result) if g.table.sym(unwrapped_typ).kind == .alias { unaliased_type := g.table.unaliased_type(unwrapped_typ) - if unaliased_type.has_flag(.optional) || unaliased_type.has_flag(.result) { - unwrapped_typ = unaliased_type.clear_flag(.optional).clear_flag(.result) + if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) { + unwrapped_typ = unaliased_type.clear_flag(.option).clear_flag(.result) } } unwrapped_styp := g.typ(unwrapped_typ) @@ -1429,7 +1429,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { g.writeln('));') return } - if g.is_autofree && !typ.has_flag(.optional) && !typ.has_flag(.result) { + if g.is_autofree && !typ.has_flag(.option) && !typ.has_flag(.result) { // Create a temporary variable so that the value can be freed tmp := g.new_tmp_var() g.write('string ${tmp} = ') @@ -1569,7 +1569,7 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) { // Create a temporary var before fn call for each argument in order to free it (only if it's a complex expression, // like `foo(get_string())` or `foo(a + b)` mut free_tmp_arg_vars := g.is_autofree && !g.is_builtin_mod && node.args.len > 0 - && !node.args[0].typ.has_flag(.optional) + && !node.args[0].typ.has_flag(.option) && !node.args[0].typ.has_flag(.result) // TODO copy pasta checker.v if !free_tmp_arg_vars { return @@ -1666,9 +1666,9 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) { // // TODO why 0? // continue // } - is_optional := obj.typ.has_flag(.optional) - if is_optional { - // TODO: free optionals + is_option := obj.typ.has_flag(.option) + if is_option { + // TODO: free options continue } is_result := obj.typ.has_flag(.result) @@ -1943,7 +1943,7 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { if g.pref.os == .windows && node.call_expr.return_type != ast.void_type { g.writeln('${arg_tmp_var}->ret_ptr = malloc(sizeof(${s_ret_typ}));') } - is_opt := node.call_expr.return_type.has_flag(.optional) + is_opt := node.call_expr.return_type.has_flag(.option) is_res := node.call_expr.return_type.has_flag(.result) mut gohandle_name := '' if node.call_expr.return_type == ast.void_type { diff --git a/vlib/v/gen/c/for.v b/vlib/v/gen/c/for.v index 944d81755..e652ebb68 100644 --- a/vlib/v/gen/c/for.v +++ b/vlib/v/gen/c/for.v @@ -179,7 +179,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { node.cond_type = node.val_type } mut cond_var := '' - if (node.cond is ast.Ident && !node.cond_type.has_flag(.optional)) + if (node.cond is ast.Ident && !node.cond_type.has_flag(.option)) || node.cond is ast.SelectorExpr { cond_var = g.expr_string(node.cond) } else { @@ -192,8 +192,8 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var } op_field := g.dot_or_ptr(node.cond_type) g.empty_line = true - opt_expr := '(*(${g.typ(node.cond_type.clear_flag(.optional))}*)${cond_var}${op_field}data)' - cond_expr := if node.cond_type.has_flag(.optional) { + opt_expr := '(*(${g.typ(node.cond_type.clear_flag(.option))}*)${cond_var}${op_field}data)' + cond_expr := if node.cond_type.has_flag(.option) { '/*opt*/ ${opt_expr}${op_field}len' } else { '${cond_var}${op_field}len' @@ -214,7 +214,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { // instead of // `int* val = ((int**)arr.data)[i];` // right := if node.val_is_mut { styp } else { styp + '*' } - right := if node.cond_type.has_flag(.optional) { + right := if node.cond_type.has_flag(.option) { '/*opt*/ ((${styp}*)${opt_expr}${op_field}data)[${i}]' } else if node.val_is_mut || node.val_is_ref { '((${styp})${cond_var}${op_field}data) + ${i}' diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index cb500de66..a4fc28159 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -7,7 +7,7 @@ import v.ast fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool { if node.is_expr && g.inside_ternary == 0 { - if g.is_autofree || node.typ.has_flag(.optional) || node.typ.has_flag(.result) { + if g.is_autofree || node.typ.has_flag(.option) || node.typ.has_flag(.result) { return true } for branch in node.branches { @@ -81,7 +81,7 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool { ast.ConcatExpr { for val in expr.vals { if val is ast.CallExpr { - if val.return_type.has_flag(.optional) || val.return_type.has_flag(.result) { + if val.return_type.has_flag(.option) || val.return_type.has_flag(.result) { return true } } @@ -169,12 +169,12 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { mut cur_line := '' mut raw_state := false if needs_tmp_var { - if node.typ.has_flag(.optional) { - raw_state = g.inside_if_optional + if node.typ.has_flag(.option) { + raw_state = g.inside_if_option defer { - g.inside_if_optional = raw_state + g.inside_if_option = raw_state } - g.inside_if_optional = true + g.inside_if_option = true } else if node.typ.has_flag(.result) { raw_state = g.inside_if_result defer { @@ -257,7 +257,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { var_name = g.new_tmp_var() guard_vars[i] = var_name // for `else` g.tmp_count-- - if branch.cond.expr_type.has_flag(.optional) { + if branch.cond.expr_type.has_flag(.option) { g.writeln('if (${var_name}.state == 0) {') } else if branch.cond.expr_type.has_flag(.result) { g.writeln('if (!${var_name}.is_error) {') @@ -265,7 +265,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { } else { g.write('if (${var_name} = ') g.expr(branch.cond.expr) - if branch.cond.expr_type.has_flag(.optional) { + if branch.cond.expr_type.has_flag(.option) { g.writeln(', ${var_name}.state == 0) {') } else if branch.cond.expr_type.has_flag(.result) { g.writeln(', !${var_name}.is_error) {') diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 57b09d38e..a9640ee73 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -23,7 +23,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) { tmp_opt := g.new_tmp_var() cur_line := g.go_before_stmt(0) g.out.write_string(util.tabs(g.indent)) - opt_elem_type := g.typ(ast.u8_type.set_flag(.optional)) + opt_elem_type := g.typ(ast.u8_type.set_flag(.option)) g.write('${opt_elem_type} ${tmp_opt} = string_at_with_check(') g.expr(node.left) g.write(', ') @@ -72,7 +72,7 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) { tmp_opt = g.new_tmp_var() cur_line = g.go_before_stmt(0) g.out.write_string(util.tabs(g.indent)) - opt_elem_type := g.typ(ast.string_type.set_flag(.optional)) + opt_elem_type := g.typ(ast.string_type.set_flag(.option)) g.write('${opt_elem_type} ${tmp_opt} = string_substr_with_check(') } else { g.write('string_substr(') @@ -248,7 +248,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { is_direct_array_access := g.is_direct_array_access || node.is_direct // do not clone inside `opt_ok(opt_ok(&(string[]) {..})` before returns needs_clone := info.elem_type == ast.string_type_idx && g.is_autofree && !(g.inside_return - && g.fn_decl != unsafe { nil } && g.fn_decl.return_type.has_flag(.optional)) + && g.fn_decl != unsafe { nil } && g.fn_decl.return_type.has_flag(.option)) && !g.is_assign_lhs is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { @@ -319,7 +319,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { } if gen_or { g.writeln(';') - opt_elem_type := g.typ(elem_type.set_flag(.optional)) + opt_elem_type := g.typ(elem_type.set_flag(.option)) g.writeln('${opt_elem_type} ${tmp_opt} = {0};') g.writeln('if (${tmp_opt_ptr}) {') g.writeln('\t*((${elem_type_str}*)&${tmp_opt}.data) = *((${elem_type_str}*)${tmp_opt_ptr});') @@ -390,7 +390,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { if g.inside_return { g.typ(elem_type) } else { - g.typ(elem_type.clear_flag(.optional).clear_flag(.result)) + g.typ(elem_type.clear_flag(.option).clear_flag(.result)) } } get_and_set_types := elem_sym.kind in [.struct_, .map] @@ -502,7 +502,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { } if gen_or { g.writeln(';') - opt_elem_type := g.typ(elem_type.set_flag(.optional)) + opt_elem_type := g.typ(elem_type.set_flag(.option)) g.writeln('${opt_elem_type} ${tmp_opt} = {0};') g.writeln('if (${tmp_opt_ptr}) {') g.writeln('\t*((${elem_type_str}*)&${tmp_opt}.data) = *((${elem_type_str}*)${tmp_opt_ptr});') diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 483ccbdf9..6ff4725ed 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -63,7 +63,7 @@ fn (mut g Gen) infix_expr_arrow_op(node ast.InfixExpr) { tmp_opt := if gen_or { g.new_tmp_var() } else { '' } if gen_or { elem_styp := g.typ(elem_type) - g.register_chan_push_optional_fn(elem_styp, styp) + g.register_chan_push_option_fn(elem_styp, styp) g.write('${option_name}_void ${tmp_opt} = __Option_${styp}_pushval(') } else { g.write('__${styp}_pushval(') diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 84fa2d877..dcbd0d345 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -26,7 +26,7 @@ import strings fn (mut g Gen) gen_json_for_type(typ ast.Type) { utyp := g.unwrap_generic(typ).set_nr_muls(0) sym := g.table.sym(utyp) - if (is_js_prim(sym.name) && !utyp.has_flag(.optional)) || sym.kind == .enum_ { + if (is_js_prim(sym.name) && !utyp.has_flag(.option)) || sym.kind == .enum_ { return } g.json_types << utyp @@ -148,7 +148,7 @@ ${enc_fn_dec} { verror('json: ${sym.name} is not a sumtype') } g.gen_sumtype_enc_dec(sym, mut enc, mut dec) - } else if utyp.has_flag(.optional) { + } else if utyp.has_flag(.option) { g.gen_option_enc_dec(utyp, mut enc, mut dec) } else { enc.writeln('\to = cJSON_CreateObject();') @@ -172,7 +172,7 @@ fn (mut g Gen) gen_option_enc_dec(typ ast.Type, mut enc strings.Builder, mut dec enc.writeln('\tif (val.state == 2) {') enc.writeln('\t\treturn cJSON_CreateNull();') enc.writeln('\t}') - type_str := g.typ(typ.clear_flag(.optional)) + type_str := g.typ(typ.clear_flag(.option)) encode_name := js_enc_name(type_str) enc.writeln('\to = ${encode_name}(*(${type_str}*)val.data);') } diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index d58b2dac4..ffbdce28a 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -8,7 +8,7 @@ import v.ast fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool { if node.is_expr && node.return_type != ast.void_type && node.return_type != 0 { if g.table.sym(node.return_type).kind in [.sum_type, .multi_return] - || node.return_type.has_flag(.optional) || node.return_type.has_flag(.result) { + || node.return_type.has_flag(.option) || node.return_type.has_flag(.result) { return true } if g.table.final_sym(node.cond_type).kind == .enum_ && node.branches.len > 5 { @@ -51,12 +51,12 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { g.inside_ternary++ } if is_expr { - if node.return_type.has_flag(.optional) { - old := g.inside_match_optional + if node.return_type.has_flag(.option) { + old := g.inside_match_option defer { - g.inside_match_optional = old + g.inside_match_option = old } - g.inside_match_optional = true + g.inside_match_option = true } else if node.return_type.has_flag(.result) { old := g.inside_match_result defer { diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 7b7c7e324..8838c6609 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -305,7 +305,7 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool { return true } - if (field.typ.has_flag(.optional) && !field.default_expr_typ.has_flag(.optional)) + if (field.typ.has_flag(.option) && !field.default_expr_typ.has_flag(.option)) || (field.typ.has_flag(.result) && !field.default_expr_typ.has_flag(.result)) { tmp_var := g.new_tmp_var() g.expr_with_tmp_var(field.default_expr, field.default_expr_typ, field.typ, @@ -314,7 +314,7 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool { } g.expr(field.default_expr) - } else if field.typ.has_flag(.optional) { + } else if field.typ.has_flag(.option) { tmp_var := g.new_tmp_var() g.expr_with_tmp_var(ast.None{}, ast.none_type, field.typ, tmp_var) return true @@ -380,22 +380,22 @@ fn (mut g Gen) struct_decl(s ast.Struct, name string, is_anon bool) { if s.fields.len > 0 || s.embeds.len > 0 { for field in s.fields { // Some of these structs may want to contain - // optionals that may not be defined at this point + // options that may not be defined at this point // if this is the case then we are going to // buffer manip out in front of the struct - // write the optional in and then continue - // FIXME: for parallel cgen (two different files using the same optional in struct fields) - if field.typ.has_flag(.optional) { + // write the option in and then continue + // FIXME: for parallel cgen (two different files using the same option in struct fields) + if field.typ.has_flag(.option) { // Dont use g.typ() here because it will register - // optional and we dont want that - styp, base := g.optional_type_name(field.typ) - lock g.done_optionals { - if base !in g.done_optionals { - g.done_optionals << base + // option and we dont want that + styp, base := g.option_type_name(field.typ) + lock g.done_options { + if base !in g.done_options { + g.done_options << base last_text := g.type_definitions.after(start_pos).clone() g.type_definitions.go_back_to(start_pos) g.typedefs.writeln('typedef struct ${styp} ${styp};') - g.type_definitions.writeln('${g.optional_type_text(styp, base)};') + g.type_definitions.writeln('${g.option_type_text(styp, base)};') g.type_definitions.write_string(last_text) } } @@ -503,7 +503,7 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua g.write('/* autoref */&') } - if (sfield.expected_type.has_flag(.optional) && !sfield.typ.has_flag(.optional)) + if (sfield.expected_type.has_flag(.option) && !sfield.typ.has_flag(.option)) || (sfield.expected_type.has_flag(.result) && !sfield.typ.has_flag(.result)) { tmp_var := g.new_tmp_var() g.expr_with_tmp_var(sfield.expr, sfield.typ, sfield.expected_type, tmp_var) diff --git a/vlib/v/gen/c/testdata/comptime_optional_call.out b/vlib/v/gen/c/testdata/comptime_option_call.out similarity index 100% rename from vlib/v/gen/c/testdata/comptime_optional_call.out rename to vlib/v/gen/c/testdata/comptime_option_call.out diff --git a/vlib/v/gen/c/testdata/comptime_optional_call.vv b/vlib/v/gen/c/testdata/comptime_option_call.vv similarity index 100% rename from vlib/v/gen/c/testdata/comptime_optional_call.vv rename to vlib/v/gen/c/testdata/comptime_option_call.vv diff --git a/vlib/v/gen/golang/golang.v b/vlib/v/gen/golang/golang.v index 2b038ec8a..87b085d4b 100644 --- a/vlib/v/gen/golang/golang.v +++ b/vlib/v/gen/golang/golang.v @@ -1264,7 +1264,7 @@ pub fn (mut f Gen) fn_type_decl(node ast.FnTypeDecl) { ret_str := f.no_cur_mod(f.table.type_to_str_using_aliases(fn_info.return_type, f.mod2alias)) f.write(' ${ret_str}') - } else if fn_info.return_type.has_flag(.optional) { + } else if fn_info.return_type.has_flag(.option) { f.write(' ?') } else if fn_info.return_type.has_flag(.result) { f.write(' !') diff --git a/vlib/v/gen/js/auto_str_methods.v b/vlib/v/gen/js/auto_str_methods.v index 1c79b5cf2..eb558b118 100644 --- a/vlib/v/gen/js/auto_str_methods.v +++ b/vlib/v/gen/js/auto_str_methods.v @@ -22,8 +22,8 @@ fn (mut g JsGen) get_str_fn(typ ast.Type) string { } } - if typ.has_flag(.optional) { - unwrapped.set_flag(.optional) + if typ.has_flag(.option) { + unwrapped.set_flag(.option) } styp := g.typ(unwrapped) mut sym := g.table.sym(unwrapped) @@ -47,7 +47,7 @@ fn (mut g JsGen) final_gen_str(typ StrType) { } g.generated_str_fns << typ sym := g.table.sym(typ.typ) - if sym.has_method('str') && !typ.typ.has_flag(.optional) { + if sym.has_method('str') && !typ.typ.has_flag(.option) { return } styp := typ.styp @@ -55,7 +55,7 @@ fn (mut g JsGen) final_gen_str(typ StrType) { return } str_fn_name := styp_to_str_fn_name(styp) - if typ.typ.has_flag(.optional) { + if typ.typ.has_flag(.option) { g.gen_str_for_option(typ.typ, styp, str_fn_name) return } @@ -217,7 +217,7 @@ fn (mut g JsGen) gen_str_default(sym ast.TypeSymbol, styp string, str_fn_name st } fn (mut g JsGen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string) { - parent_type := typ.clear_flag(.optional) + parent_type := typ.clear_flag(.option) sym := g.table.sym(parent_type) sym_has_str_method, _, _ := sym.str_method_info() parent_str_fn_name := g.get_str_fn(parent_type) @@ -417,7 +417,7 @@ fn (mut g JsGen) fn_decl_str(info ast.FnType) string { fn_str += ' !' } else if info.func.return_type != ast.void_type { x := util.strip_main_name(g.table.get_type_name(g.unwrap_generic(info.func.return_type))) - if info.func.return_type.has_flag(.optional) { + if info.func.return_type.has_flag(.option) { fn_str += ' ?${x}' } else { fn_str += ' ${x}' diff --git a/vlib/v/gen/js/comptime.v b/vlib/v/gen/js/comptime.v index 40ed9bd29..53d7cf54b 100644 --- a/vlib/v/gen/js/comptime.v +++ b/vlib/v/gen/js/comptime.v @@ -168,7 +168,7 @@ fn (mut g JsGen) comptime_if_cond(cond ast.Expr, pkg_exist bool) bool { } } -fn (mut g JsGen) comptime_if_to_ifdef(name string, is_comptime_optional bool) !string { +fn (mut g JsGen) comptime_if_to_ifdef(name string, is_comptime_option bool) !string { match name { // platforms/os-es: 'windows' { @@ -306,7 +306,7 @@ fn (mut g JsGen) comptime_if_to_ifdef(name string, is_comptime_optional bool) !s return '(\$os.endianess == "BE")' } else { - if is_comptime_optional + if is_comptime_option || (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) { return 'checkDefine("CUSTOM_DEFINE_${name}")' } diff --git a/vlib/v/gen/js/deep_copy.v b/vlib/v/gen/js/deep_copy.v index 7d78012e4..e6c360e7f 100644 --- a/vlib/v/gen/js/deep_copy.v +++ b/vlib/v/gen/js/deep_copy.v @@ -18,8 +18,8 @@ fn (mut g JsGen) get_copy_fn(typ ast.Type) string { unwrapped = ast.u64_type } } - if typ.has_flag(.optional) { - unwrapped.set_flag(.optional) + if typ.has_flag(.option) { + unwrapped.set_flag(.option) } styp := g.typ(unwrapped) mut sym := g.table.sym(unwrapped) @@ -189,12 +189,12 @@ fn (mut g JsGen) final_gen_copy(typ StrType) { } g.generated_copy_fns << typ sym := g.table.sym(typ.typ) - if sym.has_method('\$copy') && !typ.typ.has_flag(.optional) { + if sym.has_method('\$copy') && !typ.typ.has_flag(.option) { return } styp := typ.styp copy_fn_name := styp_to_copy_fn_name(styp) - if typ.typ.has_flag(.optional) { + if typ.typ.has_flag(.option) { g.gen_copy_for_option(typ.typ, styp, copy_fn_name) return } diff --git a/vlib/v/gen/js/fn.v b/vlib/v/gen/js/fn.v index 75b457414..843470409 100644 --- a/vlib/v/gen/js/fn.v +++ b/vlib/v/gen/js/fn.v @@ -66,9 +66,9 @@ fn (mut g JsGen) js_mname(name_ string) string { fn (mut g JsGen) js_call(node ast.CallExpr) { g.call_stack << node it := node - call_return_is_optional := it.return_type.has_flag(.optional) + call_return_is_option := it.return_type.has_flag(.option) is_await := node.name == 'JS.await' - if call_return_is_optional { + if call_return_is_option { if is_await { g.writeln('await (async function () {') } else { @@ -96,7 +96,7 @@ fn (mut g JsGen) js_call(node ast.CallExpr) { // end call g.write(')') } - if call_return_is_optional { + if call_return_is_option { g.write(';\n') prev_inside_or := g.inside_or g.inside_or = true @@ -117,7 +117,7 @@ fn (mut g JsGen) js_call(node ast.CallExpr) { g.stmt(it.or_block.stmts.last()) } .propagate_option { - panicstr := '`optional not set (\${err + ""})`' + panicstr := '`option not set (\${err + ""})`' if g.file.mod.name == 'main' && g.fn_decl.name == 'main.main' { g.writeln('return builtin__panic(${panicstr})') } else { @@ -137,8 +137,8 @@ fn (mut g JsGen) js_call(node ast.CallExpr) { fn (mut g JsGen) js_method_call(node ast.CallExpr) { g.call_stack << node it := node - call_return_is_optional := it.return_type.has_flag(.optional) - if call_return_is_optional { + call_return_is_option := it.return_type.has_flag(.option) + if call_return_is_option { g.writeln('(function () {') g.writeln('try {') g.writeln('let tmp = ') @@ -156,7 +156,7 @@ fn (mut g JsGen) js_method_call(node ast.CallExpr) { } // end method call g.write(')') - if call_return_is_optional { + if call_return_is_option { prev_inside_or := g.inside_or g.inside_or = true defer { @@ -177,7 +177,7 @@ fn (mut g JsGen) js_method_call(node ast.CallExpr) { g.stmt(it.or_block.stmts.last()) } .propagate_option { - panicstr := '`optional not set (\${err + ""})`' + panicstr := '`option not set (\${err + ""})`' if g.file.mod.name == 'main' && g.fn_decl.name == 'main.main' { g.writeln('return builtin__panic(${panicstr})') } else { @@ -202,8 +202,8 @@ fn (mut g JsGen) method_call(node ast.CallExpr) { return } is_async := node.name == 'wait' && g.table.sym(node.receiver_type).name.starts_with('Promise<') - call_return_is_optional := it.return_type.has_flag(.optional) - if call_return_is_optional { + call_return_is_option := it.return_type.has_flag(.option) + if call_return_is_option { if is_async { g.writeln('(async function (){') } else { @@ -346,7 +346,7 @@ fn (mut g JsGen) method_call(node ast.CallExpr) { g.write(')') } - if call_return_is_optional { + if call_return_is_option { // end unwrap g.writeln(')') g.dec_indent() @@ -368,7 +368,7 @@ fn (mut g JsGen) method_call(node ast.CallExpr) { g.stmt(it.or_block.stmts.last()) } .propagate_option { - panicstr := '`optional not set (\${err.valueOf().msg})`' + panicstr := '`option not set (\${err.valueOf().msg})`' if g.file.mod.name == 'main' && g.fn_decl.name == 'main.main' { g.writeln('return builtin__panic(${panicstr})') } else { @@ -417,8 +417,8 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) { g.write(ret_sym.name) g.write('(') } - call_return_is_optional := it.return_type.has_flag(.optional) - if call_return_is_optional { + call_return_is_option := it.return_type.has_flag(.option) + if call_return_is_option { g.writeln('(function(){') g.inc_indent() g.writeln('try {') @@ -446,7 +446,7 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) { } // end method call g.write(')') - if call_return_is_optional { + if call_return_is_option { // end unwrap prev_inside_or := g.inside_or g.inside_or = true @@ -469,7 +469,7 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) { g.stmt(it.or_block.stmts.last()) } .propagate_option { - panicstr := '`optional not set (\${err.valueOf().msg})`' + panicstr := '`option not set (\${err.valueOf().msg})`' if g.file.mod.name == 'main' && g.fn_decl.name == 'main.main' { g.writeln('return builtin__panic(${panicstr})') } else { diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index fc3c43349..1397d6a8b 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -59,7 +59,7 @@ mut: inside_loop bool inside_map_set bool // map.set(key, value) inside_builtin bool - inside_if_optional bool + inside_if_option bool generated_builtin bool inside_def_typ_decl bool is_test bool @@ -940,7 +940,7 @@ fn (mut g JsGen) expr(node_ ast.Expr) { g.gen_if_expr(node) } ast.IfGuardExpr { - // TODO no optionals yet + // TODO no options yet } ast.IndexExpr { g.gen_index_expr(node) @@ -1588,7 +1588,7 @@ fn (mut g JsGen) gen_enum_decl(it ast.EnumDecl) { fn (mut g JsGen) gen_expr_stmt(it ast.ExprStmt) { g.expr(it.expr) - if !it.is_expr && it.expr !is ast.IfExpr && !g.inside_ternary && !g.inside_if_optional { + if !it.is_expr && it.expr !is ast.IfExpr && !g.inside_ternary && !g.inside_if_option { g.writeln(';') } } @@ -1844,7 +1844,7 @@ fn (mut g JsGen) gen_interface_decl(it ast.InterfaceDecl) { g.writeln('function ${g.js_name(it.name)} (arg) { return new \$ref(arg); }') } -fn (mut g JsGen) gen_optional_error(expr ast.Expr) { +fn (mut g JsGen) gen_option_error(expr ast.Expr) { g.write('new Option({ state: new u8(2),err: ') g.expr(expr) g.write('})') @@ -1853,9 +1853,9 @@ fn (mut g JsGen) gen_optional_error(expr ast.Expr) { fn (mut g JsGen) gen_return_stmt(it ast.Return) { node := it // sym := g.table.sym(g.fn_decl.return_type) - fn_return_is_optional := g.fn_decl.return_type.has_flag(.optional) + fn_return_is_option := g.fn_decl.return_type.has_flag(.option) if node.exprs.len == 0 { - if fn_return_is_optional { + if fn_return_is_option { if g.inside_or { g.writeln('throw new ReturnException({state: new int(0)});') } else { @@ -1871,11 +1871,11 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) { return } - if fn_return_is_optional { - optional_none := node.exprs[0] is ast.None + if fn_return_is_option { + option_none := node.exprs[0] is ast.None ftyp := g.typ(node.types[0]) mut is_regular_option := ftyp == js.option_name - if optional_none || is_regular_option || node.types[0] == ast.error_type_idx { + if option_none || is_regular_option || node.types[0] == ast.error_type_idx { if !isnil(g.fn_decl) && g.fn_decl.is_test { test_error_var := g.new_tmp_var() g.writeln('let ${test_error_var} = "TODO";') @@ -1887,7 +1887,7 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) { } else { g.write('throw new ReturnException(') } - g.gen_optional_error(it.exprs[0]) + g.gen_option_error(it.exprs[0]) if g.inside_or { g.writeln(')') } @@ -1895,7 +1895,7 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) { return } } - if fn_return_is_optional { + if fn_return_is_option { tmp := g.new_tmp_var() g.write('const ${tmp} = new ') @@ -2201,7 +2201,7 @@ fn (mut g JsGen) gen_ident(node ast.Ident) { name = g.new_tmp_var() } // TODO `is` - // TODO handle optionals + // TODO handle options g.write(name) // TODO: Generate .val for basic types @@ -2476,7 +2476,7 @@ fn (mut g JsGen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) { prev := g.inside_ternary for i, stmt in stmts { if i == stmts.len - 1 && tmp_var != '' { - if g.inside_if_optional { + if g.inside_if_option { if stmt is ast.ExprStmt { if stmt.typ == ast.error_type_idx || stmt.expr is ast.None { g.writeln('${tmp_var}.state = 2;') @@ -2496,7 +2496,7 @@ fn (mut g JsGen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) { } } else { g.stmt(stmt) - if g.inside_if_optional && stmt is ast.ExprStmt { + if g.inside_if_option && stmt is ast.ExprStmt { g.writeln(';') } } @@ -2728,7 +2728,7 @@ fn (mut g JsGen) match_expr_switch(node ast.MatchExpr, is_expr bool, cond_var Ma fn (mut g JsGen) need_tmp_var_in_if(node ast.IfExpr) bool { if node.is_expr && g.inside_ternary { - if node.typ.has_flag(.optional) { + if node.typ.has_flag(.option) { return true } @@ -2770,8 +2770,8 @@ fn (mut g JsGen) gen_if_expr(node ast.IfExpr) { tmp := if needs_tmp_var { g.new_tmp_var() } else { '' } if needs_tmp_var { - if node.typ.has_flag(.optional) { - g.inside_if_optional = true + if node.typ.has_flag(.option) { + g.inside_if_option = true } g.writeln('let ${tmp}; /* if prepend */') @@ -2882,8 +2882,8 @@ fn (mut g JsGen) gen_if_expr(node ast.IfExpr) { if needs_tmp_var { g.write('${tmp}') } - if node.typ.has_flag(.optional) { - g.inside_if_optional = false + if node.typ.has_flag(.option) { + g.inside_if_option = false } } diff --git a/vlib/v/gen/js/tests/optional.v b/vlib/v/gen/js/tests/option.v similarity index 100% rename from vlib/v/gen/js/tests/optional.v rename to vlib/v/gen/js/tests/option.v diff --git a/vlib/v/gen/native/comptime.v b/vlib/v/gen/native/comptime.v index 8a24409c3..1b428eb4b 100644 --- a/vlib/v/gen/native/comptime.v +++ b/vlib/v/gen/native/comptime.v @@ -78,7 +78,7 @@ fn (mut g Gen) comptime_is_truthy(cond ast.Expr) bool { return false } -fn (mut g Gen) comptime_ident(name string, is_comptime_optional bool) bool { +fn (mut g Gen) comptime_ident(name string, is_comptime_option bool) bool { return match name { // // Operating systems @@ -204,7 +204,7 @@ fn (mut g Gen) comptime_ident(name string, is_comptime_optional bool) bool { g.pref.is_script } else { - if is_comptime_optional + if is_comptime_option || (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) { true } else { diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 5642e5306..b86c8743a 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -242,7 +242,7 @@ pub fn mark_used(mut table ast.Table, pref &pref.Preferences, ast_files []&ast.F if pref.is_debug { all_fn_root_names << 'panic_debug' } - all_fn_root_names << 'panic_optional_not_set' + all_fn_root_names << 'panic_option_not_set' all_fn_root_names << 'panic_result_not_set' if pref.is_test { all_fn_root_names << 'main.cb_assertion_ok' diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index a2a49c016..4dd6eb297 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -114,7 +114,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { comments << p.eat_comments() expr := p.expr(0) if expr !in [ast.CallExpr, ast.IndexExpr, ast.PrefixExpr, ast.SelectorExpr] { - p.error_with_pos('if guard condition expression is illegal, it should return optional', + p.error_with_pos('if guard condition expression is illegal, it should return an option', expr.pos()) } p.check_undefined_variables(var_names, expr) or { diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 650ca61b3..04a1bccfe 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -171,7 +171,7 @@ pub fn (mut p Parser) parse_thread_type() ast.Type { p.next() if is_opt { mut ret_type := ast.void_type - ret_type = ret_type.set_flag(.optional) + ret_type = ret_type.set_flag(.option) idx := p.table.find_or_register_thread(ret_type) return ast.new_type(idx) } else { @@ -372,28 +372,28 @@ pub fn (mut p Parser) parse_sum_type_variants() []ast.TypeNode { } pub fn (mut p Parser) parse_type() ast.Type { - // optional or result - mut is_optional := false + // option or result + mut is_option := false mut is_result := false line_nr := p.tok.line_nr - optional_pos := p.tok.pos() + option_pos := p.tok.pos() if p.tok.kind == .question { p.next() - is_optional = true + is_option = true } else if p.tok.kind == .not { p.next() is_result = true } - if is_optional || is_result { + if is_option || is_result { // maybe the '[' is the start of the field attribute is_required_field := p.inside_struct_field_decl && p.tok.kind == .lsbr && p.peek_tok.kind == .name && p.peek_tok.lit == 'required' if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar] || is_required_field { mut typ := ast.void_type - if is_optional { - typ = typ.set_flag(.optional) + if is_option { + typ = typ.set_flag(.option) } else if is_result { typ = typ.set_flag(.result) } @@ -454,12 +454,12 @@ pub fn (mut p Parser) parse_type() ast.Type { return 0 } sym := p.table.sym(typ) - if is_optional && sym.info is ast.SumType && (sym.info as ast.SumType).is_anon { - p.error_with_pos('an inline sum type cannot be optional', optional_pos.extend(p.prev_tok.pos())) + if is_option && sym.info is ast.SumType && (sym.info as ast.SumType).is_anon { + p.error_with_pos('an inline sum type cannot be an option', option_pos.extend(p.prev_tok.pos())) } } - if is_optional { - typ = typ.set_flag(.optional) + if is_option { + typ = typ.set_flag(.option) } if is_result { typ = typ.set_flag(.result) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 4bd83cce9..71cddc00a 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2483,7 +2483,7 @@ pub fn (mut p Parser) name_expr() ast.Expr { } else { false } - is_optional := p.tok.kind == .question + is_option := p.tok.kind == .question is_generic_call := p.is_generic_call() is_generic_cast := p.is_generic_cast() is_generic_struct_init := p.is_generic_struct_init() @@ -2500,9 +2500,9 @@ pub fn (mut p Parser) name_expr() ast.Expr { } } } else if p.peek_tok.kind == .lpar || is_generic_call || is_generic_cast - || (is_optional && p.peek_token(2).kind == .lpar) { + || (is_option && p.peek_token(2).kind == .lpar) { // foo(), foo() or type() cast - mut name := if is_optional { p.peek_tok.lit } else { p.tok.lit } + mut name := if is_option { p.peek_tok.lit } else { p.tok.lit } if mod.len > 0 { name = '${mod}.${name}' } @@ -2548,7 +2548,7 @@ pub fn (mut p Parser) name_expr() ast.Expr { return node } else { // fn call - if is_optional { + if is_option { p.unexpected_with_pos(p.prev_tok.pos(), got: '${p.prev_tok}' ) diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 5376f97c3..470669047 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -201,7 +201,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { mut typ := ast.Type(0) mut type_pos := token.Pos{} mut field_pos := token.Pos{} - mut optional_pos := token.Pos{} + mut option_pos := token.Pos{} mut anon_struct_decl := ast.StructDecl{} if is_embed { // struct embedding @@ -262,8 +262,8 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { return ast.StructDecl{} } field_pos = field_start_pos.extend(p.prev_tok.pos()) - if typ.has_flag(.optional) || typ.has_flag(.result) { - optional_pos = p.peek_token(-2).pos() + if typ.has_flag(.option) || typ.has_flag(.result) { + option_pos = p.peek_token(-2).pos() } } // Comments after type (same line) @@ -297,7 +297,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { typ: typ pos: field_pos type_pos: type_pos - optional_pos: optional_pos + option_pos: option_pos comments: comments i: i default_expr: default_expr @@ -317,7 +317,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { typ: typ pos: field_pos type_pos: type_pos - optional_pos: optional_pos + option_pos: option_pos comments: comments i: i default_expr: default_expr diff --git a/vlib/v/parser/tests/if_guard_cond_err.out b/vlib/v/parser/tests/if_guard_cond_err.out index 70a11b41f..b8507e132 100644 --- a/vlib/v/parser/tests/if_guard_cond_err.out +++ b/vlib/v/parser/tests/if_guard_cond_err.out @@ -1,7 +1,7 @@ -vlib/v/parser/tests/if_guard_cond_err.vv:16:16: error: if guard condition expression is illegal, it should return optional +vlib/v/parser/tests/if_guard_cond_err.vv:16:16: error: if guard condition expression is illegal, it should return an option 14 | fp.usage_example('GOOG AAPL') 15 | _ := fp.bool('version', `v`, false, 'version information.') 16 | if args := fp.finalize() && args.len > 0 { | ~~~~~~~~~~~~~~~~~~~~~~~~~~ 17 | return args - 18 | } else { + 18 | } else { \ No newline at end of file diff --git a/vlib/v/parser/tests/index_expr_option_err.out b/vlib/v/parser/tests/index_expr_option_err.out new file mode 100644 index 000000000..b96f9f18f --- /dev/null +++ b/vlib/v/parser/tests/index_expr_option_err.out @@ -0,0 +1,6 @@ +vlib/v/parser/tests/index_expr_option_err.vv:3:14: error: `?` for propagating errors from index expressions is no longer supported, use `!` instead of `?` + 1 | fn main() { + 2 | a := [1, 2] + 3 | println(a[0]?) + | ^ + 4 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/index_expr_optional_err.vv b/vlib/v/parser/tests/index_expr_option_err.vv similarity index 100% rename from vlib/v/parser/tests/index_expr_optional_err.vv rename to vlib/v/parser/tests/index_expr_option_err.vv diff --git a/vlib/v/parser/tests/index_expr_optional_err.out b/vlib/v/parser/tests/index_expr_optional_err.out deleted file mode 100644 index e91773916..000000000 --- a/vlib/v/parser/tests/index_expr_optional_err.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/parser/tests/index_expr_optional_err.vv:3:14: error: `?` for propagating errors from index expressions is no longer supported, use `!` instead of `?` - 1 | fn main() { - 2 | a := [1, 2] - 3 | println(a[0]?) - | ^ - 4 | } diff --git a/vlib/v/parser/tests/inline_sum_type_option_err.out b/vlib/v/parser/tests/inline_sum_type_option_err.out new file mode 100644 index 000000000..768ca12ad --- /dev/null +++ b/vlib/v/parser/tests/inline_sum_type_option_err.out @@ -0,0 +1,10 @@ +vlib/v/parser/tests/inline_sum_type_option_err.vv:1:11: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead + 1 | fn foo() ?string | int { + | ~~~~~~ + 2 | return 0 + 3 | } +vlib/v/parser/tests/inline_sum_type_option_err.vv:1:10: error: an inline sum type cannot be an option + 1 | fn foo() ?string | int { + | ~~~~~~~~~~~~~ + 2 | return 0 + 3 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/inline_sum_type_optional_err.vv b/vlib/v/parser/tests/inline_sum_type_option_err.vv similarity index 100% rename from vlib/v/parser/tests/inline_sum_type_optional_err.vv rename to vlib/v/parser/tests/inline_sum_type_option_err.vv diff --git a/vlib/v/parser/tests/inline_sum_type_optional_err.out b/vlib/v/parser/tests/inline_sum_type_optional_err.out deleted file mode 100644 index 85e7f5c34..000000000 --- a/vlib/v/parser/tests/inline_sum_type_optional_err.out +++ /dev/null @@ -1,10 +0,0 @@ -vlib/v/parser/tests/inline_sum_type_optional_err.vv:1:11: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead - 1 | fn foo() ?string | int { - | ~~~~~~ - 2 | return 0 - 3 | } -vlib/v/parser/tests/inline_sum_type_optional_err.vv:1:10: error: an inline sum type cannot be optional - 1 | fn foo() ?string | int { - | ~~~~~~~~~~~~~ - 2 | return 0 - 3 | } diff --git a/vlib/v/parser/tests/option_sum_type_return_err.out b/vlib/v/parser/tests/option_sum_type_return_err.out index fb8591062..5f95f14d3 100644 --- a/vlib/v/parser/tests/option_sum_type_return_err.out +++ b/vlib/v/parser/tests/option_sum_type_return_err.out @@ -3,8 +3,8 @@ vlib/v/parser/tests/option_sum_type_return_err.vv:1:22: warning: inline sum type | ~~~~~~ 2 | return 0 3 | } -vlib/v/parser/tests/option_sum_type_return_err.vv:1:21: error: an inline sum type cannot be optional +vlib/v/parser/tests/option_sum_type_return_err.vv:1:21: error: an inline sum type cannot be an option 1 | fn option_sumtype() ?string | int { | ~~~~~~~~~~~~~ 2 | return 0 - 3 | } + 3 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/struct_field_required_fn_option_type.out b/vlib/v/parser/tests/struct_field_required_fn_option_type.out new file mode 100644 index 000000000..a2824797e --- /dev/null +++ b/vlib/v/parser/tests/struct_field_required_fn_option_type.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/struct_field_required_fn_option_type.vv:6:7: error: field `Foo.foo` must be initialized + 4 | + 5 | fn main() { + 6 | f := Foo{} + | ~~~~~ + 7 | println(f) + 8 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/struct_field_required_fn_optional_type.vv b/vlib/v/parser/tests/struct_field_required_fn_option_type.vv similarity index 69% rename from vlib/v/parser/tests/struct_field_required_fn_optional_type.vv rename to vlib/v/parser/tests/struct_field_required_fn_option_type.vv index 0432b754d..d33fdb441 100644 --- a/vlib/v/parser/tests/struct_field_required_fn_optional_type.vv +++ b/vlib/v/parser/tests/struct_field_required_fn_option_type.vv @@ -1,5 +1,5 @@ struct Foo { - foo fn() ? [required] + foo fn () ? [required] } fn main() { diff --git a/vlib/v/parser/tests/struct_field_required_fn_optional_type.out b/vlib/v/parser/tests/struct_field_required_fn_optional_type.out deleted file mode 100644 index c84b64b25..000000000 --- a/vlib/v/parser/tests/struct_field_required_fn_optional_type.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/parser/tests/struct_field_required_fn_optional_type.vv:6:7: error: field `Foo.foo` must be initialized - 4 | - 5 | fn main() { - 6 | f := Foo{} - | ~~~~~ - 7 | println(f) - 8 | } diff --git a/vlib/v/tests/aliased_optional_fn_call_test.v b/vlib/v/tests/aliased_option_fn_call_test.v similarity index 74% rename from vlib/v/tests/aliased_optional_fn_call_test.v rename to vlib/v/tests/aliased_option_fn_call_test.v index 8bc9d4c7b..95b7b9e3f 100644 --- a/vlib/v/tests/aliased_optional_fn_call_test.v +++ b/vlib/v/tests/aliased_option_fn_call_test.v @@ -4,7 +4,7 @@ fn foo() OptStr { return 'abc' } -fn test_aliased_optional_fn_call() { +fn test_aliased_option_fn_call() { ret := foo()? println(ret) assert ret == 'abc' diff --git a/vlib/v/tests/anon_fn_optional_call_in_if_expr_test.v b/vlib/v/tests/anon_fn_option_call_in_if_expr_test.v similarity index 69% rename from vlib/v/tests/anon_fn_optional_call_in_if_expr_test.v rename to vlib/v/tests/anon_fn_option_call_in_if_expr_test.v index ee613b430..4e57bc0d2 100644 --- a/vlib/v/tests/anon_fn_optional_call_in_if_expr_test.v +++ b/vlib/v/tests/anon_fn_option_call_in_if_expr_test.v @@ -1,4 +1,4 @@ -fn test_anon_fn_optional_call_in_if_expr_1() { +fn test_anon_fn_option_call_in_if_expr_1() { if fn () ?bool { return true }() or { false } @@ -10,7 +10,7 @@ fn test_anon_fn_optional_call_in_if_expr_1() { } } -fn test_anon_fn_optional_call_in_if_expr_2() { +fn test_anon_fn_option_call_in_if_expr_2() { if fn () ?bool { return true }()? @@ -22,7 +22,7 @@ fn test_anon_fn_optional_call_in_if_expr_2() { } } -fn test_anon_fn_optional_call_in_if_expr_3() { +fn test_anon_fn_option_call_in_if_expr_3() { if fn () !bool { return true }()! diff --git a/vlib/v/tests/anon_fn_returning_question_test.v b/vlib/v/tests/anon_fn_returning_question_test.v index b19461409..3cf021929 100644 --- a/vlib/v/tests/anon_fn_returning_question_test.v +++ b/vlib/v/tests/anon_fn_returning_question_test.v @@ -9,7 +9,7 @@ fn create_and_call_anon_function() ? { println('test') // Note: the anon function does NOT return explicitly, // so V should generate an implicit "OK" value and - // return it. Previously, it created an implicit optional + // return it. Previously, it created an implicit option // filled with 0s => .ok was false, and that was treated // as a failure, triggering or blocks. } diff --git a/vlib/v/tests/anon_fn_with_optional_test.v b/vlib/v/tests/anon_fn_with_option_test.v similarity index 93% rename from vlib/v/tests/anon_fn_with_optional_test.v rename to vlib/v/tests/anon_fn_with_option_test.v index aa6164866..86a9cf218 100644 --- a/vlib/v/tests/anon_fn_with_optional_test.v +++ b/vlib/v/tests/anon_fn_with_option_test.v @@ -13,7 +13,7 @@ pub fn check_auth(mut app App) ?Response { return none } -fn test_anon_fn_with_optional() { +fn test_anon_fn_with_option() { app := App{ routes: [ Route{ diff --git a/vlib/v/tests/array_elements_with_optional_test.v b/vlib/v/tests/array_elements_with_option_test.v similarity index 50% rename from vlib/v/tests/array_elements_with_optional_test.v rename to vlib/v/tests/array_elements_with_option_test.v index 1e3d3e0f9..e31e50b99 100644 --- a/vlib/v/tests/array_elements_with_optional_test.v +++ b/vlib/v/tests/array_elements_with_option_test.v @@ -3,20 +3,20 @@ struct Foo1 { arr2 [5]int } -fn get_has_optional_fixed() ?int { +fn get_has_option_fixed() ?int { foo := Foo1{} return foo.arr1[0] } -fn get_no_optional_fixed() int { +fn get_no_option_fixed() int { foo := Foo1{} return foo.arr2[0] } -fn test_optional_fixed() ? { - x := get_has_optional_fixed()? +fn test_option_fixed() ? { + x := get_has_option_fixed()? assert x == 0 - assert get_no_optional_fixed() == 0 + assert get_no_option_fixed() == 0 } struct Foo2 { @@ -25,20 +25,20 @@ mut: arr2 []int } -fn get_has_optional() ?int { +fn get_has_option() ?int { mut foo := Foo2{} foo.arr1 << 0 return foo.arr1[0] } -fn get_no_optional() int { +fn get_no_option() int { mut foo := Foo2{} foo.arr2 << 0 return foo.arr2[0] } -fn test_optional_non_fixed() ? { - x := get_has_optional()? +fn test_option_non_fixed() ? { + x := get_has_option()? assert x == 0 - assert get_no_optional() == 0 + assert get_no_option() == 0 } diff --git a/vlib/v/tests/array_index_optional_test.v b/vlib/v/tests/array_index_option_test.v similarity index 75% rename from vlib/v/tests/array_index_optional_test.v rename to vlib/v/tests/array_index_option_test.v index 90c651223..4f73e41e8 100644 --- a/vlib/v/tests/array_index_optional_test.v +++ b/vlib/v/tests/array_index_option_test.v @@ -3,7 +3,7 @@ mut: x [][]int } -fn test_mut_array_index_optional() { +fn test_mut_array_index_option() { mut arr := Arrs{ x: [[1, 2]] } @@ -14,7 +14,7 @@ fn test_mut_array_index_optional() { } } -fn test_array_index_optional_with_if_expr() { +fn test_array_index_option_with_if_expr() { ret := []string{}[0] or { if true { 'a' } else { 'b' } } diff --git a/vlib/v/tests/array_init_test.v b/vlib/v/tests/array_init_test.v index 5f5ae48f2..910a17f1e 100644 --- a/vlib/v/tests/array_init_test.v +++ b/vlib/v/tests/array_init_test.v @@ -230,7 +230,7 @@ fn test_array_init_with_sumtype() { } } -fn test_array_init_inferred_from_optional() { +fn test_array_init_inferred_from_option() { a := read() or { [] } x := 1 b := read() or { diff --git a/vlib/v/tests/array_with_it_test.v b/vlib/v/tests/array_with_it_test.v index 1117407a6..89d80083a 100644 --- a/vlib/v/tests/array_with_it_test.v +++ b/vlib/v/tests/array_with_it_test.v @@ -16,7 +16,7 @@ fn test_array_with_it() { assert a4 == [5, 4, 3, 2, 1] } -fn test_array_init_with_optional() { +fn test_array_init_with_option() { input := [3.1, 1.1] arr := []f64{len: 3, init: input[it] or { 0.0 }} println(arr) diff --git a/vlib/v/tests/assign_optional_of_multi_return_with_heap_test.v b/vlib/v/tests/assign_option_of_multi_return_with_heap_test.v similarity index 93% rename from vlib/v/tests/assign_optional_of_multi_return_with_heap_test.v rename to vlib/v/tests/assign_option_of_multi_return_with_heap_test.v index d0a83413d..2ba9ce8ea 100644 --- a/vlib/v/tests/assign_optional_of_multi_return_with_heap_test.v +++ b/vlib/v/tests/assign_option_of_multi_return_with_heap_test.v @@ -44,7 +44,7 @@ fn somefun() ?int { return 0 } -fn test_assign_optional_of_multi_return_with_heap() { +fn test_assign_option_of_multi_return_with_heap() { somefun() or { return } println('success') } diff --git a/vlib/v/tests/c_function_mut_param/optional_args_test.v b/vlib/v/tests/c_function_mut_param/option_args_test.v similarity index 100% rename from vlib/v/tests/c_function_mut_param/optional_args_test.v rename to vlib/v/tests/c_function_mut_param/option_args_test.v diff --git a/vlib/v/tests/cast_optional_to_interface_test.v b/vlib/v/tests/cast_option_to_interface_test.v similarity index 92% rename from vlib/v/tests/cast_optional_to_interface_test.v rename to vlib/v/tests/cast_option_to_interface_test.v index 658d2a36f..54f21e65b 100644 --- a/vlib/v/tests/cast_optional_to_interface_test.v +++ b/vlib/v/tests/cast_option_to_interface_test.v @@ -28,7 +28,7 @@ struct Engine { parser Parser } -fn test_cast_optional_to_interface() { +fn test_cast_option_to_interface() { parser := new_parser()? assert parser.main.str == 'test' eprintln(voidptr(parser.main)) diff --git a/vlib/v/tests/check_in_is_consistency_test.v b/vlib/v/tests/check_in_is_consistency_test.v index 2a1563366..183db8a21 100644 --- a/vlib/v/tests/check_in_is_consistency_test.v +++ b/vlib/v/tests/check_in_is_consistency_test.v @@ -7,7 +7,7 @@ struct Test { fn check[T](val T) string { $if T in [?int, ?int] { - return 'optional int' + return 'option int' } $if T in [int, int] { return 'int' @@ -19,7 +19,7 @@ fn check2[T](val T) string { mut str := string{} $for field in T.fields { $if field.typ in [?int, ?int] { - str += 'optional int' + str += 'option int' } $if field.typ in [int, int] { str += 'int' @@ -30,7 +30,7 @@ fn check2[T](val T) string { fn check_is[T](val T) string { $if T is ?int { - return 'optional int' + return 'option int' } $if T is int { return 'int' @@ -42,7 +42,7 @@ fn check_is2[T](val T) string { mut str := string{} $for field in T.fields { $if field.typ is ?int { - str += 'optional int' + str += 'option int' } $if field.typ is int { str += 'int' @@ -65,7 +65,7 @@ fn test_in_2() { a: 1 b: 2 } - assert check2(var) == 'intoptional int' + assert check2(var) == 'intoption int' } fn test_is() { @@ -82,5 +82,5 @@ fn test_is_2() { a: 1 b: 2 } - assert check_is2(var) == 'intoptional int' + assert check_is2(var) == 'intoption int' } diff --git a/vlib/v/tests/comptime_for_in_fields_FieldData_test.v b/vlib/v/tests/comptime_for_in_fields_FieldData_test.v index a27d5b478..f726c3fb4 100644 --- a/vlib/v/tests/comptime_for_in_fields_FieldData_test.v +++ b/vlib/v/tests/comptime_for_in_fields_FieldData_test.v @@ -26,7 +26,7 @@ struct Complex { o_i ?int o_ch_i ?chan int = chan int{cap: 10} o_my_alias ?MyInt = 123 - // o_atomic_i ?atomic int // TODO: cgen error, but should be probably a checker one, since optional atomics do not make sense + // o_atomic_i ?atomic int // TODO: cgen error, but should be probably a checker one, since option atomics do not make sense o_pointer1_i ?&int = unsafe { nil } o_pointer2_i ?&&int = unsafe { nil } o_pointer3_i ?&&&int = unsafe { nil } @@ -57,12 +57,12 @@ fn test_is_atomic() { } } -fn test_is_optional() { +fn test_is_option() { $for f in Complex.fields { if f.name.starts_with('o_') { - assert f.is_optional, 'Complex.${f.name} should have f.is_optional set' + assert f.is_option, 'Complex.${f.name} should have f.is_option set' } else { - assert !f.is_optional, 'Complex.${f.name} should NOT have f.is_optional set' + assert !f.is_option, 'Complex.${f.name} should NOT have f.is_option set' } } } diff --git a/vlib/v/tests/const_can_use_optionals_results_test.v b/vlib/v/tests/const_can_use_options_results_test.v similarity index 100% rename from vlib/v/tests/const_can_use_optionals_results_test.v rename to vlib/v/tests/const_can_use_options_results_test.v diff --git a/vlib/v/tests/const_test.v b/vlib/v/tests/const_test.v index 583fd4359..e727715d5 100644 --- a/vlib/v/tests/const_test.v +++ b/vlib/v/tests/const_test.v @@ -18,7 +18,7 @@ fn test_const() { assert d == (e / 2) + 7 } -// const optional test +// const option test struct Foo { name string = 'foo' } diff --git a/vlib/v/tests/const_use_nested_optionals_test.v b/vlib/v/tests/const_use_nested_options_test.v similarity index 79% rename from vlib/v/tests/const_use_nested_optionals_test.v rename to vlib/v/tests/const_use_nested_options_test.v index 7e9287bf1..623bd37d7 100644 --- a/vlib/v/tests/const_use_nested_optionals_test.v +++ b/vlib/v/tests/const_use_nested_options_test.v @@ -2,7 +2,7 @@ import os const iterations = (os.getenv_opt('ITERATIONS') or { '5' }).int() -fn test_const_use_nested_optionals() { +fn test_const_use_nested_options() { println('Number of iterations: ${iterations}') assert iterations == 5 } diff --git a/vlib/v/tests/enum_test.v b/vlib/v/tests/enum_test.v index 1831a1b20..af4a1bd63 100644 --- a/vlib/v/tests/enum_test.v +++ b/vlib/v/tests/enum_test.v @@ -4,15 +4,15 @@ enum Color { green } -fn enum_optional_helper(b bool) ?Color { +fn enum_option_helper(b bool) ?Color { if b { return .red } return error('failed') } -fn test_enum_optional() { - a := enum_optional_helper(true) or { +fn test_enum_option() { + a := enum_option_helper(true) or { assert false return } diff --git a/vlib/v/tests/for_in_optional_test.v b/vlib/v/tests/for_in_option_test.v similarity index 78% rename from vlib/v/tests/for_in_optional_test.v rename to vlib/v/tests/for_in_option_test.v index a7052b3e7..ff00a8213 100644 --- a/vlib/v/tests/for_in_optional_test.v +++ b/vlib/v/tests/for_in_option_test.v @@ -1,6 +1,6 @@ import os -fn test_for_in_optional() { +fn test_for_in_option() { for d in os.read_lines(@FILE) or { panic('not found') } { println(d) } diff --git a/vlib/v/tests/for_loop_with_optional2_test.v b/vlib/v/tests/for_loop_with_option2_test.v similarity index 94% rename from vlib/v/tests/for_loop_with_optional2_test.v rename to vlib/v/tests/for_loop_with_option2_test.v index 11a755120..86a23e818 100644 --- a/vlib/v/tests/for_loop_with_optional2_test.v +++ b/vlib/v/tests/for_loop_with_option2_test.v @@ -5,7 +5,7 @@ struct Test { d ?[][]string } -fn test_for_in_optional_fields() { +fn test_for_in_option_fields() { mut out := []string{} test := Test{ a: [1, 2, 3] diff --git a/vlib/v/tests/for_loop_with_optional_test.v b/vlib/v/tests/for_loop_with_option_test.v similarity index 100% rename from vlib/v/tests/for_loop_with_optional_test.v rename to vlib/v/tests/for_loop_with_option_test.v diff --git a/vlib/v/tests/generics_interface_with_generic_sumtype_test.v b/vlib/v/tests/generics_interface_with_generic_sumtype_test.v index 558b5d899..b6b133d14 100644 --- a/vlib/v/tests/generics_interface_with_generic_sumtype_test.v +++ b/vlib/v/tests/generics_interface_with_generic_sumtype_test.v @@ -1,17 +1,16 @@ -// optional.v struct NonValue {} -pub type Optional[T] = NonValue | T +pub type OptionSumType[T] = NonValue | T -pub fn (self Optional[T]) is_some[T]() bool { +pub fn (self OptionSumType[T]) is_some[T]() bool { return self is T } -pub fn (self Optional[T]) is_none[T]() bool { +pub fn (self OptionSumType[T]) is_none[T]() bool { return !self.is_some() } -pub fn (self Optional[T]) expect[T](msg string) T { +pub fn (self OptionSumType[T]) expect[T](msg string) T { if self.is_some() { return self as T } else { @@ -19,11 +18,11 @@ pub fn (self Optional[T]) expect[T](msg string) T { } } -pub fn (self Optional[T]) unwrap[T]() T { +pub fn (self OptionSumType[T]) unwrap[T]() T { return self.expect('unwrap on a none value') } -pub fn (self Optional[T]) unwrap_or[T](default T) T { +pub fn (self OptionSumType[T]) unwrap_or[T](default T) T { if self.is_some() { return self as T } else { @@ -31,7 +30,7 @@ pub fn (self Optional[T]) unwrap_or[T](default T) T { } } -pub fn (self Optional[T]) unwrap_or_else[T](else_fn fn () T) T { +pub fn (self OptionSumType[T]) unwrap_or_else[T](else_fn fn () T) T { if self.is_some() { return self as T } else { @@ -39,18 +38,18 @@ pub fn (self Optional[T]) unwrap_or_else[T](else_fn fn () T) T { } } -pub fn some[T](value T) Optional[T] { - return Optional[T](value as T) +pub fn some[T](value T) OptionSumType[T] { + return OptionSumType[T](value as T) } -pub fn null[T]() Optional[T] { - return Optional[T](NonValue{}) +pub fn null[T]() OptionSumType[T] { + return OptionSumType[T](NonValue{}) } // iter.v pub interface Iterator[T] { mut: - next() Optional[T] + next() OptionSumType[T] } pub fn (mut self Iterator[T]) count[T]() int { @@ -65,8 +64,8 @@ struct EmptyIter[T] { value NonValue } -fn (mut self EmptyIter[T]) next[T]() Optional[T] { - return Optional[T](self.value) +fn (mut self EmptyIter[T]) next[T]() OptionSumType[T] { + return OptionSumType[T](self.value) } // iter_test.v diff --git a/vlib/v/tests/generics_method_returning_optional_test.v b/vlib/v/tests/generics_method_returning_option_test.v similarity index 91% rename from vlib/v/tests/generics_method_returning_optional_test.v rename to vlib/v/tests/generics_method_returning_option_test.v index 237c30fb2..f83a80905 100644 --- a/vlib/v/tests/generics_method_returning_optional_test.v +++ b/vlib/v/tests/generics_method_returning_option_test.v @@ -23,7 +23,7 @@ pub fn (mut x Abc) diagnostics() !Res { return got.params } -fn test_generic_method_returning_optional() { +fn test_generic_method_returning_option() { mut a := Abc{} a.diagnostics()! assert true diff --git a/vlib/v/tests/generics_test.v b/vlib/v/tests/generics_test.v index 6df92bc4c..29d72f89c 100644 --- a/vlib/v/tests/generics_test.v +++ b/vlib/v/tests/generics_test.v @@ -174,7 +174,7 @@ fn opt[T](v T) ?T { return none } -fn test_optional() { +fn test_option() { s := opt('hi') or { '' } assert s == 'hi' i := opt(5) or { 0 } diff --git a/vlib/v/tests/if_cond_with_optional_test.v b/vlib/v/tests/if_cond_with_option_test.v similarity index 94% rename from vlib/v/tests/if_cond_with_optional_test.v rename to vlib/v/tests/if_cond_with_option_test.v index c2828b0fa..014d8c07c 100644 --- a/vlib/v/tests/if_cond_with_optional_test.v +++ b/vlib/v/tests/if_cond_with_option_test.v @@ -29,7 +29,7 @@ fn create_sampler() Sample { return if rand.intn(1) or { 0 } == 0 { Sample(SampleA{}) } else { Sample(SampleB{}) } } -fn test_if_cond_with_optional() { +fn test_if_cond_with_option() { mut sample := create_sampler() mut ret := sample.get_next() println(ret) diff --git a/vlib/v/tests/if_expr_nested_with_optional_result_test.v b/vlib/v/tests/if_expr_nested_with_option_result_test.v similarity index 91% rename from vlib/v/tests/if_expr_nested_with_optional_result_test.v rename to vlib/v/tests/if_expr_nested_with_option_result_test.v index b6cc8dc04..b37346252 100644 --- a/vlib/v/tests/if_expr_nested_with_optional_result_test.v +++ b/vlib/v/tests/if_expr_nested_with_option_result_test.v @@ -1,4 +1,3 @@ -// optional pub fn foo1() bool { return false } @@ -38,7 +37,7 @@ pub fn bar2(i int) !int { } } -fn test_if_expr_nested_with_optional_result() { +fn test_if_expr_nested_with_option_result() { ret11 := bar1(0) or { 0 } println(ret11) assert ret11 == 2 diff --git a/vlib/v/tests/if_expr_of_optional_test.v b/vlib/v/tests/if_expr_of_option_test.v similarity index 91% rename from vlib/v/tests/if_expr_of_optional_test.v rename to vlib/v/tests/if_expr_of_option_test.v index 164ac7fcf..25178cd22 100644 --- a/vlib/v/tests/if_expr_of_optional_test.v +++ b/vlib/v/tests/if_expr_of_option_test.v @@ -14,7 +14,7 @@ fn foo4() ?int { return if true { 2 } else { 0 } } -fn test_if_expr_of_optional() { +fn test_if_expr_of_option() { a1 := foo1() or { panic('error') } println(a1) assert a1 == 0 @@ -48,7 +48,7 @@ fn foo_complex() ?int { } } -fn test_if_expr_of_optional_complex() { +fn test_if_expr_of_option_complex() { a := foo_complex() or { panic('error') } println(a) assert a == 10 diff --git a/vlib/v/tests/if_expr_with_method_call_optional_test.v b/vlib/v/tests/if_expr_with_method_call_option_test.v similarity index 87% rename from vlib/v/tests/if_expr_with_method_call_optional_test.v rename to vlib/v/tests/if_expr_with_method_call_option_test.v index e8e9e2f98..c5162f7dd 100644 --- a/vlib/v/tests/if_expr_with_method_call_optional_test.v +++ b/vlib/v/tests/if_expr_with_method_call_option_test.v @@ -6,7 +6,7 @@ fn from_bit_string(bits []u8) rune { return if bits == [] { 0 } else { bits.map(it.str()).join('').parse_uint(2, 8) or { 0 } } } -fn test_if_expr_with_method_call_optional() { +fn test_if_expr_with_method_call_option() { a := from_bit_string(to_bit_string(u32(100))) println(a) assert a == `d` diff --git a/vlib/v/tests/if_expression_test.v b/vlib/v/tests/if_expression_test.v index e20a541a5..7a4b37a39 100644 --- a/vlib/v/tests/if_expression_test.v +++ b/vlib/v/tests/if_expression_test.v @@ -214,15 +214,15 @@ fn test_if_expr_with_complex_array_methods() { assert ret == ['a', 'b', 'c'] } -fn return_optional() ?int { +fn return_option() ?int { return 1 } -fn test_if_expr_with_optional() { +fn test_if_expr_with_option() { m := map[string]int{} v := if a := m['a'] { println('${a}') - return_optional()? + return_option()? } else { 2 } @@ -231,7 +231,7 @@ fn test_if_expr_with_optional() { fn test_if_expr_with_or_block() { arr := ['a'] - a := if arr.len == 0 || arr[0] == '-' { 123 } else { return_optional() or { -1 } } + a := if arr.len == 0 || arr[0] == '-' { 123 } else { return_option() or { -1 } } assert a == 1 } diff --git a/vlib/v/tests/init_multiple_branches_test.v b/vlib/v/tests/init_multiple_branches_test.v index 7f1c34cb0..aa0773126 100644 --- a/vlib/v/tests/init_multiple_branches_test.v +++ b/vlib/v/tests/init_multiple_branches_test.v @@ -20,7 +20,7 @@ fn test_init_multiple_branches() { t: m['t'] or { 0 }.bool() s: if a := m['a'] { println('a => ${a}') - b := return_optional(a)? // Fails only if the expr in this line has or_block (or_block kind (.propagation or .block) doesn't matter) + b := return_option(a)? // Fails only if the expr in this line has or_block (or_block kind (.propagation or .block) doesn't matter) b } else { map[string]Sum{} @@ -29,6 +29,6 @@ fn test_init_multiple_branches() { println('app => ${app}') } -fn return_optional(input json2.Any) ?map[string]Sum { +fn return_option(input json2.Any) ?map[string]Sum { return map[string]Sum{} } diff --git a/vlib/v/tests/inout/comptime_bool_fields.out b/vlib/v/tests/inout/comptime_bool_fields.out index efb962b6b..b02c32aaa 100644 --- a/vlib/v/tests/inout/comptime_bool_fields.out +++ b/vlib/v/tests/inout/comptime_bool_fields.out @@ -5,7 +5,7 @@ b is mut?: true b is struct?: true field: c c is pub?: true -c is optional?: true +c is option?: true field: d d is pub?: true d is map?: true @@ -19,4 +19,4 @@ f is array?: true field: zzz zzz is pub?: true zzz is mut?: true -zzz is alias?: true +zzz is alias?: true \ No newline at end of file diff --git a/vlib/v/tests/inout/comptime_bool_fields.vv b/vlib/v/tests/inout/comptime_bool_fields.vv index 9a23fd6a5..8da05e7ef 100644 --- a/vlib/v/tests/inout/comptime_bool_fields.vv +++ b/vlib/v/tests/inout/comptime_bool_fields.vv @@ -42,8 +42,8 @@ fn foo[U](val U) { $if field.is_shared { println(field.name + ' is shared?: ${field.is_shared}') } - $if field.is_optional { - println(field.name + ' is optional?: ${field.is_optional}') + $if field.is_option { + println(field.name + ' is option?: ${field.is_option}') } $if field.is_atomic { println(field.name + ' is atomic?: ${field.is_atomic}') diff --git a/vlib/v/tests/inout/optional_fn_arg.out b/vlib/v/tests/inout/option_fn_arg.out similarity index 100% rename from vlib/v/tests/inout/optional_fn_arg.out rename to vlib/v/tests/inout/option_fn_arg.out diff --git a/vlib/v/tests/inout/optional_fn_arg.vv b/vlib/v/tests/inout/option_fn_arg.vv similarity index 100% rename from vlib/v/tests/inout/optional_fn_arg.vv rename to vlib/v/tests/inout/option_fn_arg.vv diff --git a/vlib/v/tests/inout/printing_optional_in_if_expr.out b/vlib/v/tests/inout/printing_option_in_if_expr.out similarity index 100% rename from vlib/v/tests/inout/printing_optional_in_if_expr.out rename to vlib/v/tests/inout/printing_option_in_if_expr.out diff --git a/vlib/v/tests/inout/printing_optional_in_if_expr.vv b/vlib/v/tests/inout/printing_option_in_if_expr.vv similarity index 58% rename from vlib/v/tests/inout/printing_optional_in_if_expr.vv rename to vlib/v/tests/inout/printing_option_in_if_expr.vv index f1c9f9b20..90dac7f13 100644 --- a/vlib/v/tests/inout/printing_optional_in_if_expr.vv +++ b/vlib/v/tests/inout/printing_option_in_if_expr.vv @@ -1,10 +1,10 @@ -fn optional() ?int { +fn option() ?int { return 1 } fn main() { println(if true { - i32(optional() or { 2 }) + i32(option() or { 2 }) } else { i32(8) }) diff --git a/vlib/v/tests/inout/struct_field_option.out b/vlib/v/tests/inout/struct_field_option.out new file mode 100644 index 000000000..7292b2a8a --- /dev/null +++ b/vlib/v/tests/inout/struct_field_option.out @@ -0,0 +1,26 @@ +1 +[vlib/v/tests/inout/struct_field_option.vv:15] f.bar?: 1 +2 +[vlib/v/tests/inout/struct_field_option.vv:22] f.bar?: 2 +3 +[vlib/v/tests/inout/struct_field_option.vv:26] f.bar?: 3 +3 +[vlib/v/tests/inout/struct_field_option.vv:30] a: 3 +9999 +[vlib/v/tests/inout/struct_field_option.vv:33] b: 9999 +4 +[vlib/v/tests/inout/struct_field_option.vv:37] sum: 4 +4 +[vlib/v/tests/inout/struct_field_option.vv:40] sum: 4 +3 +none +3 +Foo{ + bar: 3 + baz: 0 +} +[vlib/v/tests/inout/struct_field_option.vv:61] f: Foo{ + bar: 3 + baz: 0 +} +1 \ No newline at end of file diff --git a/vlib/v/tests/inout/struct_field_optional.vv b/vlib/v/tests/inout/struct_field_option.vv similarity index 100% rename from vlib/v/tests/inout/struct_field_optional.vv rename to vlib/v/tests/inout/struct_field_option.vv diff --git a/vlib/v/tests/inout/struct_field_optional.out b/vlib/v/tests/inout/struct_field_optional.out deleted file mode 100644 index 9d125ac42..000000000 --- a/vlib/v/tests/inout/struct_field_optional.out +++ /dev/null @@ -1,26 +0,0 @@ -1 -[vlib/v/tests/inout/struct_field_optional.vv:15] f.bar?: 1 -2 -[vlib/v/tests/inout/struct_field_optional.vv:22] f.bar?: 2 -3 -[vlib/v/tests/inout/struct_field_optional.vv:26] f.bar?: 3 -3 -[vlib/v/tests/inout/struct_field_optional.vv:30] a: 3 -9999 -[vlib/v/tests/inout/struct_field_optional.vv:33] b: 9999 -4 -[vlib/v/tests/inout/struct_field_optional.vv:37] sum: 4 -4 -[vlib/v/tests/inout/struct_field_optional.vv:40] sum: 4 -3 -none -3 -Foo{ - bar: 3 - baz: 0 -} -[vlib/v/tests/inout/struct_field_optional.vv:61] f: Foo{ - bar: 3 - baz: 0 -} -1 diff --git a/vlib/v/tests/interface_only_decl_with_optional_test.v b/vlib/v/tests/interface_only_decl_with_option_test.v similarity index 64% rename from vlib/v/tests/interface_only_decl_with_optional_test.v rename to vlib/v/tests/interface_only_decl_with_option_test.v index ed385d8b6..b8c552cb0 100644 --- a/vlib/v/tests/interface_only_decl_with_optional_test.v +++ b/vlib/v/tests/interface_only_decl_with_option_test.v @@ -2,7 +2,7 @@ interface Message { serialize() ?[]u8 } -fn test_interface_only_decl_with_optional() { +fn test_interface_only_decl_with_option() { println('test interface') assert true } diff --git a/vlib/v/tests/map_value_with_optional_result_test.v b/vlib/v/tests/map_value_with_option_result_test.v similarity index 89% rename from vlib/v/tests/map_value_with_optional_result_test.v rename to vlib/v/tests/map_value_with_option_result_test.v index 5ddb76234..9849e3f4c 100644 --- a/vlib/v/tests/map_value_with_optional_result_test.v +++ b/vlib/v/tests/map_value_with_option_result_test.v @@ -4,7 +4,7 @@ struct AdbDevice { opts map[string]?string } -fn test_map_value_with_optional_result() { +fn test_map_value_with_option_result() { // avoid warnings _ := os.max_path_len assert true diff --git a/vlib/v/tests/match_expr_nested_with_optional_result1_test.v b/vlib/v/tests/match_expr_nested_with_option_result1_test.v similarity index 90% rename from vlib/v/tests/match_expr_nested_with_optional_result1_test.v rename to vlib/v/tests/match_expr_nested_with_option_result1_test.v index 730daa141..a0064c3dd 100644 --- a/vlib/v/tests/match_expr_nested_with_optional_result1_test.v +++ b/vlib/v/tests/match_expr_nested_with_option_result1_test.v @@ -1,4 +1,3 @@ -// optional fn foo1() ?int { return 1 } @@ -46,7 +45,7 @@ fn is_ok2(y int, m int) !bool { } } -fn test_match_expr_nested_with_optional_result() { +fn test_match_expr_nested_with_option_result() { ret1 := is_ok1(2, 1) or { false } println(ret1) assert ret1 diff --git a/vlib/v/tests/match_expr_nested_with_optional_result2_test.v b/vlib/v/tests/match_expr_nested_with_option_result2_test.v similarity index 91% rename from vlib/v/tests/match_expr_nested_with_optional_result2_test.v rename to vlib/v/tests/match_expr_nested_with_option_result2_test.v index c30e5181a..20588e95d 100644 --- a/vlib/v/tests/match_expr_nested_with_optional_result2_test.v +++ b/vlib/v/tests/match_expr_nested_with_option_result2_test.v @@ -1,4 +1,3 @@ -// optional fn foo1() ?int { return 1 } @@ -52,7 +51,7 @@ fn is_ok2(y int, m int) !bool { } } -fn test_match_expr_nested_with_optional_result() { +fn test_match_expr_nested_with_option_result() { ret1 := is_ok1(2, 1) or { false } println(ret1) assert ret1 diff --git a/vlib/v/tests/match_expr_returning_optional_test.v b/vlib/v/tests/match_expr_returning_option_test.v similarity index 77% rename from vlib/v/tests/match_expr_returning_optional_test.v rename to vlib/v/tests/match_expr_returning_option_test.v index 708497d73..01f123f50 100644 --- a/vlib/v/tests/match_expr_returning_optional_test.v +++ b/vlib/v/tests/match_expr_returning_option_test.v @@ -22,7 +22,7 @@ fn fails(s string) ?Any { } } -fn test_match_expr_returning_optional() { +fn test_match_expr_returning_option() { ret1 := ok('foo') println(ret1) assert ret1 == Any(1) @@ -39,11 +39,11 @@ fn func() ?string { code := 0 return match code { 0 { 'zero' } - else { error('as we are returning an optional') } + else { error('as we are returning an option') } } } -fn test_match_expr_returning_optional_with_error() { +fn test_match_expr_returning_option_with_error() { ret := func() or { 'error' } println(ret) assert ret == 'zero' diff --git a/vlib/v/tests/multi_line_with_optionals_test.v b/vlib/v/tests/multi_line_with_optionals_test.v deleted file mode 100644 index 9bd5e183a..000000000 --- a/vlib/v/tests/multi_line_with_optionals_test.v +++ /dev/null @@ -1,21 +0,0 @@ -fn has_optional() ?int { - return 51 -} - -fn test_optionals_multi_line() { - var11, var12, var13, var14, var15 := if true { - println('dd') - if true { - has_optional()?, has_optional()?, has_optional()?, 28, has_optional()? - } else { - has_optional()?, has_optional()?, has_optional()?, has_optional()?, has_optional()? - } - } else { - has_optional()?, 4, 4, 4, 4 - } - println(var11) - println(var12) - println(var13) - println(var14) - println(var15) -} diff --git a/vlib/v/tests/multi_line_with_options_test.v b/vlib/v/tests/multi_line_with_options_test.v new file mode 100644 index 000000000..150f87bfd --- /dev/null +++ b/vlib/v/tests/multi_line_with_options_test.v @@ -0,0 +1,21 @@ +fn has_option() ?int { + return 51 +} + +fn test_options_multi_line() { + var11, var12, var13, var14, var15 := if true { + println('dd') + if true { + has_option()?, has_option()?, has_option()?, 28, has_option()? + } else { + has_option()?, has_option()?, has_option()?, has_option()?, has_option()? + } + } else { + has_option()?, 4, 4, 4, 4 + } + println(var11) + println(var12) + println(var13) + println(var14) + println(var15) +} diff --git a/vlib/v/tests/optional_compvar_types_test.v b/vlib/v/tests/option_compvar_types_test.v similarity index 100% rename from vlib/v/tests/optional_compvar_types_test.v rename to vlib/v/tests/option_compvar_types_test.v diff --git a/vlib/v/tests/optional_compvar_val_test.v b/vlib/v/tests/option_compvar_val_test.v similarity index 97% rename from vlib/v/tests/optional_compvar_val_test.v rename to vlib/v/tests/option_compvar_val_test.v index a329dd172..2d0f06a24 100644 --- a/vlib/v/tests/optional_compvar_val_test.v +++ b/vlib/v/tests/option_compvar_val_test.v @@ -37,7 +37,7 @@ fn encode_struct[T](val T) map[string][]string { out[field.name] << value.str() } // This work well - $if field.is_optional { + $if field.is_option { write1(value) wr.write2(value) out[field.name] << '${value}' diff --git a/vlib/v/tests/option_default_values_test.v b/vlib/v/tests/option_default_values_test.v index 395593a1f..4c4d43a3b 100644 --- a/vlib/v/tests/option_default_values_test.v +++ b/vlib/v/tests/option_default_values_test.v @@ -35,7 +35,7 @@ fn return_a_string() string { } // -fn test_optional_int() { +fn test_option_int() { a := i_0(0) or { 4999 } assert a == 4999 b := i_0(4123) or { 4999 } @@ -43,7 +43,7 @@ fn test_optional_int() { } /* -fn test_optional_bool() { +fn test_option_bool() { a := true && b_0(false) { true } @@ -54,14 +54,14 @@ fn test_optional_bool() { assert b == true } */ -fn test_optional_struct() { +fn test_option_struct() { sa := struct_0(0) or { Abc{7999} } assert sa.x == 7999 sb := struct_0(3456) or { Abc{7999} } assert sb.x == 3456 } -fn test_optional_with_statements_before_last_expression() { +fn test_option_with_statements_before_last_expression() { s := struct_0(0) or { eprintln('hello') Abc{12345} @@ -71,12 +71,12 @@ fn test_optional_with_statements_before_last_expression() { assert b == true } -fn test_optional_with_fn_call_as_last_expression() { +fn test_option_with_fn_call_as_last_expression() { s := string_0(0) or { return_a_string() } assert s == 'abcdef' } -fn test_optional_with_fn_call_last_expr_and_preceding_statements() { +fn test_option_with_fn_call_last_expr_and_preceding_statements() { s := string_0(0) or { eprintln('hello') println('world') @@ -85,7 +85,7 @@ fn test_optional_with_fn_call_last_expr_and_preceding_statements() { assert s == 'abcdef' } -fn test_nested_optional() { +fn test_nested_option() { a := i_0(1) or { b := i_0(0) or { 3 } b @@ -103,14 +103,14 @@ fn test_nested_optional() { assert c == 3 } -fn test_nested_optional_with_opt_fn_call_as_last_value() { +fn test_nested_option_with_opt_fn_call_as_last_value() { a := i_0(1) or { i_0(0) or { 3 } } assert a == 1 b := i_0(0) or { i_0(1) or { 3 } } assert b == 1 c := i_0(0) or { i_0(0) or { 3 } } assert c == 3 - // TODO Enable once optional in boolean expressions are working + // TODO Enable once option in boolean expressions are working // d := b_0(true) or { // false && b_0(true) or { // true diff --git a/vlib/v/tests/optional_multi_return_test.v b/vlib/v/tests/option_multi_return_test.v similarity index 76% rename from vlib/v/tests/optional_multi_return_test.v rename to vlib/v/tests/option_multi_return_test.v index c73ddd3d2..89cc8db68 100644 --- a/vlib/v/tests/optional_multi_return_test.v +++ b/vlib/v/tests/option_multi_return_test.v @@ -2,7 +2,7 @@ fn tuple() ?(int, int) { return 1, 2 } -fn test_optional_multi_return() { +fn test_option_multi_return() { println(tuple()?) a, b := tuple()? assert a == 1 diff --git a/vlib/v/tests/option_struct_init_test.v b/vlib/v/tests/option_struct_init_test.v index 908a8e314..37e3b9bed 100644 --- a/vlib/v/tests/option_struct_init_test.v +++ b/vlib/v/tests/option_struct_init_test.v @@ -14,7 +14,7 @@ fn add(mut h Holder) ?int { return h.i } -fn test_struct_init_with_multiple_optionals() { +fn test_struct_init_with_multiple_options() { mut h := Holder{} foo := Foo{add(mut h) or { 0 }, add(mut h) or { 0 }, add(mut h) or { 0 }} diff --git a/vlib/v/tests/option_test.v b/vlib/v/tests/option_test.v index 2d12a4743..c951991d5 100644 --- a/vlib/v/tests/option_test.v +++ b/vlib/v/tests/option_test.v @@ -99,7 +99,7 @@ fn foo_str() ?string { return 'something' } -fn propagate_optional(b bool) ?int { +fn propagate_option(b bool) ?int { a := err_call(b)? return a } @@ -111,11 +111,11 @@ fn propagate_different_type(b bool) ?bool { fn test_propagation() { println(1) - a := propagate_optional(true) or { 0 } + a := propagate_option(true) or { 0 } println(2) assert a == 42 println(3) - if _ := propagate_optional(false) { + if _ := propagate_option(false) { assert false } println(4) @@ -269,12 +269,12 @@ fn test_multi_return_opt() { } */ -fn test_optional_val_with_empty_or() { +fn test_option_val_with_empty_or() { ret_none() or {} assert true } -fn test_optional_void_return_types_of_anon_fn() { +fn test_option_void_return_types_of_anon_fn() { f := fn (i int) ? { if i == 0 { return error('0') @@ -320,7 +320,7 @@ struct CC { str string } -fn optional_sum_type(a int) ?AA { +fn option_sum_type(a int) ?AA { match a { 1 { return BB{'Test'} @@ -334,12 +334,12 @@ fn optional_sum_type(a int) ?AA { } } -fn test_optional_sum_type() { - res1 := optional_sum_type(1) or { +fn test_option_sum_type() { + res1 := option_sum_type(1) or { assert false BB{} } - res2 := optional_sum_type(2) or { + res2 := option_sum_type(2) or { assert false CC{} } @@ -353,14 +353,14 @@ fn test_optional_sum_type() { } else { assert false } - optional_sum_type(3) or { + option_sum_type(3) or { assert true return } assert false } -struct MultiOptionalFieldTest { +struct MultiOptionFieldTest { a ?int b ?int } @@ -386,16 +386,16 @@ fn get_opt_pointer_to_c_struct() ?&C.stat { return none } -fn test_optional_ref_c_struct_gen() { +fn test_option_ref_c_struct_gen() { _ := get_opt_pointer_to_c_struct() or { &C.stat{} } } -// For issue #16070: cgen error: missing * of optional non-ref structs +// For issue #16070: cgen error: missing * of option non-ref structs fn get_opt_to_c_struct() ?C.stat { return none } -fn test_optional_c_struct_gen() { +fn test_option_c_struct_gen() { _ := get_opt_to_c_struct() or { C.stat{} } } diff --git a/vlib/v/tests/option_void_2_test.v b/vlib/v/tests/option_void_2_test.v index 621d35408..30555bf01 100644 --- a/vlib/v/tests/option_void_2_test.v +++ b/vlib/v/tests/option_void_2_test.v @@ -1,4 +1,4 @@ -fn test_optional_void() { +fn test_option_void() { foo(22)? assert true } diff --git a/vlib/v/tests/option_void_test.v b/vlib/v/tests/option_void_test.v index 2d15d473c..aa627e8f3 100644 --- a/vlib/v/tests/option_void_test.v +++ b/vlib/v/tests/option_void_test.v @@ -2,7 +2,7 @@ fn foo() ? { return error('something') } -fn test_optional_void() { +fn test_option_void() { foo() or { println(err) assert err.msg() == 'something' @@ -14,7 +14,7 @@ fn bar() ? { return error('bar error') } -fn test_optional_void_only_question() { +fn test_option_void_only_question() { bar() or { println(err) assert err.msg() == 'bar error' @@ -22,7 +22,7 @@ fn test_optional_void_only_question() { } } -fn test_optional_void_with_empty_or() { +fn test_option_void_with_empty_or() { foo() or {} assert true } @@ -35,7 +35,7 @@ fn option_void(a int) ? { } } -fn test_optional_void_with_return() { +fn test_option_void_with_return() { option_void(0) or { println(err) assert err.msg() == 'zero error' diff --git a/vlib/v/tests/repl/optional_call.repl b/vlib/v/tests/repl/option_call.repl similarity index 100% rename from vlib/v/tests/repl/optional_call.repl rename to vlib/v/tests/repl/option_call.repl diff --git a/vlib/v/tests/return_optional_test.v b/vlib/v/tests/return_option_test.v similarity index 84% rename from vlib/v/tests/return_optional_test.v rename to vlib/v/tests/return_option_test.v index 44ae9acc2..445b25ded 100644 --- a/vlib/v/tests/return_optional_test.v +++ b/vlib/v/tests/return_option_test.v @@ -6,7 +6,7 @@ fn func2() ?(int, int) { return func1()?, 1 } -fn test_return_optional() { +fn test_return_option() { a, b := func2()? println('${a}, ${b}') assert a == 0 diff --git a/vlib/v/tests/shared_optional_test.v b/vlib/v/tests/shared_option_test.v similarity index 89% rename from vlib/v/tests/shared_optional_test.v rename to vlib/v/tests/shared_option_test.v index 22c0c749b..960921158 100644 --- a/vlib/v/tests/shared_optional_test.v +++ b/vlib/v/tests/shared_option_test.v @@ -3,7 +3,7 @@ mut: s string } -fn test_shared_optional() { +fn test_shared_option() { shared abc := foo() or { panic('scared') } rlock abc { println(abc) diff --git a/vlib/v/tests/skip_unused/const_used_in_struct_default_init_recursive.vv b/vlib/v/tests/skip_unused/const_used_in_struct_default_init_recursive.vv index ea1653027..e3664b833 100644 --- a/vlib/v/tests/skip_unused/const_used_in_struct_default_init_recursive.vv +++ b/vlib/v/tests/skip_unused/const_used_in_struct_default_init_recursive.vv @@ -17,7 +17,7 @@ pub mut: generic_placeholder_len int sumtype_children_len int interface_children_len int - children []&Symbol // methods, sum types, map types, optionals, struct fields, etc. + children []&Symbol // methods, sum types, map types, options, struct fields, etc. file_path string [required] // required in order to register the symbol at its appropriate directory. file_version int [required] // file version when the symbol was registered } diff --git a/vlib/v/tests/skip_unused/shorthands_for_optionals_and_results_in_main.run.out b/vlib/v/tests/skip_unused/shorthands_for_options_and_results_in_main.run.out similarity index 100% rename from vlib/v/tests/skip_unused/shorthands_for_optionals_and_results_in_main.run.out rename to vlib/v/tests/skip_unused/shorthands_for_options_and_results_in_main.run.out diff --git a/vlib/v/tests/skip_unused/shorthands_for_optionals_and_results_in_main.skip_unused.run.out b/vlib/v/tests/skip_unused/shorthands_for_options_and_results_in_main.skip_unused.run.out similarity index 100% rename from vlib/v/tests/skip_unused/shorthands_for_optionals_and_results_in_main.skip_unused.run.out rename to vlib/v/tests/skip_unused/shorthands_for_options_and_results_in_main.skip_unused.run.out diff --git a/vlib/v/tests/skip_unused/shorthands_for_optionals_and_results_in_main.vv b/vlib/v/tests/skip_unused/shorthands_for_options_and_results_in_main.vv similarity index 100% rename from vlib/v/tests/skip_unused/shorthands_for_optionals_and_results_in_main.vv rename to vlib/v/tests/skip_unused/shorthands_for_options_and_results_in_main.vv diff --git a/vlib/v/tests/string_optional_none_test.v b/vlib/v/tests/string_option_none_test.v similarity index 87% rename from vlib/v/tests/string_optional_none_test.v rename to vlib/v/tests/string_option_none_test.v index e785fc42c..50b7bd9a2 100644 --- a/vlib/v/tests/string_optional_none_test.v +++ b/vlib/v/tests/string_option_none_test.v @@ -15,7 +15,7 @@ fn foo() int|none|IError { return MyError{} } -fn test_string_optional_none() { +fn test_string_option_none() { x := foo() println(x) assert true diff --git a/vlib/v/tests/struct_optional_field_zero_test.v b/vlib/v/tests/struct_option_field_zero_test.v similarity index 84% rename from vlib/v/tests/struct_optional_field_zero_test.v rename to vlib/v/tests/struct_option_field_zero_test.v index b2befb9d2..01891ab16 100644 --- a/vlib/v/tests/struct_optional_field_zero_test.v +++ b/vlib/v/tests/struct_option_field_zero_test.v @@ -2,7 +2,7 @@ struct Foo { foo ?string } -fn test_struct_optional_field_zero() { +fn test_struct_option_field_zero() { a := Foo{} if foo := a.foo { println(foo) diff --git a/vlib/v/tests/struct_test.v b/vlib/v/tests/struct_test.v index 7548a039b..80286b0ea 100644 --- a/vlib/v/tests/struct_test.v +++ b/vlib/v/tests/struct_test.v @@ -314,38 +314,38 @@ fn test_levels() { // Struct where an inizialized field is after a non-initilized field. struct StructWithDefaultValues1 { - field_required int - field_optional int = 5 + field_uninitialized int + field_initialized int = 5 } // Struct where an inizialized field is before a non-initilized field. struct StructWithDefaultValues2 { - field_optional int = 3 - field_required int + field_initialized int = 3 + field_uninitialized int } // Struct where an inizialized field is before several non-initilized fields. struct StructWithDefaultValues3 { - field_optional int = 2 - field_required int - field_required_too int + field_initialized int = 2 + field_uninitialized int + field_uninitialized_too int } fn test_struct_with_default_values_init() { s1 := StructWithDefaultValues1{ - field_required: 5 + field_uninitialized: 5 } s2 := StructWithDefaultValues2{ - field_required: 5 + field_uninitialized: 5 } // Partially initialized s3 := StructWithDefaultValues3{ - field_required: 5 + field_uninitialized: 5 } - assert s1.field_optional == 5 - assert s2.field_optional == 3 - assert s3.field_optional == 2 + assert s1.field_initialized == 5 + assert s2.field_initialized == 3 + assert s3.field_initialized == 2 } fn test_struct_with_default_values_no_init() { @@ -354,18 +354,18 @@ fn test_struct_with_default_values_no_init() { s2 := StructWithDefaultValues2{} s3 := StructWithDefaultValues3{} - assert s1.field_optional == 5 - assert s2.field_optional == 3 - assert s3.field_optional == 2 + assert s1.field_initialized == 5 + assert s2.field_initialized == 3 + assert s3.field_initialized == 2 } -struct FieldsWithOptionalVoidReturnType { +struct FieldsWithOptionVoidReturnType { f fn () ? g fn () ? } -fn test_fields_anon_fn_with_optional_void_return_type() { - foo := FieldsWithOptionalVoidReturnType{ +fn test_fields_anon_fn_with_option_void_return_type() { + foo := FieldsWithOptionVoidReturnType{ f: fn () ? { return error('oops') } diff --git a/vlib/v/tests/typeof_type_test.v b/vlib/v/tests/typeof_type_test.v index 33a9dba36..5b1b1b9af 100644 --- a/vlib/v/tests/typeof_type_test.v +++ b/vlib/v/tests/typeof_type_test.v @@ -17,7 +17,7 @@ fn test_typeof_string() { assert typeof[string]().name == 'string' } -fn test_typeof_optional_type() { +fn test_typeof_option_type() { assert typeof[?string]().name == '?string' } diff --git a/vlib/v/tests/valgrind/1.strings_and_arrays.v b/vlib/v/tests/valgrind/1.strings_and_arrays.v index 6c6c27eee..0b98ef033 100644 --- a/vlib/v/tests/valgrind/1.strings_and_arrays.v +++ b/vlib/v/tests/valgrind/1.strings_and_arrays.v @@ -151,23 +151,23 @@ fn opt(s string) ?int { return 1 } -fn optional_str() { +fn option_str() { q := 'select' s := 'query: select' - // optional fn args must be freed + // option fn args must be freed pos2 := opt('query:${q}') or { // pos := s.index('query: $q') or { println('exiting') return } println(pos2 + 1) - // optional method args must be freed + // option method args must be freed pos := s.index('query: ${q}') or { println('exiting') return } println(pos + 1) - // test assigning an optional to an existing var + // test assigning an option to an existing var mut p := 0 for { p = opt('query:${q}') or { break } @@ -183,7 +183,7 @@ fn return_error_with_freed_expr() ?string { return 'ok' } -fn optional_return() { +fn option_return() { return_error_with_freed_expr() or { return } } @@ -374,7 +374,7 @@ fn parse_header1(s string) ?string { return words[0] } -fn advanced_optionals() { +fn advanced_options() { s := parse_header0('foo:bar') or { return } s2 := parse_header1('foo:bar') or { return } } @@ -389,8 +389,8 @@ fn main() { str_tmp_expr_advanced_var_decl() str_inter() match_expr() - optional_str() - // optional_return() + option_str() + // option_return() str_replace() str_replace2() if_cond() @@ -405,7 +405,7 @@ fn main() { s2 := return_sb_str() // free_map() // loop_map() - advanced_optionals() + advanced_options() free_array_except_returned_element() println('end') } diff --git a/vlib/x/json2/decode_struct_test.v b/vlib/x/json2/decode_struct_test.v index 997ea9ce5..56f0560cc 100644 --- a/vlib/x/json2/decode_struct_test.v +++ b/vlib/x/json2/decode_struct_test.v @@ -31,7 +31,7 @@ mut: val T } -struct StructTypeOptional[T] { +struct StructTypeOption[T] { mut: val ?T } diff --git a/vlib/x/json2/decode_struct_todo_test.vv b/vlib/x/json2/decode_struct_todo_test.vv index e1fe43fe2..d802ec2ef 100644 --- a/vlib/x/json2/decode_struct_todo_test.vv +++ b/vlib/x/json2/decode_struct_todo_test.vv @@ -1,50 +1,51 @@ +import json -fn test_optional_types() { - assert json.decode[StructTypeOptional[string]]('{}')!.val == none - assert json.decode[StructTypeOptional[string]]('{"val": ""}')!.val == '' - assert json.decode[StructTypeOptional[string]]('{"val": "0"}')!.val == '0' - assert json.decode[StructTypeOptional[string]]('{"val": "1"}')!.val == '1' - assert json.decode[StructTypeOptional[string]]('{"val": "2"}')!.val == '2' - assert json.decode[StructTypeOptional[string]]('{"val": 0}')!.val == '0' - assert json.decode[StructTypeOptional[string]]('{"val": 1}')!.val == '1' - assert json.decode[StructTypeOptional[string]]('{"val": 2}')!.val == '2' - assert json.decode[StructTypeOptional[string]]('{"val": "true"}')!.val == 'true' - assert json.decode[StructTypeOptional[string]]('{"val": "false"}')!.val == 'false' - assert json.decode[StructTypeOptional[string]]('{"val": true}')!.val == 'true' - assert json.decode[StructTypeOptional[string]]('{"val": false}')!.val == 'false' +fn test_option_types() { + assert json.decode[StructTypeOption[string]]('{}')!.val == none + assert json.decode[StructTypeOption[string]]('{"val": ""}')!.val == '' + assert json.decode[StructTypeOption[string]]('{"val": "0"}')!.val == '0' + assert json.decode[StructTypeOption[string]]('{"val": "1"}')!.val == '1' + assert json.decode[StructTypeOption[string]]('{"val": "2"}')!.val == '2' + assert json.decode[StructTypeOption[string]]('{"val": 0}')!.val == '0' + assert json.decode[StructTypeOption[string]]('{"val": 1}')!.val == '1' + assert json.decode[StructTypeOption[string]]('{"val": 2}')!.val == '2' + assert json.decode[StructTypeOption[string]]('{"val": "true"}')!.val == 'true' + assert json.decode[StructTypeOption[string]]('{"val": "false"}')!.val == 'false' + assert json.decode[StructTypeOption[string]]('{"val": true}')!.val == 'true' + assert json.decode[StructTypeOption[string]]('{"val": false}')!.val == 'false' - assert json.decode[StructTypeOptional[bool]]('{}')!.val == none - assert json.decode[StructTypeOptional[bool]]('{"val": ""}')!.val == false - assert json.decode[StructTypeOptional[bool]]('{"val": "0"}')!.val == false - assert json.decode[StructTypeOptional[bool]]('{"val": "1"}')!.val == true - assert json.decode[StructTypeOptional[bool]]('{"val": "2"}')!.val == true - assert json.decode[StructTypeOptional[bool]]('{"val": 0}')!.val == false - assert json.decode[StructTypeOptional[bool]]('{"val": 1}')!.val == true - assert json.decode[StructTypeOptional[bool]]('{"val": 2}')!.val == true - assert json.decode[StructTypeOptional[bool]]('{"val": "true"}')!.val == true - assert json.decode[StructTypeOptional[bool]]('{"val": "false"}')!.val == false - assert json.decode[StructTypeOptional[bool]]('{"val": true}')!.val == true - assert json.decode[StructTypeOptional[bool]]('{"val": false}')!.val == false + assert json.decode[StructTypeOption[bool]]('{}')!.val == none + assert json.decode[StructTypeOption[bool]]('{"val": ""}')!.val == false + assert json.decode[StructTypeOption[bool]]('{"val": "0"}')!.val == false + assert json.decode[StructTypeOption[bool]]('{"val": "1"}')!.val == true + assert json.decode[StructTypeOption[bool]]('{"val": "2"}')!.val == true + assert json.decode[StructTypeOption[bool]]('{"val": 0}')!.val == false + assert json.decode[StructTypeOption[bool]]('{"val": 1}')!.val == true + assert json.decode[StructTypeOption[bool]]('{"val": 2}')!.val == true + assert json.decode[StructTypeOption[bool]]('{"val": "true"}')!.val == true + assert json.decode[StructTypeOption[bool]]('{"val": "false"}')!.val == false + assert json.decode[StructTypeOption[bool]]('{"val": true}')!.val == true + assert json.decode[StructTypeOption[bool]]('{"val": false}')!.val == false - assert json.decode[StructTypeOptional[int]]('{}')!.val == none - assert json.decode[StructTypeOptional[int]]('{"val": ""}')!.val == 0 - assert json.decode[StructTypeOptional[int]]('{"val": "0"}')!.val == 0 - assert json.decode[StructTypeOptional[int]]('{"val": "1"}')!.val == 1 - assert json.decode[StructTypeOptional[int]]('{"val": "2"}')!.val == 2 - assert json.decode[StructTypeOptional[int]]('{"val": 0}')!.val == 0 - assert json.decode[StructTypeOptional[int]]('{"val": 1}')!.val == 1 - assert json.decode[StructTypeOptional[int]]('{"val": 2}')!.val == 2 - assert json.decode[StructTypeOptional[int]]('{"val": "true"}')!.val == 1 - assert json.decode[StructTypeOptional[int]]('{"val": "false"}')!.val == 0 - assert json.decode[StructTypeOptional[int]]('{"val": true}')!.val == 1 - assert json.decode[StructTypeOptional[int]]('{"val": false}')!.val == 0 + assert json.decode[StructTypeOption[int]]('{}')!.val == none + assert json.decode[StructTypeOption[int]]('{"val": ""}')!.val == 0 + assert json.decode[StructTypeOption[int]]('{"val": "0"}')!.val == 0 + assert json.decode[StructTypeOption[int]]('{"val": "1"}')!.val == 1 + assert json.decode[StructTypeOption[int]]('{"val": "2"}')!.val == 2 + assert json.decode[StructTypeOption[int]]('{"val": 0}')!.val == 0 + assert json.decode[StructTypeOption[int]]('{"val": 1}')!.val == 1 + assert json.decode[StructTypeOption[int]]('{"val": 2}')!.val == 2 + assert json.decode[StructTypeOption[int]]('{"val": "true"}')!.val == 1 + assert json.decode[StructTypeOption[int]]('{"val": "false"}')!.val == 0 + assert json.decode[StructTypeOption[int]]('{"val": true}')!.val == 1 + assert json.decode[StructTypeOption[int]]('{"val": false}')!.val == 0 } fn test_array() { // TODO } -fn test_optional_array() { +fn test_option_array() { // TODO } @@ -52,7 +53,7 @@ fn test_alias() { // TODO } -fn test_optional_alias() { +fn test_option_alias() { // TODO } @@ -60,7 +61,7 @@ fn test_sumtypes() { // TODO } -fn test_optional_sumtypes() { +fn test_option_sumtypes() { // TODO } @@ -74,4 +75,4 @@ fn test_caos() { fn test_caos_array() { // TODO -} \ No newline at end of file +} diff --git a/vlib/x/json2/encode_struct_test.v b/vlib/x/json2/encode_struct_test.v index 8b417d505..d8531d961 100644 --- a/vlib/x/json2/encode_struct_test.v +++ b/vlib/x/json2/encode_struct_test.v @@ -31,7 +31,7 @@ mut: val T } -struct StructTypeOptional[T] { +struct StructTypeOption[T] { mut: val ?T } @@ -64,25 +64,25 @@ fn test_types() { }) == '{"val":{"val":1}}' } -fn test_optional_types() { - assert json.encode(StructTypeOptional[string]{ val: none }) == '{}' - assert json.encode(StructTypeOptional[string]{}) == '{}' - assert json.encode(StructTypeOptional[string]{ val: '' }) == '{"val":""}' - assert json.encode(StructTypeOptional[string]{ val: 'a' }) == '{"val":"a"}' - - assert json.encode(StructTypeOptional[bool]{ val: none }) == '{}' - assert json.encode(StructTypeOptional[bool]{}) == '{}' - assert json.encode(StructTypeOptional[bool]{ val: false }) == '{"val":false}' - assert json.encode(StructTypeOptional[bool]{ val: true }) == '{"val":true}' - - assert json.encode(StructTypeOptional[int]{ val: none }) == '{}' - assert json.encode(StructTypeOptional[int]{}) == '{}' - assert json.encode(StructTypeOptional[int]{ val: 0 }) == '{"val":0}' - assert json.encode(StructTypeOptional[int]{ val: 1 }) == '{"val":1}' - - assert json.encode(StructTypeOptional[time.Time]{}) == '{}' - assert json.encode(StructTypeOptional[time.Time]{ val: time.Time{} }) == '{"val":"0000-00-00T00:00:00.000Z"}' - assert json.encode(StructTypeOptional[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}' +fn test_option_types() { + assert json.encode(StructTypeOption[string]{ val: none }) == '{}' + assert json.encode(StructTypeOption[string]{}) == '{}' + assert json.encode(StructTypeOption[string]{ val: '' }) == '{"val":""}' + assert json.encode(StructTypeOption[string]{ val: 'a' }) == '{"val":"a"}' + + assert json.encode(StructTypeOption[bool]{ val: none }) == '{}' + assert json.encode(StructTypeOption[bool]{}) == '{}' + assert json.encode(StructTypeOption[bool]{ val: false }) == '{"val":false}' + assert json.encode(StructTypeOption[bool]{ val: true }) == '{"val":true}' + + assert json.encode(StructTypeOption[int]{ val: none }) == '{}' + assert json.encode(StructTypeOption[int]{}) == '{}' + assert json.encode(StructTypeOption[int]{ val: 0 }) == '{"val":0}' + assert json.encode(StructTypeOption[int]{ val: 1 }) == '{"val":1}' + + assert json.encode(StructTypeOption[time.Time]{}) == '{}' + assert json.encode(StructTypeOption[time.Time]{ val: time.Time{} }) == '{"val":"0000-00-00T00:00:00.000Z"}' + assert json.encode(StructTypeOption[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}' } fn test_array() { @@ -135,54 +135,54 @@ fn test_array() { assert json.encode(StructType[[]StructType[bool]]{ val: array_of_struct }) == '{"val":[{"val":true},{"val":false}]}' } -fn test_optional_array() { - assert json.encode(StructTypeOptional[[]string]{}) == '{}' - assert json.encode(StructTypeOptional[[]string]{ val: [] }) == '{"val":[]}' - assert json.encode(StructTypeOptional[[]string]{ val: ['0'] }) == '{"val":["0"]}' - assert json.encode(StructTypeOptional[[]string]{ val: ['1'] }) == '{"val":["1"]}' - - assert json.encode(StructTypeOptional[[]int]{}) == '{}' - assert json.encode(StructTypeOptional[[]int]{ val: [] }) == '{"val":[]}' - assert json.encode(StructTypeOptional[[]int]{ val: [0] }) == '{"val":[0]}' - assert json.encode(StructTypeOptional[[]int]{ val: [1] }) == '{"val":[1]}' - assert json.encode(StructTypeOptional[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' - - assert json.encode(StructTypeOptional[[]byte]{}) == '{}' - assert json.encode(StructTypeOptional[[]byte]{ val: [] }) == '{"val":[]}' - assert json.encode(StructTypeOptional[[]byte]{ val: [byte(0)] }) == '{"val":[0]}' - assert json.encode(StructTypeOptional[[]byte]{ val: [byte(1)] }) == '{"val":[1]}' - assert json.encode(StructTypeOptional[[]byte]{ val: [byte(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' - - assert json.encode(StructTypeOptional[[]i64]{}) == '{}' - assert json.encode(StructTypeOptional[[]i64]{ val: [] }) == '{"val":[]}' - assert json.encode(StructTypeOptional[[]i64]{ val: [i64(0)] }) == '{"val":[0]}' - assert json.encode(StructTypeOptional[[]i64]{ val: [i64(1)] }) == '{"val":[1]}' - assert json.encode(StructTypeOptional[[]i64]{ val: [i64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' - - assert json.encode(StructTypeOptional[[]u64]{}) == '{}' - assert json.encode(StructTypeOptional[[]u64]{ val: [] }) == '{"val":[]}' - assert json.encode(StructTypeOptional[[]u64]{ val: [u64(0)] }) == '{"val":[0]}' - assert json.encode(StructTypeOptional[[]u64]{ val: [u64(1)] }) == '{"val":[1]}' - assert json.encode(StructTypeOptional[[]u64]{ val: [u64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' - - assert json.encode(StructTypeOptional[[]f64]{}) == '{}' - assert json.encode(StructTypeOptional[[]f64]{ val: [] }) == '{"val":[]}' - assert json.encode(StructTypeOptional[[]f64]{ val: [f64(0)] }) == '{"val":[0.0]}' - assert json.encode(StructTypeOptional[[]f64]{ val: [f64(1)] }) == '{"val":[1.0]}' - assert json.encode(StructTypeOptional[[]f64]{ val: [f64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0.0,1.0,0.0,2.0,3.0,2.0,5.0,1.0]}' - - assert json.encode(StructTypeOptional[[]bool]{}) == '{}' - assert json.encode(StructTypeOptional[[]bool]{ val: [] }) == '{"val":[]}' - assert json.encode(StructTypeOptional[[]bool]{ val: [true] }) == '{"val":[true]}' - assert json.encode(StructTypeOptional[[]bool]{ val: [false] }) == '{"val":[false]}' - assert json.encode(StructTypeOptional[[]bool]{ val: [false, true, false] }) == '{"val":[false,true,false]}' +fn test_option_array() { + assert json.encode(StructTypeOption[[]string]{}) == '{}' + assert json.encode(StructTypeOption[[]string]{ val: [] }) == '{"val":[]}' + assert json.encode(StructTypeOption[[]string]{ val: ['0'] }) == '{"val":["0"]}' + assert json.encode(StructTypeOption[[]string]{ val: ['1'] }) == '{"val":["1"]}' + + assert json.encode(StructTypeOption[[]int]{}) == '{}' + assert json.encode(StructTypeOption[[]int]{ val: [] }) == '{"val":[]}' + assert json.encode(StructTypeOption[[]int]{ val: [0] }) == '{"val":[0]}' + assert json.encode(StructTypeOption[[]int]{ val: [1] }) == '{"val":[1]}' + assert json.encode(StructTypeOption[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' + + assert json.encode(StructTypeOption[[]byte]{}) == '{}' + assert json.encode(StructTypeOption[[]byte]{ val: [] }) == '{"val":[]}' + assert json.encode(StructTypeOption[[]byte]{ val: [byte(0)] }) == '{"val":[0]}' + assert json.encode(StructTypeOption[[]byte]{ val: [byte(1)] }) == '{"val":[1]}' + assert json.encode(StructTypeOption[[]byte]{ val: [byte(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' + + assert json.encode(StructTypeOption[[]i64]{}) == '{}' + assert json.encode(StructTypeOption[[]i64]{ val: [] }) == '{"val":[]}' + assert json.encode(StructTypeOption[[]i64]{ val: [i64(0)] }) == '{"val":[0]}' + assert json.encode(StructTypeOption[[]i64]{ val: [i64(1)] }) == '{"val":[1]}' + assert json.encode(StructTypeOption[[]i64]{ val: [i64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' + + assert json.encode(StructTypeOption[[]u64]{}) == '{}' + assert json.encode(StructTypeOption[[]u64]{ val: [] }) == '{"val":[]}' + assert json.encode(StructTypeOption[[]u64]{ val: [u64(0)] }) == '{"val":[0]}' + assert json.encode(StructTypeOption[[]u64]{ val: [u64(1)] }) == '{"val":[1]}' + assert json.encode(StructTypeOption[[]u64]{ val: [u64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}' + + assert json.encode(StructTypeOption[[]f64]{}) == '{}' + assert json.encode(StructTypeOption[[]f64]{ val: [] }) == '{"val":[]}' + assert json.encode(StructTypeOption[[]f64]{ val: [f64(0)] }) == '{"val":[0.0]}' + assert json.encode(StructTypeOption[[]f64]{ val: [f64(1)] }) == '{"val":[1.0]}' + assert json.encode(StructTypeOption[[]f64]{ val: [f64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0.0,1.0,0.0,2.0,3.0,2.0,5.0,1.0]}' + + assert json.encode(StructTypeOption[[]bool]{}) == '{}' + assert json.encode(StructTypeOption[[]bool]{ val: [] }) == '{"val":[]}' + assert json.encode(StructTypeOption[[]bool]{ val: [true] }) == '{"val":[true]}' + assert json.encode(StructTypeOption[[]bool]{ val: [false] }) == '{"val":[false]}' + assert json.encode(StructTypeOption[[]bool]{ val: [false, true, false] }) == '{"val":[false,true,false]}' array_of_struct := [StructType[bool]{ val: true }, StructType[bool]{ val: false }] - assert json.encode(StructTypeOptional[[]StructType[bool]]{ val: array_of_struct }) == '{"val":[{"val":true},{"val":false}]}' + assert json.encode(StructTypeOption[[]StructType[bool]]{ val: array_of_struct }) == '{"val":[{"val":true},{"val":false}]}' } fn test_alias() { diff --git a/vlib/x/json2/encode_struct_todo_test.vv b/vlib/x/json2/encode_struct_todo_test.vv index 158c57894..9b97eab77 100644 --- a/vlib/x/json2/encode_struct_todo_test.vv +++ b/vlib/x/json2/encode_struct_todo_test.vv @@ -11,7 +11,7 @@ mut: val T } -struct StructTypeOptional[T] { +struct StructTypeOption[T] { mut: val ?T } @@ -21,16 +21,16 @@ mut: val &T } -fn test_optional_alias() { - assert json.encode(StructTypeOptional[BoolAlias]{ val: none }) == '{}' - assert json.encode(StructTypeOptional[BoolAlias]{}) == '{"val":false}' - assert json.encode(StructTypeOptional[BoolAlias]{ val: false }) == '{"val":false}' - assert json.encode(StructTypeOptional[BoolAlias]{ val: true }) == '{"val":true}' +fn test_option_alias() { + assert json.encode(StructTypeOption[BoolAlias]{ val: none }) == '{}' + assert json.encode(StructTypeOption[BoolAlias]{}) == '{"val":false}' + assert json.encode(StructTypeOption[BoolAlias]{ val: false }) == '{"val":false}' + assert json.encode(StructTypeOption[BoolAlias]{ val: true }) == '{"val":true}' - assert json.encode(StructTypeOptional[IntAlias]{ val: none }) == '{}' - assert json.encode(StructTypeOptional[IntAlias]{}) == '{"val":0}' - assert json.encode(StructTypeOptional[IntAlias]{ val: 0 }) == '{"val":0}' - assert json.encode(StructTypeOptional[IntAlias]{ val: 1 }) == '{"val":1}' + assert json.encode(StructTypeOption[IntAlias]{ val: none }) == '{}' + assert json.encode(StructTypeOption[IntAlias]{}) == '{"val":0}' + assert json.encode(StructTypeOption[IntAlias]{ val: 0 }) == '{"val":0}' + assert json.encode(StructTypeOption[IntAlias]{ val: 1 }) == '{"val":1}' } fn test_sumtypes() { @@ -46,17 +46,17 @@ fn test_sumtypes() { assert json.encode(StructType[SumTypes]{ val: 1 }) == '{"val":1}' } -fn test_optional_sumtypes() { - assert json.encode(StructTypeOptional[SumTypes]{}) == '{}' // REVIEW +fn test_option_sumtypes() { + assert json.encode(StructTypeOption[SumTypes]{}) == '{}' // REVIEW - assert json.encode(StructTypeOptional[SumTypes]{ val: '' }) == '{"val":""}' - assert json.encode(StructTypeOptional[SumTypes]{ val: 'a' }) == '{"val":"a"}' + assert json.encode(StructTypeOption[SumTypes]{ val: '' }) == '{"val":""}' + assert json.encode(StructTypeOption[SumTypes]{ val: 'a' }) == '{"val":"a"}' - assert json.encode(StructTypeOptional[SumTypes]{ val: false }) == '{"val":false}' - assert json.encode(StructTypeOptional[SumTypes]{ val: true }) == '{"val":true}' + assert json.encode(StructTypeOption[SumTypes]{ val: false }) == '{"val":false}' + assert json.encode(StructTypeOption[SumTypes]{ val: true }) == '{"val":true}' - assert json.encode(StructTypeOptional[SumTypes]{ val: 0 }) == '{"val":0}' - assert json.encode(StructTypeOptional[SumTypes]{ val: 1 }) == '{"val":1}' + assert json.encode(StructTypeOption[SumTypes]{ val: 0 }) == '{"val":0}' + assert json.encode(StructTypeOption[SumTypes]{ val: 1 }) == '{"val":1}' } fn test_pointer() { @@ -90,10 +90,10 @@ fn test_caos() { assert json.encode(StructType[StructType[StringAlias]]{ val: typed_string_struct }) == 'TODO' assert json.encode(StructType[StructType[SumTypes]]{ val: typed_string_struct }) == 'TODO' - assert json.encode(StructType[StructTypeOptional]{typed_string_struct}) == 'TODO' - assert json.encode(StructType[StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO' - assert json.encode(StructType[StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructType[StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructType[StructTypeOption]{typed_string_struct}) == 'TODO' + assert json.encode(StructType[StructTypeOption[string]]{ val: StructType{'a'} }) == 'TODO' + assert json.encode(StructType[StructTypeOption[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructType[StructTypeOption[SumTypes]]{typed_string_struct}) == 'TODO' assert json.encode(StructType[StructTypePointer]{typed_string_struct}) == 'TODO' assert json.encode(StructType[StructTypePointer[string]]{typed_string_struct}) == 'TODO' @@ -101,20 +101,20 @@ fn test_caos() { assert json.encode(StructType[StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO' // StructTypeOptional - assert json.encode(StructTypeOptional[StructType]{}) == 'TODO' - assert json.encode(StructTypeOptional[StructType[string]]{ val: '' }) == 'TODO' - assert json.encode(StructTypeOptional[StructType[StringAlias]]{ val: '' }) == 'TODO' - assert json.encode(StructTypeOptional[StructType[SumTypes]]{ val: '' }) == 'TODO' + assert json.encode(StructTypeOption[StructType]{}) == 'TODO' + assert json.encode(StructTypeOption[StructType[string]]{ val: '' }) == 'TODO' + assert json.encode(StructTypeOption[StructType[StringAlias]]{ val: '' }) == 'TODO' + assert json.encode(StructTypeOption[StructType[SumTypes]]{ val: '' }) == 'TODO' - assert json.encode(StructTypeOptional[StructTypeOptional]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO' - assert json.encode(StructTypeOptional[StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[StructTypeOption]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[StructTypeOption[string]]{ val: StructType{'a'} }) == 'TODO' + assert json.encode(StructTypeOption[StructTypeOption[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[StructTypeOption[SumTypes]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[StructTypePointer]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[StructTypePointer[string]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[StructTypePointer]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[StructTypePointer[string]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO' // StructTypePointer assert json.encode(StructTypePointer[StructType]{}) == 'TODO' @@ -122,10 +122,10 @@ fn test_caos() { assert json.encode(StructTypePointer[StructType[StringAlias]]{ val: '' }) == 'TODO' assert json.encode(StructTypePointer[StructType[SumTypes]]{ val: '' }) == 'TODO' - assert json.encode(StructTypePointer[StructTypeOptional]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypePointer[StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO' - assert json.encode(StructTypePointer[StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypePointer[StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypePointer[StructTypeOption]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypePointer[StructTypeOption[string]]{ val: StructType{'a'} }) == 'TODO' + assert json.encode(StructTypePointer[StructTypeOption[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypePointer[StructTypeOption[SumTypes]]{typed_string_struct}) == 'TODO' assert json.encode(StructTypePointer[StructTypePointer]{typed_string_struct}) == 'TODO' assert json.encode(StructTypePointer[StructTypePointer[string]]{typed_string_struct}) == 'TODO' @@ -144,10 +144,10 @@ fn test_caos_array() { assert json.encode(StructType[[]StructType[StringAlias]]{ val: typed_string_struct }) == 'TODO' assert json.encode(StructType[[]StructType[SumTypes]]{ val: typed_string_struct }) == 'TODO' - assert json.encode(StructType[[]StructTypeOptional]{typed_string_struct}) == 'TODO' - assert json.encode(StructType[[]StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO' - assert json.encode(StructType[[]StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructType[[]StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructType[[]StructTypeOption]{typed_string_struct}) == 'TODO' + assert json.encode(StructType[[]StructTypeOption[string]]{ val: StructType{'a'} }) == 'TODO' + assert json.encode(StructType[[]StructTypeOption[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructType[[]StructTypeOption[SumTypes]]{typed_string_struct}) == 'TODO' assert json.encode(StructType[[]StructTypePointer]{typed_string_struct}) == 'TODO' assert json.encode(StructType[[]StructTypePointer[string]]{typed_string_struct}) == 'TODO' @@ -155,20 +155,20 @@ fn test_caos_array() { assert json.encode(StructType[[]StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO' // StructTypeOptional - assert json.encode(StructTypeOptional[[]StructType]{}) == 'TODO' - assert json.encode(StructTypeOptional[[]StructType[string]]{ val: '' }) == 'TODO' - assert json.encode(StructTypeOptional[[]StructType[StringAlias]]{ val: '' }) == 'TODO' - assert json.encode(StructTypeOptional[[]StructType[SumTypes]]{ val: '' }) == 'TODO' + assert json.encode(StructTypeOption[[]StructType]{}) == 'TODO' + assert json.encode(StructTypeOption[[]StructType[string]]{ val: '' }) == 'TODO' + assert json.encode(StructTypeOption[[]StructType[StringAlias]]{ val: '' }) == 'TODO' + assert json.encode(StructTypeOption[[]StructType[SumTypes]]{ val: '' }) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypeOptional]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypeOption]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypeOption[string]]{ val: StructType{'a'} }) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypeOption[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypeOption[SumTypes]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypePointer]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypePointer[string]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypeOptional[[]StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypePointer]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypePointer[string]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypePointer[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypeOption[[]StructTypePointer[SumTypes]]{typed_string_struct}) == 'TODO' // StructTypePointer assert json.encode(StructTypePointer[[]StructType]{}) == 'TODO' @@ -176,10 +176,10 @@ fn test_caos_array() { assert json.encode(StructTypePointer[[]StructType[StringAlias]]{ val: '' }) == 'TODO' assert json.encode(StructTypePointer[[]StructType[SumTypes]]{ val: '' }) == 'TODO' - assert json.encode(StructTypePointer[[]StructTypeOptional]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypePointer[[]StructTypeOptional[string]]{ val: StructType{'a'} }) == 'TODO' - assert json.encode(StructTypePointer[[]StructTypeOptional[StringAlias]]{typed_string_struct}) == 'TODO' - assert json.encode(StructTypePointer[[]StructTypeOptional[SumTypes]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypePointer[[]StructTypeOption]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypePointer[[]StructTypeOption[string]]{ val: StructType{'a'} }) == 'TODO' + assert json.encode(StructTypePointer[[]StructTypeOption[StringAlias]]{typed_string_struct}) == 'TODO' + assert json.encode(StructTypePointer[[]StructTypeOption[SumTypes]]{typed_string_struct}) == 'TODO' assert json.encode(StructTypePointer[[]StructTypePointer]{typed_string_struct}) == 'TODO' assert json.encode(StructTypePointer[[]StructTypePointer[string]]{typed_string_struct}) == 'TODO' diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index 06c06c510..ca67f439e 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -165,7 +165,7 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! { } } - $if field.is_optional { + $if field.is_option { is_none := value.str() == 'Option(error: none)' if !is_none { @@ -182,29 +182,29 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! { } $if field.typ is ?string { - optional_value := val.$(field.name) as ?string - e.encode_string(optional_value, mut wr)! + option_value := val.$(field.name) as ?string + e.encode_string(option_value, mut wr)! } $else $if field.typ is ?bool { - optional_value := val.$(field.name) as ?bool - wr.write(Any(optional_value).str().bytes())! + option_value := val.$(field.name) as ?bool + wr.write(Any(option_value).str().bytes())! } $else $if field.typ is ?f32 { - optional_value := val.$(field.name) as ?f32 - wr.write(Any(optional_value).str().bytes())! + option_value := val.$(field.name) as ?f32 + wr.write(Any(option_value).str().bytes())! } $else $if field.typ is ?f64 { - optional_value := val.$(field.name) as ?f64 - wr.write(Any(optional_value).str().bytes())! + option_value := val.$(field.name) as ?f64 + wr.write(Any(option_value).str().bytes())! } $else $if field.typ is ?i8 { - optional_value := val.$(field.name) as ?i8 - wr.write(Any(optional_value).str().bytes())! + option_value := val.$(field.name) as ?i8 + wr.write(Any(option_value).str().bytes())! } $else $if field.typ is ?i16 { - optional_value := val.$(field.name) as ?i16 - wr.write(Any(optional_value).str().bytes())! + option_value := val.$(field.name) as ?i16 + wr.write(Any(option_value).str().bytes())! } $else $if field.typ is ?int { - optional_value := val.$(field.name) as ?int - wr.write(Any(optional_value).int().str().bytes())! + option_value := val.$(field.name) as ?int + wr.write(Any(option_value).int().str().bytes())! } $else $if field.typ is ?time.Time { - optional_value := val.$(field.name) as ?time.Time - parsed_time := optional_value as time.Time + option_value := val.$(field.name) as ?time.Time + parsed_time := option_value as time.Time e.encode_string(parsed_time.format_rfc3339(), mut wr)! } $else $if field.is_array { e.encode_array(value, level + 1, mut wr)! diff --git a/vlib/x/json2/json_module_compatibility_test/json_decode_with_optional_arg_test.v b/vlib/x/json2/json_module_compatibility_test/json_decode_with_option_arg_test.v similarity index 86% rename from vlib/x/json2/json_module_compatibility_test/json_decode_with_optional_arg_test.v rename to vlib/x/json2/json_module_compatibility_test/json_decode_with_option_arg_test.v index 1c8008e06..be1755aa2 100644 --- a/vlib/x/json2/json_module_compatibility_test/json_decode_with_optional_arg_test.v +++ b/vlib/x/json2/json_module_compatibility_test/json_decode_with_option_arg_test.v @@ -3,7 +3,7 @@ import os struct DbConfig {} -fn test_json_decode_with_optional_arg() { +fn test_json_decode_with_option_arg() { if ret := print_info() { println(ret) } else { -- 2.30.2