diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index e272cc36985953..f32f7c9042af66 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -72,6 +72,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { p.check(.dollar) } } + if_pos := p.tok.pos() // `if` or `else if` p.check(.key_if) if p.tok.kind == .key_match { @@ -139,6 +140,17 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { p.comptime_if_cond = true p.inside_if_cond = true cond = p.expr(0) + if mut cond is ast.InfixExpr && !is_comptime { + if cond.op in [.key_is, .not_is] { + if mut cond.left is ast.Ident { + if cond.left.name.len == 1 && cond.left.name.is_capital() + && cond.right is ast.TypeNode { + p.error_with_pos('use `\$if` instead of `if`', if_pos) + return ast.IfExpr{} + } + } + } + } p.inside_if_cond = false if p.if_cond_comments.len > 0 { comments << p.if_cond_comments diff --git a/vlib/v/parser/tests/not_using_if_comptime_in_comptime_err.out b/vlib/v/parser/tests/not_using_if_comptime_in_comptime_err.out new file mode 100644 index 00000000000000..d239c9ccc052a3 --- /dev/null +++ b/vlib/v/parser/tests/not_using_if_comptime_in_comptime_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/not_using_if_comptime_in_comptime_err.vv:10:2: error: use `$if` instead of `if` + 8 | + 9 | fn check[T]() bool { + 10 | if T is Checkable { + | ~~ + 11 | return true + 12 | } else { diff --git a/vlib/v/parser/tests/not_using_if_comptime_in_comptime_err.vv b/vlib/v/parser/tests/not_using_if_comptime_in_comptime_err.vv new file mode 100644 index 00000000000000..19f982cea77de6 --- /dev/null +++ b/vlib/v/parser/tests/not_using_if_comptime_in_comptime_err.vv @@ -0,0 +1,17 @@ +interface Checkable { + test int +} + +struct Test { + test int +} + +fn check[T]() bool { + if T is Checkable { + return true + } else { + return false + } +} + +println(check[Test]())