Skip to content

Commit 97d819a

Browse files
authored
add bit type overloads of $ and repr (#24865)
fixes #24864
1 parent b961ee6 commit 97d819a

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

lib/system/dollars.nim

+18-14
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ when not defined(nimPreviewSlimSystem):
1414
result = ""
1515
result.addFloat(x)
1616

17-
proc `$`*(x: int): string {.raises: [].} =
18-
## Outplace version of `addInt`.
19-
result = ""
20-
result.addInt(x)
21-
22-
proc `$`*(x: int64): string {.raises: [].} =
23-
## Outplace version of `addInt`.
24-
result = ""
25-
result.addInt(x)
26-
27-
proc `$`*(x: uint64): string {.raises: [].} =
28-
## Outplace version of `addInt`.
29-
result = ""
30-
addInt(result, x)
17+
template addIntAlias(T: typedesc) =
18+
proc `$`*(x: T): string {.raises: [].} =
19+
## Outplace version of `addInt`.
20+
result = ""
21+
result.addInt(x)
22+
23+
# need to declare for bit types as well to not clash with converters:
24+
addIntAlias int
25+
addIntAlias int8
26+
addIntAlias int16
27+
addIntAlias int32
28+
addIntAlias int64
29+
30+
addIntAlias uint
31+
addIntAlias uint8
32+
addIntAlias uint16
33+
addIntAlias uint32
34+
addIntAlias uint64
3135

3236
# same as old `ctfeWhitelist` behavior, whether or not this is a good idea.
3337
template gen(T) =

lib/system/repr_v2.nim

+20-15
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@ proc rangeBase(T: typedesc): typedesc {.magic: "TypeTrait".}
1414

1515
proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.}
1616

17-
proc repr*(x: int): string =
18-
## Same as $x
19-
$x
20-
21-
proc repr*(x: int64): string =
22-
## Same as $x
23-
$x
24-
25-
proc repr*(x: uint64): string {.noSideEffect.} =
26-
## Same as $x
27-
$x
28-
29-
proc repr*(x: float): string =
30-
## Same as $x
31-
$x
17+
template dollarAlias(T: typedesc) =
18+
proc repr*(x: T): string {.noSideEffect.} =
19+
## Same as $x
20+
$x
21+
22+
# need to declare for bit types as well to not clash with converters:
23+
dollarAlias int
24+
dollarAlias int8
25+
dollarAlias int16
26+
dollarAlias int32
27+
dollarAlias int64
28+
29+
dollarAlias uint
30+
dollarAlias uint8
31+
dollarAlias uint16
32+
dollarAlias uint32
33+
dollarAlias uint64
34+
35+
dollarAlias float
36+
dollarAlias float32
3237

3338
proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
3439
## repr for a boolean argument. Returns `x`

tests/system/treprconverter.nim

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# issue #24864
2+
3+
type S = distinct uint16
4+
converter d(field: uint8 | uint16): S = discard
5+
discard (repr(0'u16), repr(0'u8))

0 commit comments

Comments
 (0)