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

[ICE] Combination of concept, dotOperators & static leads to internal error #20996

Closed
Vindaar opened this issue Dec 2, 2022 · 2 comments · Fixed by #21016
Closed

[ICE] Combination of concept, dotOperators & static leads to internal error #20996

Vindaar opened this issue Dec 2, 2022 · 2 comments · Fixed by #21016

Comments

@Vindaar
Copy link
Contributor

Vindaar commented Dec 2, 2022

Description

This is a highly reduced case of an issue I encountered because a bug in a macro meant the wrong code was generated, leaving me with an internal error.

edit: See metagn's version in the first response for a smaller example.

import std / macros
macro matchMe*(x: typed): untyped =
  discard x.getTypeImpl 

type
  SomeUnit* = concept x
    matchMe(x)

template `.`*[T: SomeUnit](x: T): untyped =  discard

proc foo*() = discard

type
  ElementRT* = object
  Element*[Z: static int] = ElementRT # this version is needed, even though we don't use it

let ar = ElementRT()
discard ar.mutate(foo())

Nim Version

Nim Compiler Version 1.7.3 [Linux: amd64]
Compiled at 2022-12-02
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 10b6e4f
active boot switches: -d:release -d:danger

Current Output

SIGSEGV: Illegal storage access. (Attempt to read from nil?)
zsh: segmentation fault  nim c /t/bug3

Expected Output

Something along the lines of: Error: expression 'foo()' has no type (or is ambiguous)

Possible Solution

Running koch temp on it yields:

/home/basti/src/nim/nim_git_repo/compiler/nim.nim(153) nim
/home/basti/src/nim/nim_git_repo/compiler/nim.nim(108) handleCmdLine
/home/basti/src/nim/nim_git_repo/compiler/main.nim(285) mainCommand
/home/basti/src/nim/nim_git_repo/compiler/main.nim(254) compileToBackend
/home/basti/src/nim/nim_git_repo/compiler/main.nim(110) commandCompileToC
/home/basti/src/nim/nim_git_repo/compiler/modules.nim(136) compileProject
/home/basti/src/nim/nim_git_repo/compiler/modules.nim(56) compileModule
/home/basti/src/nim/nim_git_repo/compiler/passes.nim(186) processModule
/home/basti/src/nim/nim_git_repo/compiler/passes.nim(79) processTopLevelStmt
/home/basti/src/nim/nim_git_repo/compiler/sem.nim(783) myProcess
/home/basti/src/nim/nim_git_repo/compiler/sem.nim(751) semStmtAndGenerateGenerics
/home/basti/src/nim/nim_git_repo/compiler/semstmts.nim(2520) semStmt
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(1090) semExprNoType
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(3122) semExpr
/home/basti/src/nim/nim_git_repo/lib/system.nim(910) semStmtList
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(3128) semExpr
/home/basti/src/nim/nim_git_repo/compiler/semstmts.nim(45) semDiscard
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(87) semExprWithType
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(71) semExprCheck
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(3036) semExpr
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(983) semIndirectOp
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(3028) semExpr
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(1071) semDirectOp
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(908) semOverloadedCallAnalyseEffects
/home/basti/src/nim/nim_git_repo/compiler/semcall.nim(574) semOverloadedCall
/home/basti/src/nim/nim_git_repo/compiler/semcall.nim(386) resolveOverloads
/home/basti/src/nim/nim_git_repo/compiler/semcall.nim(95) pickBestCandidate
/home/basti/src/nim/nim_git_repo/compiler/sigmatch.nim(2586) matches
/home/basti/src/nim/nim_git_repo/compiler/sigmatch.nim(2520) matchesAux
/home/basti/src/nim/nim_git_repo/compiler/sigmatch.nim(2227) paramTypesMatch
/home/basti/src/nim/nim_git_repo/compiler/sigmatch.nim(2068) paramTypesMatchAux
/home/basti/src/nim/nim_git_repo/compiler/sigmatch.nim(1720) typeRel
/home/basti/src/nim/nim_git_repo/compiler/sigmatch.nim(1644) typeRel
/home/basti/src/nim/nim_git_repo/compiler/sigmatch.nim(752) matchUserTypeClass
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(2209) tryExpr
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(3122) semExpr
/home/basti/src/nim/nim_git_repo/lib/system.nim(910) semStmtList
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(3034) semExpr
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(1072) semDirectOp
/home/basti/src/nim/nim_git_repo/compiler/semexprs.nim(953) afterCallActions
/home/basti/src/nim/nim_git_repo/compiler/sem.nim(500) semMacroExpr
/home/basti/src/nim/nim_git_repo/compiler/vm.nim(2471) evalMacroCall
/home/basti/src/nim/nim_git_repo/compiler/vm.nim(1813) rawExecute
/home/basti/src/nim/nim_git_repo/compiler/vmdeps.nim(323) opMapTypeImplToAst
/home/basti/src/nim/nim_git_repo/compiler/vmdeps.nim(150) mapTypeToAstX
/home/basti/src/nim/nim_git_repo/compiler/vmdeps.nim(172) mapTypeToAstX
/home/basti/src/nim/nim_git_repo/lib/system/fatal.nim(54) sysFatal
Error: unhandled exception: field 'sons' is not accessible for type 'TNode' using 'kind = 3' [FieldDefect]
FAILURE

checking the source of the problem indicates the following line (https://github.com/nim-lang/Nim/blob/devel/compiler/vmdeps.nim#L170):

      result.add objectDef[0].copyTree  # copy object pragmas

so it accesses the first child of objectDef, which in this case is invalid. Extending the nkRefTy check above to:

      elif objectDef.kind == nkSym:
        objectDef = objectDef.sym.ast[2]

gives the correct compiler error in this case. But this is just me copying the logic from a few lines above for the case where the AST of the input symbol is itself a symbol. I suppose there is a cleaner solution?

Additional Information

No response

@metagn
Copy link
Collaborator

metagn commented Dec 3, 2022

Minimized? Unsure if this is the same error

import std/macros

macro matchMe(x: typed): untyped =
  discard x.getTypeImpl

type
  ElementRT = object
  Element[Z] = ElementRT # this version is needed, even though we don't use it

let ar = ElementRT()
matchMe(ar)

For some reason this "generic alias impl AST replaces original type impl AST" situation feels familiar but I can't find a related issue.

@Vindaar
Copy link
Contributor Author

Vindaar commented Dec 3, 2022

Haha, after manically reducing the code from having to remove 3 big dependencies to reproduce it and keeping "one bit" from each made me miss the forest for the trees. :) Yeah, your code is the same issue!

metagn added a commit to metagn/Nim that referenced this issue Dec 4, 2022
@metagn metagn mentioned this issue Dec 4, 2022
Araq pushed a commit that referenced this issue Dec 4, 2022
* fix #20996

* hopefully fix
survivorm pushed a commit to survivorm/Nim that referenced this issue Feb 28, 2023
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants