cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Can't use `#include`d declaration in requires clause

Open JohelEGP opened this issue 2 years ago • 2 comments

Title: Can't use #included declaration in requires clause.

Description:

A header include is treated like Cpp1 code and is emitted in phase 2. A forward declaration in phase 1 can't make use of the header's symbols.

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

main.h2:

v: const bool = true;

main.cpp2:

#include "main.h2"

t: <T> type requires v = { }

main: () = { }

Commands:

cppfront main.h2 -o _cppfront/main.h
cppfront main.cpp2 -o _cppfront/main-3.cpp
clang++17 -std=c++2b -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -I . _cppfront/main-3.cpp

Expected result:

A well-formed program.

Actual result and error:

Cpp2 lowered to Cpp1.

_cppfront/main.h:



//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"



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

extern bool const v;

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

bool const v {true}; 

_cppfront/main-3.cpp:



//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"


template<typename T> requires( v )

class t;

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

#include "main.h"

template<typename T> requires( v )

class t {
      public: t() = default;
      public: t(t const&) = delete; /* No 'that' constructor, suppress copy */
      public: auto operator=(t const&) -> void = delete;

};

auto main() -> int;


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


auto main() -> int{}

Output.

build/_cppfront/main-3.cpp:6:32: error: 'v' was not declared in this scope
    6 | template<typename T> requires( v )
      |                                ^

JohelEGP avatar May 25 '23 04:05 JohelEGP

A header include is treated like Cpp1 code and is emitted in phase 2.

Obviously, Cpp1 headers are also affected (can't use included symbols in phase 1).

cppfront also outputs:

main.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)

So it really treats #include "main.h2" as Cpp1, even though it transforms it.

JohelEGP avatar May 25 '23 16:05 JohelEGP

imports are also emitted in Phase 2 "Cpp2 type definitions and function declarations".

JohelEGP avatar Jan 02 '24 11:01 JohelEGP