nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.47k stars 1.47k forks source link

`type is int` crashes the compiler #22850

Closed ggb-sw closed 1 month ago

ggb-sw commented 11 months ago

Description

#!/usr/bin/env -S nim c

# Nim Compiler Version 2.0.0 [Linux: amd64]

template isNotNumber(num: type) : bool =
    not(type is SomeNumber)

echo "Type: ", type("abc"), " isNotNumber? ", type("abc").isNotNumber
echo "Type: ", type(20), " isNotNumber? ", type(20).isNotNumber

Nim Version

Nim Compiler Version 2.0.0 [Linux: amd64] Compiled at 2023-08-01 Copyright (c) 2006-2023 by Andreas Rumpf

git hash: a488067a4130f029000be4550a0fb1b39e0e9e7c active boot switches: -d:release

Current Output

Hint: used config file '/home/ggb/.choosenim/toolchains/nim-2.0.0/config/nim.cfg' [Conf]
Hint: used config file '/home/ggb/.choosenim/toolchains/nim-2.0.0/config/config.nims' [Conf]
Hint: used config file '/home/ggb/Temp/nim/tests/config.nims' [Conf]
......................................................................
/home/ggb/Temp/nim/tests/test5.nim(6, 14) Error: internal error: genMagicExpr: mIs
No stack traceback available
To create a stacktrace, rerun compilation with './koch temp c <file>', see https://nim-lang.github.io/Nim/intern.html#debugging-the-c

Expected Output

No response

Possible Solution

No response

Additional Information

No response

ggb-sw commented 11 months ago

OK, I realise what I did wrong (mistyped type where I meant to type num), but nonetheless it should give me a compilation error and not a compiler crash.

metagn commented 11 months ago

Minimal: echo type is int

Not necessarily invalid code, type is still a type.

metagn commented 11 months ago

Probably relevant:

echo typeof(type)
None

Likely the expression type has nil type.

metagn commented 9 months ago

Culprit:

https://github.com/nim-lang/Nim/blob/080a0723369bbfc2f4bdc1aaa2b0717d467b84aa/compiler/semexprs.nim#L558

When does an expression have the type typedesc[None] while also not being equal to just type/typedesc?