Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[BUG] Malformed lambda call inside inspect lead to compilation without error and silent default value #433

Open

Description

@realgdman

Inside inspect, I have lambda where I forgot to pass argument. Like
:(x) -> _ = {return "int"} (); instead of
:(x) -> _ = {return "int"} (x);
It was accepted by compilers and emitted code, which runs and silently returns ""

To Reproduce
https://cpp2.godbolt.org/z/aerezGKqq

Reproduction code

gettype: (x: _) -> std::string = { 
  return (inspect x -> std::string {
    is int = :(x) -> _ = { std::cout<<x<<":"; return "int";} ();  //forgot to pass argument to lambda
  //is int = :(x) -> _ = { std::cout<<x<<":"; return "int";} (x); //ok, this would output  "13:int, other"

    is _ = "other";
  });
}

main: () = {
  std::cout << gettype(13) << ", " << gettype(false) << std::endl;	
  //outputs ", other" instead of diagnostic
    
  // :(x) -> _ = { std::cout<<x<<":"; return "int";} ();  //correct cpp1 error diagnostic
}

Command line
cppfront/cppfront $1.cpp2 -p
clang++-15 -Icppfront/include $1.cpp -std=c++20 -o $1

Expected result
Diagnostic like cpp1 for line 13:

error: no matching function for call to object of type '(lambda at lambdaparam.cpp2:13:6)'
note: candidate function template not viable: requires single argument 'x', but no arguments were provided

Actual result/error
Code silently compiles at cpp2 and cpp1 and silently runs without calling lambda and returning default value for inspect case, in this case an empty string.

Additional context
I understand this may be alpha version limitation of inspect

Emitted cpp1 code is roughly
if (cpp2::is(__expr)) { if constexpr( requires{V} ) if constexpr( std::is_convertible_v<V,T>) return V; else return T{}; else return T{}; }

Edit: after some investigation, it appears that cpp1
if constexpr( requires{[](auto const& x) -> auto{ ... }();} ) returns false.

Activity

  1. added
    bug
    on May 7, 2023
  2. realgdman commented on May 7, 2023

    @realgdman
    Author

    Assert is good but I'm surprised that cpp1 if constexpr( requires{ eats non-compilable code. Probably this can be addressed with some extra check.

  3. filipsajdak commented on May 8, 2023

    @filipsajdak
    Contributor

    This is more complicated than that. These multiple constexpr ifs are there to eliminate cases when the evaluation of a branch is invalid for a specific inspected value/type.

    I need to check if asserts will work for complex cases. For sure static_asserts were not an option.

  4. realgdman commented on May 8, 2023

    @realgdman
    Author

    (Sorry I haven't tried any fixes)

    Another observation, currently it's impossible to capture in lambda inside inspect

    S: type = {
      operator=: (out this) = {}
      operator=: (out this, that) = {}
      print: (this) = std::cout << "S"; 
    }
    
    main: () -> int = {
      s := S();
        
      l := :() -> int = { (s$ as S).print(); return 1; };  //ok
      l();
        
      tmp := (inspect s -> int {
      //is S = l(); //works
        is S = :() -> int = { (s$ as S).print(); return 1; }(); //error
        is _ = 0; 
      });
    }
    

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

    l can capture s, but exact same lambda inside inspect cannot.
    Same with s&$*
    Cpp1 Error message is
    error: reference to local variable 's' declared in enclosing function 'main'
    As far as I can tell it points at
    if constexpr( requires{[_0 = (&s)]() ....

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions