Skip to content

[Variadic Generics] fixit to remove keyword 'each' on non-pack types #64882

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
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
12 changes: 10 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Sema/SILTypeResolutionContext.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/Parse/Lexer.h"
#include "swift/Sema/SILTypeResolutionContext.h"
#include "swift/Strings.h"
#include "swift/Subsystems.h"
#include "clang/AST/ASTContext.h"
Expand Down Expand Up @@ -4615,14 +4616,21 @@ NeverNullType TypeResolver::resolvePackElement(PackElementTypeRepr *repr,
if (!packReference->isParameterPack()) {
auto diag =
ctx.Diags.diagnose(repr->getLoc(), diag::each_non_pack, packReference);
bool addEachFixitApplied = false;
if (auto *packIdent = dyn_cast<IdentTypeRepr>(repr->getPackType())) {
if (auto *packIdentBinding = packIdent->getBoundDecl()) {
if (packIdentBinding->getLoc().isValid()) {
diag.fixItInsert(packIdentBinding->getLoc(), "each ");
addEachFixitApplied = true;
}
}
}
// TODO: else 'each' probably should be removed preceding repr
if (const auto eachLoc = repr->getEachLoc();
!addEachFixitApplied && eachLoc.isValid()) {
const auto eachLocEnd =
Lexer::getLocForEndOfToken(ctx.SourceMgr, eachLoc);
diag.fixItRemoveChars(eachLoc, eachLocEnd);
}
return packReference;
}

Expand Down
3 changes: 2 additions & 1 deletion test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ func packElementInvalidBinding<each T>(_ arg: repeat each T) {
let x = 1
repeat print(each x)
// expected-error@-1 {{'each' cannot be applied to non-pack type 'Int'}}
// TODO: fixit to remove 'each' keyword
}

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

// Don't propagate errors for invalid declaration reference
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/variadic_generic_functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func min<each T: Comparable>(_ values: repeat each T) -> (repeat each T)? {
}

func invalidPacks() {
func monovariadic1() -> (each String) {} // expected-error {{'each' cannot be applied to non-pack type 'String'}}
func monovariadic1() -> (each String) {} // expected-error {{'each' cannot be applied to non-pack type 'String'}}{{28-32=}}
func monovariadic2<T>() -> (repeat T) {} // expected-error {{pack expansion 'T' must contain at least one pack reference}}
func monovariadic3<T, U>() -> (T, repeat U) {} // expected-error {{pack expansion 'U' must contain at least one pack reference}}
}
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/variadic_generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ func g<each T>(_: repeat each T) {

_ = (repeat each Int).self
// expected-error@-1 {{pack expansion 'Int' must contain at least one pack reference}}
// expected-error@-2 {{'each' cannot be applied to non-pack type 'Int'}}
// expected-error@-2 {{'each' cannot be applied to non-pack type 'Int'}}{{15-19=}}
}
2 changes: 1 addition & 1 deletion test/type/pack_expansion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func packRef<each T>(_: repeat each T) where each T: P {}

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

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

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