Skip to content

Commit 83a7e05

Browse files
simanerushSima Nerush
andauthored
[Sema] Record a fix about extra parens for computed properties (#67042)
Co-authored-by: Sima Nerush <[email protected]>
1 parent ad11ef7 commit 83a7e05

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
@@ -7756,7 +7756,23 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
77567756
if (type1->isPlaceholder() || type2->isPlaceholder())
77577757
return getTypeMatchSuccess();
77587758

7759+
// If parameter pack expansion contains more than one element and the other
7760+
// side is a tuple, record a fix.
77597761
auto *loc = getConstraintLocator(locator);
7762+
if (loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
7763+
if (auto packExpansion = type2->getAs<PackExpansionType>()) {
7764+
auto countType = simplifyType(packExpansion->getCountType(), flags);
7765+
if (auto paramPack = countType->getAs<PackType>()) {
7766+
if (type1->is<TupleType>() && paramPack->getNumElements() >= 1) {
7767+
if (recordFix(DestructureTupleToMatchPackExpansionParameter::create(
7768+
*this, paramPack, loc))) {
7769+
return getTypeMatchFailure(loc);
7770+
}
7771+
return getTypeMatchSuccess();
7772+
}
7773+
}
7774+
}
7775+
}
77607776
if (recordFix(AllowInvalidPackExpansion::create(*this, loc)))
77617777
return getTypeMatchFailure(locator);
77627778

@@ -13473,14 +13489,16 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifySameShapeConstraint(
1347313489
auto paramPack = type2->castTo<PackType>();
1347413490

1347513491
// Tailed diagnostic to explode tuples.
13492+
// FIXME: This is very similar to
13493+
// 'cannot_convert_single_tuple_into_multiple_arguments'; can we emit
13494+
// both of these in the same place?
1347613495
if (argPack->getNumElements() == 1) {
13477-
if (auto *tuple = argPack->getElementType(0)->getAs<TupleType>()) {
13478-
if (tuple->getNumElements() == paramPack->getNumElements()) {
13479-
return recordShapeFix(
13480-
DestructureTupleToMatchPackExpansionParameter::create(
13481-
*this, paramPack, loc),
13482-
/*impact=*/2 * paramPack->getNumElements());
13483-
}
13496+
if (argPack->getElementType(0)->is<TupleType>() &&
13497+
paramPack->getNumElements() >= 1) {
13498+
return recordShapeFix(
13499+
DestructureTupleToMatchPackExpansionParameter::create(
13500+
*this, paramPack, loc),
13501+
/*impact=*/2 * paramPack->getNumElements());
1348413502
}
1348513503
}
1348613504

test/Constraints/pack-expansion-expressions.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,47 @@ do {
617617
_ = { (repeat test(x: each x)) }()
618618
}
619619
}
620+
621+
// https://github.com/apple/swift/issues/66393
622+
do {
623+
struct S<each T> {
624+
var property: (repeat each T) -> Void { // expected-note 4 {{'property' declared here}}
625+
get {}
626+
}
627+
628+
func method(_: repeat each T) {} // expected-note 4 {{'method' declared here}}
629+
}
630+
S<Int, Bool>().method((5, true))
631+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
632+
633+
S<Int, Bool>().method((5, true, 6))
634+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
635+
636+
S<Int, Bool>().property((5, true))
637+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
638+
639+
S<Int, Bool>().property((5, true, 6))
640+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
641+
642+
func foo<each U>(u: repeat each U) {
643+
S<repeat each U>().property((3, 4, 5))
644+
// expected-error@-1 {{value pack expansion at parameter #0 expects 1 separate arguments; remove extra parentheses to change tuple into separate arguments}}
645+
646+
// FIXME: The count of 'repeat each U' is not statically known, but error suggests that it is 1.
647+
S<repeat each U>().method((3, 4, 5))
648+
// expected-error@-1 {{value pack expansion at parameter #0 expects 1 separate arguments; remove extra parentheses to change tuple into separate arguments}}
649+
// FIXME: Bad diagnostics
650+
// expected-error@-3 {{pack expansion requires that 'each U' and '_' have the same shape}}
651+
// expected-error@-4 {{pack expansion requires that 'each U' and '_.RawValue' have the same shape}}
652+
653+
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
654+
S<(Int, Int), repeat each U>().method((3, 4))
655+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
656+
// FIXME: Duplicate diagnostics
657+
// expected-error@-3 2 {{pack expansion requires that 'each U' and '' have the same shape}}
658+
659+
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
660+
S<(Int, Int), repeat each U>().property((3, 4))
661+
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
662+
}
663+
}

0 commit comments

Comments
 (0)