Skip to content

Commit a3baee9

Browse files
committed
[Variadic Generics] fixit to remove keyword 'each' on non-pack types
1 parent 8fd028c commit a3baee9

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
#include "swift/Basic/SourceManager.h"
4343
#include "swift/Basic/Statistic.h"
4444
#include "swift/Basic/StringExtras.h"
45-
#include "swift/Sema/SILTypeResolutionContext.h"
4645
#include "swift/ClangImporter/ClangImporter.h"
46+
#include "swift/Parse/Lexer.h"
47+
#include "swift/Sema/SILTypeResolutionContext.h"
4748
#include "swift/Strings.h"
4849
#include "swift/Subsystems.h"
4950
#include "clang/AST/ASTContext.h"
@@ -4615,14 +4616,21 @@ NeverNullType TypeResolver::resolvePackElement(PackElementTypeRepr *repr,
46154616
if (!packReference->isParameterPack()) {
46164617
auto diag =
46174618
ctx.Diags.diagnose(repr->getLoc(), diag::each_non_pack, packReference);
4619+
bool addEachFixitApplied = false;
46184620
if (auto *packIdent = dyn_cast<IdentTypeRepr>(repr->getPackType())) {
46194621
if (auto *packIdentBinding = packIdent->getBoundDecl()) {
46204622
if (packIdentBinding->getLoc().isValid()) {
46214623
diag.fixItInsert(packIdentBinding->getLoc(), "each ");
4624+
addEachFixitApplied = true;
46224625
}
46234626
}
46244627
}
4625-
// TODO: else 'each' probably should be removed preceding repr
4628+
if (const auto eachLoc = repr->getEachLoc();
4629+
!addEachFixitApplied && eachLoc.isValid()) {
4630+
const auto eachLocEnd =
4631+
Lexer::getLocForEndOfToken(ctx.SourceMgr, eachLoc);
4632+
diag.fixItRemoveChars(eachLoc, eachLocEnd);
4633+
}
46264634
return packReference;
46274635
}
46284636

test/Constraints/pack-expansion-expressions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,14 @@ func packElementInvalidBinding<each T>(_ arg: repeat each T) {
153153
let x = 1
154154
repeat print(each x)
155155
// expected-error@-1 {{'each' cannot be applied to non-pack type 'Int'}}
156+
// TODO: fixit to remove 'each' keyword
156157
}
157158

158159
func copyIntoTuple<each T>(_ arg: repeat each T) -> (repeat each T) {
159160
return (repeat each arg)
160161
}
161162
func callCopyAndBind<T>(_ arg: repeat each T) {
162-
// expected-error@-1 {{'each' cannot be applied to non-pack type 'T'}}
163+
// expected-error@-1 {{'each' cannot be applied to non-pack type 'T'}}{{22-22=each }}
163164
// expected-error@-2 {{pack expansion 'T' must contain at least one pack reference}}
164165

165166
// Don't propagate errors for invalid declaration reference

test/Constraints/variadic_generic_functions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func min<each T: Comparable>(_ values: repeat each T) -> (repeat each T)? {
2121
}
2222

2323
func invalidPacks() {
24-
func monovariadic1() -> (each String) {} // expected-error {{'each' cannot be applied to non-pack type 'String'}}
24+
func monovariadic1() -> (each String) {} // expected-error {{'each' cannot be applied to non-pack type 'String'}}{{28-32=}}
2525
func monovariadic2<T>() -> (repeat T) {} // expected-error {{pack expansion 'T' must contain at least one pack reference}}
2626
func monovariadic3<T, U>() -> (T, repeat U) {} // expected-error {{pack expansion 'U' must contain at least one pack reference}}
2727
}

test/Constraints/variadic_generic_types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ func g<each T>(_: repeat each T) {
2222

2323
_ = (repeat each Int).self
2424
// expected-error@-1 {{pack expansion 'Int' must contain at least one pack reference}}
25-
// expected-error@-2 {{'each' cannot be applied to non-pack type 'Int'}}
25+
// expected-error@-2 {{'each' cannot be applied to non-pack type 'Int'}}{{15-19=}}
2626
}

test/type/pack_expansion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func packRef<each T>(_: repeat each T) where each T: P {}
6969

7070
func packMemberRef<each T>(_: repeat each T.T) where each T: P {}
7171

72-
// expected-error@+1 {{'each' cannot be applied to non-pack type 'Int'}}
72+
// expected-error@+1 {{'each' cannot be applied to non-pack type 'Int'}}{{31-35=}}
7373
func invalidPackRefEachInt(_: each Int) {}
7474

7575
// expected-error@+1 {{pack expansion 'Int' must contain at least one pack reference}}

0 commit comments

Comments
 (0)