Skip to content

Commit b0303bb

Browse files
committed
Sema: Restore old behavior in ConstraintSystem::openType()
My change to preserve type sugar in type transforms introduced a performance regression here, because openType() is called for every disjunction choice. My hope is to optimize the TypeAliasType representation and remove this at some point, but for now, let's just restore the old desugaring behavior in this case.
1 parent e47f996 commit b0303bb

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

include/swift/AST/TypeTransform.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,27 @@ case TypeKind::Id:
472472
return newUnderlyingTy;
473473

474474
auto oldSubMap = alias->getSubstitutionMap();
475-
auto newSubMap = asDerived().transformSubMap(oldSubMap);
476-
if (oldSubMap && !newSubMap)
477-
return Type();
475+
SubstitutionMap newSubMap;
476+
477+
// We leave the old behavior behind for ConstraintSystem::openType(), where
478+
// preserving sugar introduces a performance penalty.
479+
if (asDerived().shouldDesugarTypeAliases()) {
480+
for (auto oldReplacementType : oldSubMap.getReplacementTypes()) {
481+
Type newReplacementType = doIt(oldReplacementType, TypePosition::Invariant);
482+
if (!newReplacementType)
483+
return Type();
484+
485+
// If anything changed with the replacement type, we lose the sugar.
486+
if (newReplacementType.getPointer() != oldReplacementType.getPointer())
487+
return newUnderlyingTy;
488+
}
489+
490+
newSubMap = oldSubMap;
491+
} else {
492+
newSubMap = asDerived().transformSubMap(oldSubMap);
493+
if (oldSubMap && !newSubMap)
494+
return Type();
495+
}
478496

479497
if (oldParentType.getPointer() == newParentType.getPointer() &&
480498
oldUnderlyingTy.getPointer() == newUnderlyingTy.getPointer() &&
@@ -1011,6 +1029,8 @@ case TypeKind::Id:
10111029

10121030
bool shouldUnwrapVanishingTuples() const { return true; }
10131031

1032+
bool shouldDesugarTypeAliases() const { return false; }
1033+
10141034
CanType transformSILField(CanType fieldTy, TypePosition pos) {
10151035
return doIt(fieldTy, pos)->getCanonicalType();
10161036
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,10 @@ struct TypeOpener : public TypeTransform<TypeOpener> {
12401240
bool shouldUnwrapVanishingTuples() const {
12411241
return false;
12421242
}
1243+
1244+
bool shouldDesugarTypeAliases() const {
1245+
return true;
1246+
}
12431247
};
12441248

12451249
}

test/IDE/complete_call_arg.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ func testLValueBaseTyOfSubscript() {
11191119
var cache: [String: Codable] = [:]
11201120
if let cached = cache[#^LVALUEBASETY^#
11211121

1122-
// LVALUEBASETY-DAG: Decl[Subscript]/CurrNominal/Flair[ArgLabels]/IsSystem: ['[']{#(position): Dictionary<String, any Codable>.Index#}[']'][#Dictionary<String, any Codable>.Element#];
1122+
// LVALUEBASETY-DAG: Decl[Subscript]/CurrNominal/Flair[ArgLabels]/IsSystem: ['[']{#(position): Dictionary<String, any Codable>.Index#}[']'][#(key: String, value: any Codable)#];
11231123
// LVALUEBASETY-DAG: Decl[Subscript]/CurrNominal/Flair[ArgLabels]/IsSystem: ['[']{#(key): String#}[']'][#@lvalue (any Codable)?#];
11241124
}
11251125

test/Macros/macro_default_argument_diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ func testIdentifier(notOkay: Stringified<String> = #stringify(myString)) {}
5555
// expected-error@+1{{only literals are permitted}}
5656
func testString(interpolated: Stringified<String> = #stringify("Hello \(0b10001)")) {}
5757

58-
// expected-error@+1{{default argument value of type 'Stringified<Int>' (aka '(Int, String)') cannot be converted to type 'Int'}}
58+
// expected-error@+1{{default argument value of type '(Int, String)' cannot be converted to type 'Int'}}
5959
func testReturn(wrongType: Int = #stringify(0)) {}

0 commit comments

Comments
 (0)