Skip to content

Commit

Permalink
consistent use of scForceOpen for generic dot field symbols (#21738)
Browse files Browse the repository at this point in the history
* always force open generic dot field symbols?

fixes #21724 but might break code

* alternative, should fix CI

* other alternative, add test for previous CI failure

* not needed

* make sure call doesn't compile too

* ok actual second test

* ok final actual correct test

* apply performance idea

* don't make fromDotExpr static
  • Loading branch information
metagn authored May 5, 2023
1 parent 62f1ad5 commit e92d768
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
22 changes: 11 additions & 11 deletions compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,19 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
fromDotExpr=false): PNode =
semIdeForTemplateOrGenericCheck(c.config, n, ctx.cursorInBody)
incl(s.flags, sfUsed)
template maybeDotChoice(c: PContext, n: PNode, s: PSym, fromDotExpr: bool) =
if fromDotExpr:
result = symChoice(c, n, s, scForceOpen)
if result.len == 1:
result.transitionSonsKind(nkClosedSymChoice)
else:
result = symChoice(c, n, s, scOpen)
case s.kind
of skUnknown:
# Introduced in this pass! Leave it as an identifier.
result = n
of skProc, skFunc, skMethod, skIterator, skConverter, skModule:
result = symChoice(c, n, s, scOpen)
of skProc, skFunc, skMethod, skIterator, skConverter, skModule, skEnumField:
maybeDotChoice(c, n, s, fromDotExpr)
of skTemplate, skMacro:
# alias syntax, see semSym for skTemplate, skMacro
if sfNoalias notin s.flags and not fromDotExpr:
Expand All @@ -79,7 +86,7 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
result = semGenericStmt(c, result, {}, ctx)
discard c.friendModules.pop()
else:
result = symChoice(c, n, s, scOpen)
maybeDotChoice(c, n, s, fromDotExpr)
of skGenericParam:
if s.typ != nil and s.typ.kind == tyStatic:
if s.typ.n != nil:
Expand All @@ -99,8 +106,6 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
else:
result = n
onUse(n.info, s)
of skEnumField:
result = symChoice(c, n, s, scOpen)
else:
result = newSymNode(s, n.info)
onUse(n.info, s)
Expand Down Expand Up @@ -157,12 +162,7 @@ proc fuzzyLookup(c: PContext, n: PNode, flags: TSemGenericFlags,
result = newDot(result, symChoice(c, n, s, scForceOpen))
else:
let syms = semGenericStmtSymbol(c, n, s, ctx, flags, fromDotExpr=true)
if syms.kind == nkSym:
let choice = symChoice(c, n, s, scForceOpen)
choice.transitionSonsKind(nkClosedSymChoice)
result = newDot(result, choice)
else:
result = newDot(result, syms)
result = newDot(result, syms)

proc addTempDecl(c: PContext; n: PNode; kind: TSymKind) =
let s = newSymS(skUnknown, getIdentNode(c, n), c)
Expand Down
5 changes: 5 additions & 0 deletions tests/generics/mdotlookup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ var intset = initHashSet[int]()
proc fn*[T](a: T) =
if a in intset: echo("true")
else: echo("false")

import strutils

proc doStrip*[T](a: T): string =
result = ($a).strip()
55 changes: 55 additions & 0 deletions tests/generics/tbaddeprecated.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
discard """
output: '''
not deprecated
not deprecated
not error
not error
'''
"""

# issue #21724

block: # deprecated
{.push warningAsError[Deprecated]: on.}
type
SomeObj = object
hey: bool
proc hey() {.deprecated: "Shouldn't use this".} = echo "hey"
proc gen(o: auto) =
doAssert not compiles(o.hey())
if o.hey:
echo "not deprecated"
gen(SomeObj(hey: true))
doAssert not (compiles do:
proc hey(o: SomeObj) {.deprecated: "Shouldn't use this".} = echo "hey"
proc gen2(o: auto) =
if o.hey():
echo "not deprecated"
gen2(SomeObj(hey: true)))
proc hey(o: SomeObj) {.deprecated: "Shouldn't use this".} = echo "hey"
proc gen3(o: auto) =
if o.hey:
echo "not deprecated"
gen3(SomeObj(hey: true))
{.pop.}
block: # error
type
SomeObj = object
hey: bool
proc hey() {.error: "Shouldn't use this".} = echo "hey"
proc gen(o: auto) =
doAssert not compiles(o.hey())
if o.hey:
echo "not error"
gen(SomeObj(hey: true))
doAssert not (compiles do:
proc hey(o: SomeObj) {.error: "Shouldn't use this".} = echo "hey"
proc gen2(o: auto) =
if o.hey():
echo "not error"
gen2(SomeObj(hey: true)))
proc hey(o: SomeObj) {.error: "Shouldn't use this".} = echo "hey"
proc gen3(o: auto) =
if o.hey:
echo "not error"
gen3(SomeObj(hey: true))
3 changes: 2 additions & 1 deletion tests/generics/timports.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ block tclosed_sym:
proc same(r:R, d:int) = echo "TEST1"
doIt(Data[int](d:123), R())

import strutils, unicode # ambiguous `strip`

block tdotlookup:
foo(7)
# bug #1444
fn(4)

doAssert doStrip(123) == "123"

block tmodule_same_as_proc:
# bug #1965
Expand Down

0 comments on commit e92d768

Please sign in to comment.