Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type mismatch on init of static[T] object with T being a static[U] #11142

Closed
mratsim opened this issue Apr 29, 2019 · 2 comments · Fixed by #15847
Closed

Type mismatch on init of static[T] object with T being a static[U] #11142

mratsim opened this issue Apr 29, 2019 · 2 comments · Fixed by #15847

Comments

@mratsim
Copy link
Collaborator

mratsim commented Apr 29, 2019

The following works:

type
  MyObjParam = object
    x: int

  MyObj[P: static MyObjParam] = object
    y: int

const P = MyObjParam(x: 2)
let Q = MyObj[P](y: 2)
echo Q

But the following fails

type
  MyObjParam[N: static int] = object
    x: int

  MyObj[P: static MyObjParam] = object
    y: int

const P = MyObjParam[256](x: 2)
let Q = MyObj[P](y: 2)
echo Q

The error is:

Error: cannot instantiate MyObj
got: <static[MyObjParam[256]]((x: 2))>
but expected: <P>

I expect the issue rootcause is closely related to #9679.

Use case: I'm trying to implement a bigint backend for an elliptic curve crypto library. Everything is done modulo a bigint that is known at compile-time. The types are like this:

type
  # https://github.com/status-im/nim-constantine/blob/master/constantine/bigints.nim
  BigInt*[bits: static int] = object
    limbs*: array[bits.wordsRequired, Word]
  
  # https://github.com/status-im/nim-constantine/blob/master/constantine/field_fp.nim
  Fp*[P: static BigInt] = object
    ## P is a prime number
    ## All operations on a field are modulo P
    value: type(P)

I expect that @andreaferretti would also like to use this in Emmy

@mratsim
Copy link
Collaborator Author

mratsim commented Apr 29, 2019

Investigating a bit where the semcheck is failing:

Full stacktrace

Error: cannot instantiate MyObj
got: <static[MyObjParam[256]]((x: 2))>
but expected: <P>
Traceback (most recent call last)
nim.nim(98)              nim
nim.nim(75)              handleCmdLine
cmdlinehelper.nim(92)    loadConfigsAndRunMainCommand
main.nim(194)            mainCommand
main.nim(90)             commandCompileToC
modules.nim(147)         compileProject
modules.nim(88)          compileModule
passes.nim(197)          processModule
passes.nim(86)           processTopLevelStmt
sem.nim(603)             myProcess
sem.nim(571)             semStmtAndGenerateGenerics
semstmts.nim(2159)       semStmt
semexprs.nim(939)        semExprNoType
semexprs.nim(2624)       semExpr
semstmts.nim(2099)       semStmtList
semexprs.nim(2627)       semExpr
semstmts.nim(447)        semVarOrLet
semexprs.nim(64)         semExprWithType
semexprs.nim(2590)       semExpr
semobjconstr.nim(258)    semObjConstr
semtypes.nim(1665)       semTypeNode
semtypes.nim(1329)       semGeneric
msgs.nim(530)            localError
msgs.nim(515)            liMessage
msgs.nim(346)            handleError
msgs.nim(331)            quit
FAILURE

Investigation

Diving a bit into sigmatch, the failure seems to be triggered on this code path:

Nim/compiler/sigmatch.nim

Lines 2399 to 2408 in bb8e2ee

else:
m.baseTypeMatch = false
m.typedescMatched = false
n.sons[a] = prepareOperand(c, formal.typ, n.sons[a])
arg = paramTypesMatch(m, formal.typ, n.sons[a].typ,
n.sons[a], nOrig.sons[a])
if arg == nil:
m.state = csNoMatch
m.firstMismatch = f
return

Small stacktrace before returning from the branch

semtypes.nim(1665)       semTypeNode
semtypes.nim(1323)       semGeneric
sigmatch.nim(2481)       matches
sigmatch.nim(2424)       matchesAux

Diving deeper into sigmatch, it's typeRel called by paramTypesMatchAux that doesn't match.

var r = typeRel(m, f, a)

Tracing typeRel

Turning on tracing produces this interesting output for the inner static param, notice that we have tyStatic on one side and tyObject on the other.

