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] 1 as std::string errors #717

Closed
JohelEGP opened this issue Oct 2, 2023 · 5 comments
Closed

[BUG] 1 as std::string errors #717

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

Comments

@JohelEGP
Copy link
Contributor

JohelEGP commented Oct 2, 2023

Title: 1 as std::string errors.

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

main: () = {
  _ = 1 as std::string;
}
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: Program returned: 0

Actual result and error:

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


#include "cpp2util.h"



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

auto main() -> int;
  

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

auto main() -> int{
  static_cast<void>(cpp2::as_<std::string, 1>());
}
Output:
In file included from /app/build/main.cpp:6:
In file included from /app/cpp2util.h:1:
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1825:13: error: static assertion failed due to requirement 'program_violates_type_safety_guarantee<std::string, int>': No safe 'as' cast available - please check your cast
 1825 |             program_violates_type_safety_guarantee<C, CPP2_TYPEOF(x)>,
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
build/main.cpp:18:27: note: in instantiation of function template specialization 'cpp2::as_<std::string, 1>' requested here
   18 |   static_cast<void>(cpp2::as_<std::string, 1>());
      |                           ^
In file included from /app/build/main.cpp:6:
In file included from /app/cpp2util.h:1:
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1830:12: error: no matching function for call to 'as'
 1830 |     return as<C,x>();
      |            ^~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1104:23: note: candidate template ignored: constraints not satisfied [with C = std::string, x = 1]
 1104 | inline constexpr auto as() -> auto
      |                       ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1103:15: note: because 'std::is_arithmetic_v<std::string>' evaluated to false
 1103 |     requires (std::is_arithmetic_v<C> && std::is_arithmetic_v<CPP2_TYPEOF(x)>)
      |               ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1520:16: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1520 | constexpr auto as( X const& x ) -> decltype(auto)
      |                ^   ~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1474:16: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1474 | constexpr auto as( X const& x ) -> T
      |                ^   ~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1406:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1406 | auto as( std::variant<Ts...> const& x ) -> decltype(auto) {
      |      ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1381:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1381 | auto as( std::variant<Ts...> & x ) -> decltype(auto) {
      |      ^   ~~~~~~~~~~~~~~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1356:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1356 | auto as( std::variant<Ts...> && x ) -> decltype(auto) {
      |      ^   ~~~~~~~~~~~~~~~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1213:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1213 | auto as( X x ) -> C {
      |      ^   ~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1202:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1202 | auto as( X const& x ) -> C const& {
      |      ^   ~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1196:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1196 | auto as( X& x ) -> C& {
      |      ^   ~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1190:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1190 | auto as( X const& x ) -> C const& {
      |      ^   ~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1184:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1184 | auto as( X& x ) -> C& {
      |      ^   ~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1166:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1166 | auto as( X const& x ) -> auto
      |      ^   ~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1158:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1158 | auto as(X const& x) -> C
      |      ^  ~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1152:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1152 | auto as( X& x ) -> decltype(auto) {
      |      ^   ~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1146:6: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1146 | auto as( X const& x ) -> decltype(auto) {
      |      ^   ~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1128:23: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1128 | inline constexpr auto as(auto const& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> auto
      |                       ^  ~~~~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1114:23: note: candidate function template not viable: requires single argument 'x', but no arguments were provided
 1114 | inline constexpr auto as(auto const& x) -> auto
      |                       ^  ~~~~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1098:6: note: candidate function template not viable: requires 1 argument, but 0 were provided
 1098 | auto as(auto const&) -> auto {
      |      ^  ~~~~~~~~~~~
2 errors generated.
@JohelEGP JohelEGP added the bug Something isn't working label Oct 2, 2023
@leejy12
Copy link

leejy12 commented Oct 2, 2023

What is the expected value of 1 as std::string?

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 2, 2023

Like this: https://cpp2.godbolt.org/z/E7ebY9GYW.

main: () = {
  i :== 1;
  s := i as std::string;
  [[assert: s == "1"]]
}
Program returned: 0

@filipsajdak
Copy link
Contributor

It was already fixed in #701

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 3, 2023

Then you may add that to its opening comment so that this is automatically closed when that is merged.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Oct 4, 2024

Now it works in the main branch.

@JohelEGP JohelEGP closed this as completed Oct 4, 2024
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