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

Problems using methodmap as a default argument #1006

Open
Fyren opened this issue Nov 29, 2024 · 2 comments
Open

Problems using methodmap as a default argument #1006

Fyren opened this issue Nov 29, 2024 · 2 comments

Comments

@Fyren
Copy link
Contributor

Fyren commented Nov 29, 2024

methodmap MM {
        public MM(int i) { return view_as<MM>(i); }
}

void f(MM mm = MM(0)) {}

Given this code, both 1.12 and master error, saying the argument default isn't a constant. I guess this is the expected behavior, but maybe could actually work? As a nitpick, the location given by master is off, pointing at the opening paren:

     5 | void f(MM mm = MM(0)) {}
---------------^

However, if we flip around the declarations like so and actually use f:

void f(MM mm = MM(0)) {}

methodmap MM {
        public MM(int i) { return view_as<MM>(i); }
}

public void main() { f(); }

Now 1.12 errors saying f's parameter doesn't have a default value. If we just don't use f at all, then it compiles. I didn't look into what ends up in the binary.

However, master segfaults here:

if (fun->return_type()->isArray() || fun->return_type()->isEnumStruct()) {

The FunctionDecl for MM is probably wrong as fun->decl_.type.type is null, which is what return_type() returns. I didn't look into what 1.12 is actually doing.

@dvander
Copy link
Member

dvander commented Nov 29, 2024

I'm actually kind of surprised this parses. I think for now I'll just address the segfault, and figure out how to get more advanced default arguments working in 1.13.

@dvander
Copy link
Member

dvander commented Nov 29, 2024

In this case, when binding the default argument, we don't have a return type for "MM(0)" because we haven't bound "MM" yet. This is surprisingly difficult to fix and is part of a class of bugs that I'm hoping to resolve more completely, eventually. I think we'll need to recursively resolve name bindings (something the experimental compiler could do), but I'm a ways off from getting there.

I'll leave the bug open in case a simpler fix pops in mind. I think trying to harden all of Semantics against partial ASTs is a losing battle though.

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

2 participants