----- TYPE REL 4
{
  "kind": "tyStatic",
  "id": 171017,
  "sym": {
    "kind": "skGenericParam",
    "name": "P",
    "id": 171019,
    "typ": <defined 7 lines upwards>
  },
  "flags": ["tfHasMeta", "tfHasStatic", "tfGenericTypeParam"],
  "sons": [{
    "kind": "tyGenericBody",
    "id": 171004,
    "sym": {
      "kind": "skType",
      "name": "MyObjParam",
      "id": 171003,
      "flags": ["sfUsed", "sfGlobal"],
      "typ": <defined 8 lines upwards>
    },
    "flags": ["tfHasMeta"],
    "sons": [{
      "kind": "tyStatic",
      "id": 171009,
      "sym": {
        "kind": "skGenericParam",
        "name": "N",
        "id": 171011,
        "typ": <defined 7 lines upwards>
      },
      "flags": ["tfHasStatic", "tfGenericTypeParam"],
      "sons": [{
        "kind": "tyInt",
        "id": 42006,
        "sym": {
          "kind": "skType",
          "name": "int",
          "id": 42005,
          "flags": ["sfUsed", "sfExported", "sfGlobal"],
          "typ": <defined 8 lines upwards>
        },
        "flags": ["tfCheckedForDestructor"]
      }]
    }, {
      "kind": "tyObject",
      "id": 171013,
      "sym": <defined 33 lines upwards>,
      "flags": ["tfFinal"],
      "sons": [null],
      "n": {
        "kind": "nkRecList",
        "sons": [{
          "kind": "nkSym",
          "sym": {
            "kind": "skField",
            "name": "x",
            "id": 171014,
            "position": 0,
            "typ": <defined 27 lines upwards>
          }
        }]
      }
    }],
    "n": {
      "kind": "nkGenericParams",
      "sons": [{
        "kind": "nkSym",
        "sym": <defined 43 lines upwards>
      }]
    }
  }]
}
{
  "kind": "tyObject",
  "id": 171013,
  "sym": {
    "kind": "skType",
    "name": "MyObjParam",
    "id": 171003,
    "flags": ["sfUsed", "sfGlobal"],
    "typ": {
      "kind": "tyGenericBody",
      "id": 171004,
      "sym": <defined 8 lines upwards>,
      "flags": ["tfHasMeta"],
      "sons": [{
        "kind": "tyStatic",
        "id": 171009,
        "sym": {
          "kind": "skGenericParam",
          "name": "N",
          "id": 171011,
          "typ": <defined 7 lines upwards>
        },
        "flags": ["tfHasStatic", "tfGenericTypeParam"],
        "sons": [{
          "kind": "tyInt",
          "id": 42006,
          "sym": {
            "kind": "skType",
            "name": "int",
            "id": 42005,
            "flags": ["sfUsed", "sfExported", "sfGlobal"],
            "typ": <defined 8 lines upwards>
          },
          "flags": ["tfCheckedForDestructor"]
        }]
      }, <defined 35 lines upwards>],
      "n": {
        "kind": "nkGenericParams",
        "sons": [{
          "kind": "nkSym",
          "sym": <defined 24 lines upwards>
        }]
      }
    }
  },
  "flags": ["tfFinal"],
  "sons": [null],
  "n": {
    "kind": "nkRecList",
    "sons": [{
      "kind": "nkSym",
      "sym": {
        "kind": "skField",
        "name": "x",
        "id": 171014,
        "position": 0,
        "typ": <defined 33 lines upwards>
      }
    }]
  }
}
----- TYPE REL 4 RESULT: isNone

@cooldome
Copy link
Member

cooldome commented Nov 5, 2020

Works as expected now. My latest change, fixed it.

cooldome added a commit that referenced this issue Nov 5, 2020
@cooldome cooldome mentioned this issue Nov 5, 2020
Araq pushed a commit that referenced this issue Nov 5, 2020
Araq pushed a commit that referenced this issue Nov 5, 2020
* close #11142

* fix #12636

* undo unwanted changes

* fix illegal recursion case
narimiran pushed a commit that referenced this issue Nov 9, 2020
(cherry picked from commit 9455a0c)
narimiran pushed a commit that referenced this issue Nov 9, 2020
* close #11142

* fix #12636

* undo unwanted changes

* fix illegal recursion case

(cherry picked from commit 3af7818)
PMunch pushed a commit to PMunch/Nim that referenced this issue Jan 6, 2021
PMunch pushed a commit to PMunch/Nim that referenced this issue Jan 6, 2021
* close nim-lang#11142

* fix nim-lang#12636

* undo unwanted changes

* fix illegal recursion case
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
* close nim-lang#11142

* fix nim-lang#12636

* undo unwanted changes

* fix illegal recursion case
irdassis pushed a commit to irdassis/Nim that referenced this issue Mar 16, 2021
irdassis pushed a commit to irdassis/Nim that referenced this issue Mar 16, 2021
* close nim-lang#11142

* fix nim-lang#12636

* undo unwanted changes

* fix illegal recursion case
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
* close nim-lang#11142

* fix nim-lang#12636

* undo unwanted changes

* fix illegal recursion case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants