From 3237e0c780ee3d064f12d5e26f70434f847c37c5 Mon Sep 17 00:00:00 2001 From: hlaaftana Date: Thu, 16 Aug 2018 20:22:48 +0300 Subject: [PATCH 1/2] Fix #8610 and #8654 --- compiler/semstmts.nim | 3 +++ compiler/types.nim | 4 ++-- tests/errmsgs/t8610.nim | 8 ++++++++ tests/errmsgs/t8654_1.nim | 8 ++++++++ tests/errmsgs/t8654_2.nim | 7 +++++++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/errmsgs/t8610.nim create mode 100644 tests/errmsgs/t8654_1.nim create mode 100644 tests/errmsgs/t8654_2.nim diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 170ac799ef16..dfc68337253f 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -409,6 +409,9 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode = var typ: PType if a.sons[length-2].kind != nkEmpty: typ = semTypeNode(c, a.sons[length-2], nil) + if typ.kind == tyTypeDesc and c.p.owner.kind != skMacro: + localError(c.config, a.sons[length-2].info, "'typedesc' metatype is not valid here") + continue else: typ = nil var def: PNode = c.graph.emptyNode diff --git a/compiler/types.nim b/compiler/types.nim index 80624502c5a4..c4631c6b67f3 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1183,8 +1183,8 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind, if result.isNil and t.sons[0] != nil: result = typeAllowedAux(marker, t.sons[0], skResult, flags) of tyTypeDesc: - # XXX: This is still a horrible idea... - result = nil + # Could be inside a macro, so var and let are allowed here and checked later + if kind notin {skParam, skResult, skVar, skLet}: result = t of tyExpr, tyStmt, tyStatic: if kind notin {skParam, skResult}: result = t of tyVoid: diff --git a/tests/errmsgs/t8610.nim b/tests/errmsgs/t8610.nim new file mode 100644 index 000000000000..1200e59a6427 --- /dev/null +++ b/tests/errmsgs/t8610.nim @@ -0,0 +1,8 @@ +discard """ + errormsg: "invalid type for const: type int" + line: 6 +""" + +const Foo=int +echo Foo is int # true +echo int is Foo # Error: type expected diff --git a/tests/errmsgs/t8654_1.nim b/tests/errmsgs/t8654_1.nim new file mode 100644 index 000000000000..ed7d7b21fdeb --- /dev/null +++ b/tests/errmsgs/t8654_1.nim @@ -0,0 +1,8 @@ +discard """ + errormsg: "'typedesc' metatype is not valid here" + line: 6 +""" + +var a: typedesc +a = typedesc[int] +echo a is typedesc[int] diff --git a/tests/errmsgs/t8654_2.nim b/tests/errmsgs/t8654_2.nim new file mode 100644 index 000000000000..82642e95b931 --- /dev/null +++ b/tests/errmsgs/t8654_2.nim @@ -0,0 +1,7 @@ +discard """ + errormsg: "invalid type for const: typedesc" + line: 6 +""" + +const a: typedesc = typedesc[int] +echo a is typedesc[int] From d97905fdfe2e57d2ee7d7554088548ab903b7a69 Mon Sep 17 00:00:00 2001 From: hlaaftana Date: Thu, 16 Aug 2018 22:13:10 +0300 Subject: [PATCH 2/2] Improved const typedesc error message --- compiler/semstmts.nim | 3 +++ tests/errmsgs/t8610.nim | 2 +- tests/errmsgs/t8654_2.nim | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index dfc68337253f..895a151b31c4 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -542,6 +542,9 @@ proc semConst(c: PContext, n: PNode): PNode = if typ == nil: localError(c.config, a.sons[2].info, errConstExprExpected) continue + if typ.kind == tyTypeDesc: + localError(c.config, a.info, "cannot have typedesc as const value, " & + "use 'type $1 = $2' instead", [$a.sons[0], typeToString(def.typ.skipTypes({tyTypeDesc}))]) if typeAllowed(typ, skConst) != nil and def.kind != nkNilLit: localError(c.config, a.info, "invalid type for const: " & typeToString(typ)) continue diff --git a/tests/errmsgs/t8610.nim b/tests/errmsgs/t8610.nim index 1200e59a6427..b6ed1637d176 100644 --- a/tests/errmsgs/t8610.nim +++ b/tests/errmsgs/t8610.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "invalid type for const: type int" + errormsg: "cannot have typedesc as const value, use 'type Foo = int' instead" line: 6 """ diff --git a/tests/errmsgs/t8654_2.nim b/tests/errmsgs/t8654_2.nim index 82642e95b931..7f80d96afc9e 100644 --- a/tests/errmsgs/t8654_2.nim +++ b/tests/errmsgs/t8654_2.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "invalid type for const: typedesc" + errormsg: "cannot have typedesc as const value, use 'type a = int' instead" line: 6 """