Skip to content

[BUG] Non-operator= out this function triggers assertion #421

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

Open
JohelEGP opened this issue May 5, 2023 · 1 comment
Open

[BUG] Non-operator= out this function triggers assertion #421

JohelEGP opened this issue May 5, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@JohelEGP
Copy link
Contributor

JohelEGP commented May 5, 2023

Title: Non-operator= out this function triggers assertion.

Minimal reproducer (https://cpp2.godbolt.org/z/KG7rW4an9):

t: type = {
  x: i32 = 0;
  operator=: (out this, y: i32) = x = y;
  set: (out this, y: i32) = this = y;
}
main: () = {
  a: t;
  (out a).set(42);
}

Commands:

cppfront -clean-cpp1 main.cpp2

Expected result:

A diagnostic indicating that
a non-operator= out this function is not supported, and
to write a non-this function instead.

Since calling the function would require member access (and thus an object),
a non-operator= out this function can't be supported.

It might be possible to make it work.
A non-operator= out this function could be lowered to Cpp1 as a friend.
Then UFCS on (out x).f(args...); could call it.

Actual result and error:

cppfront: source/parse.h:2787: bool cpp2::function_type_node::is_constructor() const: Assertion `my_decl->has_name("operator=")' failed.

Other observations:

I came up with this after writing a setter.
I was curious as to what would happen if I mis-typed out instead of inout.

@JohelEGP JohelEGP added the bug Something isn't working label May 5, 2023
@JohelEGP
Copy link
Contributor Author

See also

And out this on a non-operator= function is disabled because the only way for it to be useful would be to allow calling the function on an uninitialized object. I may allow that extension, but it seems a bit novel and I want to see actual motivating use cases down the road before spending time allowing and teaching it.
-- #266 (comment) (extract)

The main difference I mean above is that if I were to allow a non-operator= member function to have an out this parameter specifically, say a member function f(out this), then I could (but don't yet) allow out also on the front of the function call, i.e., a syntax like out myobject.f(). Because a member function has access to the class's private data, such a member function could potentially also choose how to directly construct the individual members of the class (whereas other out parameters can construct the object but only using the provided constructors). Other than that granularity, though, cppfront already allows any function with an out parameter to act as a delegating constructor.(*)
-- #266 (comment) (extract)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant