llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

constant evaluator loses track of most-derived array information when reconstructing `LValue` from `APValue`

Open zygoloid opened this issue 2 months ago • 9 comments

Testcase:

struct A { int n; };
struct B : A {} b[2];

constexpr int *f() {
  // Loses MostDerived array information.
  A *p = b;
  return &static_cast<B*>(p)[1].n;
}

static_assert(f() == &b[1].n);

Clang incorrectly rejects:

<source>:10:15: error: static assertion expression is not an integral constant expression
   10 | static_assert(f() == &b[1].n);
      |               ^~~~~~~~~~~~~~
<source>:7:11: note: cannot access base class of pointer past the end of object
    7 |   return &static_cast<B*>(p)[1].n;
      |           ^
<source>:10:15: note: in call to 'f()'
   10 | static_assert(f() == &b[1].n);
      |               ^~~

This is happening because reconstruction of an LValue from an APValue (specifically, SubobjectDesignator construction) is miscomputing the values for the MostDerivedIsArrayElement and MostDerivedArraySize fields in the case where the most-derived object that the lvalue refers to is an array element, but the lvalue itself is not (that is, when the lvalue refers to a base subobject of an array element).

That's happening here:

https://github.com/llvm/llvm-project/blob/364bed30971df7fb50d7aadfc21f3996a97bf3a4/clang/lib/AST/ExprConstant.cpp#L238-L242

I think deleting that else case would be the proper fix here: for a base subobject, we just want to inherit the "most derived" properties (the "is array" flag and array size) from the previous element.

zygoloid avatar Sep 11 '26 21:09 zygoloid

@llvm/issue-subscribers-clang-frontend

Author: Richard Smith (zygoloid)

[Testcase](https://godbolt.org/z/v8fa8e3Kz): ```c++ struct A { int n; }; struct B : A {} b[2];

constexpr int *f() { // Loses MostDerived array information. A p = b; return &static_cast<B>(p)[1].n; }

static_assert(f() == &b[1].n);

Clang incorrectly rejects:
```console
&lt;source&gt;:10:15: error: static assertion expression is not an integral constant expression
   10 | static_assert(f() == &amp;b[1].n);
      |               ^~~~~~~~~~~~~~
&lt;source&gt;:7:11: note: cannot access base class of pointer past the end of object
    7 |   return &amp;static_cast&lt;B*&gt;(p)[1].n;
      |           ^
&lt;source&gt;:10:15: note: in call to 'f()'
   10 | static_assert(f() == &amp;b[1].n);
      |               ^~~

This is happening because reconstruction of an LValue from an APValue (specifically, SubobjectDesignator construction) is miscomputing the values for the MostDerivedIsArrayElement and MostDerivedArraySize fields in the case where the most-derived object that the lvalue refers to is an array element, but the lvalue itself is not (that is, when the lvalue refers to a base subobject of an array element).

That's happening here:

https://github.com/llvm/llvm-project/blob/364bed30971df7fb50d7aadfc21f3996a97bf3a4/clang/lib/AST/ExprConstant.cpp#L238-L242

I think deleting that else case would be the proper fix here: for a base subobject, we just want to inherit the "most derived" properties (the "is array" flag and array size) from the previous element.

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Richard Smith (zygoloid)

[Testcase](https://godbolt.org/z/v8fa8e3Kz): ```c++ struct A { int n; }; struct B : A {} b[2];

constexpr int *f() { // Loses MostDerived array information. A p = b; return &static_cast<B>(p)[1].n; }

static_assert(f() == &b[1].n);

Clang incorrectly rejects:
```console
&lt;source&gt;:10:15: error: static assertion expression is not an integral constant expression
   10 | static_assert(f() == &amp;b[1].n);
      |               ^~~~~~~~~~~~~~
&lt;source&gt;:7:11: note: cannot access base class of pointer past the end of object
    7 |   return &amp;static_cast&lt;B*&gt;(p)[1].n;
      |           ^
&lt;source&gt;:10:15: note: in call to 'f()'
   10 | static_assert(f() == &amp;b[1].n);
      |               ^~~

This is happening because reconstruction of an LValue from an APValue (specifically, SubobjectDesignator construction) is miscomputing the values for the MostDerivedIsArrayElement and MostDerivedArraySize fields in the case where the most-derived object that the lvalue refers to is an array element, but the lvalue itself is not (that is, when the lvalue refers to a base subobject of an array element).

That's happening here:

https://github.com/llvm/llvm-project/blob/364bed30971df7fb50d7aadfc21f3996a97bf3a4/clang/lib/AST/ExprConstant.cpp#L238-L242

I think deleting that else case would be the proper fix here: for a base subobject, we just want to inherit the "most derived" properties (the "is array" flag and array size) from the previous element.

Hi, I'd like to work on this!

benzhongg avatar Sep 11 '26 22:09 benzhongg

Hi, I'd like to work on this!

We don't assign an issue until there is a PR and we check that it is moving in the right direction but letting folks know you are working on it can prevent duplicate work.

shafik avatar Sep 12 '26 05:09 shafik

Hi! I would like to work on this issue. I have reviewed the requirements and am ready to implement the fix. Could you please assign this to me?

Adarsh-Me avatar Sep 12 '26 08:09 Adarsh-Me

Hi! I'm working on this. Opened a PR, please review. @zygoloid @shafik , thanks

anandkvinu avatar Sep 12 '26 20:09 anandkvinu

Hey @zygoloid @shafik pinged a pr, let me know if it fixes the issue then we can merge it

akshaydubey05 avatar Sep 15 '26 12:09 akshaydubey05

Hi @shafik @zygoloid Is this issue still open? Ill look into this and start preparing an MR if it is. The last work on this was 3 weeks ago, so is this still open? Neither provided MRs are merged.

adiroy28852 avatar Oct 09 '26 02:10 adiroy28852