Skip to content

Commit

Permalink
add tests for nim-lang#23854 and nim-lang#23855, prepare for cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Aug 20, 2024
1 parent 97539e7 commit 44704b1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
4 changes: 0 additions & 4 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,6 @@ proc semResolvedCall(c: PContext, x: var TCandidate,
else:
# For macros and templates, the resolved generic params
# are added as normal params.
# This is not done with unresolved static arguments, as typed macros
# cannot be instantiated yet and semMacroExpr/semTemplateExpr will
# reject them and delay their instantiation, when fully resolved types
# will be added instead.
c.inheritBindings(x, expectedType)
for s in instantiateGenericParamList(c, gp, x.bindings):
case s.kind
Expand Down
2 changes: 1 addition & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ proc tryReadingGenericParam(c: PContext, n: PNode, i: PIdent, t: PType): PNode =
result.typ = makeTypeFromExpr(c, copyTree(result))
else:
result = nil
of tyGenericParam, tyAnything:
of tyFromExpr, tyGenericParam, tyAnything:
if true or c.inGenericContext > 0:
result = semGenericStmt(c, n)
result.typ = makeTypeFromExpr(c, copyTree(result))
Expand Down
4 changes: 4 additions & 0 deletions compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
if s.typ.n != nil:
result = s.typ.n
elif c.inGenericContext > 0 and withinConcept notin flags:
# fine to give a symbol node a generic type here since
# we are in a generic context and `prepareNode` will be called
result = newSymNodeTypeDesc(s, c.idgen, n.info)
if canOpenSym(result.sym):
if genericsOpenSym in c.features:
Expand Down Expand Up @@ -137,6 +139,8 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
result.flags.incl nfDisabledOpenSym
result.typ = nil
elif c.inGenericContext > 0 and withinConcept notin flags:
# fine to give a symbol node a generic type here since
# we are in a generic context and `prepareNode` will be called
result = newSymNodeTypeDesc(s, c.idgen, n.info)
if canOpenSym(result.sym):
if genericsOpenSym in c.features:
Expand Down
65 changes: 65 additions & 0 deletions tests/generics/t23854.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# issue #23854

import std/bitops

const WordBitWidth = sizeof(pointer) * 8

func wordsRequired*(bits: int): int {.inline.} =
const divShiftor = fastLog2(uint32(WordBitWidth))
result = (bits + WordBitWidth - 1) shr divShiftor

type
Algebra* = enum
BLS12_381

BigInt*[bits: static int] = object
limbs*: array[wordsRequired(bits), uint]

Fr*[Name: static Algebra] = object
residue_form*: BigInt[255]

Fp*[Name: static Algebra] = object
residue_form*: BigInt[381]

FF*[Name: static Algebra] = Fp[Name] or Fr[Name]

type
EC_ShortW_Aff*[F] = object
## Elliptic curve point for a curve in Short Weierstrass form
## y² = x³ + a x + b
##
## over a field F
x*, y*: F

type FieldKind* = enum
kBaseField
kScalarField

template getBigInt*[Name: static Algebra](T: type FF[Name]): untyped =
## Get the underlying BigInt type.
BigInt[123]

func bits*[Name: static Algebra](T: type FF[Name]): static int =
T.getBigInt().bits

template getScalarField*(EC: type EC_ShortW_Aff): untyped =
Fr[EC.F.Name]

# ------------------------------------------------------------------------------

type
ECFFT_Descriptor*[EC] = object
## Metadata for FFT on Elliptic Curve
order*: int
rootsOfUnity1*: ptr UncheckedArray[BigInt[EC.getScalarField().bits()]] # Error: in expression 'EC.getScalarField()': identifier expected, but found 'EC.getScalarField'
rootsOfUnity2*: ptr UncheckedArray[BigInt[getScalarField(EC).bits()]] # Compiler SIGSEGV: Illegal Storage Access

func new*(T: type ECFFT_Descriptor): T =
discard

# ------------------------------------------------------------------------------

proc main() =
let ctx = ECFFT_Descriptor[EC_ShortW_Aff[Fp[BLS12_381]]].new()

main()
57 changes: 57 additions & 0 deletions tests/generics/t23855.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# issue #23855

import std/bitops

const WordBitWidth = sizeof(pointer) * 8

func wordsRequired*(bits: int): int {.inline.} =
const divShiftor = fastLog2(uint32(WordBitWidth))
result = (bits + WordBitWidth - 1) shr divShiftor

type
Algebra* = enum
BLS12_381

BigInt*[bits: static int] = object
limbs*: array[wordsRequired(bits), uint]

Fr*[Name: static Algebra] = object
residue_form*: BigInt[255]

Fp*[Name: static Algebra] = object
residue_form*: BigInt[381]

FF*[Name: static Algebra] = Fp[Name] or Fr[Name]

template getBigInt*[Name: static Algebra](T: type FF[Name]): untyped =
## Get the underlying BigInt type.
BigInt[123]

type
EC_ShortW_Aff*[F] = object
## Elliptic curve point for a curve in Short Weierstrass form
## y² = x³ + a x + b
##
## over a field F
x*, y*: F

func bits*[Name: static Algebra](T: type FF[Name]): static int =
T.getBigInt().bits

# ------------------------------------------------------------------------------

type
ECFFT_Descriptor*[EC] = object
## Metadata for FFT on Elliptic Curve
order*: int
rootsOfUnity*: ptr UncheckedArray[BigInt[Fr[EC.F.Name].bits()]] # Undeclared identifier `Name`

func new*(T: type ECFFT_Descriptor): T =
discard

# ------------------------------------------------------------------------------

proc main() =
let ctx = ECFFT_Descriptor[EC_ShortW_Aff[Fp[BLS12_381]]].new()

main()

0 comments on commit 44704b1

Please sign in to comment.