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

Invalid consteval constructor invocation does not cause an error #51593

Closed
stbergmann opened this issue Oct 21, 2021 · 6 comments
Closed

Invalid consteval constructor invocation does not cause an error #51593

stbergmann opened this issue Oct 21, 2021 · 6 comments
Assignees
Labels
accepts-invalid bugzilla Issues migrated from bugzilla c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party consteval C++20 consteval

Comments

@stbergmann
Copy link
Collaborator

Bugzilla Link 52251
Version trunk
OS Linux
CC @AaronBallman,@zygoloid

Extended Description

At least with Clang 13 and recent Clang 14 trunk,

$ cat test.cc
struct S {
consteval S() {}
int a;
};
S s2;

$ clang++ -std=c++20 -fsyntax-only test.cc

erroneously succeeds, while e.g. with GCC 11 it fails with

$ g++ -std=c++20 -fsyntax-only test.cc
test.cc:5:3: error: ‘S()’ is not a constant expression
5 | S s2;
| ^~
test.cc:5:3: error: ‘s2.S::S()’ is not a constant expression because it refers to an incompletely initialized variable

(I think this is different from bug 51560, as the initialization of s2 here asks for default-initialization via the S() constructor.)

@AaronBallman
Copy link
Collaborator

This is an issue. We currently fail to check immediate invocations of constructors in other interesting ways as well:

template <typename Ty>
struct S {
  consteval S() = default;
  Ty Val;
};

struct foo {
  foo();
};

S<int> s1; // Correctly accepted
S<foo> s2; // Incorrectly accepted

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@Quuxplusone Quuxplusone added accepts-invalid c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category labels Jan 17, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 17, 2022

@llvm/issue-subscribers-c-20

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 17, 2022

@llvm/issue-subscribers-clang-frontend

@llvmbot llvmbot added the confirmed Verified by a second party label Jan 26, 2022
@AaronBallman
Copy link
Collaborator

I've been looking into this issue pretty deeply lately and have some new thoughts.

struct S {
  consteval S() {}
  int a;
};
S s2;

After the adoption of P1331R2 in C++20, this code becomes valid because static initialization kicks in before constant evaluation does, so s2 has its members initialized per http://eel.is/c++draft/basic.start.static#1, https://eel.is/c++draft/basic.start.static#2.sentence-2, and http://eel.is/c++draft/dcl.init#general-6.2. So I think GCC is incorrect to diagnose that code and Clang is correct to accept it.

However, the example I gave in my comment is still code that should not be accepted in the S<foo> case because that calls a constructor which is not constant evaluatable (foo::foo()).

@AaronBallman
Copy link
Collaborator

That said, I think the fix for the second case will end up breaking the first case so that we wind up diagnosing in Clang the same as GCC, ICC, and MSCV all currently diagnose. One of the problems we have is that we do not add an expression evaluation context for the translation unit itself, so we don't issue any constant expression diagnostics for globals currently. Another problem is that we don't seem to model static initialization as being zero initialization in the constant expression evaluator, and so we consider the members of a statically initialized structure as being uninitialized.

@AaronBallman
Copy link
Collaborator

1c55f05 addresses part of the issues here.

@usx95 usx95 self-assigned this Aug 8, 2022
@usx95 usx95 closed this as completed in 72ac7ca Aug 12, 2022
@usx95 usx95 added the consteval C++20 consteval label Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepts-invalid bugzilla Issues migrated from bugzilla c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party consteval C++20 consteval
Projects
Status: Done
Development

No branches or pull requests

5 participants
  翻译: