[Bug c++/111272] [13/14 Regression] Truncated error messages with -std=c++23 and -std=c++26

2024-01-12 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111272

--- Comment #8 from Paul Keir  ---
I wonder if a small part of this bug still remains. The code below should
#include , but as it doesn't, we get an error message. The
message now gives all the right information but, with -std=c++23 and
-std=c++26, a part of the text is still truncated. Instead of "declared here"
we get "is not usable as a 'constexpr' function because:". Perhaps it should
also say "declared here" with -std=c++23 and -std=c++26.

At https://godbolt.org/z/6c9hME7hh the code is:

// #include 

constexpr bool init_list()
{
  int total{};
  for (int x : {1, 2, 3})
total += x;
  return total == 6;
}

static_assert(init_list(), "");

The error message is:

: In function 'constexpr bool init_list()':
:6:24: error: deducing from brace-enclosed initializer list requires
'#include '
  +++ |+#include 
1 | // #include 
..
6 |   for (int x : {1, 2, 3})
  |^
: At global scope:
:11:24: error: non-constant condition for static assertion
   11 | static_assert(init_list(), "");
  |   ~^~
:11:24: error: 'constexpr bool init_list()' called in a constant
expression
:3:16: note: 'constexpr bool init_list()' is not usable as a
'constexpr' function because:
3 | constexpr bool init_list()
  |^
Compiler returned: 1

[Bug c++/113306] New: constexpr cast from void* fails with typeid result target

2024-01-10 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113306

Bug ID: 113306
   Summary: constexpr cast from void* fails with typeid result
target
   Product: gcc
   Version: 14.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

