Skip to content

Commit 1d73b2c

Browse files
authored
[clang] don't create type source info for vardecl created for structured bindings (#153923)
These are implicit vardecls which its type was never written in source code. Don't create a TypeLoc and give it a fake source location. The fake as-written type also didn't match the actual type, which after fixing this gives some unrelated test churn on a CFG dump, since statement printing prefers type source info if thats available. Fixes #153649 This is a regression introduced in #147835 This regression was never released, so no release notes are added.
1 parent 627f801 commit 1d73b2c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %check_clang_tidy -std=c++20 %s modernize-type-traits %t
2+
3+
namespace std {
4+
template <class> struct tuple_size {
5+
static const int value = 1;
6+
};
7+
template <int, class> struct tuple_element {
8+
using type = int;
9+
};
10+
}
11+
12+
struct A {};
13+
template <int> int get(const A&);
14+
15+
auto [a] = A();

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,10 +1373,13 @@ static bool checkTupleLikeDecomposition(Sema &S,
13731373
S.BuildReferenceType(T, E.get()->isLValue(), Loc, B->getDeclName());
13741374
if (RefType.isNull())
13751375
return true;
1376-
auto *RefVD = VarDecl::Create(
1377-
S.Context, Src->getDeclContext(), Loc, Loc,
1378-
B->getDeclName().getAsIdentifierInfo(), RefType,
1379-
S.Context.getTrivialTypeSourceInfo(T, Loc), Src->getStorageClass());
1376+
1377+
// Don't give this VarDecl a TypeSourceInfo, since this is a synthesized
1378+
// entity and this type was never written in source code.
1379+
auto *RefVD =
1380+
VarDecl::Create(S.Context, Src->getDeclContext(), Loc, Loc,
1381+
B->getDeclName().getAsIdentifierInfo(), RefType,
1382+
/*TInfo=*/nullptr, Src->getStorageClass());
13801383
RefVD->setLexicalDeclContext(Src->getLexicalDeclContext());
13811384
RefVD->setTSCSpec(Src->getTSCSpec());
13821385
RefVD->setImplicit();

clang/test/Analysis/anonymous-decls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ int main() {
7878
// CHECK-NEXT: 8: decomposition-a-b
7979
// CHECK-NEXT: 9: [B3.7]([B3.8])
8080
// CHECK-NEXT: 10: [B3.9]
81-
// CHECK-NEXT: 11: std::tuple_element<0UL, std::pair<int, int>>::type a = get<0UL>(decomposition-a-b);
81+
// CHECK-NEXT: 11: std::tuple_element<0UL, std::pair<int, int>>::type &&a = get<0UL>(decomposition-a-b);
8282
// CHECK-NEXT: 12: get<1UL>
8383
// CHECK-NEXT: 13: [B3.12] (ImplicitCastExpr, FunctionToPointerDecay, tuple_element<1L, pair<int, int> >::type (*)(pair<int, int> &))
8484
// CHECK-NEXT: 14: decomposition-a-b
8585
// CHECK-NEXT: 15: [B3.13]([B3.14])
8686
// CHECK-NEXT: 16: [B3.15]
87-
// CHECK-NEXT: 17: std::tuple_element<1UL, std::pair<int, int>>::type b = get<1UL>(decomposition-a-b);
87+
// CHECK-NEXT: 17: std::tuple_element<1UL, std::pair<int, int>>::type &&b = get<1UL>(decomposition-a-b);
8888
// CHECK-NEXT: Preds (1): B1
8989
// CHECK-NEXT: Succs (1): B2

0 commit comments

Comments
 (0)