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

static(ObjectWithCaseInit(...)) generates invalid code #17571

Open
gabbhack opened this issue Mar 29, 2021 · 1 comment · May be fixed by #24442 or #24441
Open

static(ObjectWithCaseInit(...)) generates invalid code #17571

gabbhack opened this issue Mar 29, 2021 · 1 comment · May be fixed by #24442 or #24441

Comments

@gabbhack
Copy link

Example

type
  MyEnum = enum
    F, S, T
  Foo = object
    case o: MyEnum
    of F:
      f: string
    of S:
      s: string
    of T:
      t: string

echo static(Foo(o: F, f: "foo")).f

https://play.nim-lang.org/#ix=2UsR

Current Output

Expected Output

foo

Additional Information

Generated C

/* section: NIM_merge_TYPES */
struct tyObject_Foo__OxIF3ue9bZBY4jPJ8Gj2BXg {
tyEnum_MyEnum__wtIc4D3WdTYB2od8p7ouOw o;
union{
struct {NimStringV2 f;
} _o_1;
struct {NimStringV2 s;
} _o_2;
struct {NimStringV2 t;
} _o_3;
};
};

/* section: NIM_merge_DATA */
static const struct {
  NI cap; NIM_CHAR data[3+1];
} TM__Ixqz4CXXiRZU9bYUmI9cyI2w_2 = { 3 | NIM_STRLIT_FLAG, "foo" };
static const NimStringV2 TM__Ixqz4CXXiRZU9bYUmI9cyI2w_3 = {3, (NimStrPayload*)&TM__Ixqz4CXXiRZU9bYUmI9cyI2w_2};
static const struct {
  NI cap; NIM_CHAR data[0+1];
} TM__Ixqz4CXXiRZU9bYUmI9cyI2w_4 = { 0 | NIM_STRLIT_FLAG, "" };
static const NimStringV2 TM__Ixqz4CXXiRZU9bYUmI9cyI2w_5 = {0, (NimStrPayload*)&TM__Ixqz4CXXiRZU9bYUmI9cyI2w_4};
static const NimStringV2 TM__Ixqz4CXXiRZU9bYUmI9cyI2w_6 = {0, (NimStrPayload*)&TM__Ixqz4CXXiRZU9bYUmI9cyI2w_4};

/* main */
tyArray__nHXaesL0DJZHyVS07ARPRA T1_;
tyObject_Foo__OxIF3ue9bZBY4jPJ8Gj2BXg T2_;
nimZeroMem((void*)(&T2_), sizeof(tyObject_Foo__OxIF3ue9bZBY4jPJ8Gj2BXg));
T2_.o = ((tyEnum_MyEnum__wtIc4D3WdTYB2od8p7ouOw) 0);
T2_._o_1.f = TM__Ixqz4CXXiRZU9bYUmI9cyI2w_3;
T2_._o_2.s = TM__Ixqz4CXXiRZU9bYUmI9cyI2w_5;
T2_._o_3.t = TM__Ixqz4CXXiRZU9bYUmI9cyI2w_6;
...
T1_[0] = T2_._o_1.f;
echoBinSafe(T1_, 1);
$ nim -v
Nim Compiler Version 1.4.4 [Windows: amd64]
Compiled at 2021-02-23
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release
@metagn
Copy link
Collaborator

metagn commented Nov 15, 2024

static(Foo(o: F, f: "foo")) expands to

ObjConstr
  Empty
  ExprColonExpr
    Sym "o"
    IntLit 0
  ExprColonExpr
    Sym "f"
    StrLit "foo"
  ExprColonExpr
    Sym "s"
    StrLit ""
  ExprColonExpr
    Sym "t"
    StrLit ""

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