Skip to content

Commit a1d38c1

Browse files
authored
Merge pull request #22468 from slavapestov/remove-some-param-old-type
Remove some uses of FunctionType::Param::getOldType() and other cleanups
2 parents 26bf98f + dffa29f commit a1d38c1

20 files changed

+107
-55
lines changed

include/swift/Basic/Statistics.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ FRONTEND_STATISTIC(Sema, NumDeclsDeserialized)
179179
/// Number of declarations validated.
180180
FRONTEND_STATISTIC(Sema, NumDeclsValidated)
181181

182+
/// Number of declarations type checked.
183+
FRONTEND_STATISTIC(Sema, NumDeclsTypechecked)
184+
185+
/// Number of declarations finalized.
186+
FRONTEND_STATISTIC(Sema, NumDeclsFinalized)
187+
182188
/// Number of full function bodies typechecked.
183189
FRONTEND_STATISTIC(Sema, NumFunctionsTypechecked)
184190

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,13 +1034,17 @@ FuncDecl *ASTContext::getEqualIntDecl() const {
10341034
return nullptr;
10351035

10361036
auto intType = getIntDecl()->getDeclaredType();
1037+
auto isIntParam = [&](AnyFunctionType::Param param) {
1038+
return (!param.isVariadic() && !param.isInOut() &&
1039+
param.getPlainType()->isEqual(intType));
1040+
};
10371041
auto boolType = getBoolDecl()->getDeclaredType();
10381042
auto decl = lookupOperatorFunc(*this, "==",
10391043
intType, [=](FunctionType *type) {
10401044
// Check for the signature: (Int, Int) -> Bool
10411045
if (type->getParams().size() != 2) return false;
1042-
if (!type->getParams()[0].getOldType()->isEqual(intType) ||
1043-
!type->getParams()[1].getOldType()->isEqual(intType)) return false;
1046+
if (!isIntParam(type->getParams()[0]) ||
1047+
!isIntParam(type->getParams()[1])) return false;
10441048
return type->getResult()->isEqual(boolType);
10451049
});
10461050
getImpl().EqualIntDecl = decl;
@@ -1227,8 +1231,9 @@ FuncDecl *ASTContext::getIsOSVersionAtLeastDecl() const {
12271231
if (intrinsicsParams.size() != 3)
12281232
return nullptr;
12291233

1230-
if (llvm::any_of(intrinsicsParams, [](const AnyFunctionType::Param &p) {
1231-
return !isBuiltinWordType(p.getOldType());
1234+
if (llvm::any_of(intrinsicsParams, [](AnyFunctionType::Param param) {
1235+
return (param.isVariadic() || param.isInOut() ||
1236+
!isBuiltinWordType(param.getPlainType()));
12321237
})) {
12331238
return nullptr;
12341239
}

lib/AST/Decl.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,8 +4127,15 @@ findProtocolSelfReferences(const ProtocolDecl *proto, Type type,
41274127
// the parameter type.
41284128
if (auto funcTy = type->getAs<AnyFunctionType>()) {
41294129
auto inputKind = SelfReferenceKind::None();
4130-
for (auto &elt : funcTy->getParams()) {
4131-
inputKind |= findProtocolSelfReferences(proto, elt.getOldType(),
4130+
for (auto param : funcTy->getParams()) {
4131+
// inout parameters are invariant.
4132+
if (param.isInOut()) {
4133+
if (findProtocolSelfReferences(proto, param.getPlainType(),
4134+
skipAssocTypes)) {
4135+
return SelfReferenceKind::Other();
4136+
}
4137+
}
4138+
inputKind |= findProtocolSelfReferences(proto, param.getParameterType(),
41324139
skipAssocTypes);
41334140
}
41344141
auto resultKind = findProtocolSelfReferences(proto, funcTy->getResult(),
@@ -4159,14 +4166,6 @@ findProtocolSelfReferences(const ProtocolDecl *proto, Type type,
41594166
skipAssocTypes);
41604167
}
41614168

4162-
// InOut types are invariant.
4163-
if (auto inOutType = type->getAs<InOutType>()) {
4164-
if (findProtocolSelfReferences(proto, inOutType->getObjectType(),
4165-
skipAssocTypes)) {
4166-
return SelfReferenceKind::Other();
4167-
}
4168-
}
4169-
41704169
// Bound generic types are invariant.
41714170
if (auto boundGenericType = type->getAs<BoundGenericType>()) {
41724171
for (auto paramType : boundGenericType->getGenericArgs()) {
@@ -4237,8 +4236,15 @@ ProtocolDecl::findProtocolSelfReferences(const ValueDecl *value,
42374236
// as a function result type.
42384237
if (!allowCovariantParameters) {
42394238
auto inputKind = SelfReferenceKind::None();
4240-
for (auto &elt : type->castTo<AnyFunctionType>()->getParams()) {
4241-
inputKind |= ::findProtocolSelfReferences(this, elt.getOldType(),
4239+
for (auto param : type->castTo<AnyFunctionType>()->getParams()) {
4240+
// inout parameters are invariant.
4241+
if (param.isInOut()) {
4242+
if (::findProtocolSelfReferences(this, param.getPlainType(),
4243+
skipAssocTypes)) {
4244+
return SelfReferenceKind::Other();
4245+
}
4246+
}
4247+
inputKind |= ::findProtocolSelfReferences(this, param.getParameterType(),
42424248
skipAssocTypes);
42434249
}
42444250

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ static std::pair<Type, ParamDecl *> decomposeSubscriptSetter(FuncDecl *setter) {
16541654
->castTo<AnyFunctionType>()
16551655
->getResult()
16561656
->castTo<AnyFunctionType>()
1657-
->getParams().front().getOldType();
1657+
->getParams().front().getParameterType();
16581658
ParamDecl *keyDecl = PL->get(1);
16591659

16601660
return {elementType, keyDecl};

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2849,7 +2849,7 @@ void NecessaryBindings::addTypeMetadata(CanType type) {
28492849
}
28502850
if (auto fn = dyn_cast<FunctionType>(type)) {
28512851
for (const auto &elt : fn.getParams())
2852-
addTypeMetadata(elt.getOldType());
2852+
addTypeMetadata(elt.getPlainType());
28532853
addTypeMetadata(fn.getResult());
28542854
return;
28552855
}

lib/SILGen/SILGenBridging.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,10 @@ static void expandTupleTypes(CanType type, SmallVectorImpl<CanType> &results) {
350350
static SmallVector<CanType, 8>
351351
expandTupleTypes(AnyFunctionType::CanParamArrayRef params) {
352352
SmallVector<CanType, 8> results;
353-
for (auto param : params)
354-
expandTupleTypes(param.getOldType(), results);
353+
for (auto param : params) {
354+
assert(!param.isInOut() && !param.isVariadic());
355+
expandTupleTypes(param.getPlainType(), results);
356+
}
355357
return results;
356358
}
357359

lib/SILGen/SILGenConstructor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ static SILValue emitConstructorMetatypeArg(SILGenFunction &SGF,
3535
auto ctorFnType = ctor->getInterfaceType()->castTo<AnyFunctionType>();
3636
assert(ctorFnType->getParams().size() == 1 &&
3737
"more than one self parameter?");
38-
Type metatype = ctorFnType->getParams()[0].getOldType();
38+
auto param = ctorFnType->getParams()[0];
39+
assert(!param.isVariadic() && !param.isInOut());
40+
Type metatype = param.getPlainType();
3941
auto *DC = ctor->getInnermostDeclContext();
4042
auto &AC = SGF.getASTContext();
4143
auto VD =

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static std::pair<unsigned, unsigned> getTypeDepthAndWidth(Type t) {
156156
for (auto &Param : Params) {
157157
unsigned TypeWidth;
158158
unsigned TypeDepth;
159-
std::tie(TypeDepth, TypeWidth) = getTypeDepthAndWidth(Param.getOldType());
159+
std::tie(TypeDepth, TypeWidth) = getTypeDepthAndWidth(Param.getParameterType());
160160
if (TypeDepth > MaxTypeDepth)
161161
MaxTypeDepth = TypeDepth;
162162
Width += TypeWidth;

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -874,8 +874,7 @@ std::pair<Type, Type>
874874
ConstraintSystem::getTypeOfReference(ValueDecl *value,
875875
FunctionRefKind functionRefKind,
876876
ConstraintLocatorBuilder locator,
877-
DeclContext *useDC,
878-
const DeclRefExpr *base) {
877+
DeclContext *useDC) {
879878
if (value->getDeclContext()->isTypeContext() && isa<FuncDecl>(value)) {
880879
// Unqualified lookup can find operator names within nominal types.
881880
auto func = cast<FuncDecl>(value);
@@ -963,7 +962,8 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
963962
// Determine the type of the value, opening up that type if necessary.
964963
bool wantInterfaceType = !varDecl->getDeclContext()->isLocalContext();
965964
Type valueType =
966-
getUnopenedTypeOfReference(varDecl, Type(), useDC, base, wantInterfaceType);
965+
getUnopenedTypeOfReference(varDecl, Type(), useDC, /*base=*/nullptr,
966+
wantInterfaceType);
967967

968968
assert(!valueType->hasUnboundGenericType() &&
969969
!valueType->hasTypeParameter());
@@ -1185,7 +1185,7 @@ ConstraintSystem::getTypeOfMemberReference(
11851185

11861186
// If the base is a module type, just use the type of the decl.
11871187
if (baseObjTy->is<ModuleType>()) {
1188-
return getTypeOfReference(value, functionRefKind, locator, useDC, base);
1188+
return getTypeOfReference(value, functionRefKind, locator, useDC);
11891189
}
11901190

11911191
FunctionType::Param baseObjParam(baseObjTy);

lib/Sema/ConstraintSystem.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,8 +2279,7 @@ class ConstraintSystem {
22792279
ValueDecl *decl,
22802280
FunctionRefKind functionRefKind,
22812281
ConstraintLocatorBuilder locator,
2282-
DeclContext *useDC,
2283-
const DeclRefExpr *base = nullptr);
2282+
DeclContext *useDC);
22842283

22852284
/// Return the type-of-reference of the given value.
22862285
///

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,8 +4224,7 @@ Optional<Identifier> TypeChecker::omitNeedlessWords(VarDecl *var) {
42244224
return None;
42254225

42264226
// Dig out the type of the variable.
4227-
Type type = var->getInterfaceType()->getReferenceStorageReferent()
4228-
->getWithoutSpecifierType();
4227+
Type type = var->getValueInterfaceType();
42294228
while (auto optObjectTy = type->getOptionalObjectType())
42304229
type = optObjectTy;
42314230

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,10 +2108,10 @@ bool isSubscriptReturningString(const ValueDecl *D, ASTContext &Context) {
21082108
return false;
21092109

21102110
const auto &param = params.front();
2111-
if (param.hasLabel() || param.isVariadic())
2111+
if (param.hasLabel() || param.isVariadic() || param.isInOut())
21122112
return false;
21132113

2114-
auto inputTy = param.getOldType()->getAs<BoundGenericStructType>();
2114+
auto inputTy = param.getPlainType()->getAs<BoundGenericStructType>();
21152115
if (!inputTy)
21162116
return false;
21172117

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class FindCapturedVars : public ASTWalker {
166166
});
167167

168168
for (const auto &param : gft->getParams())
169-
param.getOldType().walk(walker);
169+
param.getPlainType().walk(walker);
170170

171171
gft->getResult().walk(walker);
172172
}

lib/Sema/TypeCheckDecl.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20632063
explicit DeclChecker(TypeChecker &TC) : TC(TC) {}
20642064

20652065
void visit(Decl *decl) {
2066+
if (TC.Context.Stats)
2067+
TC.Context.Stats->getFrontendCounters().NumDeclsTypechecked++;
2068+
20662069
FrontendStatsTracer StatsTracer(TC.Context.Stats, "typecheck-decl", decl);
20672070
PrettyStackTraceDecl StackTrace("type-checking", decl);
20682071

@@ -3862,13 +3865,8 @@ void TypeChecker::validateDecl(ValueDecl *D) {
38623865
auto valueParams = accessor->getParameters();
38633866

38643867
// Determine the value type.
3865-
Type valueIfaceTy;
3866-
if (auto VD = dyn_cast<VarDecl>(storage)) {
3867-
valueIfaceTy = VD->getInterfaceType()->getReferenceStorageReferent();
3868-
} else {
3869-
auto SD = cast<SubscriptDecl>(storage);
3870-
valueIfaceTy = SD->getElementInterfaceType();
3871-
3868+
Type valueIfaceTy = storage->getValueInterfaceType();
3869+
if (auto SD = dyn_cast<SubscriptDecl>(storage)) {
38723870
// Copy the index types instead of re-validating them.
38733871
auto indices = SD->getIndices();
38743872
for (size_t i = 0, e = indices->size(); i != e; ++i) {
@@ -4413,6 +4411,9 @@ static void finalizeType(TypeChecker &TC, NominalTypeDecl *nominal) {
44134411
}
44144412

44154413
void TypeChecker::finalizeDecl(ValueDecl *decl) {
4414+
if (Context.Stats)
4415+
Context.Stats->getFrontendCounters().NumDeclsFinalized++;
4416+
44164417
validateDecl(decl);
44174418

44184419
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,18 +825,22 @@ bool swift::isRepresentableInObjC(const SubscriptDecl *SD, ObjCReason Reason) {
825825
if (SubscriptType->getParams().size() != 1)
826826
return false;
827827

828-
Type IndicesType = SubscriptType->getParams()[0].getOldType();
829-
if (IndicesType->hasError())
828+
auto IndexParam = SubscriptType->getParams()[0];
829+
if (IndexParam.isInOut())
830830
return false;
831831

832-
bool IndicesResult =
833-
IndicesType->isRepresentableIn(ForeignLanguage::ObjectiveC,
834-
SD->getDeclContext());
832+
Type IndexType = SubscriptType->getParams()[0].getParameterType();
833+
if (IndexType->hasError())
834+
return false;
835+
836+
bool IndexResult =
837+
IndexType->isRepresentableIn(ForeignLanguage::ObjectiveC,
838+
SD->getDeclContext());
835839

836840
Type ElementType = SD->getElementInterfaceType();
837841
bool ElementResult = ElementType->isRepresentableIn(
838842
ForeignLanguage::ObjectiveC, SD->getDeclContext());
839-
bool Result = IndicesResult && ElementResult;
843+
bool Result = IndexResult && ElementResult;
840844

841845
if (Result && checkObjCInExtensionContext(SD, Diagnose))
842846
return false;
@@ -845,7 +849,7 @@ bool swift::isRepresentableInObjC(const SubscriptDecl *SD, ObjCReason Reason) {
845849
return Result;
846850

847851
SourceRange TypeRange;
848-
if (!IndicesResult)
852+
if (!IndexResult)
849853
TypeRange = SD->getIndices()->getSourceRange();
850854
else
851855
TypeRange = SD->getElementTypeLoc().getSourceRange();
@@ -854,8 +858,8 @@ bool swift::isRepresentableInObjC(const SubscriptDecl *SD, ObjCReason Reason) {
854858
.highlight(TypeRange);
855859

856860
diagnoseTypeNotRepresentableInObjC(SD->getDeclContext(),
857-
!IndicesResult ? IndicesType
858-
: ElementType,
861+
!IndexResult ? IndexType
862+
: ElementType,
859863
TypeRange);
860864
describeObjCReason(SD, Reason);
861865

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ void TypeChecker::checkReferencedGenericParams(GenericContext *dc) {
322322
ReferencedGenericTypeWalker paramsAndResultWalker;
323323
auto *funcTy = decl->getInterfaceType()->castTo<GenericFunctionType>();
324324
for (const auto &param : funcTy->getParams())
325-
param.getOldType().walk(paramsAndResultWalker);
325+
param.getPlainType().walk(paramsAndResultWalker);
326326
funcTy->getResult().walk(paramsAndResultWalker);
327327

328328
// Set of generic params referenced in parameter types,

lib/Sema/TypeCheckPattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ bool TypeChecker::coerceParameterListToType(ParameterList *P, ClosureExpr *CE,
15871587
bool hadError = false;
15881588
for (const auto &param : FN->getParams()) {
15891589
params.push_back(param);
1590-
hadError |= param.getOldType()->hasError();
1590+
hadError |= param.getPlainType()->hasError();
15911591
}
15921592

15931593
// Local function to check if the given type is valid e.g. doesn't have

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,12 @@ swift::matchWitness(TypeChecker &tc,
885885
= cs->getTypeOfMemberReference(selfTy, witness, dc,
886886
/*isDynamicResult=*/false,
887887
FunctionRefKind::DoubleApply,
888-
witnessLocator,
889-
/*base=*/nullptr);
888+
witnessLocator);
890889
} else {
891890
std::tie(openedFullWitnessType, openWitnessType)
892891
= cs->getTypeOfReference(witness,
893892
FunctionRefKind::DoubleApply,
894-
witnessLocator,
895-
/*base=*/nullptr);
893+
witnessLocator, /*useDC=*/nullptr);
896894
}
897895
openWitnessType = openWitnessType->getRValueType();
898896

test/IRGen/partial_apply_generic.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ let g = dietaryFad(Chicken())
9191
do {
9292
try g()
9393
} catch {}
94+
95+
//
96+
// Incorrect assertion regarding inout parameters in NecessaryBindings
97+
//
98+
99+
func coyote<T, U>(_ t: T, _ u: U) {}
100+
101+
func hawk<A, B, C>(_: A, _ b: B, _ c: C) {
102+
let fn: (Optional<(A) -> B>, @escaping (inout B, C) -> ()) -> () = coyote
103+
}

test/decl/func/dynamic_self.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,23 @@ final class FinalFactory : FactoryPattern {
393393
self.init(factory: FinalFactory(_string: string))
394394
}
395395
}
396+
397+
// Operators returning Self
398+
399+
class SelfOperator {
400+
required init() {}
401+
402+
static func +(lhs: SelfOperator, rhs: SelfOperator) -> Self {
403+
return self.init()
404+
}
405+
406+
func double() -> Self {
407+
// FIXME: Should this work?
408+
return self + self // expected-error {{cannot convert return expression of type 'SelfOperator' to return type 'Self'}}
409+
}
410+
}
411+
412+
func useSelfOperator() {
413+
let s = SelfOperator()
414+
_ = s + s
415+
}

0 commit comments

Comments
 (0)