Skip to content

Commit cfcad2b

Browse files
committed
[Sema] Refactor code and improve documentation pertaining to SE-0408.
1 parent a076d54 commit cfcad2b

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5863,6 +5863,9 @@ enum class PropertyWrapperSynthesizedPropertyKind {
58635863
class VarDecl : public AbstractStorageDecl {
58645864
friend class NamingPatternRequest;
58655865
NamedPattern *NamingPattern = nullptr;
5866+
/// The opened element environment of the pack expansion to use as
5867+
/// the variable's context generic environment when the variable is
5868+
/// declared in context of a for-in loop over the elements of a parameter pack.
58665869
GenericEnvironment *OpenedElementEnvironment = nullptr;
58675870

58685871
public:

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3766,7 +3766,10 @@ class ConstraintSystem {
37663766
ConstraintLocatorBuilder locator,
37673767
ConstraintFix *compatFix = nullptr);
37683768

3769-
/// Add a materialize constraint for a pack expansion.
3769+
/// For a given `patternType`, materialize the pack expansion and return a type variable with the result.
3770+
///
3771+
/// \param patternType A single unlabeled tuple with pack expansion.
3772+
/// \param locator The locator.
37703773
TypeVariableType *
37713774
addMaterializePackExpansionConstraint(Type patternType,
37723775
ConstraintLocatorBuilder locator);

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);

0 commit comments

Comments
 (0)