constant evaluator loses track of most-derived array information when reconstructing `LValue` from `APValue`
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.
@llvm/issue-subscribers-clang-frontend
Author: Richard Smith (zygoloid)
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
<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.
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:
- 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.
- 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.
- 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.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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)
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
<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.
Hi, I'd like to work on this!
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.
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?
Hi! I'm working on this. Opened a PR, please review. @zygoloid @shafik , thanks
Hey @zygoloid @shafik pinged a pr, let me know if it fixes the issue then we can merge it
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.