Nim icon indicating copy to clipboard operation
Nim copied to clipboard

`dereferencing pointer to incomplete type` error with gcc 9.4 with statics/`cast`

Open tersec opened this issue 3 years ago • 1 comments

A certain combination of using a cast as part of a template when that template also has a static block cause Nim 1.6.7 and 1.7.1 to generate code that gcc 9.4.0 on Ubuntu 20.04 LTS views as invalid.

Example

type
  A = object
  B = object
  U = proc()

proc m(h: var B) = discard

template n[T, U](x: U): T =
  static: doAssert true
  cast[ptr T](addr x)[]

proc k() =
  var res: A
  m(n[B](res))

proc w(mounter: U) = discard

proc mount(proto: U) = discard
proc v() = mount k

# This is required for failure
w(v)

Current Output

r.nim.c:111:12: error: dereferencing pointer to incomplete type ‘tyObject_B__bAZgQasieRqiVnUTx5wMeg’ {aka ‘struct tyObject_B__bAZgQasieRqiVnUTx5wMeg’}
  111 |  m__r_6((&(*((tyObject_B__bAZgQasieRqiVnUTx5wMeg*) ((&res))))));
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Tested with:

Nim Compiler Version 1.7.1 [Linux: amd64]
Compiled at 2022-08-03
Copyright (c) 2006-2022 by Andreas Rumpf

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

and

Nim Compiler Version 1.6.7 [Linux: amd64]
Compiled at 2022-08-02
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 5f61f1594d0c11caf0fb650e3d3bc32cf8f38890
active boot switches: -d:release

With gcc version:

gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Expected Output

Not a gcc compiler failure.

tersec avatar Aug 03 '22 01:08 tersec

The Nim compiler doesn't generate the struct definition for cast[ptr T](addr x)[]. Instead it only generates a struct forward declaration which causes dereferencing pointer to incomplete type. It seems to be an undefined behaviour for C because the code above works with mingw on Windows.

ringabout avatar Aug 03 '22 08:08 ringabout