Skip to content

[BUG] @basic_value default constructor is not implicit #553

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

Closed
JohelEGP opened this issue Jul 20, 2023 · 3 comments
Closed

[BUG] @basic_value default constructor is not implicit #553

JohelEGP opened this issue Jul 20, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@JohelEGP
Copy link
Contributor

Title: @basic_value default constructor is not implicit.

Description:

We expect a value to be assignable from an empty argument list, like x = ().

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

t: @basic_value type = { }
u: type = {
  a: t = ();
  operator=: (out this, x: i32) = _ = x;
}
main: () = { }
Commands:
cppfront main.cpp2
clang++17 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -I . main.cpp

Expected result: public: t();

Actual result and error: public: explicit t();

Cpp2 lowered to Cpp1:
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

class t;
class u;
  

//=== Cpp2 type definitions and function declarations ===========================

class t {
public: t(t const& that);

public: auto operator=(t const& that) -> t& ;
public: t(t&& that) noexcept;
public: auto operator=(t&& that) noexcept -> t& ;
public: explicit t();

};
class u {
  private: t a {}; 
  public: explicit u(cpp2::in<cpp2::i32> x);
  public: auto operator=(cpp2::in<cpp2::i32> x) -> u& ;

  public: u(u const&) = delete; /* No 'that' constructor, suppress copy */
  public: auto operator=(u const&) -> void = delete;
};
auto main() -> int;


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



t::t(t const& that){}
auto t::operator=(t const& that) -> t& {
                                return *this;}
t::t(t&& that) noexcept{}
auto t::operator=(t&& that) noexcept -> t& {
                                return *this;}
t::t(){}
  u::u(cpp2::in<cpp2::i32> x) { (void) x;  }
  auto u::operator=(cpp2::in<cpp2::i32> x) -> u&  { 
                                  a = {};
  (void) x;
                                  return *this;
   }

auto main() -> int{}
Output:
main.cpp2:5:37: error: no viable overloaded '='
    5 |                                   a = {};
      |                                   ~ ^ ~~
@JohelEGP JohelEGP added the bug Something isn't working label Jul 20, 2023
@JohelEGP

This comment was marked as off-topic.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Nov 14, 2023

See https://cpp2.godbolt.org/z/8fE3sGxvn:

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Jan 2, 2024

But aggregates of basic_values with the generated default constructor
can't be value initialized or initialized from an empty list (https://cpp2.godbolt.org/z/f76WbYv4P):

t: @basic_value type = { }
u: @struct type = {
  a: t;
}
main: () = {
  _ = :u = ();
}
main.cpp2:6:23: error: chosen constructor is explicit in copy-initialization
    6 |   static_cast<void>(u{});
      |                       ^
main.cpp2:9:4: note: explicit constructor declared here
    9 | t::t(){}
      |    ^
main.cpp2:3:13: note: in implicit initialization of field 'a' with omitted initializer
    3 |   public: t a; 
      |             ^
1 error generated.

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