Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{.pure.} has no effect on objects #10721

Closed
yglukhov opened this issue Feb 21, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@yglukhov
Copy link
Member

commented Feb 21, 2019

Not sure, when this happened, but...

type
  BaseRef {.inheritable, pure.} = ref object
    a: int
  SubRef = ref object of BaseRef

  BasePtr {.inheritable, pure.} = ptr object
    a: int
  SubPtr = ptr object of BasePtr

var r: SubRef
var p: SubPtr
assert(sizeof(r[]) == sizeof(int)) # Fails
assert(sizeof(p[]) == sizeof(int)) # Also fails

Expected result: The objects should not contain the hidden type pointer.

@yglukhov yglukhov added the Regression label Feb 21, 2019

@krux02

This comment has been minimized.

Copy link
Contributor

commented Feb 21, 2019

Well it is not that much of a regression as you might think. There is actually no hidden type pointer. It is just that when I wrote the sizeof implementation I didn't take the pure flag into consideration. This version works.

proc csizeof[T](t: T): int {.importc: "sizeof", nodecl.}


type
  BaseRef {.inheritable, pure.} = ref object
    a: int
  SubRef = ref object of BaseRef

  BasePtr {.inheritable, pure.} = ptr object
    a: int
  SubPtr = ptr object of BasePtr

var r: SubRef
var p: SubPtr

var i: int

assert(csizeof(r[]) == csizeof(i)) # Fails
assert(csizeof(p[]) == csizeof(i)) # Also fails

I guess I have to fix sizeof here.

@krux02 krux02 self-assigned this Feb 21, 2019

@krux02 krux02 changed the title {.pure.} has no effect on objects {.pure.} is not taken into consideration for sizeof Feb 22, 2019

@yglukhov

This comment has been minimized.

Copy link
Member Author

commented Feb 22, 2019

@krux02, I'm afraid that's not the case. Your sample doesn't compile for me with latest nim, but more importantly, here's the relevant C code:

struct tyObject_BaseRefcolonObjectType__R7LMyl8qAV9baINE9cSbZiiQ {
TNimType* m_type;
NI a;
};
@yglukhov

This comment has been minimized.

Copy link
Member Author

commented Feb 22, 2019

Here's a sample that doesn't use sizeof:

type
  BaseRef {.inheritable, pure.} = ref object
    a: int
  SubRef = ref object of BaseRef

  BasePtr {.inheritable, pure.} = ptr object
    a: int
  SubPtr = ptr object of BasePtr

var r: SubRef
var p: SubPtr

r.new()
p = cast[SubPtr](alloc0(50))

cast[ptr int](r)[] = 5
cast[ptr int](p)[] = 5

assert(r.a == 5)
assert(p.a == 5)

@yglukhov yglukhov changed the title {.pure.} is not taken into consideration for sizeof {.pure.} has no effect on objects Feb 22, 2019

@krux02

This comment has been minimized.

Copy link
Contributor

commented Feb 22, 2019

@yglukhov hmm, seems you are right. Interesting though, your original example doesn't even generate the type implementation. I had to change it a bit, so that the type would be generated.

krux02 added a commit to krux02/Nim that referenced this issue Apr 3, 2019

@krux02 krux02 referenced this issue Apr 3, 2019

Merged

Pure ref object #10955

@Araq Araq closed this in 65ee80e Apr 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.