Skip to content

Commit

Permalink
AST: Remove FuncDecl::getResultType()
Browse files Browse the repository at this point in the history
  • Loading branch information
slavapestov committed Nov 24, 2016
1 parent 1dc14e2 commit 5b8524a
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 34 deletions.
3 changes: 0 additions & 3 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4988,9 +4988,6 @@ class FuncDecl final : public AbstractFunctionDecl,
TypeLoc &getBodyResultTypeLoc() { return FnRetType; }
const TypeLoc &getBodyResultTypeLoc() const { return FnRetType; }

/// Retrieve the result type of this function.
Type getResultType() const;

/// Retrieve the result interface type of this function.
Type getResultInterfaceType() const;

Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,8 @@ struct ASTNodeBase {};
auto func = Functions.back();
Type resultType;
if (FuncDecl *FD = dyn_cast<FuncDecl>(func)) {
resultType = FD->getResultType();
resultType = FD->getResultInterfaceType();
resultType = ArchetypeBuilder::mapTypeIntoContext(FD, resultType);
} else if (auto closure = dyn_cast<AbstractClosureExpr>(func)) {
resultType = closure->getResultType();
} else {
Expand Down
17 changes: 0 additions & 17 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4458,23 +4458,6 @@ void FuncDecl::setDeserializedSignature(ArrayRef<ParameterList *> BodyParams,
this->FnRetType = FnRetType;
}

Type FuncDecl::getResultType() const {
if (!hasType())
return nullptr;

Type resultTy = getType();
if (resultTy->hasError())
return resultTy;

for (unsigned i = 0, e = getNumParameterLists(); i != e; ++i)
resultTy = resultTy->castTo<AnyFunctionType>()->getResult();

if (!resultTy)
resultTy = TupleType::getEmpty(getASTContext());

return resultTy;
}

Type FuncDecl::getResultInterfaceType() const {
if (!hasType())
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6488,7 +6488,7 @@ void ClangImporter::Implementation::importAttributes(
// Map __attribute__((warn_unused_result)).
if (!ClangDecl->hasAttr<clang::WarnUnusedResultAttr>()) {
if (auto MD = dyn_cast<FuncDecl>(MappedDecl)) {
if (!MD->getResultType()->isVoid()) {
if (!MD->getResultInterfaceType()->isVoid()) {
MD->getAttrs().add(new (C) DiscardableResultAttr(/*implicit*/true));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ static bool isTestCandidate(ValueDecl *D) {
return false;

// 3. ...that returns void...
Type RetTy = FD->getResultType();
Type RetTy = FD->getResultInterfaceType();
if (RetTy && !RetTy->isVoid())
return false;

Expand Down
3 changes: 2 additions & 1 deletion lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
} else {
auto func = cast<FuncDecl>(AFD);
OptionalTypeKind optionalKind;
(void)func->getResultType()->getAnyOptionalObjectType(optionalKind);
(void)func->getResultInterfaceType()
->getAnyOptionalObjectType(optionalKind);
printNullability(optionalKind,
NullabilityPrintKind::ContextSensitive);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2342,8 +2342,8 @@ static bool mayLieAboutNonOptionalReturn(SILModule &M,
// Functions that return non-optional reference type and were imported from
// Objective-C.
if (auto func = dyn_cast<FuncDecl>(decl)) {
assert((isVerbatimNullableTypeInC(M, func->getResultType())
|| func->getResultType()->hasArchetype())
assert((func->getResultInterfaceType()->hasTypeParameter()
|| isVerbatimNullableTypeInC(M, func->getResultInterfaceType()))
&& "func's result type is not nullable?!");
return func->hasClangNode();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ SILGenFunction::emitClosureValue(SILLocation loc, SILDeclRef constant,
void SILGenFunction::emitFunction(FuncDecl *fd) {
MagicFunctionName = SILGenModule::getMagicFunctionName(fd);

Type resultTy = fd->getResultType();
Type resultTy = ArchetypeBuilder::mapTypeIntoContext(
fd, fd->getResultInterfaceType());
emitProlog(fd, fd->getParameterLists(), resultTy, fd->hasThrows());
prepareEpilog(resultTy, fd->hasThrows(), CleanupLocation(fd));

Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Mandatory/DataflowDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void diagnoseMissingReturn(const UnreachableInst *UI,
Type ResTy;

if (auto *FD = FLoc.getAsASTNode<FuncDecl>()) {
ResTy = FD->getResultType();
ResTy = FD->getResultInterfaceType();
} else if (auto *CE = FLoc.getAsASTNode<ClosureExpr>()) {
ResTy = CE->getResultType();
} else {
Expand Down
8 changes: 5 additions & 3 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,9 @@ bool swift::fixItOverrideDeclarationTypes(TypeChecker &TC,
}
if (auto *method = dyn_cast<FuncDecl>(decl)) {
auto *baseMethod = cast<FuncDecl>(base);
fixedAny |= checkType(method->getBodyResultType(),
baseMethod->getResultType(),
auto resultType = ArchetypeBuilder::mapTypeIntoContext(
baseMethod, baseMethod->getResultInterfaceType());
fixedAny |= checkType(method->getBodyResultType(), resultType,
method->getBodyResultTypeLoc().getSourceRange());
}
return fixedAny;
Expand Down Expand Up @@ -4018,7 +4019,8 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
bool returnsSelf = false;

if (auto func = dyn_cast<FuncDecl>(afd)) {
resultType = func->getResultType();
resultType = func->getResultInterfaceType();
resultType = ArchetypeBuilder::mapTypeIntoContext(func, resultType);
returnsSelf = func->hasDynamicSelf();
} else if (isa<ConstructorDecl>(afd)) {
resultType = contextType;
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3188,7 +3188,7 @@ bool TypeChecker::isRepresentableInObjC(
}

if (auto FD = dyn_cast<FuncDecl>(AFD)) {
Type ResultType = FD->getResultType();
Type ResultType = FD->getResultInterfaceType();
if (!ResultType->isVoid() &&
!ResultType->isRepresentableIn(ForeignLanguage::ObjectiveC,
const_cast<FuncDecl *>(FD))) {
Expand All @@ -3212,7 +3212,7 @@ bool TypeChecker::isRepresentableInObjC(

const ConstructorDecl *ctor = nullptr;
if (auto func = dyn_cast<FuncDecl>(AFD)) {
resultType = func->getResultType();
resultType = func->getResultInterfaceType();
throwsLoc = func->getThrowsLoc();
} else {
ctor = cast<ConstructorDecl>(AFD);
Expand Down
8 changes: 8 additions & 0 deletions test/api-digester/source-stability.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Var Set.startIndex has declared type change from SetIndex<Element> to Set<Elemen
Func Dictionary.index(after:) has return type change from DictionaryIndex<Key, Value> to Dictionary<Key, Value>.Index
Func Dictionary.index(forKey:) has return type change from DictionaryIndex<Key, Value>? to Dictionary<Key, Value>.Index?
Func Dictionary.remove(at:) has 1st parameter type change from DictionaryIndex<Key, Value> to Dictionary<Key, Value>.Index

/* False positives due to archetype -> interface type change */
Func OptionSet.insert(_:) has return type change from (inserted: Bool, memberAfterInsert: Self) to (inserted: Bool, memberAfterInsert: Self.Element)
Func OptionSet.remove(_:) has return type change from Self? to Self.Element?
Func OptionSet.update(with:) has return type change from Self? to Self.Element?
Func RandomAccessCollection.distance(from:to:) has return type change from Self.IndexDistance to Self.Index.Stride

/* More DictionaryIndex / SetIndex */
Func Set.index(after:) has return type change from SetIndex<Element> to Set<Element>.Index
Func Set.index(of:) has return type change from SetIndex<Element>? to Set<Element>.Index?
Func Set.remove(at:) has 1st parameter type change from SetIndex<Element> to Set<Element>.Index
Expand Down
2 changes: 1 addition & 1 deletion tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ static SDKNode *constructTypeNode(SDKContext &Ctx, Type T) {
static SDKNode *constructFunctionNode(SDKContext &Ctx, FuncDecl* FD,
SDKNodeKind Kind, bool SkipFirst) {
auto Func = SDKNodeInitInfo(Ctx, FD).createSDKNode(Kind);
Func->addChild(constructTypeNode(Ctx, FD->getResultType()));
Func->addChild(constructTypeNode(Ctx, FD->getResultInterfaceType()));
for (auto *paramList : FD->getParameterLists()) {
for (auto param : *paramList)
Func->addChild(constructTypeNode(Ctx, param->getInterfaceType()));
Expand Down

0 comments on commit 5b8524a

Please sign in to comment.