nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
279 stars 39 forks source link

`distinct object` type definition crashes the compiler #1434

Open zerbina opened 3 months ago

zerbina commented 3 months ago

Example

type Type = distinct object

var x = default(Type)

Actual Output

The compiler crashes:

SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

A proper compiler error about distinct object not being a valid type.

Additional Information

The crash is due to the object part resulting in an incomplete tyObject type instance. Among other things, it's missing a symbol, violating the invariant of tyObject always having a symbol attached, thus crashing the compiler once the symbol is attempted to be accessed.

There are two problems here:

  1. object as it's used here doesn't construct a tyObject type, but instead names the built-in object type-class (i.e., (BuiltInTypeClass (Object))). The logic in semtypes.semObjectNode doesn't handle this properly.

  2. distinct T where T is a type-class is only valid in a routine parameter or generic parameter position, but there's nothing enforcing this.

References

The issue was discovered with the cps test suite and reported by @alaviss on Matrix.

zerbina commented 3 months ago

It's not yet clear what distinct object in a type definition should mean. My preference is that it should mean the same as in a usage context (i.e., type-class), whereas @alaviss argues that it should be a shorthand for object type definition + distinct type construction (similar to how ref object and ptr object work).

The discussion so far can be found here.