Skip to content

Dereferencing a ref type buggy #11773

@timotheecour

Description

@timotheecour

after #11751 was merged, the behavior of type dereferencing seems buggy

  • the compiler SIGSEGV for when compiles(Foo2[])
  • dereferencing a ref type (ref to a generic) doesn't work (see below: type Goo1d = Goo1c[])
  • some dereferencing behavior seem suspicious; in general we shouldn't conflate dereferencing (only for ref / ptr would make sense IMO) vs accessing generic arguments (for which other apis should be used eg typetraits.genericHead, and extractGeneric ([superseded] new macro for generic reflection: extractGeneric(Foo2[float, string], 0) is float #8554) otherwise it gives ambiguities
  • inconsistent, eg:
doAssert (Bar[]) is string # passes
type Bar2 = Bar[] # Error: illformed AST

Example

type
  MyType = object
    a: int32
    b: int32
    c: int32

  MyRefType = ref MyType

echo sizeof(MyRefType[])
echo MyRefType[]

type Foo2 = object
  x: int

doAssert MyRefType[] is MyType
# echo sizeof(Foo2[]) # SIGSEGV: Illegal storage access
# when compiles(Foo2[]):discard # even checking that gives: SIGSEGV: Illegal storage access

type
  MyType2 = ref object
    a: int32
    b: int32
    c: int32
  MyRefType2 = ref MyType2

type Bar = seq[string]
doAssert (Bar[]) is string # is that ok? there's other ways to access generic parameters
# type Bar2 = Bar[] # Error: illformed AST: Bar[]
# doAssert string is (Bar[]) # Error: illformed AST: Bar[]

doAssert (ref ref string)[][] is string

# these pass, i guess they're ok?
doAssert (static string)[] is string
doAssert string[] is char
# doAssert char is string[] # Error: illformed AST: string[]
doAssert string isnot (ref char)

type Goo1[T] = object
  # x: T
type Goo1b = Goo1[float]
echo Goo1b[] # BUG: prints as 
echo Goo1b[].default
doAssert Goo1b[] is Goo1 # bug: runtime error if you uncomment x: T
# doAssert Goo1 is Goo1b[] # Error: illformed AST: Goo1b[] (same as above cases)

type Goo1c = ref Goo1b
doAssert Goo1c[] is Goo1b
# type Goo1d = Goo1c[] # Error: illformed AST: Goo1c[]; this should definitely work: Goo1c was a `ref X`

Current Output

inline in the example

Possible Solution

make [] only compile for ref T or ptr T
other use cases (eg generic access, including for special seq) should use extractGeneric

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions