{"action":"created","issue":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/7791","repository_url":"https://api.github.com/repos/nim-lang/Nim","labels_url":"https://api.github.com/repos/nim-lang/Nim/issues/7791/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/Nim/issues/7791/comments","events_url":"https://api.github.com/repos/nim-lang/Nim/issues/7791/events","html_url":"https://github.com/nim-lang/Nim/issues/7791","id":320782733,"node_id":"MDU6SXNzdWUzMjA3ODI3MzM=","number":7791,"title":"result variable is not hygienic in templates","user":{"login":"krux02","id":929961,"node_id":"MDQ6VXNlcjkyOTk2MQ==","avatar_url":"https://avatars2.githubusercontent.com/u/929961?v=4","gravatar_id":"","url":"https://api.github.com/users/krux02","html_url":"https://github.com/krux02","followers_url":"https://api.github.com/users/krux02/followers","following_url":"https://api.github.com/users/krux02/following{/other_user}","gists_url":"https://api.github.com/users/krux02/gists{/gist_id}","starred_url":"https://api.github.com/users/krux02/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krux02/subscriptions","organizations_url":"https://api.github.com/users/krux02/orgs","repos_url":"https://api.github.com/users/krux02/repos","events_url":"https://api.github.com/users/krux02/events{/privacy}","received_events_url":"https://api.github.com/users/krux02/received_events","type":"User","site_admin":false},"labels":[{"id":431542100,"node_id":"MDU6TGFiZWw0MzE1NDIxMDA=","url":"https://api.github.com/repos/nim-lang/Nim/labels/Templates","name":"Templates","color":"c9d615","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-05-07T12:18:55Z","updated_at":"2018-11-04T19:55:15Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"When a template introduces a new variable, it is normally hidden in the context where the templates expands into, to prevent name clashes. The problem is, that when a template generates a function header, this function has this implicit result variable that is not hidden.\r\n\r\nBest seen in this example\r\n\r\n```nim\r\ntemplate foobar(targ: untyped): untyped =\r\n  ## this template should only put ``arg`` in a context where a result\r\n  ## variable exist.  The variables ``hidden`` I have put here to\r\n  ## create a comparison of how a variables shold behave.\r\n  proc foo(arg: bool): bool =\r\n    result = arg\r\n    # this variable is correctly hidden with the hygenic template.\r\n    let hidden = \"mystring\"\r\n    targ\r\n\r\n  # Well this part of the template is pretty pointless.  I just put it\r\n  # here, so the generated function is actually used.\r\n  if foo(true):\r\n    echo \"bar\"\r\n\r\n  if foo(false):\r\n    echo \"bara\"\r\n\r\nproc bananas(): int =\r\n  result = 123 # <-- should bind to this, but it doesn't ----,\r\n  var hidden = 111 #                                          \\\r\n  foobar: #                                                   |\r\n    # this binds correctly to the local variable.             |\r\n    hidden += 123 #                                           |\r\n    # this ``result`` binds to the result:bool defined in the |\r\n    # template.  This violates hygenic macros.                /\r\n    result += 123  # ----------------------------------------´\r\n```\r\ncompilation output:\r\n```\r\nscratch.nim(20, 3) template/generic instantiation from here\r\nscratch.nim(24, 12) Error: type mismatch: got <bool, int literal(123)>\r\nbut expected one of: \r\nproc `+=`[T: SomeOrdinal | uint | uint64](x: var T; y: T)\r\n  first type mismatch at position: 2\r\n  required type: T: int or int8\r\n  but expression '123' is of type: int literal(123)\r\nproc `+=`[T: float | float32 | float64](x: var T; y: T)\r\n  first type mismatch at position: 1\r\n  required type: var T: float or float32\r\n  but expression 'result' is of type: bool\r\n\r\nexpression: result += 123\r\n\r\nCompilation exited abnormally with code 1 at Mon May  7 13:58:34\r\n```\r\n\r\nThis causes a problem in my ast pattern matching library:\r\nhttps://github.com/krux02/ast-pattern-matching/issues/6"},"comment":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/comments/435700400","html_url":"https://github.com/nim-lang/Nim/issues/7791#issuecomment-435700400","issue_url":"https://api.github.com/repos/nim-lang/Nim/issues/7791","id":435700400,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNTcwMDQwMA==","user":{"login":"krux02","id":929961,"node_id":"MDQ6VXNlcjkyOTk2MQ==","avatar_url":"https://avatars2.githubusercontent.com/u/929961?v=4","gravatar_id":"","url":"https://api.github.com/users/krux02","html_url":"https://github.com/krux02","followers_url":"https://api.github.com/users/krux02/followers","following_url":"https://api.github.com/users/krux02/following{/other_user}","gists_url":"https://api.github.com/users/krux02/gists{/gist_id}","starred_url":"https://api.github.com/users/krux02/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krux02/subscriptions","organizations_url":"https://api.github.com/users/krux02/orgs","repos_url":"https://api.github.com/users/krux02/repos","events_url":"https://api.github.com/users/krux02/events{/privacy}","received_events_url":"https://api.github.com/users/krux02/received_events","type":"User","site_admin":false},"created_at":"2018-11-04T19:55:15Z","updated_at":"2018-11-04T19:55:15Z","author_association":"CONTRIBUTOR","body":"I just tested it, it is still broken."}}
{"actor":{"display_login":"krux02"}}