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.54k stars 1.47k forks source link

`if nil == X` crashes the compiler #23534

Open PMunch opened 5 months ago

PMunch commented 5 months ago

Description

Fairly simple, passed some C code through c2nim and it spat out some if nil == X statements. These statements unfortunately crash the compiler with Error: internal error: getTypeDescAux(tyGenericParam) so figuring out what was wrong was a bit annoying. It works if typeof(X) is pointer, but crashes if it is for example ptr int.

Nim Version

Nim Compiler Version 2.0.4 [Linux: amd64] Compiled at 2024-03-28 Copyright (c) 2006-2023 by Andreas Rumpf

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

Current Output

Error: internal error: getTypeDescAux(tyGenericParam)

Expected Output

Some kind of error message explaining that it can't infer the type.

Possible Solution

No response

Additional Information

var x: ptr int

if nil == x: # Crashes the compiler
  echo "X is nil!"
metagn commented 5 months ago

Same issue as #16148 (though this is only realized after 15 comments), related is #15154 but this is a simpler case of just proc foo[T](a, b: ptr T).

If nil is matching ptr T and resolving T to itself (i.e. T = T vs T = nil) or stopping the resolution of T in some other simple way then this should be fixable (isGeneric?), but if T is being resolved to int and the type of the nil parameter is not populated after the fact then this is a limitation and we need to detect if we assign an unresolved type to a position that won't be populated with resolved parameters later.

Very easy bandaid is:

proc `==`[T](a: typeof(nil) | typeof(nil), b: T) {.magic: "WhateverMagicPointerEqualityUses".}

but in general typeof(nil) | typeof(nil) is a hack and there is a chance this is easy to fix in the compiler.