Skip to content

[BUG] Cpp2 types can't model std::assignable_from<SomeType> #277

Closed
@JohelEGP

Description

@JohelEGP

Describe the bug
Cpp2 doesn't, by default, make generated operator='s return value this and return type an lvalue to the type of this.
Doing this manually results in cppfront generating a Cpp1 constructor that returns *this.

To Reproduce
Steps to reproduce the behavior:

  1. Sample code x.cpp2:
myclass : type = {

    operator=: (out this, that) -> _ = {
        return this;
    }
}
  1. Command lines
$ ../root/bin/cppfront -p x.cpp2 
$ ../root/clang/bin/clang++ -std=c++20 -I ../root/include/ x.cpp -o x
  1. Expected result
    The generated constructor doesn't return *this.
  2. Actual result/error
#define CPP2_USE_MODULES         Yes

#include "cpp2util.h"


#line 1 "x.cpp2"
class myclass;

//=== Cpp2 definitions ==========================================================

#line 1 "x.cpp2"
class myclass   {

    public: [[nodiscard]] explicit myclass(myclass const& that) -> auto{
        return (*this); 
    }public: [[nodiscard]] auto operator=(myclass const& that) -> auto{return (*this); }
};

$ ../root/bin/cppfront -p x.cpp2 
x.cpp2... ok (all Cpp2, passes safety checks)

$ ../root/clang/bin/clang++ -std=c++20 -I ../root/include/ x.cpp -o x
error: function with trailing return type must specify return type 'auto', not 'void'
x.cpp2:4:9: error: constructor 'myclass' should not return a value [-Wreturn-type]
        return (*this); 
        ^      ~~~~~~~
2 errors generated.

Additional context
I looked at 482bac8 and 4c52d2d. I found it unfortunate how no generated type of the latter was a model of std::semiregular by default, and generally of std::assignable_from<SomeType> for the former, due to the generated operator='s return type being void.

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