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] Support _ unnamed template parameter. #812

Closed
JohelEGP opened this issue Nov 9, 2023 · 5 comments
Closed

[BUG] Support _ unnamed template parameter. #812

JohelEGP opened this issue Nov 9, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@JohelEGP
Copy link
Contributor

JohelEGP commented Nov 9, 2023

Title: Support _ unnamed template parameter.

Description:

Commit 6ce2643 added support only for function parameters.

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

f: <_, _> (_, _) = { }
main: () = { }
Commands:
cppfront main.cpp2
clang++18 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result -I . main.cpp

Expected result:

The template parameters to be named similar to the function parameters.

template<typename tparam1, typename tparam2> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;

Actual result and error:

template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
Cpp2 lowered to Cpp1:
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"



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

template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
auto main() -> int;


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

template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void{}
auto main() -> int{}
Output:
build/main.cpp:12:31: error: declaration of '_' shadows template parameter
   12 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
      |                               ^
build/main.cpp:12:19: note: template parameter is declared here
   12 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
      |                   ^
build/main.cpp:18:31: error: declaration of '_' shadows template parameter
   18 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void{}
      |                               ^
build/main.cpp:18:19: note: template parameter is declared here
   18 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void{}
      |                   ^
2 errors generated.

See also:

@JohelEGP JohelEGP added the bug Something isn't working label Nov 9, 2023
@gregmarr
Copy link
Contributor

gregmarr commented Nov 9, 2023

I'm curious what the use case is for unused template parameters. Is it for specializing an existing template?

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Nov 9, 2023

Compilers don't seem to warn on these.
But I have to invent names,
and maybe I'll run out of T, U, V, W, ???.
Of course, this mostly happens in tests.

@gregmarr
Copy link
Contributor

gregmarr commented Nov 9, 2023

Okay, thanks.

@hsutter
Copy link
Owner

hsutter commented Nov 10, 2023

Thanks!

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Nov 24, 2023

This is now #877.

Repeat of #877

It doesn't work for NTTPs: https://cpp2.godbolt.org/z/rdb6dbvv1.
Also, <_...: _> doesn't lower to a parameter pack declaration.

t: @struct <_...: _> type = {
  f: <_: int> () -> i32 = 0;
}
main: () = { }
template<auto _> class t {
  public: template<int _> [[nodiscard]] static auto f() -> cpp2::i32;
};
main.cpp2:2:24: error: declaration of '_' shadows template parameter
    2 |   public: template<int _> [[nodiscard]] static auto f() -> cpp2::i32;
      |                        ^
main.cpp2:1:15: note: template parameter is declared here
    1 | template<auto _> class t {
      |               ^

zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
Also require an `operator=` second parameter of the same type to be named `that` - see hsutter#475 discussion thread
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

3 participants