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] Can't use #included declaration in requires clause #470

Open
JohelEGP opened this issue May 25, 2023 · 2 comments
Open

[BUG] Can't use #included declaration in requires clause #470

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

Comments

@JohelEGP
Copy link
Contributor

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 JohelEGP added the bug Something isn't working label May 25, 2023
@JohelEGP
Copy link
Contributor Author

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
Copy link
Contributor Author

JohelEGP commented Jan 2, 2024

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

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

1 participant