From 2dcf32f5c8d575e7583cb83d47f60daa352bea70 Mon Sep 17 00:00:00 2001
From: Andreas Rumpf <rumpf_a@web.de>
Date: Wed, 18 Mar 2020 10:37:36 +0100
Subject: [PATCH] fixes #13671 [backport] (#13678)

(cherry picked from commit 5f6997794e1482187decbd074ea274fe32a223d8)
---
 compiler/semexprs.nim        | 2 +-
 tests/misc/tunsignedconv.nim | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 38c440f106101..5ea316da85f74 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -508,7 +508,7 @@ proc changeType(c: PContext; n: PNode, newType: PType, check: bool) =
           addSon(a, m)
           changeType(m, tup.sons[i], check)
   of nkCharLit..nkUInt64Lit:
-    if check and n.kind != nkUInt64Lit:
+    if check and n.kind != nkUInt64Lit and not sameType(n.typ, newType):
       let value = n.intVal
       if value < firstOrd(c.config, newType) or value > lastOrd(c.config, newType):
         localError(c.config, n.info, "cannot convert " & $value &
diff --git a/tests/misc/tunsignedconv.nim b/tests/misc/tunsignedconv.nim
index af334dd1905ac..085d13aca2634 100644
--- a/tests/misc/tunsignedconv.nim
+++ b/tests/misc/tunsignedconv.nim
@@ -55,3 +55,8 @@ const x0 = fun()
 echo typeof(x0)
 
 discard $x0
+
+# bug #13671
+
+const x1 = cast[uint](-1)
+discard $(x1,)