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

Increment incorrectly allowed for bool in unevaluated contexts #47517

Closed
CaseyCarter opened this issue Nov 13, 2020 · 3 comments
Closed

Increment incorrectly allowed for bool in unevaluated contexts #47517

CaseyCarter opened this issue Nov 13, 2020 · 3 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla c++17 c++20 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party

Comments

@CaseyCarter
Copy link
Member

Bugzilla Link 48173
Version trunk
OS All
CC @Quuxplusone,@zygoloid

Extended Description

Compiling this well-formed program:

template <class T>
concept can_increment = requires(T t) {
    ++t;
};

template <class T>
void f() {
    static_assert(requires(T t) { ++t; }); // Incorrectly allowed
}

int main() {
    f<bool>();

    static_assert(!can_increment<bool>); // Incorrectly fails

    bool b = false;
    ++b; // Correctly rejected
}

with "clang++ -std=c++2a" (https://godbolt.org/z/39xM1E) diagnoses:

<source>:14:5: error: static_assert failed due to requirement '!can_increment<bool>'
    static_assert(!can_increment<bool>); // Incorrectly fails
    ^             ~~~~~~~~~~~~~~~~~~~~
<source>:17:5: error: ISO C++17 does not allow incrementing expression of type bool [-Wincrement-bool]
    ++b;
    ^ ~

The compiler knows that the language forbids increment of bools outside a requires-expression, but apparently not within a requires-expression.

@Quuxplusone
Copy link
Contributor

Quuxplusone commented Aug 10, 2021

Relatedly, the ill-formedness of "++bool" is not SFINAE-friendly.

https://godbolt.org/z/on6rhE6nr

template<class T> auto f(T t) -> decltype(++t);
auto f(...) -> void;
void g() {
    f(true);  // Clang wrongly makes this a hard error
}

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@Quuxplusone Quuxplusone changed the title Increment incorrectly allowed for bool in requires-expressions Increment incorrectly allowed for bool in unevaluated contexts Jan 16, 2022
@llvmbot
Copy link
Member

llvmbot commented Jan 16, 2022

@llvm/issue-subscribers-c-17

@llvmbot llvmbot added the confirmed Verified by a second party label Jan 26, 2022
yronglin added a commit that referenced this issue Jun 6, 2023
…C++17

Clang now incorrectly allowed increment of bool in unevaluated contexts, we set `diagnostic::ext_increment_bool` to be SFINAEFailure to fix this issue.

```
template<class T> auto f(T t) -> decltype(++t);
auto f(...) -> void;
void g() {
  f(true);  // Clang wrongly makes this a hard error
}
```

```
template <class T>
concept can_increment = requires(T t) { ++t; };

template <class T> void f() {
  static_assert(requires(T t) { ++t; }); // Incorrectly allowed
}

int main() {
  f<bool>();

  static_assert(!can_increment<bool>); // Incorrectly fails

  bool b = false;
  ++b; // Correctly rejected
}
```
Fix issue: #47517

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D152259
@yronglin
Copy link
Contributor

yronglin commented Jun 6, 2023

Fixed in https://reviews.llvm.org/D152259

@EugeneZelenko EugeneZelenko added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Jun 6, 2023
@yronglin yronglin self-assigned this Jun 6, 2023
@yronglin yronglin closed this as completed Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++17 c++20 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party
Projects
Status: Done
Development

No branches or pull requests

5 participants