mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-05 03:14:08 +00:00
expose rangeBase typetrait, fix enum conversion warning (#24056)
refs #21682, refs #24038 The `rangeBase` typetrait added in #21682 which gives the base type of a range type is now added publicly to `typetraits`. Previously it was only privately used in `repr_v2` and in `enumutils` since #24052 (coincidentally I didn't see this until now). This is part of an effort to make range types easier to work with in generics, as mentioned in #24038. Its use combined with #24037 is also tested. The condition for the "enum to enum conversion" warning is now also restricted to conversions between different enum base types, i.e. conversion between an enum type and a range type of itself doesn't give a warning. I put this in this PR since the test gave the warning and so works as a regression test.
This commit is contained in:
@@ -55,6 +55,8 @@
|
||||
- Added `setLenUninit` to system, which doesn't initialize
|
||||
slots when enlarging a sequence.
|
||||
- Added `hasDefaultValue` to `std/typetraits` to check if a type has a valid default value.
|
||||
- Added `rangeBase` to `std/typetraits` to obtain the base type of a range type or
|
||||
convert a value with a range type to its base type.
|
||||
- Added Viewport API for the JavaScript targets in the `dom` module.
|
||||
- Added `toSinglyLinkedRing` and `toDoublyLinkedRing` to `std/lists` to convert from `openArray`s.
|
||||
- ORC: To be enabled via `nimOrcStats` there is a new API called `GC_orcStats` that can be used to query how many
|
||||
|
||||
@@ -279,7 +279,8 @@ proc checkConvertible(c: PContext, targetTyp: PType, src: PNode): TConvStatus =
|
||||
result = checkConversionBetweenObjects(d.skipTypes(abstractInst), s.skipTypes(abstractInst), pointers)
|
||||
elif (targetBaseTyp.kind in IntegralTypes) and
|
||||
(srcBaseTyp.kind in IntegralTypes):
|
||||
if targetTyp.kind == tyEnum and srcBaseTyp.kind == tyEnum:
|
||||
if targetTyp.kind == tyEnum and srcBaseTyp.kind == tyEnum and
|
||||
not sameType(targetTyp, srcBaseTyp):
|
||||
message(c.config, src.info, warnSuspiciousEnumConv, "suspicious code: enum to enum conversion")
|
||||
# `elif` would be incorrect here
|
||||
if targetTyp.kind == tyBool:
|
||||
|
||||
@@ -226,8 +226,9 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)
|
||||
of "rangeBase":
|
||||
# return the base type of a range type
|
||||
var arg = operand.skipTypes({tyGenericInst})
|
||||
assert arg.kind == tyRange
|
||||
result = getTypeDescNode(c, arg.base, operand.owner, traitCall.info)
|
||||
if arg.kind == tyRange:
|
||||
arg = arg.base
|
||||
result = getTypeDescNode(c, arg, operand.owner, traitCall.info)
|
||||
of "isCyclic":
|
||||
var operand = operand.skipTypes({tyGenericInst})
|
||||
let isCyclic = canFormAcycle(c.graph, operand)
|
||||
|
||||
@@ -130,6 +130,40 @@ template pointerBase*[T](_: typedesc[ptr T | ref T]): typedesc =
|
||||
assert (var s = "abc"; s[0].addr).typeof.pointerBase is char
|
||||
T
|
||||
|
||||
proc rangeBase*(T: typedesc[range]): typedesc {.magic: "TypeTrait".} =
|
||||
## Returns the base type for range types, or the type itself otherwise.
|
||||
##
|
||||
## **See also:**
|
||||
## * `rangeBase template <#rangeBase.t,T>`_
|
||||
runnableExamples:
|
||||
type MyRange = range[0..5]
|
||||
type MyEnum = enum a, b, c
|
||||
type MyEnumRange = range[b..c]
|
||||
doAssert rangeBase(MyRange) is int
|
||||
doAssert rangeBase(MyEnumRange) is MyEnum
|
||||
doAssert rangeBase(range['a'..'z']) is char
|
||||
|
||||
template rangeBase*[T: range](a: T): untyped =
|
||||
## Overload of `rangeBase <#rangeBase,typedesc,static[bool]>`_ for values.
|
||||
runnableExamples:
|
||||
type MyRange = range[0..5]
|
||||
type MyEnum = enum a, b, c
|
||||
type MyEnumRange = range[b..c]
|
||||
let x = MyRange(3)
|
||||
doAssert rangeBase(x) is int
|
||||
doAssert $typeof(rangeBase(x)) == "int"
|
||||
doAssert rangeBase(x) == 3
|
||||
let y: set[MyEnumRange] = {c}
|
||||
for e in y:
|
||||
doAssert rangeBase(e) is MyEnum
|
||||
doAssert $typeof(rangeBase(e)) == "MyEnum"
|
||||
doAssert rangeBase(e) == c
|
||||
let z: seq[range['a'..'z']] = @['c']
|
||||
doAssert rangeBase(z[0]) is char
|
||||
doAssert $typeof(rangeBase(z[0])) == "char"
|
||||
doAssert rangeBase(z[0]) == 'c'
|
||||
rangeBase(typeof(T))(a)
|
||||
|
||||
proc distinctBase*(T: typedesc, recursive: static bool = true): typedesc {.magic: "TypeTrait".} =
|
||||
## Returns the base type for distinct types, or the type itself otherwise.
|
||||
## If `recursive` is false, only the immediate distinct base will be returned.
|
||||
|
||||
@@ -144,6 +144,32 @@ block distinctBase:
|
||||
doAssert($distinctBase(typeof(b2)) == "string")
|
||||
doAssert($distinctBase(typeof(c2)) == "int")
|
||||
|
||||
block: # rangeBase
|
||||
{.push warningAsError[EnumConv]: on.}
|
||||
proc foo[T: not range](x: T): string =
|
||||
$T & "(" & $x & ")"
|
||||
proc foo[T: range](x: T): string =
|
||||
"ranged(" & $low(T) & ".." & $high(T) & " of " & $rangeBase(T) & ") " & foo(rangeBase(x))
|
||||
doAssert foo(123) == "int(123)"
|
||||
type IntRange = range[0..3]
|
||||
let x: IntRange = 2
|
||||
doAssert foo(x) == "ranged(0..3 of int) int(2)"
|
||||
type E = enum a, b, c, d, e, f
|
||||
type EnumRange = range[c..e]
|
||||
let y: EnumRange = d
|
||||
doAssert foo(y) == "ranged(c..e of E) E(d)"
|
||||
let z: range['a'..'z'] = 'g'
|
||||
doAssert foo(z) == "ranged(a..z of char) char(g)"
|
||||
{.pop.}
|
||||
|
||||
# works only with #24037:
|
||||
var toChange: range[0..3] = 1
|
||||
proc bar[T: int and not range](y: var T) =
|
||||
inc y
|
||||
doAssert not compiles(bar(toChange))
|
||||
bar(rangeBase(toChange))
|
||||
doAssert toChange == 2
|
||||
|
||||
block: # tupleLen
|
||||
doAssert not compiles(tupleLen(int))
|
||||
|
||||
|
||||
@@ -88,6 +88,13 @@ block: # https://github.com/nim-lang/RFCs/issues/294
|
||||
reject: Goo(k2)
|
||||
reject: k2.Goo
|
||||
|
||||
type KooRange = range[k2..k2]
|
||||
accept: KooRange(k2)
|
||||
accept: k2.KooRange
|
||||
let k2ranged: KooRange = k2
|
||||
accept: Koo(k2ranged)
|
||||
accept: k2ranged.Koo
|
||||
|
||||
reject:
|
||||
# bug #18550
|
||||
proc f(c: char): cstring =
|
||||
|
||||
Reference in New Issue
Block a user