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

[BUG] Type of this can't use the current type beyond its full name #471

Open
JohelEGP opened this issue May 25, 2023 · 8 comments
Open

[BUG] Type of this can't use the current type beyond its full name #471

JohelEGP opened this issue May 25, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@JohelEGP
Copy link
Contributor

JohelEGP commented May 25, 2023

Title: Type of this can't be a nested type.

Description:

It's common to alias complicated base types.
Cpp2's member declaration order suggests a nested type could be used for a this member.
But it's not generally possible for the lowered Cpp1 to make use of it
(it's directly impossible,
and only for the simple cases could cppfront recursively replace nested types for their initializer).

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

#include <chrono>
my_time: type = {
  this: duration;
  rep: type == cpp2::longdouble;
  period: type == std::ratio<17, 29>;
  duration: type == std::chrono::duration<rep, period>;
  operator=: (out this, d: duration) = { duration = d; }
}
main: () = { }
Commands:
cppfront -clean-cpp1 main.cpp2
clang++17 -std=c++2b -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -I . main.cpp

Expected result:

A well-formed program or a diagnostic by cppfront.

Actual result and error:

Cpp2 lowered to Cpp1.
#include "cpp2util.h"


class my_time;

#include <chrono>
class my_time: public duration {

  public: using rep = cpp2::longdouble;
  public: using period = std::ratio<17,29>;
  public: using duration = std::chrono::duration<rep,period>;
  public: explicit my_time(cpp2::in<duration> d);

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

  my_time::my_time(cpp2::in<duration> d)
                                         : duration{ d }
  {}

auto main() -> int{}
Output.
build/_cppfront/main.cpp:9:23: error: expected class name
class my_time: public duration {
                      ^
@JohelEGP JohelEGP added the bug Something isn't working label May 25, 2023
@realgdman
Copy link

Is this supposed to work? I can't get it to work in cpp1 either. But if aliases/using moved outside, before class, its fine.
https://cpp.godbolt.org/z/cKb38erdo
https://cpp2.godbolt.org/z/xrYe7K9ab

@JohelEGP
Copy link
Contributor Author

If you put a type alias before a this member, it generates bad code:

my_time: type = {
  rep: type == cpp2::longdouble;
  this: duration;
}
class my_time {
  public: using rep = cpp2::longdouble;
  : public duration

The code in the OP is wrong.
Nested types aren't order-independent: https://compiler-explorer.com/z/7761n18Ej.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Jul 6, 2023

The issue is also relevant when the type of a member-as-base uses the type's template parameters.
From commit 7e80110:

t: @struct <T: type> type = { // clang-format on
  // Emits `struct t_i32_as_base { typename T::value_type i32; };`,
  // errors with `‘T’ has not been declared`.
  // i32: T::value_type = ();
  this: T::type = ();
}

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Jul 6, 2023

Title: Type of this member can't be a template parameter of the current type.

Description:

See #478 (comment), which also applies to the type.

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

t: @struct <T: type> type = {
  x: T = 0;
  this: std::monostate = ();
}
main: () = { _ = :t<i32> = (); }
Commands:
cppfront main.cpp2
clang++17 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -I . main.cpp

Expected result: I don't know. Maybe make the emitted base type a template.

Actual result and error:

main.cpp2:7:22: error: unknown type name 'T'
    7 | struct t_x_as_base { T x; };
      |                      ^
1 error generated.
Cpp2 lowered to Cpp1:
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

template<typename T> class t;


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


struct t_x_as_base { T x; };
template<typename T> class t: public t_x_as_base, public std::monostate {

};
auto main() -> int;


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


auto main() -> int{(void) t<cpp2::i32>{}; }

@JohelEGP JohelEGP changed the title [BUG] Type of this can't be a nested type [BUG] Type of this can't use the current type beyond its full name Jul 6, 2023
@JohelEGP
Copy link
Contributor Author

JohelEGP commented Jul 6, 2023

I have changed the issue's title to

[BUG] Type of this can't use the current type beyond its full name

This works (https://cpp2.godbolt.org/z/xzr8G1roe):

t: @struct <T: type> type = {
  this: std::ranges::view_interface<t<T>> = ();
}
main: () = { _ = :t<i32> = (); }

But not attempting to use t as an injected-class-name (https://cpp2.godbolt.org/z/EfEErWMe6):

t: @struct <T: type> type = {
  this: std::ranges::view_interface<t> = ();
}
main: () = { _ = :t<i32> = (); }
main.cpp2:1:66: error: use of class template 't' requires template arguments
    1 | template<typename T> class t: public std::ranges::view_interface<t> {
      |                                                                  ^

That's why I use "full name".

@JohelEGP
Copy link
Contributor Author

Members before bases also can't use template parameters.
With the algorithms from #533, it'd be possible to fix that.
See https://cpp2.godbolt.org/z/vE3zohhfG:

t: @struct <T: type> type = {
  x: T;
  this: T;
}
main: () = { }
struct t_x_as_base { T x; };
template<typename T> class t: public t_x_as_base, public T {

};
main.cpp2:7:22: error: 'T' does not name a type

@JohelEGP
Copy link
Contributor Author

This is similar in theme to #704.
In Cpp1, the expectation is that you can use names in a scope that are declared
(or will be in type scope; limitations apply).
That declaration could be in a previous statement, or lexically earlier in the current statement.
Because Cppfront lowers Cpp2 to Cpp1, the same expectation can fail to apply to Cpp2 source code.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Nov 6, 2023

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

2 participants