c3c icon indicating copy to clipboard operation
c3c copied to clipboard

(bug?) Unexpected behaviour when using global const strings as enum associated values.

Open Falkgaard opened this issue 1 year ago • 1 comments

Regular case (works fine):

import std::io;

enum Example : int (String str) {
   FOO = { "foo" },
}

fn void main() {
   io::printfn( "%s", Example.FOO.str ); // prints `foo`
}

But the following:

import std::io;

const FOO_STR = "foo";

enum Example : int (String str) {
   FOO = { FOO_STR },
}

fn void main() {
   io::printfn( "%s", Example.FOO.str ); // error!
}

will print something like:

/usr/bin/ld: build/tmp/app.o:(.data.rel.ro.Example$str[Example$str]+0x0): undefined reference to `.tempglobal'

Currently it seems that the expected behaviour can be achieved by explicitly defining the type of the const thusly:

const char[] FOO_STR = "foo";

Falkgaard avatar Aug 05 '24 20:08 Falkgaard

This should now work.

lerno avatar Aug 05 '24 22:08 lerno

Can you check so we can close it?

lerno avatar Sep 14 '24 14:09 lerno

@lerno I tried this example and it works. However, the syntax is a bit off here and not sure how it worked (unless it was a typo). A single element associated enum cannot use {} syntax, and must be assigned directly.

Can confirm this works in the latest. This is the example:

import std::io;

const FOO_STR = "foo";

enum Example : int (String str) {
   FOO = FOO_STR,
}

fn int main() {
	io::printfn("%s", Example.FOO.str); // foo
	return 0;
}

Caleb-o avatar Sep 16 '24 07:09 Caleb-o