diff options
author | metagn <metagngn@gmail.com> | 2024-09-06 12:18:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-06 11:18:20 +0200 |
commit | d77ea0783786e863804a1c51186d6811187215f7 (patch) | |
tree | 804c99d3ef4db0d9b29a6533954ed8d641784b5d /compiler | |
parent | bf865fa75aba5ed61f123e528044285a87bdd0b1 (diff) | |
download | Nim-d77ea0783786e863804a1c51186d6811187215f7.tar.gz |
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.
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/semexprs.nim | 3 | ||||
-rw-r--r-- | compiler/semmagic.nim | 5 |
2 files changed, 5 insertions, 3 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index fd53c07ff..39121a671 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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: diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index 1e579a959..1d97f1926 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -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) |