Skip to content

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

Open

Description

@JohelEGP

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.

Activity

  1. JohelEGP commented on May 22, 2023

    @JohelEGP
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions