Skip to content

Commit 045b3d5

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 5579942 commit 045b3d5

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
@@ -4177,7 +4177,6 @@ GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
41774177
Type result,
41784178
Optional<ExtInfo> info) {
41794179
assert(sig && "no generic signature for generic function type?!");
4180-
assert(!result->hasTypeVariable());
41814180

41824181
llvm::FoldingSetNodeID id;
41834182
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
@@ -2379,17 +2379,12 @@ namespace {
23792379
/// \param locator The locator to use for generated constraints and
23802380
/// type variables.
23812381
///
2382-
/// \param externalPatternType The type imposed by the enclosing pattern,
2383-
/// if any. This will be non-null in cases where there is, e.g., a
2384-
/// pattern such as "is SubClass".
2385-
///
23862382
/// \param bindPatternVarsOneWay When true, generate fresh type variables
23872383
/// for the types of each variable declared within the pattern, along
23882384
/// with a one-way constraint binding that to the type to which the
23892385
/// variable will be ascribed or inferred.
23902386
Type getTypeForPattern(
23912387
Pattern *pattern, ConstraintLocatorBuilder locator,
2392-
Type externalPatternType,
23932388
bool bindPatternVarsOneWay,
23942389
PatternBindingDecl *patternBinding = nullptr,
23952390
unsigned patternBindingIndex = 0) {
@@ -2417,19 +2412,11 @@ namespace {
24172412
case PatternKind::Paren: {
24182413
auto *paren = cast<ParenPattern>(pattern);
24192414

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

24342421
if (!underlyingType)
24352422
return Type();
@@ -2438,7 +2425,7 @@ namespace {
24382425
}
24392426
case PatternKind::Binding: {
24402427
auto *subPattern = cast<BindingPattern>(pattern)->getSubPattern();
2441-
auto type = getTypeForPattern(subPattern, locator, externalPatternType,
2428+
auto type = getTypeForPattern(subPattern, locator,
24422429
bindPatternVarsOneWay);
24432430

24442431
if (!type)
@@ -2464,9 +2451,7 @@ namespace {
24642451
};
24652452

24662453
// Always prefer a contextual type when it's available.
2467-
if (externalPatternType) {
2468-
type = externalPatternType;
2469-
} else if (auto *initializer = getInitializerExpr()) {
2454+
if (auto *initializer = getInitializerExpr()) {
24702455
// For initialization always assume a type of initializer.
24712456
type = CS.getType(initializer)->getRValueType();
24722457
} else {
@@ -2543,18 +2528,13 @@ namespace {
25432528
// diagnostic in the middle of the solver path.
25442529

25452530
CS.addConstraint(ConstraintKind::OneWayEqual, oneWayVarType,
2546-
externalPatternType ? externalPatternType : varType,
2547-
locator);
2531+
varType, locator);
25482532
}
25492533

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

26482628
if (!subPatternType)
26492629
return Type();
@@ -2666,38 +2646,16 @@ namespace {
26662646
case PatternKind::Tuple: {
26672647
auto tuplePat = cast<TuplePattern>(pattern);
26682648

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

26962654
auto *eltPattern = tupleElt.getPattern();
26972655
Type eltTy = getTypeForPattern(
26982656
eltPattern,
26992657
locator.withPathElement(LocatorPathElt::PatternMatch(eltPattern)),
2700-
externalEltType, bindPatternVarsOneWay);
2658+
bindPatternVarsOneWay);
27012659

27022660
if (!eltTy)
27032661
return Type();
@@ -2709,25 +2667,12 @@ namespace {
27092667
}
27102668

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

27322677
if (!subPatternType)
27332678
return Type();
@@ -2761,7 +2706,7 @@ namespace {
27612706
auto subPatternType = getTypeForPattern(
27622707
subPattern,
27632708
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
2764-
castType, bindPatternVarsOneWay);
2709+
bindPatternVarsOneWay);
27652710

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

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

28802813
if (!subPatternType)
28812814
return Type();
@@ -4722,7 +4655,7 @@ Type ConstraintSystem::generateConstraints(
47224655
bool bindPatternVarsOneWay, PatternBindingDecl *patternBinding,
47234656
unsigned patternIndex) {
47244657
ConstraintGenerator cg(*this, nullptr);
4725-
return cg.getTypeForPattern(pattern, locator, Type(), bindPatternVarsOneWay,
4658+
return cg.getTypeForPattern(pattern, locator, bindPatternVarsOneWay,
47264659
patternBinding, patternIndex);
47274660
}
47284661

test/Constraints/patterns.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,7 @@ extension [EWithIdent<Int>] {
557557
}
558558
}
559559
}
560+
561+
struct TestRecursiveVarRef<T> {
562+
lazy var e: () -> Int = {e}()
563+
}

0 commit comments

Comments
 (0)