Skip to content

[Clang] Correct the DeclRefExpr's Type after the initializer gets instantiated #133212

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

Merged
merged 4 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ Bug Fixes to C++ Support
- Correctly diagnoses if unresolved using declarations shadows template paramters (#GH129411)
- Clang was previously coalescing volatile writes to members of volatile base class subobjects.
The issue has been addressed by propagating qualifiers during derived-to-base conversions in the AST. (#GH127824)
- Correctly propagates the instantiated array type to the ``DeclRefExpr`` that refers to it. (#GH79750), (#GH113936), (#GH133047)
- Fixed a Clang regression in C++20 mode where unresolved dependent call expressions were created inside non-dependent contexts (#GH122892)
- Clang now emits the ``-Wunused-variable`` warning when some structured bindings are unused
and the ``[[maybe_unused]]`` attribute is not applied. (#GH125810)
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19849,6 +19849,12 @@ static void DoMarkVarDeclReferenced(
SemaRef.InstantiateVariableDefinition(PointOfInstantiation, Var);
});

// The size of an incomplete array type can be updated by
// instantiating the initializer. The DeclRefExpr's type should be
// updated accordingly too, or users of it would be confused!
if (E)
SemaRef.getCompletedType(E);

// Re-set the member to trigger a recomputation of the dependence bits
// for the expression.
if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E))
Expand Down
26 changes: 26 additions & 0 deletions clang/test/SemaCXX/cxx1y-variable-templates_top_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,29 @@ namespace VexingParse {
template <typename> int var; // expected-note {{declared here}}
int x(var); // expected-error {{use of variable template 'var' requires template arguments}}
}

#ifndef PRECXX11

namespace GH79750 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming you checked by hand that #133047 passes now? It might have been helpful to ask for a reduction to ensure we had fuller test coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did test it locally :) The reduced version of it (I didn't post it) is similar to #113936 so I don't add it to the test coverage.


enum class Values { A };

template<typename E>
constexpr Values values[] = {E::A};

constexpr auto r = values<Values>[0] == Values::A;

}

namespace GH113956 {

template <class T, T... VALUES>
struct C {
static constexpr T VALUEARRAY[] = {VALUES...};
};

static_assert(C<int, 0,1,2,3,4>::VALUEARRAY[3] == 3, "");
static_assert(C<int, 0,1,2,3,4>::VALUEARRAY[0] == 0, "");

}
#endif