From b1996aee5c3e9fab7c47d3e1a6395baac7492e79 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 6 Dec 2022 20:09:50 +0800 Subject: [PATCH] fixes #21027; cast expressions need a type (#21029) * fixes #21027; cast expressions need a type * Apply suggestions from code review Thanks to @beef331 (cherry picked from commit 1564ae650f8d4d4c30adf4528f74d7707e4cb737) --- compiler/semexprs.nim | 2 ++ tests/types/t21027.nim | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 tests/types/t21027.nim diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 2c1d40ff2523f..de2259eb4f786 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -361,6 +361,8 @@ proc semCast(c: PContext, n: PNode): PNode = let castedExpr = semExprWithType(c, n[1]) if castedExpr.kind == nkClosedSymChoice: errorUseQualifier(c, n[1].info, castedExpr) + if targetType == nil: + localError(c.config, n.info, "Invalid usage of cast, cast requires a type to convert to, e.g., cast[int](0d).") if tfHasMeta in targetType.flags: localError(c.config, n[0].info, "cannot cast to a non concrete type: '$1'" % $targetType) if not isCastable(c, targetType, castedExpr.typ, n.info): diff --git a/tests/types/t21027.nim b/tests/types/t21027.nim new file mode 100644 index 0000000000000..3a992177a43a1 --- /dev/null +++ b/tests/types/t21027.nim @@ -0,0 +1,5 @@ +discard """ + errormsg: "Invalid usage of cast, cast requires a type to convert to, e.g., cast[int](0d)." +""" +# bug #21027 +let x: uint64 = cast(5)