Skip to content

[BUG] forward on non-deducible parameter is move #572

Open
@JohelEGP

Description

@JohelEGP

Title: forward on non-deducible parameter is move.

Description:

I inadvertently wrote the function from: <Number> (forward q: quantity<Number>),
thinking that would make q a forwarding parameter for any quantity.
Cppfront recognizes q's type as non-deducible,
and doesn't make q a forwarding parameter.
The end result is that the generated code is the same as using move instead of forward.

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

quantity: @struct <Number: type> type = {
  number: Number;
}
from: <Number> (forward q: quantity<Number>) = _ = q;
main: () = {
  q: quantity<i32> = ();
  q.from();
  _ = q;
}
Commands:
cppfront main.cpp2
clang++17 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -fsanitize=undefined -Werror=unused-result -I . main.cpp

Expected result: A diagnostic indicating that q can't be made a forwarding parameter.

Actual result and error:

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


#include "cpp2util.h"

template<typename Number> class quantity;
  

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

template<typename Number> class quantity {
  public: Number number; 
};
template<typename Number> auto from(quantity<Number>&& q) -> void;
auto main() -> int;
  

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


template<typename Number> auto from(quantity<Number>&& q) -> void { (void) CPP2_FORWARD(q);  }
auto main() -> int{
  quantity<cpp2::i32> q {}; 
  CPP2_UFCS_0(from, q);
  (void) std::move(q);
}
Output:
main.cpp2:7:15: error: no matching function for call to 'from'
    7 |   CPP2_UFCS_0(from, q);
      |               ^~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:727:16: note: expanded from macro 'CPP2_UFCS_0'
  727 |         return FUNCNAME(CPP2_FORWARD(obj)); \
      |                ^~~~~~~~
main.cpp2:7:3: note: in instantiation of function template specialization 'main()::(anonymous class)::operator()<quantity<int> &>' requested here
    7 |   CPP2_UFCS_0(from, q);
      |   ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:729:2: note: expanded from macro 'CPP2_UFCS_0'
  729 | }(PARAM1)
      |  ^
main.cpp2:4:32: note: candidate function [with Number = int] not viable: expects an rvalue for 1st argument
    4 | template<typename Number> auto from(quantity<Number>&& q) -> void { (void) CPP2_FORWARD(q);  }
      |                                ^    ~~~~~~~~~~~~~~~~~~~~

See also:

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