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

Compiler crashed, expr(skTemplate); unknown symbol #22100

Open
al6x opened this issue Jun 15, 2023 · 4 comments
Open

Compiler crashed, expr(skTemplate); unknown symbol #22100

al6x opened this issue Jun 15, 2023 · 4 comments
Labels
Compiler Crash Invalid Code Acceptance Everything related to compiler not complaining about invalid code Templates

Comments

@al6x
Copy link

al6x commented Jun 15, 2023

Description

Code below would crash the compiler

import std/[sequtils, tables]

var codes: Table[string, uint16]
template encode_trigram*(s: string): uint16 =
  codes.mget_or_put(s, codes.len.uint16)

echo @[""].map(encode_trigram)

Nim Version

Nim Compiler Version 1.6.12 [MacOSX: amd64]
Compiled at 2023-03-10
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release -d:nimUseLinenoise

Current Output

Error: internal error: expr(skTemplate); unknown symbol

Expected Output

No response

Possible Solution

No response

Additional Information

No response

@metagn
Copy link
Collaborator

metagn commented Jun 20, 2023

encode_trigram has a normal proc type proc (s: string): uint16, the information that it's a template is hidden too deep in the type to check during the type relation.

One might think to disable this, but this shockingly allows some stuff we can't do otherwise:

proc foo(x: static proc (a: int)) =
  x((echo "abc"; 123))
  
template bar(a: int) =
  echo a
  echo a

foo(bar)

Output:

abc
123
abc
123

Maybe we can add a new template type to separate from the normal proc one (just tyProc with tfTemplate like tfIterator) and require its values to be compile-time only (like NimNode in theory, but I don't think there is such an enforcement for NimNode, it just acts like ref object at runtime). Related #17617, nim-lang/RFCs#212

@beef331
Copy link
Collaborator

beef331 commented Jun 20, 2023

I'd say a counter point is:

proc foo(x: static proc (a: int)) =
  echo "hello"
  x(123)
  echo "good bye"
  
template bar(a: int) =
  return

foo(bar)

@metagn
Copy link
Collaborator

metagn commented Jun 20, 2023

proc foo(x: static template (a: int)) =
  echo "hello"
  x(123)
  echo "good bye"
  
template bar(a: int) =
  return

foo(bar)

Is this reasonable?

@beef331
Copy link
Collaborator

beef331 commented Jun 20, 2023

It's more reasonable to me at least, arguably though x: template would suffice since templates are explicitly static. This follows down to iterator map[T, Y](i: iterable[T], fn: proc(x: T): Y): Y working the same way.

@metagn metagn added the Invalid Code Acceptance Everything related to compiler not complaining about invalid code label Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compiler Crash Invalid Code Acceptance Everything related to compiler not complaining about invalid code Templates
Projects
None yet
Development

No branches or pull requests

3 participants