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

invalid object assignment when converting types #11592

Closed
Arnaz87 opened this issue Jun 25, 2019 · 7 comments
Closed

invalid object assignment when converting types #11592

Arnaz87 opened this issue Jun 25, 2019 · 7 comments

Comments

@Arnaz87
Copy link

Arnaz87 commented Jun 25, 2019

According to the convertible relations section, a child type should be explicitly convertible to it's base type.

Although the code does compile, at runtime it throws an ObjectAssignmentError. It should either not compile at all, or run correctly.

Example

type
  Base = object of RootObj
    name: string
  Child = object of Base
    age: int

var child = Child(name: "foo", age: 42)
var base: Base = Base(child)
echo base

Current Output

test.nim(8)              test
assign.nim(124)          genericAssign
assign.nim(97)           genericAssignAux
system.nim(2918)         sysFatal
Error: unhandled exception: invalid object assignment [ObjectAssignmentError]
Error: execution of an external program failed: '.../test'

Expected Output

(name: "foo")

Additional Information

It was working in a previous release, I don't know how long ago, but at least beginning 2018

$ nim -v
Nim Compiler Version 0.19.6 [Linux: amd64]
Compiled at 2019-05-14
Copyright (c) 2006-2018 by Andreas Rumpf

active boot switches: -d:release -d:nativeStackTrace

Backwards

It doesn't work backwards either, from child to parent. Fails at runtime too rather than at compile. I don't know if this one worked before, but according to the manual it should too, I believe.

type
  Base = object of RootObj
    name: string
  Child = object of Base
    age: int

var base = Base(name: "foo")
var child = Child(base)
echo child
@Arnaz87 Arnaz87 changed the title invalid object assignment when converting to base type invalid object assignment when converting types Jun 25, 2019
@Araq
Copy link
Member

Araq commented Jun 26, 2019

Types can be "convertible" but the conversion can fail at runtime. Which is what happens here and it's correct. Previously it went undetected.

@Araq Araq closed this as completed Jun 26, 2019
@Arnaz87
Copy link
Author

Arnaz87 commented Jun 26, 2019

Why does it fail? It had predictable behaviour: everything in the child that's not part of the parent is dropped, and in the other way, those same fields are set to default values.

How does one know now what kind of conversions are valid and which are not? Since it fails at runtime, it seems it's not a type issue.

@andreaferretti
Copy link
Collaborator

Notice that here only the last line fails

type
  Base = object of RootObj
    name: string
  Child = object of Base
    age: int

proc print(b: Base) =
  echo b.name

let child = Child(name: "foo", age: 42)
print(child) # this is ok
print(Base(child)) # strangely, this is also ok

let b = Base(child) # only when assigning, we have a failure

@Arnaz87
Copy link
Author

Arnaz87 commented Jun 26, 2019

Returning it from a procedure also fails, even if not assigning

proc convert (b: Base): Base = return b
echo convert(child)

@Araq
Copy link
Member

Araq commented Jun 27, 2019

Whenever data would be lost it fails. Consistent with all the other allowed but checked conversions.

@disruptek
Copy link
Contributor

print(Base(child))

How is this not an example of the loss of data?

@Araq
Copy link
Member

Araq commented Jun 30, 2019

Because it's not stored anywhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants