distinctBase now is identity instead of error for non distinct types (#16891)

This commit is contained in:
Timothee Cour 2021-02-01 04:10:52 -08:00 • committed by GitHub
commit 1a74576854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 16 deletions

View file

@ -119,6 +119,7 @@ with other backends. see #9125. Use `-d:nimLegacyJsRound` for previous behavior.
- nil dereference is not allowed at compile time. `cast[ptr int](nil)[]` is rejected at compile time. - nil dereference is not allowed at compile time. `cast[ptr int](nil)[]` is rejected at compile time.
- `typetraits.distinctBase` now is identity instead of error for non distinct types.
## Compiler changes ## Compiler changes

View file

@ -190,15 +190,10 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
result = newIntNodeT(toInt128(operand.len), traitCall, c.idgen, c.graph) result = newIntNodeT(toInt128(operand.len), traitCall, c.idgen, c.graph)
of "distinctBase": of "distinctBase":
var arg = operand.skipTypes({tyGenericInst}) var arg = operand.skipTypes({tyGenericInst})
if arg.kind == tyDistinct: while arg.kind == tyDistinct:
while arg.kind == tyDistinct: arg = arg.base
arg = arg.base arg = arg.skipTypes(skippedTypes + {tyGenericInst})
arg = arg.skipTypes(skippedTypes + {tyGenericInst}) result = getTypeDescNode(c, arg, operand.owner, traitCall.info)
result = getTypeDescNode(c, arg, operand.owner, traitCall.info)
else:
localError(c.config, traitCall.info,
"distinctBase expects a distinct type as argument. The given type was " & typeToString(operand))
result = newType(tyError, nextTypeId c.idgen, context).toNode(traitCall.info)
else: else:
localError(c.config, traitCall.info, "unknown trait: " & s) localError(c.config, traitCall.info, "unknown trait: " & s)
result = newNodeI(nkEmpty, traitCall.info) result = newNodeI(nkEmpty, traitCall.info)

View file

@ -86,26 +86,26 @@ proc isNamedTuple*(T: typedesc): bool {.magic: "TypeTrait".} =
doAssert isNamedTuple(tuple[name: string, age: int]) doAssert isNamedTuple(tuple[name: string, age: int])
proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".} = proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".} =
## Returns the base type for distinct types. This works only ## Returns the base type for distinct types, or the type itself otherwise.
## for distinct types and produces a compile time error otherwise.
## ##
## **See also:** ## **See also:**
## * `distinctBase template <#distinctBase.t,T>`_ ## * `distinctBase template <#distinctBase.t,T>`_
runnableExamples: runnableExamples:
type MyInt = distinct int type MyInt = distinct int
doAssert distinctBase(MyInt) is int doAssert distinctBase(MyInt) is int
doAssert not compiles(distinctBase(int)) doAssert distinctBase(int) is int
since (1, 1): since (1, 1):
template distinctBase*[T](a: T): untyped = template distinctBase*[T](a: T): untyped =
## Overload of `distinctBase <#distinctBase,typedesc>`_ for values. ## Overload of `distinctBase <#distinctBase,typedesc>`_ for values.
runnableExamples: runnableExamples:
type MyInt = distinct int type MyInt = distinct int
doAssert 12.MyInt.distinctBase == 12 doAssert 12.MyInt.distinctBase == 12
doAssert 12.distinctBase == 12
distinctBase(type(a))(a) when T is distinct:
distinctBase(type(a))(a)
else: # avoids hint ConvFromXtoItselfNotNeeded
a
proc tupleLen*(T: typedesc[tuple]): int {.magic: "TypeTrait".} = proc tupleLen*(T: typedesc[tuple]): int {.magic: "TypeTrait".} =
## Returns the number of elements of the tuple type `T`. ## Returns the number of elements of the tuple type `T`.

View file

@ -90,6 +90,8 @@ block distinctBase:
Foo[T] = distinct seq[T] Foo[T] = distinct seq[T]
var a: Foo[int] var a: Foo[int]
doAssert a.type.distinctBase is seq[int] doAssert a.type.distinctBase is seq[int]
doAssert seq[int].distinctBase is seq[int]
doAssert "abc".distinctBase == "abc"
block: block:
# simplified from https://github.com/nim-lang/Nim/pull/8531#issuecomment-410436458 # simplified from https://github.com/nim-lang/Nim/pull/8531#issuecomment-410436458