From eae0dae38c090b1d861bda7ed0178442fc4f6085 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 31 Jan 2019 16:21:52 +0100 Subject: [PATCH 1/3] Fixes #10514 (cherry picked from commit f6f789bb4db2a367384ba6ad75706edd503de1f8) --- compiler/semstmts.nim | 13 ++++++------- tests/vm/teval1.nim | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 512dbca680811..679a5365e0e04 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -595,11 +595,7 @@ proc semConst(c: PContext, n: PNode): PNode = if a.sons[length-2].kind != nkEmpty: typ = semTypeNode(c, a.sons[length-2], nil) - var def = semConstExpr(c, a.sons[length-1]) - if def == nil: - localError(c.config, a.sons[length-1].info, errConstExprExpected) - continue - + var def = semExprWithType(c, a[^1]) if def.typ.kind == tyProc and def.kind == nkSym: if def.sym.kind == skMacro: localError(c.config, def.info, errCannotAssignMacroSymbol % "constant") @@ -621,7 +617,10 @@ proc semConst(c: PContext, n: PNode): PNode = def = fitRemoveHiddenConv(c, typ, def) else: typ = def.typ - if typ == nil: + + # evaluate the node + def = semConstExpr(c, def) + if def == nil: localError(c.config, a.sons[length-1].info, errConstExprExpected) continue if typeAllowed(typ, skConst) != nil and def.kind != nkNilLit: @@ -639,7 +638,7 @@ proc semConst(c: PContext, n: PNode): PNode = b.sons[length-2] = a.sons[length-2] b.sons[length-1] = def - for j in 0 .. length-3: + for j in 0 ..< length-2: var v = semIdentDef(c, a.sons[j], skConst) if sfGenSym notin v.flags: addInterfaceDecl(c, v) elif v.owner == nil: v.owner = getCurrOwner(c) diff --git a/tests/vm/teval1.nim b/tests/vm/teval1.nim index 5c323f0e70d35..0316ea2387990 100644 --- a/tests/vm/teval1.nim +++ b/tests/vm/teval1.nim @@ -25,3 +25,18 @@ doAssert x == "" static: var i, j: set[int8] = {} var k = i + j + +type + Obj = object + x: int + +converter toObj(x: int): Obj = Obj(x: x) + +# bug #10514 +block: + const + b: Obj = 42 + bar = [b] + + let i_runtime = 0 + doAssert bar[i_runtime] == b From d63de7ee8c730827d7cc27c59ef0041c4c5203d8 Mon Sep 17 00:00:00 2001 From: Clyybber Date: Thu, 26 Sep 2019 01:19:56 +0200 Subject: [PATCH 2/3] Add comment --- compiler/semstmts.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 679a5365e0e04..8acc5ce2fda09 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -595,6 +595,7 @@ proc semConst(c: PContext, n: PNode): PNode = if a.sons[length-2].kind != nkEmpty: typ = semTypeNode(c, a.sons[length-2], nil) + # don't evaluate here since the type compatibility check below may add a converter var def = semExprWithType(c, a[^1]) if def.typ.kind == tyProc and def.kind == nkSym: if def.sym.kind == skMacro: From d9f79733611ecbca91e539b6e2200983d98fc174 Mon Sep 17 00:00:00 2001 From: Clyybber Date: Thu, 26 Sep 2019 12:12:38 +0200 Subject: [PATCH 3/3] Add changelog entry --- changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/changelog.md b/changelog.md index 290e2e73ff1e3..85e38134296b0 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,8 @@ ### Breaking changes in the compiler +- Implicit conversions for `const` behave correctly now, meaning that code like `const SOMECONST = 0.int; procThatTakesInt32(SOMECONST)` will be illegal now. + Simply write `const SOMECONST = 0` instead. ## Library additions