https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126989

            Bug ID: 126989
           Summary: Garbage diagnostic of (seemingly) valid constant
                    evaluation
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nikolasklauser at berlin dot de
  Target Milestone: ---

```
struct tree_node_base;

struct tree_end_node {
  tree_node_base* __left_;
};

struct min_pointer {
  tree_end_node *ptr_;

  constexpr tree_end_node *get() { return ptr_; }
};

struct tree_node_base : tree_end_node {
  min_pointer __parent_;
};

struct __tree {
  min_pointer __begin_node_;
  tree_end_node __end_node_;

  constexpr __tree() {
    __begin_node_.ptr_ = __end_node_.__left_ = new tree_node_base;
  }

  constexpr __tree(__tree &&__t) : __begin_node_(__t.__begin_node_),
__end_node_(__t.__end_node_) {
    __end_node_.__left_->__parent_.ptr_ = &__end_node_;
  }
};

constexpr __tree make() {
  __tree s;
  return s;
}

constexpr bool test() {
  auto s = make();
  tree_node_base* np = static_cast<tree_node_base*>(s.__begin_node_.ptr_);
  (bool)np->__parent_.get()->__left_;
  delete np;
  return true;
}
static_assert(test());
```

produces the beautiful diagnostic
```
<source>:43:19: error: non-constant condition for static assertion
   43 | static_assert(test());
      |               ~~~~^~
<source>:43:19:   in 'constexpr' expansion of 'test()'
<source>:39:30: error: ''result_decl' not supported by dump_expr<expression
error>' is not a constant expression
   39 |   (bool)np->__parent_.get()->__left_;
      |         ~~~~~~~~~~~~~~~~~~~~~^~~~~~~
```

I'm not quite sure whether the above code is actually valid. Clang accepts it
while MSVC diagnoses `s` as not being usable in a constant expression. Either
way, the diagnostic is clearly garbage and should be fixed. If people find that
this is actually invalid code there needs to be a bug filed against Clang and
otherwise against MSVC.

This has been reduced from a libc++ test case.
  • [Bug c++/126989] New: Garbage... nikolasklauser at berlin dot de via Gcc-bugs

Reply via email to