Skip to content

Commit 24a4a9e

Browse files
committed
* removed option for non recursive distinctBase after request from @Araq;
* distinctBase now requires T to be distinct (even being recursive) * fixed a bug in (now-default) recursive distinctBase that would only trigger on complex stress test in tsugar.nim
1 parent a183d93 commit 24a4a9e

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

lib/pure/sugar.nim

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -216,47 +216,40 @@ proc replaceNodes(ast: NimNode): NimNode =
216216
result = rTree
217217
result = inspect(ast)
218218

219-
macro distinctBase*(T: typedesc, recursive: static[bool] = false): untyped =
220-
## reverses ``type T = distinct A``
219+
macro distinctBase*(T: typedesc): untyped =
220+
## reverses ``type T = distinct A``; works recursively.
221221
runnableExamples:
222222
type T = distinct int
223223
doAssert distinctBase(T) is int
224224
doAssert: not compiles(distinctBase(int))
225225
type T2 = distinct T
226-
doAssert distinctBase(T2, recursive = false) is T
227-
doAssert distinctBase(int, recursive = true) is int
228-
doAssert distinctBase(T2, recursive = true) is int
226+
doAssert distinctBase(T2) is int
229227

230228
let typeNode = getTypeImpl(T)
231229
expectKind(typeNode, nnkBracketExpr)
232230
if typeNode[0].typeKind != ntyTypeDesc:
233231
error "expected typeDesc, got " & $typeNode[0]
234232
var typeSym = typeNode[1]
235-
if not recursive:
236-
let impl = getTypeImpl(typeSym)
237-
if impl.typeKind != ntyDistinct:
238-
error "type is not distinct"
239-
typeSym = getTypeInst(impl[0])
240-
else:
241-
while true:
242-
let impl = getTypeImpl(typeSym)
243-
if impl.typeKind != ntyDistinct:
244-
typeSym = impl
245-
break
246-
typeSym = getTypeInst(impl[0])
233+
typeSym = getTypeImpl(typeSym)
234+
if typeSym.typeKind != ntyDistinct:
235+
error "type is not distinct"
236+
typeSym = typeSym[0]
237+
while typeSym.typeKind == ntyDistinct:
238+
typeSym = getTypeImpl(typeSym)[0]
247239
typeSym.replaceNodes
248240

249-
func distinctBase*[T](a: T, recursive: static[bool] = false): auto {.inline.} =
241+
# using `auto` return pending https://github.com/nim-lang/Nim/issues/8551
242+
func distinctBase*[T](a: T): auto {.inline.} =
250243
## converts a distinct variable to it's original type
251244
runnableExamples:
252245
type T = distinct int
253246
var a: T = T(1)
254247
let b = a.distinctBase
255248
doAssert b is int
256249
doAssert b == 1
257-
distinctBase(T, recursive)(a)
250+
distinctBase(T)(a)
258251
259252
when isMainModule:
260253
# pending https://github.com/nim-lang/Nim/issues/7280
261-
discard distinctBase[int](0, recursive = true)
262-
254+
type T=distinct int
255+
discard distinctBase(T(0))

0 commit comments

Comments
 (0)