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

Equivalent function definitions fail at C++ compilation #1982

Open
evantypanski opened this issue Mar 3, 2025 · 3 comments · May be fixed by #1991
Open

Equivalent function definitions fail at C++ compilation #1982

evantypanski opened this issue Mar 3, 2025 · 3 comments · May be fixed by #1991
Assignees

Comments

@evantypanski
Copy link
Contributor

Found during #1978

These functions are equivalent and have a body so they should be rejected, instead they fail at C++ compilation:

module Test;

function equivalent(): int32 { return 1; }
function equivalent(): int32 { return 2; }

equivalent();

This is a little more complicated since I believe this should still be allowed:

function equivalent(): void;
function equivalent(): void {}

But a naive implementation just checking if the first name declaration has a body misses this case:

function equivalent(): int32;
function equivalent(): int32 { return 1; }
function equivalent(): int32 { return 2; }

error for reference:

$ spicyc -j test.spicy
/private/var/folders/w6/h7tg594x0yng0_yzvcsn6mv00000gn/T/Test_a9eb71556dd6d426-ede2ac17643f4e9d.cc:33:26: error: redefinition of 'equivalent'
   33 | static auto __hlt::Test::equivalent() -> ::hilti::rt::integer::safe<int32_t> {
      |                          ^
/private/var/folders/w6/h7tg594x0yng0_yzvcsn6mv00000gn/T/Test_a9eb71556dd6d426-ede2ac17643f4e9d.cc:27:26: note: previous definition is here
   27 | static auto __hlt::Test::equivalent() -> ::hilti::rt::integer::safe<int32_t> {
      |                          ^
1 error generated.
[error] spicyc: JIT compilation failed
@rsmmr
Copy link
Member

rsmmr commented Mar 4, 2025

This is a little more complicated since I believe this should still be allowed:

function equivalent(): void;
function equivalent(): void {}

I don't think we actually need to allow this. Spicy doesn't need functions to be declared before usage, i.e., no need for prototypes if the implementation can be found somewhere else. The main use case for a body-less prototype are &cxxname functions. Unless I'm missing something ...

@evantypanski
Copy link
Contributor Author

I don't think we actually need to allow this

Probably not, I assumed since it was valid that there was more use. Does that mean we can just decline any prototypes without &cxxname then?

FWIW, I think this is still necessary for hooks, and is why my initial implementation wasn't in the initial PR for overloading - I ran the tests, saw failures that looked valid, and stopped. But if we just allow equivalent hooks it seems fine :) Like this from try-across-hooks:

declare public hook void f1();

public hook void f1() &priority=5 { hilti::print("Foo: f1"); }

This can probably done pretty easily after or alongside #1985 - I'd prefer a diagnostic on non-hook prototypes without &cxxname too if that seems reasonable

@rsmmr
Copy link
Member

rsmmr commented Mar 5, 2025

Probably not, I assumed since it was valid that there was more use. Does that mean we can just decline any prototypes without &cxxname then?

Yes, I think so (assuming there's no other attribute I'm missing right now that would need prototypes too; but as long as the test suite passes we should be fine).

FWIW, I think this is still necessary for hooks,

Yeah, right, because hooks may have no implementation at all.

But if we just allow equivalent hooks it seems fine :)

+1

@evantypanski evantypanski self-assigned this Mar 6, 2025
evantypanski added a commit that referenced this issue Mar 6, 2025
@evantypanski evantypanski linked a pull request Mar 6, 2025 that will close this issue
evantypanski added a commit that referenced this issue Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants