Skip to content

[Sema] Record a fix about extra parens for computed properties #67042

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
Jul 10, 2023
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
32 changes: 25 additions & 7 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7756,7 +7756,23 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
if (type1->isPlaceholder() || type2->isPlaceholder())
return getTypeMatchSuccess();

// If parameter pack expansion contains more than one element and the other
// side is a tuple, record a fix.
auto *loc = getConstraintLocator(locator);
if (loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
if (auto packExpansion = type2->getAs<PackExpansionType>()) {
auto countType = simplifyType(packExpansion->getCountType(), flags);
if (auto paramPack = countType->getAs<PackType>()) {
if (type1->is<TupleType>() && paramPack->getNumElements() >= 1) {
if (recordFix(DestructureTupleToMatchPackExpansionParameter::create(
*this, paramPack, loc))) {
return getTypeMatchFailure(loc);
}
return getTypeMatchSuccess();
}
}
}
}
if (recordFix(AllowInvalidPackExpansion::create(*this, loc)))
return getTypeMatchFailure(locator);

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

// Tailed diagnostic to explode tuples.
// FIXME: This is very similar to
// 'cannot_convert_single_tuple_into_multiple_arguments'; can we emit
// both of these in the same place?
if (argPack->getNumElements() == 1) {
if (auto *tuple = argPack->getElementType(0)->getAs<TupleType>()) {
if (tuple->getNumElements() == paramPack->getNumElements()) {
return recordShapeFix(
DestructureTupleToMatchPackExpansionParameter::create(
*this, paramPack, loc),
/*impact=*/2 * paramPack->getNumElements());
}
if (argPack->getElementType(0)->is<TupleType>() &&
paramPack->getNumElements() >= 1) {
return recordShapeFix(
DestructureTupleToMatchPackExpansionParameter::create(
*this, paramPack, loc),
/*impact=*/2 * paramPack->getNumElements());
}
}

Expand Down
44 changes: 44 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,47 @@ do {
_ = { (repeat test(x: each x)) }()
}
}

// https://github.com/apple/swift/issues/66393
do {
struct S<each T> {
var property: (repeat each T) -> Void { // expected-note 4 {{'property' declared here}}
get {}
}

func method(_: repeat each T) {} // expected-note 4 {{'method' declared here}}
}
S<Int, Bool>().method((5, true))
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}

S<Int, Bool>().method((5, true, 6))
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}

S<Int, Bool>().property((5, true))
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}

S<Int, Bool>().property((5, true, 6))
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}

func foo<each U>(u: repeat each U) {
S<repeat each U>().property((3, 4, 5))
// expected-error@-1 {{value pack expansion at parameter #0 expects 1 separate arguments; remove extra parentheses to change tuple into separate arguments}}

// FIXME: The count of 'repeat each U' is not statically known, but error suggests that it is 1.
S<repeat each U>().method((3, 4, 5))
// expected-error@-1 {{value pack expansion at parameter #0 expects 1 separate arguments; remove extra parentheses to change tuple into separate arguments}}
// FIXME: Bad diagnostics
// expected-error@-3 {{pack expansion requires that 'each U' and '_' have the same shape}}
// expected-error@-4 {{pack expansion requires that 'each U' and '_.RawValue' have the same shape}}

// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
S<(Int, Int), repeat each U>().method((3, 4))
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
// FIXME: Duplicate diagnostics
// expected-error@-3 2 {{pack expansion requires that 'each U' and '' have the same shape}}

// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
S<(Int, Int), repeat each U>().property((3, 4))
// expected-error@-1 {{value pack expansion at parameter #0 expects 2 separate arguments; remove extra parentheses to change tuple into separate arguments}}
}
}