hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.54k stars 246 forks source link

[BUG] Parameter direction for template parameter is ignored #425

Closed JohelEGP closed 1 year ago

JohelEGP commented 1 year ago

Title: Parameter direction for template parameter is ignored.

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

a: <in T> type = { }
b: <in V: _> type = { }
main: () = { }

Commands:

cppfront -clean-cpp1 main.cpp2
clang++17 -std=c++2b -stdlib=libc++ -lc++abi -I . main.cpp

Expected result:

For the : type case, a diagnostic. For the : _ case, adherence to the specified parameter direction.

template<typename T> class a; // A Cpp2 diagnostic here.
template<const auto& V> class b;

Actual result and error:

template<typename T> class a;
template<auto V> class b;
Program returned: 0
Cpp2 lowered to Cpp1. ```C++ #include "cpp2util.h" template class a; template class b; template class a { public: a() = default; public: a(a const&) = delete; /* No 'that' constructor, suppress copy */ public: auto operator=(a const&) -> void = delete; }; template class b { public: b() = default; public: b(b const&) = delete; /* No 'that' constructor, suppress copy */ public: auto operator=(b const&) -> void = delete; }; auto main() -> int; auto main() -> int{} ```
realgdman commented 1 year ago

Possibly related: I'm using library (flecs) with API like

ecs.system<Position, const Velocity>()
    .each([] some lambda

And haven't found way to specify that const in template call to system() Which, I believe, cannot be deduced from parameters of lambda

    foo<int>();
    foo<const int>(); //error
    foo<int const>(); //error
    foo<in int>(); //error

https://cpp2.godbolt.org/z/sjMWTq3b7

JohelEGP commented 1 year ago

Seems like a bug: https://cpp2.godbolt.org/z/Tx8e9KfKc. I suggest you open a new issue, since this one is about template parameters, not template arguments.

realgdman commented 1 year ago

Ok, I do later, what I mean by related is, should I be able to put in in <> in that case.

JohelEGP commented 1 year ago

I don't think that can work. In a subexpression, a parameter direction operates on an expression, not a type. cppfront doesn't have enough semantic information for f<in int>() to work.

hsutter commented 1 year ago

Parameter direction for template parameter is ignored.

Yes, this is by design -- I don't know what it would mean.

I should probably emit a diagnostic for it though. Thanks!