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

When expansion is too eager #2002

Closed
flaviut opened this issue Jan 23, 2015 · 5 comments · Fixed by #22564
Closed

When expansion is too eager #2002

flaviut opened this issue Jan 23, 2015 · 5 comments · Fixed by #22564

Comments

@flaviut
Copy link
Contributor

flaviut commented Jan 23, 2015

import typetraits

proc isNillable(T: typedesc): bool =
  when compiles((let v: T = nil)):
    echo(T.name & " is nillable")
    return true
  else:
    echo(T.name & " is not nillable")
    return false

type
  Foo[T] = object
    when isNillable(T):
      nillable: float
    else:
      notnillable: int

var val1: Foo[ref int]
var val2: Foo[int]

echo val1, val2
T is not nillable
T is not nillable
(notnillable: 0)(notnillable: 0)
@ghost
Copy link

ghost commented Oct 15, 2017

Now crashes the compiler:

gaga.nim(3, 6) Error: internal error: proc has no result symbol
Traceback (most recent call last)
nim.nim(121)             nim
nim.nim(77)              handleCmdLine
main.nim(167)            mainCommand
main.nim(74)             commandCompileToC
modules.nim(240)         compileProject
modules.nim(180)         compileModule
passes.nim(217)          processModule
passes.nim(135)          processTopLevelStmt
cgen.nim(1338)           myProcess
ccgstmts.nim(1130)       genStmts
ccgexprs.nim(2141)       expr
ccgstmts.nim(1130)       genStmts
ccgexprs.nim(2221)       expr
cgen.nim(862)            genProc
cgen.nim(827)            genProcNoForward
cgen.nim(698)            genProcAux
msgs.nim(1048)           internalError
msgs.nim(1022)           liMessage
msgs.nim(871)            handleError
msgs.nim(861)            quit
FAILURE

@ghost ghost added the Compiler Crash label Oct 15, 2017
@zah zah self-assigned this Oct 16, 2017
@andreaferretti
Copy link
Collaborator

New kind of crash:

Error: internal error: (filename: "vmgen.nim", line: 1052, column: 19)
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp c <file>

@andreaferretti
Copy link
Collaborator

Now it compiles again, printing (notnillable: 0)(notnillable: 0)

@krux02
Copy link
Contributor

krux02 commented Apr 29, 2019

I just wanted to see if there is a workaround here, but aparently if I write the proc with just a different syntax it doesn't even compile anymore:

import typetraits

proc isNillable[T](t: typedesc[T]): bool {.compileTime.} =
  when compiles((let v: T = nil)):
    echo(T.name & " is nillable")
    return true
  else:
    echo(T.name & " is not nillable")
    return false

type
  Foo[T] = object
    when isNillable(T):
      nillable: float
    else:
      notnillable: int

var val1: Foo[ref int]
var val2: Foo[int]

echo val1, val2

output:

scratch.nim(49, 20) Error: type mismatch: got <type T>
but expected one of: 
proc isNillable[T](t: typedesc[T]): bool

This might be a serious overload resolution bug. But anyway, there is a workaround. I would try if a union would work here. This of course isn't checked, neither at compile time nor at runtime, but it enables you what you want to do.

import typetraits

proc isNilable[T](t: typedesc[T]): bool {.compileTime.} =
  when compiles((let v: T = nil)):
    echo(T.name & " is nilable")
    return true
  else:
    echo(T.name & " is not nilable")
    return false

type
  Foo[T] = object {.union.}
    nilable: float
    notnilable: int

proc `$`[T](arg: Foo[T]): string =
  when isNilable(T):
    return "(nilable: " & $arg.nilable & ")"
  else:
    return "(notnilable: " & $arg.nilable & ")"

var val1: Foo[ref int]
var val2: Foo[int]

echo val1, val2

output:

ref int is nilable
int is not nilable
Hint:  [Link]
Hint: operation successful (21206 lines compiled; 0.140 sec total; 19.98MiB peakmem; Debug Build) [SuccessX]
Hint: /tmp/scratch  [Exec]
(nilable: 0.0)(notnilable: 0.0)

@metagn
Copy link
Collaborator

metagn commented Aug 27, 2023

In 2.0:

import typetraits

proc isNillable(T: typedesc): bool =
  when compiles((let v: T = nil)):
    echo(T.name & " is nillable")
    return true
  else:
    echo(T.name & " is not nillable")
    return false

type
  Foo[T] = object
    when isNillable(T):
      nillable: float
    else:
      notnillable: int

var val1: Foo[ref int]
var val2: Foo[int]

echo val1, val2
ref int is nillable
int is not nillable

(nillable: 0.0)(notnillable: 0)

metagn added a commit to metagn/Nim that referenced this issue Aug 27, 2023
closes nim-lang#12582, closes nim-lang#19552, closes nim-lang#2465, closes nim-lang#4596, closes nim-lang#15246,
closes nim-lang#12683, closes nim-lang#7889, closes nim-lang#4547, closes nim-lang#12415, closes nim-lang#2002,
closes nim-lang#1771, closes nim-lang#5121

The test for nim-lang#5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.
@Araq Araq closed this as completed in c19fd69 Aug 27, 2023
narimiran pushed a commit that referenced this issue Sep 18, 2023
* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test

(cherry picked from commit c19fd69)
narimiran pushed a commit that referenced this issue Sep 18, 2023
* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test

(cherry picked from commit c19fd69)
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.

6 participants