Skip to content

Commit d02328e

Browse files
committed
[CS] Remove external type logic from getTypeForPattern
This shouldn't be necessary, we should be able to solve with type variables instead. This makes sure we don't end up with weird special cases that only occur when an external type is present.
1 parent 6dcf22c commit d02328e

File tree

4 files changed

+15
-79
lines changed

4 files changed

+15
-79
lines changed

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4183,7 +4183,6 @@ GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
41834183
Type result,
41844184
Optional<ExtInfo> info) {
41854185
assert(sig && "no generic signature for generic function type?!");
4186-
assert(!result->hasTypeVariable());
41874186

41884187
llvm::FoldingSetNodeID id;
41894188
GenericFunctionType::Profile(id, sig, params, result, info);

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8589,7 +8589,7 @@ bool InvalidWeakAttributeUse::diagnoseAsError() {
85898589
return false;
85908590

85918591
auto *var = pattern->getDecl();
8592-
auto varType = OptionalType::get(getType(var));
8592+
auto varType = getType(var);
85938593

85948594
auto diagnostic =
85958595
emitDiagnosticAt(var, diag::invalid_ownership_not_optional,

lib/Sema/CSGen.cpp

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,17 +2378,12 @@ namespace {
23782378
/// \param locator The locator to use for generated constraints and
23792379
/// type variables.
23802380
///
2381-
/// \param externalPatternType The type imposed by the enclosing pattern,
2382-
/// if any. This will be non-null in cases where there is, e.g., a
2383-
/// pattern such as "is SubClass".
2384-
///
23852381
/// \param bindPatternVarsOneWay When true, generate fresh type variables
23862382
/// for the types of each variable declared within the pattern, along
23872383
/// with a one-way constraint binding that to the type to which the
23882384
/// variable will be ascribed or inferred.
23892385
Type getTypeForPattern(
23902386
Pattern *pattern, ConstraintLocatorBuilder locator,
2391-
Type externalPatternType,
23922387
bool bindPatternVarsOneWay,
23932388
PatternBindingDecl *patternBinding = nullptr,
23942389
unsigned patternBindingIndex = 0) {
@@ -2416,19 +2411,11 @@ namespace {
24162411
case PatternKind::Paren: {
24172412
auto *paren = cast<ParenPattern>(pattern);
24182413

2419-
// Parentheses don't affect the canonical type, but record them as
2420-
// type sugar.
2421-
if (externalPatternType &&
2422-
isa<ParenType>(externalPatternType.getPointer())) {
2423-
externalPatternType = cast<ParenType>(externalPatternType.getPointer())
2424-
->getUnderlyingType();
2425-
}
2426-
24272414
auto *subPattern = paren->getSubPattern();
24282415
auto underlyingType = getTypeForPattern(
24292416
subPattern,
24302417
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2431-
externalPatternType, bindPatternVarsOneWay);
2418+
bindPatternVarsOneWay);
24322419

24332420
if (!underlyingType)
24342421
return Type();
@@ -2437,7 +2424,7 @@ namespace {
24372424
}
24382425
case PatternKind::Binding: {
24392426
auto *subPattern = cast<BindingPattern>(pattern)->getSubPattern();
2440-
auto type = getTypeForPattern(subPattern, locator, externalPatternType,
2427+
auto type = getTypeForPattern(subPattern, locator,
24412428
bindPatternVarsOneWay);
24422429

24432430
if (!type)
@@ -2463,9 +2450,7 @@ namespace {
24632450
};
24642451

24652452
// Always prefer a contextual type when it's available.
2466-
if (externalPatternType) {
2467-
type = externalPatternType;
2468-
} else if (auto *initializer = getInitializerExpr()) {
2453+
if (auto *initializer = getInitializerExpr()) {
24692454
// For initialization always assume a type of initializer.
24702455
type = CS.getType(initializer)->getRValueType();
24712456
} else {
@@ -2542,18 +2527,13 @@ namespace {
25422527
// diagnostic in the middle of the solver path.
25432528

25442529
CS.addConstraint(ConstraintKind::OneWayEqual, oneWayVarType,
2545-
externalPatternType ? externalPatternType : varType,
2546-
locator);
2530+
varType, locator);
25472531
}
25482532

25492533
// Ascribe a type to the declaration so it's always available to
25502534
// constraint system.
25512535
if (oneWayVarType) {
25522536
CS.setType(var, oneWayVarType);
2553-
} else if (externalPatternType) {
2554-
// If there is an externally imposed type, that's what the
2555-
// declaration is going to be bound to.
2556-
CS.setType(var, externalPatternType);
25572537
} else {
25582538
// Otherwise, let's use the type of the pattern. The type
25592539
// of the declaration has to be r-value, so let's add an
@@ -2642,7 +2622,7 @@ namespace {
26422622
Type subPatternType = getTypeForPattern(
26432623
subPattern,
26442624
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2645-
openedType, bindPatternVarsOneWay);
2625+
bindPatternVarsOneWay);
26462626

26472627
if (!subPatternType)
26482628
return Type();
@@ -2665,38 +2645,16 @@ namespace {
26652645
case PatternKind::Tuple: {
26662646
auto tuplePat = cast<TuplePattern>(pattern);
26672647

2668-
// If there's an externally-imposed type, decompose it into element
2669-
// types so long as we have the right number of such types.
2670-
SmallVector<AnyFunctionType::Param, 4> externalEltTypes;
2671-
if (externalPatternType) {
2672-
decomposeTuple(externalPatternType, externalEltTypes);
2673-
2674-
// If we have the wrong number of elements, we may not be able to
2675-
// provide more specific types.
2676-
if (tuplePat->getNumElements() != externalEltTypes.size()) {
2677-
externalEltTypes.clear();
2678-
2679-
// Implicit tupling.
2680-
if (tuplePat->getNumElements() == 1) {
2681-
externalEltTypes.push_back(
2682-
AnyFunctionType::Param(externalPatternType));
2683-
}
2684-
}
2685-
}
2686-
26872648
SmallVector<TupleTypeElt, 4> tupleTypeElts;
26882649
tupleTypeElts.reserve(tuplePat->getNumElements());
26892650
for (unsigned i = 0, e = tuplePat->getNumElements(); i != e; ++i) {
26902651
auto &tupleElt = tuplePat->getElement(i);
2691-
Type externalEltType;
2692-
if (!externalEltTypes.empty())
2693-
externalEltType = externalEltTypes[i].getPlainType();
26942652

26952653
auto *eltPattern = tupleElt.getPattern();
26962654
Type eltTy = getTypeForPattern(
26972655
eltPattern,
26982656
locator.withPathElement(LocatorPathElt::PatternMatch(eltPattern)),
2699-
externalEltType, bindPatternVarsOneWay);
2657+
bindPatternVarsOneWay);
27002658

27012659
if (!eltTy)
27022660
return Type();
@@ -2708,25 +2666,12 @@ namespace {
27082666
}
27092667

27102668
case PatternKind::OptionalSome: {
2711-
// Remove an optional from the object type.
2712-
if (externalPatternType) {
2713-
Type objVar = CS.createTypeVariable(
2714-
CS.getConstraintLocator(
2715-
locator.withPathElement(ConstraintLocator::OptionalPayload)),
2716-
TVO_CanBindToNoEscape);
2717-
CS.addConstraint(
2718-
ConstraintKind::OptionalObject, externalPatternType, objVar,
2719-
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
2720-
2721-
externalPatternType = objVar;
2722-
}
2723-
27242669
auto *subPattern = cast<OptionalSomePattern>(pattern)->getSubPattern();
27252670
// The subpattern must have optional type.
27262671
Type subPatternType = getTypeForPattern(
27272672
subPattern,
27282673
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2729-
externalPatternType, bindPatternVarsOneWay);
2674+
bindPatternVarsOneWay);
27302675

27312676
if (!subPatternType)
27322677
return Type();
@@ -2760,7 +2705,7 @@ namespace {
27602705
auto subPatternType = getTypeForPattern(
27612706
subPattern,
27622707
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2763-
castType, bindPatternVarsOneWay);
2708+
bindPatternVarsOneWay);
27642709

27652710
// NOTE: The order here is important! Pattern matching equality is
27662711
// not symmetric (we need to fix that either by using a different
@@ -2845,18 +2790,6 @@ namespace {
28452790
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
28462791

28472792
baseType = parentType;
2848-
// Perform member lookup into the external pattern metatype. e.g.
2849-
// `case let .test(tuple) as Test`.
2850-
} else if (externalPatternType) {
2851-
Type externalMetaType = MetatypeType::get(externalPatternType);
2852-
2853-
CS.addValueMemberConstraint(
2854-
externalMetaType, enumPattern->getName(), memberType, CurDC,
2855-
functionRefKind, {},
2856-
CS.getConstraintLocator(locator,
2857-
LocatorPathElt::PatternMatch(pattern)));
2858-
2859-
baseType = externalPatternType;
28602793
} else {
28612794
// Use the pattern type for member lookup.
28622795
CS.addUnresolvedValueMemberConstraint(
@@ -2874,7 +2807,7 @@ namespace {
28742807
Type subPatternType = getTypeForPattern(
28752808
subPattern,
28762809
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2877-
Type(), bindPatternVarsOneWay);
2810+
bindPatternVarsOneWay);
28782811

28792812
if (!subPatternType)
28802813
return Type();
@@ -4723,7 +4656,7 @@ Type ConstraintSystem::generateConstraints(
47234656
bool bindPatternVarsOneWay, PatternBindingDecl *patternBinding,
47244657
unsigned patternIndex) {
47254658
ConstraintGenerator cg(*this, nullptr);
4726-
return cg.getTypeForPattern(pattern, locator, Type(), bindPatternVarsOneWay,
4659+
return cg.getTypeForPattern(pattern, locator, bindPatternVarsOneWay,
47274660
patternBinding, patternIndex);
47284661
}
47294662

test/Constraints/patterns.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,7 @@ struct TestIUOMatchOp {
564564
if case self = self {}
565565
}
566566
}
567+
568+
struct TestRecursiveVarRef<T> {
569+
lazy var e: () -> Int = {e}()
570+
}

0 commit comments

Comments
 (0)