Skip to content

Put Variadic Generics Behind a Flag #41845

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 1 commit into from
Mar 17, 2022
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
3 changes: 3 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ namespace swift {
/// Enable experimental pairwise `buildBlock` for result builders.
bool EnableExperimentalPairwiseBuildBlock = false;

/// Enable variadic generics.
bool EnableExperimentalVariadicGenerics = false;

/// Disable the implicit import of the _Concurrency module.
bool DisableImplicitConcurrencyModuleImport =
!SWIFT_IMPLICIT_CONCURRENCY_IMPORT;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ def enable_experimental_string_processing :
Flag<["-"], "enable-experimental-string-processing">,
HelpText<"Enable experimental string processing">;

def enable_experimental_variadic_generics :
Flag<["-"], "enable-experimental-variadic-generics">,
HelpText<"Enable experimental support for variadic generic types">;

def disable_availability_checking : Flag<["-"],
"disable-availability-checking">,
HelpText<"Disable checking for potentially unavailable APIs">;
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
OPT_disable_experimental_opened_existential_types,
false);

Opts.EnableExperimentalVariadicGenerics |=
Args.hasArg(OPT_enable_experimental_variadic_generics);

// SwiftOnoneSupport produces different symbols when opening existentials,
// so disable it.
if (FrontendOpts.ModuleName == SWIFT_ONONE_SUPPORT)
Expand Down
8 changes: 6 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2802,8 +2802,12 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
break;
}
case DAK_TypeSequence: {
auto range = SourceRange(Loc, Tok.getRange().getStart());
Attributes.add(TypeSequenceAttr::create(Context, AtLoc, range));
if (Context.LangOpts.EnableExperimentalVariadicGenerics) {
auto range = SourceRange(Loc, Tok.getRange().getStart());
Attributes.add(TypeSequenceAttr::create(Context, AtLoc, range));
} else {
DiscardAttribute = true;
}
break;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5500,7 +5500,8 @@ ArgumentList *ExprRewriter::coerceCallArguments(
auto paramLabel = param.getLabel();

// Handle variadic generic parameters.
if (paramInfo.isVariadicGenericParameter(paramIdx)) {
if (ctx.LangOpts.EnableExperimentalVariadicGenerics &&
paramInfo.isVariadicGenericParameter(paramIdx)) {
assert(param.isVariadic());
assert(!param.isInOut());

Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,8 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
// We pull these out special because variadic parameters ban lots of
// the more interesting typing constructs called out below like
// inout and @autoclosure.
if (paramInfo.isVariadicGenericParameter(paramIdx)) {
if (cs.getASTContext().LangOpts.EnableExperimentalVariadicGenerics &&
paramInfo.isVariadicGenericParameter(paramIdx)) {
auto *PET = paramTy->castTo<PackExpansionType>();
OpenTypeSequenceElements openTypeSequence{cs, PET};
for (auto argIdx : parameterBindings[paramIdx]) {
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3772,7 +3772,8 @@ NeverNullType TypeResolver::resolveTupleType(TupleTypeRepr *repr,

bool complained = false;
if (repr->hasEllipsis()) {
if (repr->getNumElements() == 1 && !repr->hasElementNames()) {
if (getASTContext().LangOpts.EnableExperimentalVariadicGenerics &&
repr->getNumElements() == 1 && !repr->hasElementNames()) {
// This is probably a pack expansion. Try to resolve the pattern type.
auto patternTy = resolveType(repr->getElementType(0), elementOptions);
if (patternTy->hasError())
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/type_sequence.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-variadic-generics

struct TupleStruct<First, @_typeSequence Rest> {
var first: First
Expand Down
4 changes: 2 additions & 2 deletions test/decl/func/vararg.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %target-typecheck-verify-swift

var t1a: (Int...) = (1) // expected-error{{cannot create expansion with non-variadic type 'Int'}}
// expected-error@-1 {{cannot convert value of type 'Int' to specified type '(Int...)'}}
var t1a: (Int...) = (1)
// expected-error@-1 {{cannot create a variadic tuple}}
var t2d: (Double = 0.0) = 1 // expected-error {{default argument not permitted in a tuple type}} {{18-23=}}

func f1(_ a: Int...) { for _ in a {} }
Expand Down