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

Dot operators with type mismatch, shows assertion instead of compiler error #17779

Open
jtiai opened this issue Apr 19, 2021 · 2 comments
Open

Comments

@jtiai
Copy link

jtiai commented Apr 19, 2021

The following code doesn't compile and causes unhandled exception:

Example

{.experimental: "dotOperators".}

type
    Flag = enum
        A

    Flags = set[Flag]

template `.=`*(flags: Flags, key: Flag, val: bool) =
    if val: flags.incl key else: flags.excl key

var flags: Flags

flags.A = 123

Current Output

....fatal.nim(53)            sysFatal
Error: unhandled exception: semcall.nim(233, 18) `nArg != nil`  [AssertionDefect]

Expected Output

Compiler provides meanful error message or compiles.

Possible Solution

If flags.A = 123 is written as flags.A = bool(123) code compiles as expected.

$ nim -v
Nim Compiler Version 1.4.6 [Linux: amd64]
# make sure to include the git hash if not using a tagged release
@beef331
Copy link
Collaborator

beef331 commented Apr 20, 2021

As I mentioned in the realtime chat this is not due to the bool, it seems to be a dot operators with type mismatch bug.
This code shouldnt compile either, since you're passing an int as a bool and Nim doesnt implictly convert.
You can do the following and it'll work fine for you.

{.experimental: "dotOperators".}

type
    Flag = enum
        A

    Flags = set[Flag]

template `.=`*(flags: Flags, key: Flag, val: int | bool) =
    if val.bool: flags.incl key else: flags.excl key

var flags: Flags

flags.A = 123

@jtiai jtiai changed the title Template with boolean value causes unhandled exception Dot operators with type mismatch, shows assertion instead of compiler error Apr 22, 2021
@metagn
Copy link
Collaborator

metagn commented Nov 3, 2024

Now gives

(14, 9) Error: undeclared field: 'A=' for type in.Flags [type declared in (7, 5)]

which seems reasonable?

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

No branches or pull requests

3 participants