Skip to content

Commit 0482bd3

Browse files
committed
[Sema] Refactor code and improve documentation pertaining to SE-0408.
1 parent 49dbf15 commit 0482bd3

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5857,6 +5857,9 @@ enum class PropertyWrapperSynthesizedPropertyKind {
58575857
class VarDecl : public AbstractStorageDecl {
58585858
friend class NamingPatternRequest;
58595859
NamedPattern *NamingPattern = nullptr;
5860+
/// When the variable is declared in context of a for-in loop over the elements of
5861+
/// a parameter pack, this is the opened element environment of the pack expansion
5862+
/// to use as the variable's context generic environment.
58605863
GenericEnvironment *OpenedElementEnvironment = nullptr;
58615864

58625865
public:

include/swift/Sema/ConstraintSystem.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3765,10 +3765,20 @@ class ConstraintSystem {
37653765
RememberChoice_t rememberChoice,
37663766
ConstraintLocatorBuilder locator,
37673767
ConstraintFix *compatFix = nullptr);
3768-
3769-
/// Add a materialize constraint for a pack expansion.
3768+
3769+
/// Given a tuple with a single unlabeled element that represents a pack
3770+
/// expansion (either directly via \c PackExpansionType or through a type
3771+
/// variable constrained to a pack expansion), materialize the pack expansion
3772+
/// and return its pattern type as a result. The result is a type variable
3773+
/// because element of the tuple is not required to be resolved at the time of
3774+
/// the call and operation is delayed until the element is sufficiently
3775+
/// resolved (see \c simplifyMaterializePackExpansionConstraint)
3776+
///
3777+
/// \param tupleType A tuple with a single unlabeled element that represents a
3778+
/// pack expansion.
3779+
/// \param locator The locator.
37703780
TypeVariableType *
3771-
addMaterializePackExpansionConstraint(Type patternType,
3781+
addMaterializePackExpansionConstraint(Type tupleType,
37723782
ConstraintLocatorBuilder locator);
37733783

37743784
/// Add a disjunction constraint.

include/swift/Sema/SyntacticElementTarget.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
namespace swift {
3030

3131
namespace constraints {
32-
/// Describes information specific to a sequence
33-
/// in a for-each loop.
32+
/// Describes information about a for-in loop over a sequence that needs to be
33+
/// tracked in the constraint system.
3434
struct SequenceIterationInfo {
3535
/// The type of the sequence.
3636
Type sequenceType;
@@ -48,18 +48,16 @@ struct SequenceIterationInfo {
4848
Expr *nextCall;
4949
};
5050

51-
/// Describes information specific to a pack expansion expression
52-
/// in a for-each loop.
51+
/// Describes information about a for-in loop over a pack that needs to be
52+
/// tracked in the constraint system.
5353
struct PackIterationInfo {
5454
/// The type of the pattern that matches the elements.
5555
Type patternType;
5656
};
5757

58-
/// Describes information about a for-each loop that needs to be tracked
58+
/// Describes information about a for-in loop that needs to be tracked
5959
/// within the constraint system.
60-
struct ForEachStmtInfo : TaggedUnion<SequenceIterationInfo, PackIterationInfo> {
61-
using TaggedUnion::TaggedUnion;
62-
};
60+
using ForEachStmtInfo = TaggedUnion<SequenceIterationInfo, PackIterationInfo>;
6361

6462
/// Describes the target to which a constraint system's solution can be
6563
/// applied.

lib/AST/Decl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7149,8 +7149,9 @@ VarDecl::VarDecl(DeclKind kind, bool isStatic, VarDecl::Introducer introducer,
71497149
}
71507150

71517151
Type VarDecl::getTypeInContext() const {
7152-
// If we are performing pack iteration, use the generic environment of the
7153-
// pack expansion expression to get the right context of a local variable.
7152+
// If the variable is declared in context of a for-in loop over the elements
7153+
// of a parameter pack, its interface type must be mapped into context using
7154+
// the opened element environment of the pack expansion.
71547155
if (auto *env = getOpenedElementEnvironment())
71557156
return GenericEnvironment::mapTypeIntoContext(env, getInterfaceType());
71567157

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4447,8 +4447,8 @@ static bool generateInitPatternConstraints(ConstraintSystem &cs,
44474447
return false;
44484448
}
44494449

4450-
/// Generate constraints for a for-in statement preamble, expecting a
4451-
/// `PackExpansionExpr`.
4450+
/// Generate constraints for a for-in statement preamble where the expression
4451+
/// is a `PackExpansionExpr`.
44524452
static llvm::Optional<PackIterationInfo>
44534453
generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
44544454
PackExpansionExpr *expansion, Type patternType) {
@@ -4524,6 +4524,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
45244524

45254525
auto *makeIteratorCall =
45264526
CallExpr::createImplicitEmpty(ctx, makeIteratorRef);
4527+
45274528
Pattern *pattern = NamedPattern::createImplicit(ctx, makeIteratorVar);
45284529
auto *PB = PatternBindingDecl::createImplicit(
45294530
ctx, StaticSpellingKind::None, pattern, makeIteratorCall, dc);
@@ -4540,6 +4541,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
45404541
return llvm::None;
45414542

45424543
sequenceIterationInfo.makeIteratorVar = PB;
4544+
45434545
// Type of sequence expression has to conform to Sequence protocol.
45444546
//
45454547
// Note that the following emulates having `$generator` separately
@@ -4606,7 +4608,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
46064608
cs.setTargetFor(sequenceIterationInfo.nextCall, nextTarget);
46074609
}
46084610

4609-
// Generate constraints for the pattern
4611+
// Generate constraints for the pattern.
46104612
Type initType =
46114613
cs.generateConstraints(typeCheckedPattern, elementLocator,
46124614
shouldBindPatternVarsOneWay, nullptr, 0);
@@ -4677,7 +4679,7 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
46774679
if (isa<PackExpansionExpr>(forEachExpr)) {
46784680
auto *expansion = cast<PackExpansionExpr>(forEachExpr);
46794681

4680-
// Generate constraints for the pattern
4682+
// Generate constraints for the pattern.
46814683
Type patternType = cs.generateConstraints(
46824684
pattern, elementLocator, target.shouldBindPatternVarsOneWay(), nullptr,
46834685
0);

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15640,11 +15640,11 @@ void ConstraintSystem::addExplicitConversionConstraint(
1564015640
}
1564115641

1564215642
TypeVariableType *ConstraintSystem::addMaterializePackExpansionConstraint(
15643-
Type patternType, ConstraintLocatorBuilder locator) {
15644-
assert(isSingleUnlabeledPackExpansionTuple(patternType));
15643+
Type tupleType, ConstraintLocatorBuilder locator) {
15644+
assert(isSingleUnlabeledPackExpansionTuple(tupleType));
1564515645
TypeVariableType *packVar =
1564615646
createTypeVariable(getConstraintLocator(locator), TVO_CanBindToPack);
15647-
addConstraint(ConstraintKind::MaterializePackExpansion, patternType, packVar,
15647+
addConstraint(ConstraintKind::MaterializePackExpansion, tupleType, packVar,
1564815648
getConstraintLocator(locator, {ConstraintLocator::Member}));
1564915649
return packVar;
1565015650
}

0 commit comments

Comments
 (0)