diff --git a/cmd/tools/vrepl.v b/cmd/tools/vrepl.v index 4c5eb36239..ada995bda0 100644 --- a/cmd/tools/vrepl.v +++ b/cmd/tools/vrepl.v @@ -441,7 +441,7 @@ fn run_repl(workdir string, vrepl_prefix string) int { prompt = '... ' } oline := r.get_one_line(prompt) or { break } - line := oline.trim_space() + line := oline.all_before('//').trim_space() if line == '' { continue } diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 73a8a93371..3a8347f815 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -124,9 +124,10 @@ pub: typ int // the internal TypeID of the field f, unaliased_typ int // if f's type was an alias of int, this will be TypeID(int) - attrs []string // the attributes of the field f - is_pub bool // f is in a `pub:` section - is_mut bool // f is in a `mut:` section + attrs []string // the attributes of the field f + is_pub bool // f is in a `pub:` section + is_mut bool // f is in a `mut:` section + is_embed bool // f is a embedded struct is_shared bool // `f shared Abc` is_atomic bool // `f atomic int` , TODO diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 6d855fbe0c..e8d99be0df 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -1398,7 +1398,7 @@ fn (mut c Checker) comptime_if_cond(mut cond ast.Expr, mut sb strings.Builder) ( } ast.SelectorExpr { if c.comptime.comptime_for_field_var != '' && cond.expr is ast.Ident { - if (cond.expr as ast.Ident).name == c.comptime.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', 'is_enum'] { + if (cond.expr as ast.Ident).name == c.comptime.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_embed', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias', 'is_enum'] { is_true = c.type_resolver.get_comptime_selector_bool_field(cond.field_name) sb.write_string('${is_true}') return is_true, true diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 6d3530d00d..3fb651d6d9 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -221,10 +221,13 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { } } } - if node.is_expr { - c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type) - } else { - c.stmts(mut branch.stmts) + + if !node.is_comptime || (node.is_comptime && comptime_match_branch_result) { + if node.is_expr { + c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type) + } else { + c.stmts(mut branch.stmts) + } } c.smartcast_mut_pos = token.Pos{} c.smartcast_cond_pos = token.Pos{} diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 88571cd09a..c44b33b04d 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -740,6 +740,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { g.writeln('\t${node.val_var}.unaliased_typ = ${int(unaliased_styp.idx())};\t// ${g.table.type_to_str(unaliased_styp)}') g.writeln('\t${node.val_var}.is_pub = ${field.is_pub};') g.writeln('\t${node.val_var}.is_mut = ${field.is_mut};') + g.writeln('\t${node.val_var}.is_embed = ${field.is_embed};') 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)};') @@ -780,7 +781,12 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { if g.pref.translated && node.typ.is_number() { g.writeln('_const_main__${val};') } else { - g.writeln('${g.styp(node.typ)}__${val};') + node_sym := g.table.sym(node.typ) + if node_sym.info is ast.Alias { + g.writeln('${g.styp(node_sym.info.parent_type)}__${val};') + } else { + g.writeln('${g.styp(node.typ)}__${val};') + } } enum_attrs := sym.info.attrs[val] if enum_attrs.len == 0 { diff --git a/vlib/v/slow_tests/repl/line_comment.repl b/vlib/v/slow_tests/repl/line_comment.repl new file mode 100644 index 0000000000..9218e2c41a --- /dev/null +++ b/vlib/v/slow_tests/repl/line_comment.repl @@ -0,0 +1,5 @@ +math.pi +math.pi // some comment +===output=== +3.141592653589793 +3.141592653589793 diff --git a/vlib/v/tests/comptime/comptime_enum_values_test.v b/vlib/v/tests/comptime/comptime_enum_values_test.v index 912fee9e73..a652798bd3 100644 --- a/vlib/v/tests/comptime/comptime_enum_values_test.v +++ b/vlib/v/tests/comptime/comptime_enum_values_test.v @@ -5,6 +5,8 @@ enum CharacterGroup { special } +type AnotherCharGroup = CharacterGroup + fn (self CharacterGroup) value() string { return match self { .chars { 'first' } @@ -33,3 +35,21 @@ fn test_main() { assert values == [CharacterGroup.chars, CharacterGroup.alphanumerics, CharacterGroup.numeric, CharacterGroup.special] } + +fn test_alias_enum() { + mut values := []EnumData{} + $for entry in AnotherCharGroup.values { + values << entry + } + assert values[0].value == int(CharacterGroup.chars) + assert values[0].name == CharacterGroup.chars.str() + + assert values[1].value == int(CharacterGroup.alphanumerics) + assert values[1].name == CharacterGroup.alphanumerics.str() + + assert values[2].value == int(CharacterGroup.numeric) + assert values[2].name == CharacterGroup.numeric.str() + + assert values[3].value == int(CharacterGroup.special) + assert values[3].name == CharacterGroup.special.str() +} diff --git a/vlib/v/tests/comptime/comptime_for_test.v b/vlib/v/tests/comptime/comptime_for_test.v index 7a02f16b9c..ee2482ec14 100644 --- a/vlib/v/tests/comptime/comptime_for_test.v +++ b/vlib/v/tests/comptime/comptime_for_test.v @@ -1,4 +1,5 @@ struct App { + Inner a string b string mut: @@ -12,6 +13,8 @@ pub mut: h u8 } +struct Inner {} + @['foo/bar/three'] fn (mut app App) run() { } @@ -85,13 +88,16 @@ fn test_comptime_for_fields() { assert field.name in ['d', 'e'] } if field.is_mut { - assert field.name in ['c', 'd', 'g', 'h'] + assert field.name in ['c', 'd', 'g', 'h', 'Inner'] } if field.is_pub { - assert field.name in ['e', 'f', 'g', 'h'] + assert field.name in ['e', 'f', 'g', 'h', 'Inner'] } if field.is_pub && field.is_mut { - assert field.name in ['g', 'h'] + assert field.name in ['g', 'h', 'Inner'] + } + if field.is_embed { + assert field.name == 'Inner' } if field.name == 'f' { assert sizeof(field) == 8 diff --git a/vlib/v/tests/comptime/comptime_match_eval_only_true_branch_test.v b/vlib/v/tests/comptime/comptime_match_eval_only_true_branch_test.v new file mode 100644 index 0000000000..74ca289ed2 --- /dev/null +++ b/vlib/v/tests/comptime/comptime_match_eval_only_true_branch_test.v @@ -0,0 +1,17 @@ +module main + +fn func[T]() bool { + $match T { + u8, u16 { + return true + } + $else { + // return false + $compile_error('fail') + } + } +} + +fn test_comptime_match_eval_only_true_branch() { + assert func[u8]() +} diff --git a/vlib/v/type_resolver/comptime_resolver.v b/vlib/v/type_resolver/comptime_resolver.v index 79f296e19d..c33cff5365 100644 --- a/vlib/v/type_resolver/comptime_resolver.v +++ b/vlib/v/type_resolver/comptime_resolver.v @@ -224,6 +224,7 @@ pub fn (mut t TypeResolver) get_comptime_selector_bool_field(field_name string) match field_name { 'is_pub' { return field.is_pub } 'is_mut' { return field.is_mut } + 'is_embed' { return field.is_embed } 'is_shared' { return field_typ.has_flag(.shared_f) } 'is_atomic' { return field_typ.has_flag(.atomic_f) } 'is_option' { return field.typ.has_flag(.option) }