Skip to content

Commit 91dffc9

Browse files
committed
Sema: Use AnyFunctionType::printParams() to print argument lists instead of printing a tuple type
Once the '@escaping' bit is removed from TupleTypeElt, it no longer makes sense to print argument lists as if they were TupleTypes or ParenTypes, since function types are '@escaping' by default inside tuples but not in argument lists. Instead, print ArrayRef<AnyFunctionType::Param> directly. For now this introduces some awkward usages of AnyFunctionType::decomposeInput(); these will go away once the AST is changed to represent the argument list as a list of expressions and not a single tuple expression.
1 parent 7488798 commit 91dffc9

File tree

7 files changed

+60
-51
lines changed

7 files changed

+60
-51
lines changed

include/swift/AST/Types.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,9 +3023,13 @@ class AnyFunctionType : public TypeBase {
30233023
/// replaced.
30243024
AnyFunctionType *withExtInfo(ExtInfo info) const;
30253025

3026-
void printParams(raw_ostream &OS,
3027-
const PrintOptions &PO = PrintOptions()) const;
3028-
void printParams(ASTPrinter &Printer, const PrintOptions &PO) const;
3026+
static void printParams(ArrayRef<Param> Params, raw_ostream &OS,
3027+
const PrintOptions &PO = PrintOptions());
3028+
static void printParams(ArrayRef<Param> Params, ASTPrinter &Printer,
3029+
const PrintOptions &PO);
3030+
3031+
static std::string getParamListAsString(ArrayRef<Param> Params,
3032+
const PrintOptions &PO = PrintOptions());
30293033

30303034
// Implement isa/cast/dyncast/etc.
30313035
static bool classof(const TypeBase *T) {

lib/AST/ASTPrinter.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4204,17 +4204,28 @@ void Type::print(ASTPrinter &Printer, const PrintOptions &PO) const {
42044204
TypePrinter(Printer, PO).visit(*this);
42054205
}
42064206

4207-
void AnyFunctionType::printParams(raw_ostream &OS, const
4208-
PrintOptions &PO) const {
4207+
void AnyFunctionType::printParams(ArrayRef<AnyFunctionType::Param> Params,
4208+
raw_ostream &OS,
4209+
const PrintOptions &PO) {
42094210
StreamPrinter Printer(OS);
4210-
printParams(Printer, PO);
4211+
printParams(Params, Printer, PO);
42114212
}
4212-
void AnyFunctionType::printParams(ASTPrinter &Printer,
4213-
const PrintOptions &PO) const {
4214-
TypePrinter(Printer, PO).visitAnyFunctionTypeParams(getParams(),
4213+
void AnyFunctionType::printParams(ArrayRef<AnyFunctionType::Param> Params,
4214+
ASTPrinter &Printer,
4215+
const PrintOptions &PO) {
4216+
TypePrinter(Printer, PO).visitAnyFunctionTypeParams(Params,
42154217
/*printLabels*/true);
42164218
}
42174219

4220+
std::string
4221+
AnyFunctionType::getParamListAsString(ArrayRef<AnyFunctionType::Param> Params,
4222+
const PrintOptions &PO) {
4223+
SmallString<16> Scratch;
4224+
llvm::raw_svector_ostream OS(Scratch);
4225+
AnyFunctionType::printParams(Params, OS);
4226+
return OS.str();
4227+
}
4228+
42184229
void LayoutConstraintInfo::print(raw_ostream &OS,
42194230
const PrintOptions &PO) const {
42204231
StreamPrinter Printer(OS);

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ class Verifier : public ASTWalker {
17781778
"\nArgument type: ";
17791779
InputExprTy.print(Out);
17801780
Out << "\nParameter types: ";
1781-
FT->printParams(Out);
1781+
AnyFunctionType::printParams(FT->getParams(), Out);
17821782
Out << "\n";
17831783
E->dump(Out);
17841784
Out << "\n";

lib/IDE/CodeCompletion.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,8 +2454,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
24542454
{
24552455
llvm::raw_svector_ostream OS(TypeStr);
24562456
if (IsImplicitlyCurriedInstanceMethod) {
2457-
ResultType->castTo<AnyFunctionType>()->printParams(OS);
2458-
ResultType = ResultType->castTo<AnyFunctionType>()->getResult();
2457+
auto *FnType = ResultType->castTo<AnyFunctionType>();
2458+
AnyFunctionType::printParams(FnType->getParams(), OS);
2459+
ResultType = FnType->getResult();
24592460
OS << " -> ";
24602461
}
24612462

lib/Sema/CSDiag.cpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ using namespace swift;
4040
using namespace constraints;
4141

4242
namespace swift {
43-
std::string getTypeListString(Type type) {
44-
std::string result;
45-
46-
// Always make sure to have at least one set of parens
47-
bool forceParens =
48-
!type->is<TupleType>() && !type->hasParenSugar();
49-
if (forceParens)
50-
result.push_back('(');
51-
52-
llvm::raw_string_ostream OS(result);
53-
type->print(OS);
54-
OS.flush();
55-
56-
if (forceParens)
57-
result.push_back(')');
58-
59-
return result;
60-
}
61-
6243
Type replaceTypeParametersWithUnresolved(Type ty) {
6344
if (!ty) return ty;
6445

@@ -4325,8 +4306,11 @@ bool FailureDiagnosis::diagnoseArgumentGenericRequirements(
43254306
overloadName, CS.getType(ArgExpr));
43264307
} else {
43274308
bool isInitializer = isa<ConstructorDecl>(Candidate);
4309+
4310+
SmallVector<AnyFunctionType::Param, 8> Params;
4311+
AnyFunctionType::decomposeInput(CS.getType(ArgExpr), Params);
43284312
TC.diagnose(ArgExpr->getLoc(), diag::cannot_call_with_params,
4329-
overloadName, getTypeListString(CS.getType(ArgExpr)),
4313+
overloadName, AnyFunctionType::getParamListAsString(Params),
43304314
isInitializer);
43314315
}
43324316

@@ -5037,9 +5021,6 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
50375021

50385022
// If we have multiple candidates that we fail to match, just say we have
50395023
// the wrong labels and list the candidates out.
5040-
5041-
// TODO: It would be nice to use an analog of getTypeListString that
5042-
// doesn't include the argument types.
50435024
diagnose(callExpr->getLoc(), diag::wrong_argument_labels_overload,
50445025
getParamListAsString(args))
50455026
.highlight(argExpr->getSourceRange());
@@ -5227,7 +5208,9 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
52275208
if (diagnoseRawRepresentableMismatch(calleeInfo, argExpr, argLabels))
52285209
return true;
52295210

5230-
std::string argString = getTypeListString(CS.getType(argExpr));
5211+
SmallVector<AnyFunctionType::Param, 8> params;
5212+
AnyFunctionType::decomposeInput(CS.getType(argExpr), params);
5213+
auto argString = AnyFunctionType::getParamListAsString(params);
52315214

52325215
// If we couldn't get the name of the callee, then it must be something of a
52335216
// more complex "value of function type".

lib/Sema/CSDiag.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ namespace swift {
2626
class Type;
2727
class SourceLoc;
2828

29-
std::string getTypeListString(Type type);
30-
3129
/// Rewrite any type variables & archetypes in the specified type with
3230
/// UnresolvedType.
3331
Type replaceTypeParametersWithUnresolved(Type ty);

lib/Sema/CalleeCandidateInfo.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -936,19 +936,31 @@ operator=(const CalleeCandidateInfo &CCI) {
936936
void CalleeCandidateInfo::
937937
suggestPotentialOverloads(SourceLoc loc, bool isResult) {
938938
std::set<std::string> sorted;
939-
940-
// FIXME2: For (T,T) & (Self, Self), emit this as two candidates, one using
941-
// the LHS and one using the RHS type for T's.
942-
for (auto cand : candidates) {
943-
auto type = isResult ? cand.getResultType()
944-
: cand.getArgumentType(CS.getASTContext());
945-
if (type.isNull())
946-
continue;
947-
948-
// If we've already seen this (e.g. decls overridden on the result type),
949-
// ignore this one.
950-
auto name = isResult ? type->getString() : getTypeListString(type);
951-
sorted.insert(name);
939+
940+
if (isResult) {
941+
for (auto cand : candidates) {
942+
auto type = cand.getResultType();
943+
if (type.isNull())
944+
continue;
945+
946+
// If we've already seen this (e.g. decls overridden on the result type),
947+
// ignore this one.
948+
auto name = type->getString();
949+
sorted.insert(name);
950+
}
951+
} else {
952+
// FIXME2: For (T,T) & (Self, Self), emit this as two candidates, one using
953+
// the LHS and one using the RHS type for T's.
954+
for (auto cand : candidates) {
955+
auto type = cand.getFunctionType();
956+
if (type == nullptr)
957+
continue;
958+
959+
// If we've already seen this (e.g. decls overridden on the result type),
960+
// ignore this one.
961+
auto name = AnyFunctionType::getParamListAsString(type->getParams());
962+
sorted.insert(name);
963+
}
952964
}
953965

954966
if (sorted.empty())

0 commit comments

Comments
 (0)