Skip to content

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

Closed

Description

@JohelEGP

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.

Activity

  1. leejy12 commented on Oct 2, 2023

    @leejy12

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

  2. JohelEGP commented on Oct 2, 2023

    @JohelEGP
    ContributorAuthor

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

    main: () = {
      i :== 1;
      s := i as std::string;
      [[assert: s == "1"]]
    }
    
    Program returned: 0
    
  3. filipsajdak commented on Oct 3, 2023

    @filipsajdak
    Contributor

    It was already fixed in #701

  4. JohelEGP commented on Oct 3, 2023

    @JohelEGP
    ContributorAuthor

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

  5. JohelEGP commented on Oct 4, 2024

    @JohelEGP
    ContributorAuthor

    Now it works in the main branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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