Skip to content

Commit 44704b1

Browse files
committed
add tests for nim-lang#23854 and nim-lang#23855, prepare for cleanup
1 parent 97539e7 commit 44704b1

File tree

5 files changed

+127
-5
lines changed

5 files changed

+127
-5
lines changed

compiler/semcall.nim

-4
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,6 @@ proc semResolvedCall(c: PContext, x: var TCandidate,
704704
else:
705705
# For macros and templates, the resolved generic params
706706
# are added as normal params.
707-
# This is not done with unresolved static arguments, as typed macros
708-
# cannot be instantiated yet and semMacroExpr/semTemplateExpr will
709-
# reject them and delay their instantiation, when fully resolved types
710-
# will be added instead.
711707
c.inheritBindings(x, expectedType)
712708
for s in instantiateGenericParamList(c, gp, x.bindings):
713709
case s.kind

compiler/semexprs.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ proc tryReadingGenericParam(c: PContext, n: PNode, i: PIdent, t: PType): PNode =
15001500
result.typ = makeTypeFromExpr(c, copyTree(result))
15011501
else:
15021502
result = nil
1503-
of tyGenericParam, tyAnything:
1503+
of tyFromExpr, tyGenericParam, tyAnything:
15041504
if true or c.inGenericContext > 0:
15051505
result = semGenericStmt(c, n)
15061506
result.typ = makeTypeFromExpr(c, copyTree(result))

compiler/semgnrc.nim

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
105105
if s.typ.n != nil:
106106
result = s.typ.n
107107
elif c.inGenericContext > 0 and withinConcept notin flags:
108+
# fine to give a symbol node a generic type here since
109+
# we are in a generic context and `prepareNode` will be called
108110
result = newSymNodeTypeDesc(s, c.idgen, n.info)
109111
if canOpenSym(result.sym):
110112
if genericsOpenSym in c.features:
@@ -137,6 +139,8 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
137139
result.flags.incl nfDisabledOpenSym
138140
result.typ = nil
139141
elif c.inGenericContext > 0 and withinConcept notin flags:
142+
# fine to give a symbol node a generic type here since
143+
# we are in a generic context and `prepareNode` will be called
140144
result = newSymNodeTypeDesc(s, c.idgen, n.info)
141145
if canOpenSym(result.sym):
142146
if genericsOpenSym in c.features:

tests/generics/t23854.nim

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# issue #23854
2+
3+
import std/bitops
4+
5+
const WordBitWidth = sizeof(pointer) * 8
6+
7+
func wordsRequired*(bits: int): int {.inline.} =
8+
const divShiftor = fastLog2(uint32(WordBitWidth))
9+
result = (bits + WordBitWidth - 1) shr divShiftor
10+
11+
type
12+
Algebra* = enum
13+
BLS12_381
14+
15+
BigInt*[bits: static int] = object
16+
limbs*: array[wordsRequired(bits), uint]
17+
18+
Fr*[Name: static Algebra] = object
19+
residue_form*: BigInt[255]
20+
21+
Fp*[Name: static Algebra] = object
22+
residue_form*: BigInt[381]
23+
24+
FF*[Name: static Algebra] = Fp[Name] or Fr[Name]
25+
26+
type
27+
EC_ShortW_Aff*[F] = object
28+
## Elliptic curve point for a curve in Short Weierstrass form
29+
## y² = x³ + a x + b
30+
##
31+
## over a field F
32+
x*, y*: F
33+
34+
type FieldKind* = enum
35+
kBaseField
36+
kScalarField
37+
38+
template getBigInt*[Name: static Algebra](T: type FF[Name]): untyped =
39+
## Get the underlying BigInt type.
40+
BigInt[123]
41+
42+
func bits*[Name: static Algebra](T: type FF[Name]): static int =
43+
T.getBigInt().bits
44+
45+
template getScalarField*(EC: type EC_ShortW_Aff): untyped =
46+
Fr[EC.F.Name]
47+
48+
# ------------------------------------------------------------------------------
49+
50+
type
51+
ECFFT_Descriptor*[EC] = object
52+
## Metadata for FFT on Elliptic Curve
53+
order*: int
54+
rootsOfUnity1*: ptr UncheckedArray[BigInt[EC.getScalarField().bits()]] # Error: in expression 'EC.getScalarField()': identifier expected, but found 'EC.getScalarField'
55+
rootsOfUnity2*: ptr UncheckedArray[BigInt[getScalarField(EC).bits()]] # Compiler SIGSEGV: Illegal Storage Access
56+
57+
func new*(T: type ECFFT_Descriptor): T =
58+
discard
59+
60+
# ------------------------------------------------------------------------------
61+
62+
proc main() =
63+
let ctx = ECFFT_Descriptor[EC_ShortW_Aff[Fp[BLS12_381]]].new()
64+
65+
main()

tests/generics/t23855.nim

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# issue #23855
2+
3+
import std/bitops
4+
5+
const WordBitWidth = sizeof(pointer) * 8
6+
7+
func wordsRequired*(bits: int): int {.inline.} =
8+
const divShiftor = fastLog2(uint32(WordBitWidth))
9+
result = (bits + WordBitWidth - 1) shr divShiftor
10+
11+
type
12+
Algebra* = enum
13+
BLS12_381
14+
15+
BigInt*[bits: static int] = object
16+
limbs*: array[wordsRequired(bits), uint]
17+
18+
Fr*[Name: static Algebra] = object
19+
residue_form*: BigInt[255]
20+
21+
Fp*[Name: static Algebra] = object
22+
residue_form*: BigInt[381]
23+
24+
FF*[Name: static Algebra] = Fp[Name] or Fr[Name]
25+
26+
template getBigInt*[Name: static Algebra](T: type FF[Name]): untyped =
27+
## Get the underlying BigInt type.
28+
BigInt[123]
29+
30+
type
31+
EC_ShortW_Aff*[F] = object
32+
## Elliptic curve point for a curve in Short Weierstrass form
33+
## y² = x³ + a x + b
34+
##
35+
## over a field F
36+
x*, y*: F
37+
38+
func bits*[Name: static Algebra](T: type FF[Name]): static int =
39+
T.getBigInt().bits
40+
41+
# ------------------------------------------------------------------------------
42+
43+
type
44+
ECFFT_Descriptor*[EC] = object
45+
## Metadata for FFT on Elliptic Curve
46+
order*: int
47+
rootsOfUnity*: ptr UncheckedArray[BigInt[Fr[EC.F.Name].bits()]] # Undeclared identifier `Name`
48+
49+
func new*(T: type ECFFT_Descriptor): T =
50+
discard
51+
52+
# ------------------------------------------------------------------------------
53+
54+
proc main() =
55+
let ctx = ECFFT_Descriptor[EC_ShortW_Aff[Fp[BLS12_381]]].new()
56+
57+
main()

0 commit comments

Comments
 (0)