From 7616e6ee2b48a3c3504684f3324a42e95bd73790 Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Thu, 6 Jul 2023 16:18:47 +1000 Subject: [PATCH] Fix concepts with doc comments (#22228) * Add testcase This tries to use a concept with a doc comment which currently leads to a segfault * Ignore nil nodes which happen when there are doc comments in new style concept This was done instead of semming the comments since `semConceptDecl` says it only supports lists of actual statements * Go with alternative fix: Sem comments but ignore them Since `nil` could mean anything it is best to not silently ignore it (In case another nil problem happens in future Also fix test case so it isn't an infinite loop --- compiler/concepts.nim | 4 +++- tests/concepts/tconcepts.nim | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/concepts.nim b/compiler/concepts.nim index 6a383a937c..c980cf7ef2 100644 --- a/compiler/concepts.nim +++ b/compiler/concepts.nim @@ -62,7 +62,7 @@ proc semConceptDecl(c: PContext; n: PNode): PNode = result[i] = n[i] result[^1] = semConceptDecl(c, n[^1]) of nkCommentStmt: - discard + result = n else: localError(c.config, n.info, "unexpected construct in the new-styled concept: " & renderTree(n)) result = n @@ -306,6 +306,8 @@ proc conceptMatchNode(c: PContext; n: PNode; m: var MatchCon): bool = result = matchSyms(c, n, {skMethod}, m) of nkIteratorDef: result = matchSyms(c, n, {skIterator}, m) + of nkCommentStmt: + result = true else: # error was reported earlier. result = false diff --git a/tests/concepts/tconcepts.nim b/tests/concepts/tconcepts.nim index acdff6f246..ea3ddc4017 100644 --- a/tests/concepts/tconcepts.nim +++ b/tests/concepts/tconcepts.nim @@ -31,6 +31,7 @@ e 20 10 5 +9 ''' """ @@ -438,3 +439,13 @@ import mvarconcept block tvar: # bug #2346, bug #2404 echo randomInt(5) + +block tcomment: + type + Foo = concept + ## Some comment + proc bar(x: Self) + + proc bar(x: int) = echo x + proc foo(x: Foo) = x.bar + foo(9)