diff options
author | LemonBoy <LemonBoy@users.noreply.github.com> | 2018-07-17 17:50:05 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-07-17 17:50:05 +0200 |
commit | ebf4e9f7177da28fad4b81c74d4fc184581e9a70 (patch) | |
tree | 563533f8ba53324b284c5260f8b60906ec822993 | |
parent | fc0bcccc15ec5cf3351cf65b22c7a038d82d4b35 (diff) | |
download | Nim-ebf4e9f7177da28fad4b81c74d4fc184581e9a70.tar.gz |
Extend init variable tracking to tuple assignments (#8321)
Fixes #8314
-rw-r--r-- | compiler/sempass2.nim | 9 | ||||
-rw-r--r-- | tests/init/t8314.nim | 21 |
2 files changed, 30 insertions, 0 deletions
diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index e7a76ec22..bdea07ea8 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -785,6 +785,15 @@ proc track(tracked: PEffects, n: PNode) = initVar(tracked, child.sons[i], volatileCheck=false) addAsgnFact(tracked.guards, child.sons[i], last) notNilCheck(tracked, last, child.sons[i].typ) + elif child.kind == nkVarTuple and last.kind != nkEmpty: + for i in 0 .. child.len-2: + if child[i].kind == nkEmpty or + child[i].kind == nkSym and child[i].sym.name.s == "_": + continue + initVar(tracked, child[i], volatileCheck=false) + if last.kind in {nkPar, nkTupleConstr}: + addAsgnFact(tracked.guards, child[i], last[i]) + notNilCheck(tracked, last[i], child[i].typ) # since 'var (a, b): T = ()' is not even allowed, there is always type # inference for (a, b) and thus no nil checking is necessary. of nkConstSection: diff --git a/tests/init/t8314.nim b/tests/init/t8314.nim new file mode 100644 index 000000000..59d46eb33 --- /dev/null +++ b/tests/init/t8314.nim @@ -0,0 +1,21 @@ +discard """ + nimout: ''' +t8314.nim(8, 7) Hint: BEGIN [User] +t8314.nim(19, 7) Hint: END [User] + ''' +""" + +{.hint: "BEGIN".} +proc foo(x: range[1..10]) = + block: + var (y,) = (x,) + echo y + block: + var (_,y) = (1,x) + echo y + block: + var (y,_,) = (x,1,) + echo y +{.hint: "END".} + +foo(1) |