From 60350eca1a2afec86c17fce4cd8b4966e6b55e79 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 20 Feb 2023 23:50:53 +0800 Subject: [PATCH] fixes #1027; disallow templates to use ambiguous identifiers (#21405) * Add `nkFastAsgn` into `semExpr` (#20939) * Add nkFastAsgn into case statement * Add test case * fixes #1027; disallow templates to use ambiguous identifiers (#20631) * test qualifiedLookUp in templates * check later * add testcase * add 4errormsg * Update tests/template/m1027a.nim Co-authored-by: Clay Sweetser * Update tests/template/m1027b.nim Co-authored-by: Clay Sweetser Co-authored-by: Andreas Rumpf Co-authored-by: Clay Sweetser --------- Co-authored-by: Jake Leahy Co-authored-by: Andreas Rumpf Co-authored-by: Clay Sweetser --- compiler/semtempl.nim | 2 ++ tests/template/m1027a.nim | 1 + tests/template/m1027b.nim | 1 + tests/template/t1027.nim | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 tests/template/m1027a.nim create mode 100644 tests/template/m1027b.nim create mode 100644 tests/template/t1027.nim diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index bcc8be5a2b..117efb5e59 100644 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -380,6 +380,8 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode = result = newSymNode(s, n.info) onUse(n.info, s) else: + if s.kind in {skType, skVar, skLet, skConst}: + discard qualifiedLookUp(c.c, n, {checkAmbiguity, checkModule}) result = semTemplSymbol(c.c, n, s, c.noGenSym > 0) of nkBind: result = semTemplBody(c, n[0]) diff --git a/tests/template/m1027a.nim b/tests/template/m1027a.nim new file mode 100644 index 0000000000..fad915a2f4 --- /dev/null +++ b/tests/template/m1027a.nim @@ -0,0 +1 @@ +const version_str* = "mod a" diff --git a/tests/template/m1027b.nim b/tests/template/m1027b.nim new file mode 100644 index 0000000000..5ff0b97140 --- /dev/null +++ b/tests/template/m1027b.nim @@ -0,0 +1 @@ +const version_str* = "mod b" diff --git a/tests/template/t1027.nim b/tests/template/t1027.nim new file mode 100644 index 0000000000..fe03c4ea91 --- /dev/null +++ b/tests/template/t1027.nim @@ -0,0 +1,22 @@ +discard """ + cmd: "nim check --hints:off $file" + errormsg: "" + nimout: ''' +t1027.nim(20, 19) Error: ambiguous identifier: 'version_str' -- use one of the following: + m1027a.version_str: string + m1027b.version_str: string +''' +""" + + + + + + +import m1027a, m1027b + +# bug #1027 +template wrap_me(stuff): untyped = + echo "Using " & version_str + +wrap_me("hey")