The C++26 code below fails to compile with GCC trunk; with Clang it passes. The
error message indicates that the constexpr evaluator uses `const
__fundamental_type_info_pseudo_2` as the type of `typeid(int)`. (See
https://godbolt.org/z/1Mr9ao9hr.)

#include 
#include 

constexpr bool test1()
{
  const void* cvp = (int);

  return static_cast(cvp) != nullptr;
}

int main(int argc, char *argv[])
{
  static_assert(test1());
  assert(test1());
  return 0;
}

The error message is below.

bug3.cpp:13:22:   in ‘constexpr’ expansion of ‘test1()’
bug3.cpp:8:10: error: cast from ‘const void*’ is not allowed in a constant
expression because pointed-to type ‘const __fundamental_type_info_pseudo_2’ is
not similar to ‘const std::type_info’
8 |   return static_cast(cvp) != nullptr;
  |  ^~~
bug3.cpp:6:32: note: pointed-to object declared here
6 |   const void* cvp = (int);

[Bug libstdc++/113294] New: constexpr error from accessing inactive union member in basic_string after move assignment

2024-01-09 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113294

Bug ID: 113294
   Summary: constexpr error from accessing inactive union member
in basic_string after move assignment
   Product: gcc
   Version: 14.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: libstdc++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

The C++20 program below fails to compile. A call to `basic_string::clear()` in
the move assignment operator leads to an access of inactive union member
`_M_local_buf`.

#include 

constexpr bool string_test1()
{
  {
std::string str1; //  = "1"; // also a problem when small (less than 16)
std::string str2 = "1234567890123456"; // 16 chars
str1 = std::move(str2);
  }

  return true;
}

static_assert(string_test1());

Changing `__str._M_data(__str._M_local_buf);` to
`__str._M_data(__str._M_use_local_data());` in basic_string.h seems to fix it,
though I've not had time to test it fully. With this solution, the function
below also remains functional:

constexpr bool string_test2()   
{   
  { 
std::string str = "1234567890123456";  // 16 chars  
str = std::string{"1234567890123456"}; // rvalue assignment 
  } 

  return true;  
}

[Bug c++/111272] [13/14 Regression] Truncated error messages with -std=c++23 and -std=c++26

2023-09-12 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111272

--- Comment #4 from Paul Keir  ---
I believe P2448R2 would only allow the code, without the static_assert.
Explicitly calling `test()`, `Jam::Jam()` and then `Jam::ft()` here would mean
evaluating a non-constexpr function (i.e. `ft`). ft is *constexpr-suitable*,
but still needs the `constexpr` specifier when it is constant evaluated.

[Bug c++/111379] New: comparison between unequal pointers to void should be illegal during constant evaluation

2023-09-11 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111379

Bug ID: 111379
   Summary: comparison between unequal pointers to void should be
illegal during constant evaluation
   Product: gcc
   Version: 14.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

[DR 2526](https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2526)
clarifies that in C++23 comparison between unequal pointers to void has
unspecified result. As unspecified behaviour cannot be executed in a constant
expression, the code below should not compile.

In fact without DR 2526, the comparison would have undefined behaviour, which
also cannot be executed in a constant expression.

constexpr bool compare_void_pointers()
{
  int a[2];
  void* vp1 = [0], * vp2 = [1];
  return vp1 < vp2;
}

static_assert(compare_void_pointers());

[Bug c++/111272] [13/14 Regression] Truncated error messages with -std=c++23 and -std=c++26

2023-09-01 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111272

--- Comment #2 from Paul Keir  ---
Thanks. The `-Winvalid-constexpr` mentioned there is a helpful workaround.

[Bug c++/111272] New: Truncated error messages with -std=c++23 and -std=c++26

2023-09-01 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111272

Bug ID: 111272
   Summary: Truncated error messages with -std=c++23 and
-std=c++26
   Product: gcc
   Version: 14.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

The C++14 code below requires the `constexpr` specifier on the `ft` member
function. Compiling this with `-std=c++14`, `-std=c++17` or `-std=c++20` will
provide a helpful error message which identifies the need to add the
`constexpr` specifier to `ft`.

However, if `-std=c++23`, or `-std=c++26` are used, no message indicating the
need for `constexpr` on `ft` is displayed; and the message itself is truncated:
ending only in "...not usable as a 'constexpr' function because:".

struct Jam
{
  // constexpr  // n.b.
  int ft() { return 42; }

  constexpr
  Jam() { ft(); }
};

constexpr bool test()
{
  Jam j;
  return true;
}

static_assert(test(), "");

The full truncated error message is:

$ /opt/gcc-latest/bin/g++ -std=c++26 -c truncated-constexpr-error-gcc.cpp 
truncated-constexpr-error-gcc.cpp:16:19: error: non-constant condition for
static assertion
   16 | static_assert(test(), "");
  |   ^~
truncated-constexpr-error-gcc.cpp:16:19:   in ‘constexpr’ expansion of ‘test()’
truncated-constexpr-error-gcc.cpp:12:7: error: ‘constexpr Jam::Jam()’ called in
a constant expression
   12 |   Jam j;
  |   ^
truncated-constexpr-error-gcc.cpp:7:3: note: ‘constexpr Jam::Jam()’ is not
usable as a ‘constexpr’ function because:
7 |   Jam() { ft(); }
  |   ^~~

[Bug c++/70248] constexpr initialization with unspecified equality expression accepted

2023-08-22 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70248

Paul Keir  changed:

   What|Removed |Added

 CC||pkeir at outlook dot com

--- Comment #10 from Paul Keir  ---
Here is another example which GCC accepts, but Clang correctly rejects:

constexpr bool test()
{
  int arr[2]{};
  void *p1 = [0];
  void *p2 = [1];

  return p1 < p2;
}

constexpr bool b = test();

[Bug c++/110714] constexpr lifetime error: base class this pointer

2023-07-18 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110714

--- Comment #3 from Paul Keir  ---
Actually, there's no need here to delete through the base pointer; so this is
perhaps simpler:

struct Base
{
  constexpr Base* get_this() { return this; }
  int x;
};

struct Derived : public Base {};

constexpr bool test()
{
  Derived* pf = new Derived;

  delete static_cast(pf->get_this());

  return true;
}

constexpr bool b = test();

[Bug c++/110714] constexpr lifetime error: base class this pointer

2023-07-18 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110714

--- Comment #2 from Paul Keir  ---
I know. `delete pf` also works. The issue seems to be with the use of the this
pointer within the member function. This is just the MRE - I've come across
this issue twice now in our code base.

[Bug c++/110714] New: constexpr lifetime error: base class this pointer

2023-07-18 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110714

Bug ID: 110714
   Summary: constexpr lifetime error: base class this pointer
   Product: gcc
   Version: 14.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

Compiling the C++20 MRE code below fails:

struct Base
{
  constexpr virtual ~Base() {}
  constexpr Base* get_this() { return this; }
  int x;
};

struct Derived : public Base {};

constexpr bool test()
{
  Derived* pf = new Derived;
  delete pf->get_this();
  return true;
}

constexpr bool b = test();

...with the following error message:

2$ /opt/gcc-latest/bin/g++ -std=c++20 -c ce_base_alloc2.cpp 
ce_base_alloc2.cpp:17:24:   in ‘constexpr’ expansion of ‘test()’
ce_base_alloc2.cpp:13:23:   in ‘constexpr’ expansion of
‘pf->Derived::.Base::get_this()->Base::~Base()’
ce_base_alloc2.cpp:8:8: error: deallocation of storage that was not previously
allocated
8 | struct Derived : public Base {};
  |^~~

I have tried with GCC trunk (14.0.0) and also version 12.2.0.

I suspect that the this pointer in the base class is not tracking the constexpr
dynamic allocation. Clang and MSVC both compile successfully. Clang requires
the virtual destructor.

[Bug c++/106107] New: dynamic_cast fail within constant expression

2022-06-27 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106107

Bug ID: 106107
   Summary: dynamic_cast fail within constant expression
   Product: gcc
   Version: 13.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

Created attachment 53210
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53210=edit
The code described in the bug report.

The dynamic_cast, evaluated within the static_assert, in the code below, fails
(GCC trunk); while the runtime assert passes. Both pass with Clang and MSVC.

I have in place a workaround for the lack of an implicit destructor in Derived
due to bug 93413 - I am not sure if this is independent of that 93413.

#include 

struct Base {
  virtual constexpr ~Base() {}
};

struct Derived : public Base
{
#if !defined(__clang__)
  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93413
  constexpr virtual ~Derived() {};
#endif
};

constexpr bool test()
{
  Base* bp = new Derived;
  Derived* dp = dynamic_cast(bp);
  bool b = dp != nullptr;
  delete bp;
  return b;
}

int main(int argc, char *argv[])
{
  static_assert(test());

  assert(test());

  return 0;
}

[Bug c++/102406] New: ICE on array declaration sized by a struct member

2021-09-19 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102406

Bug ID: 102406
   Summary: ICE on array declaration sized by a struct member
   Product: gcc
   Version: 12.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

Created attachment 51483
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51483=edit
The code described in the bug report.

Compiling the code below (also attached) produces an internal compiler error.

struct Foo { unsigned x_; };

int main(int argc, char *argv[])
{
  const Foo a{3};
  float f[2][a.x_]{};
  return 0;
}

The full error is below:

internal compiler error: in make_decl_rtl, at varasm.c:1446
6 |   float f[2][a.x_]{};
  | ^
0x86bd48 make_decl_rtl(tree_node*)
../../gcc/varasm.c:1446
0xda0967 expand_expr_real_1(tree_node*, rtx_def*, machine_mode,
expand_modifier, rtx_def**, bool)
../../gcc/expr.c:10457
0xdb274b expand_constructor
../../gcc/expr.c:8491
0xda015a expand_expr_real_1(tree_node*, rtx_def*, machine_mode,
expand_modifier, rtx_def**, bool)
../../gcc/expr.c:10749
0xdac0ec store_expr(tree_node*, rtx_def*, int, bool, bool)
../../gcc/expr.c:5994
0xdb1ab7 store_field
../../gcc/expr.c:7491
0xdae17d expand_assignment(tree_node*, tree_node*, bool)
../../gcc/expr.c:5589
0xc76a37 expand_gimple_stmt_1
../../gcc/cfgexpand.c:3945
0xc76a37 expand_gimple_stmt
../../gcc/cfgexpand.c:4043
0xc7cf03 expand_gimple_basic_block
../../gcc/cfgexpand.c:6085
0xc7ecb7 execute
../../gcc/cfgexpand.c:6811

[Bug c++/100138] New: ICE with constructor constrained (C++20 Concepts) by parameter pack length

2021-04-18 Thread pkeir at outlook dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100138

Bug ID: 100138
   Summary: ICE with constructor constrained (C++20 Concepts) by
parameter pack length
   Product: gcc
   Version: 11.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: pkeir at outlook dot com
  Target Milestone: ---

Created attachment 50624
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50624=edit
The C++ file producing the ICE

Compiling the code below with `-c -std=c++20` produces an ICE. 

template 
struct Foo
{
  template 
  Foo(Ts... xs) requires(sizeof...(xs)==N) {}
};

void bar()
{
  Foo x{64};
}

As a workaround, using Ts rather than xs within the requires clause avoids the
ICE. The error message is below:

constrained_ctor.hpp: In substitution of ‘template Foo(Ts
...)-> Foo requires  sizeof ... ((Foo::__ct ::xs ...)) == N [with int N =
1; Ts = {int}]’:
constrained_ctor.hpp:10:11:   required from here
constrained_ctor.hpp:5:26: internal compiler error: Segmentation fault
5 |   Foo(Ts... xs) requires(sizeof...(xs)==N) {}
  |  ^
0x10d0e8f crash_signal
../../gcc/toplev.c:327
0x950d49 hash_table, tree_node*>
>::hash_entry, false, xcallocator>::find_slot_with_hash(tree_node* const&,
unsigned int, insert_option)
../../gcc/hash-table.h:963
0xac055d hash_map, tree_node*>
>::put(tree_node* const&, tree_node* const&)
../../gcc/hash-map.h:166
0xac055d register_local_specialization(tree_node*, tree_node*)
../../gcc/cp/pt.c:2004
0xad21e1 gen_elem_of_pack_expansion_instantiation
../../gcc/cp/pt.c:12496
0xad21e1 tsubst_pack_expansion(tree_node*, tree_node*, int, tree_node*)
../../gcc/cp/pt.c:13173
0xac63be tsubst_copy
../../gcc/cp/pt.c:16925
0xac7ca4 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool,
bool)
../../gcc/cp/pt.c:20896
0xac7ad3 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool,
bool)
../../gcc/cp/pt.c:19856
0xad3128 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
../../gcc/cp/pt.c:19144
0x95a455 satisfy_atom
../../gcc/cp/constraint.cc:2984
0x95a455 satisfy_constraint_r
../../gcc/cp/constraint.cc:3047
0x95a779 satisfy_normalized_constraints
../../gcc/cp/constraint.cc:3069
0x957d7e satisfy_declaration_constraints
../../gcc/cp/constraint.cc:3277
0x957d7e constraint_satisfaction_value
../../gcc/cp/constraint.cc:3297
0x95a810 constraints_satisfied_p(tree_node*, tree_node*)
../../gcc/cp/constraint.cc:3334
0xaf98a5 fn_type_unification(tree_node*, tree_node*, tree_node*, tree_node*
const*, unsigned int, tree_node*, unification_kind_t, int, conversion**, bool,
bool)
../../gcc/cp/pt.c:21632
0x9053fd add_template_candidate_real
../../gcc/cp/call.c:3456
0x905eda add_template_candidate
../../gcc/cp/call.c:3541
0x905eda add_candidates
../../gcc/cp/call.c:6031
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See  for instructions.