Skip to content

Commit 11562d2

Browse files
author
Sima Nerush
committed
Record a fix about extra parens for computed properties
Use countType and add an additional tuple check Add tests [Sema] Record a fix about extra parens for computed properties
1 parent c8ec463 commit 11562d2

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7750,7 +7750,23 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
77507750
if (type1->isPlaceholder() || type2->isPlaceholder())
77517751
return getTypeMatchSuccess();
77527752

7753+
// If parameter pack expansion contains more than one element and the other
7754+
// side is a tuple, record a fix.
77537755
auto *loc = getConstraintLocator(locator);
7756+
if (loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
7757+
if (auto packExpansion = type2->getAs<PackExpansionType>()) {
7758+
auto countType = simplifyType(packExpansion->getCountType(), flags);
7759+
if (auto paramPack = countType->getAs<PackType>()) {
7760+
if (type1->is<TupleType>() && paramPack->getNumElements() >= 1) {
7761+
if (recordFix(DestructureTupleToMatchPackExpansionParameter::create(
7762+
*this, paramPack, loc))) {
7763+
return getTypeMatchFailure(loc);
7764+
}
7765+
return getTypeMatchSuccess();
7766+
}
7767+
}
7768+
}
7769+
}
77547770
if (recordFix(AllowInvalidPackExpansion::create(*this, loc)))
77557771
return getTypeMatchFailure(locator);
77567772

@@ -13451,14 +13467,16 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifySameShapeConstraint(
1345113467
auto paramPack = type2->castTo<PackType>();
1345213468

1345313469
// Tailed diagnostic to explode tuples.
13470+
// FIXME: This is very similar to
13471+
// 'cannot_convert_single_tuple_into_multiple_arguments'; can we emit
13472+
// both of these in the same place?
1345413473
if (argPack->getNumElements() == 1) {
13455-
if (auto *tuple = argPack->getElementType(0)->getAs<TupleType>()) {
13456-
if (tuple->getNumElements() == paramPack->getNumElements()) {
13457-
return recordShapeFix(
13458-
DestructureTupleToMatchPackExpansionParameter::create(
13459-
*this, paramPack, loc),
13460-
/*impact=*/2 * paramPack->getNumElements());
13461-
}
13474+
if (argPack->getElementType(0)->is<TupleType>() &&
13475+
paramPack->getNumElements() >= 1) {
13476+
return recordShapeFix(
13477+
DestructureTupleToMatchPackExpansionParameter::create(
13478+
*this, paramPack, loc),
13479+
/*impact=*/2 * paramPack->getNumElements());
1346213480
}
1346313481
}
1346413482

test/Constraints/pack-expansion-expressions.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,47 @@ func test_that_expansions_are_bound_early() {
604604
})
605605
}
606606
}
607+
608+
// https://github.com/apple/swift/issues/66393
609+
do {
610+
struct S<each T> {
611+
var property: (repeat each T) -> Void { // expected-note 4 {{'property' declared here}}
612+
get {}
613+
}
614+
615+
func method(_: repeat each T) {} // expected-note 4 {{'method' declared here}}
616+
}
617+
S<Int, Bool>().method((5, true))
618+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
619+
620+
S<Int, Bool>().method((5, true, 6))
621+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
622+
623+
S<Int, Bool>().property((5, true))
624+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
625+
626+
S<Int, Bool>().property((5, true, 6))
627+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
628+
629+
func foo<each U>(u: repeat each U) {
630+
S<repeat each U>().property((3, 4, 5))
631+
// expected-error@-1 {{value pack expansion at parameter #0 expects 1 separate arguments; remove extra parentheses to change tuple into separate arguments}}
632+
633+
// FIXME: The count of 'repeat each U' is not statically known, but error suggests that it is 1.
634+
S<repeat each U>().method((3, 4, 5))
635+
// expected-error@-1 {{value pack expansion at parameter #0 expects 1 separate arguments; remove extra parentheses to change tuple into separate arguments}}
636+
// FIXME: Bad diagnostics
637+
// expected-error@-3 {{pack expansion requires that 'each U' and '_' have the same shape}}
638+
// expected-error@-4 {{pack expansion requires that 'each U' and '_.RawValue' have the same shape}}
639+
640+
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
641+
S<(Int, Int), repeat each U>().method((3, 4))
642+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
643+
// FIXME: Duplicate diagnostics
644+
// expected-error@-3 2 {{pack expansion requires that 'each U' and '' have the same shape}}
645+
646+
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
647+
S<(Int, Int), repeat each U>().property((3, 4))
648+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
649+
}
650+
}

0 commit comments

Comments
 (0)