Skip to content

Commit cd4c55c

Browse files
committed
Fix grammar in diagnostic for wrong arity in a structured binding.
1 parent dfc1901 commit cd4c55c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,9 @@ def err_decomp_decl_not_alone : Error<
470470
def err_decomp_decl_requires_init : Error<
471471
"decomposition declaration %0 requires an initializer">;
472472
def err_decomp_decl_wrong_number_bindings : Error<
473-
"type %0 decomposes into %2 elements, but %select{only |}3%1 "
474-
"names were provided">;
473+
"type %0 decomposes into %3 %plural{1:element|:elements}2, but "
474+
"%select{%plural{0:no|:only %1}1|%1}4 "
475+
"%plural{1:name was|:names were}1 provided">;
475476
def err_decomp_decl_unbindable_type : Error<
476477
"cannot decompose %select{union|non-class, non-array}1 type %2">;
477478
def err_decomp_decl_multiple_bases_with_members : Error<

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,8 @@ static bool checkSimpleDecomposition(
902902
llvm::function_ref<ExprResult(SourceLocation, Expr *, unsigned)> GetInit) {
903903
if ((int64_t)Bindings.size() != NumElems) {
904904
S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings)
905-
<< DecompType << (unsigned)Bindings.size() << NumElems.toString(10)
905+
<< DecompType << (unsigned)Bindings.size()
906+
<< (unsigned)NumElems.getLimitedValue(UINT_MAX) << NumElems.toString(10)
906907
<< (NumElems < Bindings.size());
907908
return true;
908909
}
@@ -1148,8 +1149,9 @@ static bool checkTupleLikeDecomposition(Sema &S,
11481149
const llvm::APSInt &TupleSize) {
11491150
if ((int64_t)Bindings.size() != TupleSize) {
11501151
S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings)
1151-
<< DecompType << (unsigned)Bindings.size() << TupleSize.toString(10)
1152-
<< (TupleSize < Bindings.size());
1152+
<< DecompType << (unsigned)Bindings.size()
1153+
<< (unsigned)TupleSize.getLimitedValue(UINT_MAX)
1154+
<< TupleSize.toString(10) << (TupleSize < Bindings.size());
11531155
return true;
11541156
}
11551157

@@ -1373,7 +1375,7 @@ static bool checkMemberDecomposition(Sema &S, ArrayRef<BindingDecl*> Bindings,
13731375
[](FieldDecl *FD) { return !FD->isUnnamedBitfield(); });
13741376
assert(Bindings.size() != NumFields);
13751377
S.Diag(Src->getLocation(), diag::err_decomp_decl_wrong_number_bindings)
1376-
<< DecompType << (unsigned)Bindings.size() << NumFields
1378+
<< DecompType << (unsigned)Bindings.size() << NumFields << NumFields
13771379
<< (NumFields < Bindings.size());
13781380
return true;
13791381
};

clang/test/SemaCXX/cxx1z-decomposition.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ void use_from_own_init() {
44
auto [a] = a; // expected-error {{binding 'a' cannot appear in the initializer of its own decomposition declaration}}
55
}
66

7+
void num_elems() {
8+
struct A0 {} a0;
9+
int a1[1], a2[2];
10+
11+
auto [] = a0; // expected-warning {{does not allow a decomposition group to be empty}}
12+
auto [v1] = a0; // expected-error {{type 'A0' decomposes into 0 elements, but 1 name was provided}}
13+
auto [] = a1; // expected-error {{type 'int [1]' decomposes into 1 element, but no names were provided}} expected-warning {{empty}}
14+
auto [v2] = a1;
15+
auto [v3, v4] = a1; // expected-error {{type 'int [1]' decomposes into 1 element, but 2 names were provided}}
16+
auto [] = a2; // expected-error {{type 'int [2]' decomposes into 2 elements, but no names were provided}} expected-warning {{empty}}
17+
auto [v5] = a2; // expected-error {{type 'int [2]' decomposes into 2 elements, but only 1 name was provided}}
18+
auto [v6, v7] = a2;
19+
auto [v8, v9, v10] = a2; // expected-error {{type 'int [2]' decomposes into 2 elements, but 3 names were provided}}
20+
}
21+
722
// As a Clang extension, _Complex can be decomposed.
823
float decompose_complex(_Complex float cf) {
924
static _Complex float scf;

0 commit comments

Comments
 (0)