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

uncheckedAssign doesn't works with example from manual.rst #19547

Closed
jangko opened this issue Feb 20, 2022 · 1 comment
Closed

uncheckedAssign doesn't works with example from manual.rst #19547

jangko opened this issue Feb 20, 2022 · 1 comment

Comments

@jangko
Copy link
Contributor

jangko commented Feb 20, 2022

uncheckedAssign works with tests\objvariant\treassign.nim but failed with example from manual.rst
Both examples compiled successfully, but the later one throw unhandled exception during runtime.

The difference of the two is treassign.nim using child object, while example from manual.rst using top level object.

Example from manual.rst

type
  TokenKind* = enum
    strLit, intLit
  Token = object
    case kind*: TokenKind
    of strLit:
      s*: string
    of intLit:
      i*: int64
      
proc passToVar(x: var TokenKind) = discard

var t = Token(kind: strLit, s: "abc")

{.cast(uncheckedAssign).}:
  # inside the 'cast' section it is allowed to pass 't.kind' to a 'var T' parameter:
  passToVar(t.kind)

  # inside the 'cast' section it is allowed to set field 's' even though the
  # constructed 'kind' field has an unknown value:
  t = Token(kind: t.kind, s: "abc")

  # inside the 'cast' section it is allowed to assign to the 't.kind' field directly:
  t.kind = intLit

Current Output

Compile with

$> nim c -r uc
F:\projects\bug\uc.nim(24) uc
F:\projects\nim\lib\system\assign.nim(296) FieldDiscriminantCheck
F:\projects\nim\lib\system\fatal.nim(53) sysFatal
Error: unhandled exception: assignment to discriminant changes object branch; compile with -d:nimOldCaseObjects for a transition period [FieldDefect]
Error: execution of an external program failed: 'F:\projects\bug\uc.exe '

Expected Output

no error/exception
$ nim -v
Nim Compiler Version 1.7.1 [Windows: amd64]
Compiled at 2022-02-10
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 27e548140b0f51a8dbc6f6094764e02f08ae509e
active boot switches: -d:release
@jangko
Copy link
Contributor Author

jangko commented Feb 20, 2022

closing, duplicate of #19266

@jangko jangko closed this as completed Feb 20, 2022
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

1 participant