{"action":"opened","issue":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/4230","repository_url":"https://api.github.com/repos/nim-lang/Nim","labels_url":"https://api.github.com/repos/nim-lang/Nim/issues/4230/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/Nim/issues/4230/comments","events_url":"https://api.github.com/repos/nim-lang/Nim/issues/4230/events","html_url":"https://github.com/nim-lang/Nim/issues/4230","id":157209215,"number":4230,"title":"Codegen bug with closures and if-elif-else","user":{"login":"SSPkrolik","id":372353,"avatar_url":"https://avatars.githubusercontent.com/u/372353?v=3","gravatar_id":"","url":"https://api.github.com/users/SSPkrolik","html_url":"https://github.com/SSPkrolik","followers_url":"https://api.github.com/users/SSPkrolik/followers","following_url":"https://api.github.com/users/SSPkrolik/following{/other_user}","gists_url":"https://api.github.com/users/SSPkrolik/gists{/gist_id}","starred_url":"https://api.github.com/users/SSPkrolik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SSPkrolik/subscriptions","organizations_url":"https://api.github.com/users/SSPkrolik/orgs","repos_url":"https://api.github.com/users/SSPkrolik/repos","events_url":"https://api.github.com/users/SSPkrolik/events{/privacy}","received_events_url":"https://api.github.com/users/SSPkrolik/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-27T13:41:10Z","updated_at":"2016-05-27T13:41:10Z","closed_at":null,"body":"The next code doesn't compile:\r\n```nim\r\n# If we make `test` function return nothing - the bug disappears\r\nproc test(dothejob: proc()): int {.discardable.} =\r\n    dothejob()\r\n\r\ntest proc() =\r\n    let f = 15\r\n    if f > 10:\r\n        test proc() = discard\r\n    # If we remove elif branch of the condition - the bug disappears\r\n    elif f < 3:\r\n        test proc() = discard\r\n    else:\r\n        test proc() = discard\r\n```\r\nThe error is as follows:\r\n```bash\r\n/home/krolik/Projects/nimcache/cloelif.c: In function ‘HEX3Aanonymous_93009_2126931004’:\r\n/home/krolik/Projects/nimcache/cloelif.c:114:3: error: ‘LOC6’ undeclared (first use in this function)\r\n   LOC6 = test_93003_2126931004(LOC10);\r\n   ^\r\n```\r\nThe C code generated for the inner proc is the next:\r\n```c\r\nN_NIMCALL(void, HEX3Aanonymous_93009_2126931004)(void) {\r\n\tNI f0;\r\n\tnimfr(\":anonymous\", \"cloelif.nim\")\r\n\tnimln(6, \"cloelif.nim\");\r\n\tf0 = ((NI) 15);\r\n\tnimln(7, \"cloelif.nim\");\r\n\t{\r\n\t\tTY93005 LOC5;\r\n\t\tNI LOC6;\r\n\t\tnimln(352, \"system.nim\");\r\n\t\tif (!(((NI) 10) < f0)) goto LA3;\r\n\t\tnimln(8, \"cloelif.nim\");\r\n\t\tmemset((void*)(&LOC5), 0, sizeof(LOC5));\r\n\t\tLOC5.ClPrc = ((TMP141) (HEX3Aanonymous_93012_2126931004)); LOC5.ClEnv = NIM_NIL;\r\n\t\tLOC6 = (NI)0;\r\n\t\tLOC6 = test_93003_2126931004(LOC5);\r\n\t}\r\n\tgoto LA1;\r\n\tLA3: ;\r\n\t{\r\n\t\tTY93005 LOC10;\r\n\t\tnimln(10, \"cloelif.nim\");\r\n\t\tif (!(f0 < ((NI) 3))) goto LA8;\r\n\t\tnimln(11, \"cloelif.nim\");\r\n\t\tmemset((void*)(&LOC10), 0, sizeof(LOC10));\r\n\t\tLOC10.ClPrc = ((TMP142) (HEX3Aanonymous_93014_2126931004)); LOC10.ClEnv = NIM_NIL;\r\n\t\tLOC6 = test_93003_2126931004(LOC10);\r\n\t}\r\n\tgoto LA1;\r\n\tLA8: ;\r\n\t{\r\n\t\tTY93005 LOC12;\r\n\t\tNI LOC13;\r\n\t\tnimln(13, \"cloelif.nim\");\r\n\t\tmemset((void*)(&LOC12), 0, sizeof(LOC12));\r\n\t\tLOC12.ClPrc = ((TMP143) (HEX3Aanonymous_93016_2126931004)); LOC12.ClEnv = NIM_NIL;\r\n\t\tLOC13 = (NI)0;\r\n\t\tLOC13 = test_93003_2126931004(LOC12);\r\n\t}\r\n\tLA1: ;\r\n\tpopFrame();\r\n}\r\n```\r\nAnd we can really see that `LOC6` var is declared in the first `if` branch, thus is not visible for others."}}
{"actor":{"display_login":"SSPkrolik"}}