Skip to content

Commit f946392

Browse files
authored
Merge pull request #31390 from CodaFi/tone-loc
Strip Out More TypeLocs
2 parents d30661d + 1319b53 commit f946392

File tree

18 files changed

+131
-144
lines changed

18 files changed

+131
-144
lines changed

include/swift/AST/Expr.h

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,7 +3386,7 @@ class BridgeToObjCExpr : public ImplicitConversionExpr {
33863386
/// UnresolvedSpecializeExpr - Represents an explicit specialization using
33873387
/// a type parameter list (e.g. "Vector<Int>") that has not been resolved.
33883388
class UnresolvedSpecializeExpr final : public Expr,
3389-
private llvm::TrailingObjects<UnresolvedSpecializeExpr, TypeLoc> {
3389+
private llvm::TrailingObjects<UnresolvedSpecializeExpr, TypeRepr *> {
33903390
friend TrailingObjects;
33913391

33923392
Expr *SubExpr;
@@ -3395,31 +3395,27 @@ class UnresolvedSpecializeExpr final : public Expr,
33953395

33963396
UnresolvedSpecializeExpr(Expr *SubExpr,
33973397
SourceLoc LAngleLoc,
3398-
ArrayRef<TypeLoc> UnresolvedParams,
3398+
ArrayRef<TypeRepr *> UnresolvedParams,
33993399
SourceLoc RAngleLoc)
34003400
: Expr(ExprKind::UnresolvedSpecialize, /*Implicit=*/false),
34013401
SubExpr(SubExpr), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc) {
34023402
Bits.UnresolvedSpecializeExpr.NumUnresolvedParams = UnresolvedParams.size();
34033403
std::uninitialized_copy(UnresolvedParams.begin(), UnresolvedParams.end(),
3404-
getTrailingObjects<TypeLoc>());
3404+
getTrailingObjects<TypeRepr *>());
34053405
}
34063406

34073407
public:
34083408
static UnresolvedSpecializeExpr *
34093409
create(ASTContext &ctx, Expr *SubExpr, SourceLoc LAngleLoc,
3410-
ArrayRef<TypeLoc> UnresolvedParams, SourceLoc RAngleLoc);
3410+
ArrayRef<TypeRepr *> UnresolvedParams, SourceLoc RAngleLoc);
34113411

34123412
Expr *getSubExpr() const { return SubExpr; }
34133413
void setSubExpr(Expr *e) { SubExpr = e; }
34143414

34153415
/// Retrieve the list of type parameters. These parameters have not yet
34163416
/// been bound to archetypes of the entity to be specialized.
3417-
ArrayRef<TypeLoc> getUnresolvedParams() const {
3418-
return {getTrailingObjects<TypeLoc>(),
3419-
Bits.UnresolvedSpecializeExpr.NumUnresolvedParams};
3420-
}
3421-
MutableArrayRef<TypeLoc> getUnresolvedParams() {
3422-
return {getTrailingObjects<TypeLoc>(),
3417+
ArrayRef<TypeRepr *> getUnresolvedParams() const {
3418+
return {getTrailingObjects<TypeRepr *>(),
34233419
Bits.UnresolvedSpecializeExpr.NumUnresolvedParams};
34243420
}
34253421

@@ -3706,15 +3702,15 @@ class ClosureExpr : public AbstractClosureExpr {
37063702
SourceLoc InLoc;
37073703

37083704
/// The explicitly-specified result type.
3709-
TypeLoc ExplicitResultType;
3705+
TypeExpr *ExplicitResultType;
37103706

37113707
/// The body of the closure, along with a bit indicating whether it
37123708
/// was originally just a single expression.
37133709
llvm::PointerIntPair<BraceStmt *, 1, bool> Body;
37143710
public:
37153711
ClosureExpr(SourceRange bracketRange, VarDecl *capturedSelfDecl,
37163712
ParameterList *params, SourceLoc throwsLoc, SourceLoc arrowLoc,
3717-
SourceLoc inLoc, TypeLoc explicitResultType,
3713+
SourceLoc inLoc, TypeExpr *explicitResultType,
37183714
unsigned discriminator, DeclContext *parent)
37193715
: AbstractClosureExpr(ExprKind::Closure, Type(), /*Implicit=*/false,
37203716
discriminator, parent),
@@ -3773,20 +3769,15 @@ class ClosureExpr : public AbstractClosureExpr {
37733769
return ThrowsLoc;
37743770
}
37753771

3776-
/// Retrieve the explicit result type location information.
3777-
TypeLoc &getExplicitResultTypeLoc() {
3772+
Type getExplicitResultType() const {
37783773
assert(hasExplicitResultType() && "No explicit result type");
3779-
return ExplicitResultType;
3774+
return ExplicitResultType->getInstanceType();
37803775
}
3776+
void setExplicitResultType(Type ty);
37813777

37823778
TypeRepr *getExplicitResultTypeRepr() const {
37833779
assert(hasExplicitResultType() && "No explicit result type");
3784-
return ExplicitResultType.getTypeRepr();
3785-
}
3786-
3787-
void setExplicitResultType(SourceLoc arrowLoc, TypeLoc resultType) {
3788-
ArrowLoc = arrowLoc;
3789-
ExplicitResultType = resultType;
3780+
return ExplicitResultType->getTypeRepr();
37903781
}
37913782

37923783
/// Determine whether the closure has a single expression for its
@@ -4939,25 +4930,20 @@ class UnresolvedPatternExpr : public Expr {
49394930
class EditorPlaceholderExpr : public Expr {
49404931
Identifier Placeholder;
49414932
SourceLoc Loc;
4942-
TypeLoc PlaceholderTy;
4933+
TypeRepr *PlaceholderTy;
49434934
TypeRepr *ExpansionTyR;
49444935
Expr *SemanticExpr;
49454936

49464937
public:
49474938
EditorPlaceholderExpr(Identifier Placeholder, SourceLoc Loc,
4948-
TypeLoc PlaceholderTy,
4949-
TypeRepr *ExpansionTyR)
4950-
: Expr(ExprKind::EditorPlaceholder, /*Implicit=*/false),
4951-
Placeholder(Placeholder), Loc(Loc),
4952-
PlaceholderTy(PlaceholderTy),
4953-
ExpansionTyR(ExpansionTyR),
4954-
SemanticExpr(nullptr) {
4955-
}
4939+
TypeRepr *PlaceholderTy, TypeRepr *ExpansionTyR)
4940+
: Expr(ExprKind::EditorPlaceholder, /*Implicit=*/false),
4941+
Placeholder(Placeholder), Loc(Loc), PlaceholderTy(PlaceholderTy),
4942+
ExpansionTyR(ExpansionTyR), SemanticExpr(nullptr) {}
49564943

49574944
Identifier getPlaceholder() const { return Placeholder; }
49584945
SourceRange getSourceRange() const { return Loc; }
4959-
TypeLoc &getTypeLoc() { return PlaceholderTy; }
4960-
TypeLoc getTypeLoc() const { return PlaceholderTy; }
4946+
TypeRepr *getPlaceholderTypeRepr() const { return PlaceholderTy; }
49614947
SourceLoc getTrailingAngleBracketLoc() const {
49624948
return Loc.getAdvancedLoc(Placeholder.getLength() - 1);
49634949
}

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ class Parser {
15511551
ParameterList *&params,
15521552
SourceLoc &throwsLoc,
15531553
SourceLoc &arrowLoc,
1554-
TypeRepr *&explicitResultType,
1554+
TypeExpr *&explicitResultType,
15551555
SourceLoc &inLoc);
15561556

15571557
Expr *parseExprAnonClosureArg();

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2704,7 +2704,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
27042704
}
27052705
}
27062706
OS << '\n';
2707-
auto *TyR = E->getTypeLoc().getTypeRepr();
2707+
auto *TyR = E->getPlaceholderTypeRepr();
27082708
auto *ExpTyR = E->getTypeForExpansion();
27092709
if (TyR)
27102710
printRec(TyR);

lib/AST/ASTWalker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
810810
Expr *visitClosureExpr(ClosureExpr *expr) {
811811
visit(expr->getParameters());
812812

813-
if (expr->hasExplicitResultType())
814-
if (doIt(expr->getExplicitResultTypeLoc()))
813+
if (expr->hasExplicitResultType()) {
814+
if (doIt(expr->getExplicitResultTypeRepr()))
815815
return nullptr;
816+
}
816817

817818
// Handle single-expression closures.
818819
if (expr->hasSingleExpressionBody()) {

lib/AST/Expr.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,9 @@ ErasureExpr *ErasureExpr::create(ASTContext &ctx, Expr *subExpr, Type type,
11911191

11921192
UnresolvedSpecializeExpr *UnresolvedSpecializeExpr::create(ASTContext &ctx,
11931193
Expr *SubExpr, SourceLoc LAngleLoc,
1194-
ArrayRef<TypeLoc> UnresolvedParams,
1194+
ArrayRef<TypeRepr *> UnresolvedParams,
11951195
SourceLoc RAngleLoc) {
1196-
auto size = totalSizeToAlloc<TypeLoc>(UnresolvedParams.size());
1196+
auto size = totalSizeToAlloc<TypeRepr *>(UnresolvedParams.size());
11971197
auto mem = ctx.Allocate(size, alignof(UnresolvedSpecializeExpr));
11981198
return ::new(mem) UnresolvedSpecializeExpr(SubExpr, LAngleLoc,
11991199
UnresolvedParams, RAngleLoc);
@@ -1861,6 +1861,11 @@ bool ClosureExpr::capturesSelfEnablingImplictSelf() const {
18611861
return false;
18621862
}
18631863

1864+
void ClosureExpr::setExplicitResultType(Type ty) {
1865+
assert(ty && !ty->hasTypeVariable());
1866+
ExplicitResultType->setType(MetatypeType::get(ty));
1867+
}
1868+
18641869
FORWARD_SOURCE_LOCS_TO(AutoClosureExpr, Body)
18651870

18661871
void AutoClosureExpr::setBody(Expr *E) {

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/Basic/SourceManager.h"
3030
#include "swift/IDE/CodeCompletion.h"
3131
#include "swift/Sema/IDETypeChecking.h"
32+
#include "swift/Subsystems.h"
3233
#include "clang/AST/Attr.h"
3334
#include "clang/AST/Decl.h"
3435

@@ -209,10 +210,23 @@ Type swift::ide::getReturnTypeFromContext(const DeclContext *DC) {
209210
if (ACE->getType() && !ACE->getType()->hasError())
210211
return ACE->getResultType();
211212
if (auto CE = dyn_cast<ClosureExpr>(ACE)) {
212-
if (CE->hasExplicitResultType())
213-
return const_cast<ClosureExpr *>(CE)
214-
->getExplicitResultTypeLoc()
215-
.getType();
213+
if (CE->hasExplicitResultType()) {
214+
if (auto ty = CE->getExplicitResultType()) {
215+
return ty;
216+
}
217+
218+
auto typeLoc = TypeLoc{CE->getExplicitResultTypeRepr()};
219+
if (swift::performTypeLocChecking(DC->getASTContext(),
220+
typeLoc,
221+
/*isSILMode*/ false,
222+
/*isSILType*/ false,
223+
DC->getGenericEnvironmentOfContext(),
224+
const_cast<DeclContext *>(DC),
225+
/*diagnostics*/ false)) {
226+
return Type();
227+
}
228+
return typeLoc.getType();
229+
}
216230
}
217231
}
218232
return Type();

lib/IDE/Formatting.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,9 +2295,8 @@ class FormatWalker : public ASTWalker {
22952295

22962296
SourceLoc ContextLoc = getContextLocForArgs(SM, USE);
22972297
ListAligner Aligner(SM, TargetLocation, ContextLoc, L, R);
2298-
for (auto &Arg: USE->getUnresolvedParams()) {
2299-
if (auto *T = Arg.getTypeRepr())
2300-
Aligner.updateAlignment(T->getSourceRange(), T);
2298+
for (auto *T : USE->getUnresolvedParams()) {
2299+
Aligner.updateAlignment(T->getSourceRange(), T);
23012300
}
23022301
return Aligner.getContextAndSetAlignment(CtxOverride);
23032302
}

lib/IDE/SyntaxModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
663663
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM, E->getSourceRange());
664664
if (Closure->hasExplicitResultType())
665665
SN.TypeRange = charSourceRangeFromSourceRange(SM,
666-
Closure->getExplicitResultTypeLoc().getSourceRange());
666+
Closure->getExplicitResultTypeRepr()->getSourceRange());
667667

668668
pushStructureNode(SN, Closure);
669669

lib/Parse/ParseExpr.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,13 +1161,10 @@ Parser::parseExprPostfixSuffix(ParserResult<Expr> Result, bool isExprBasic,
11611161
if (argStat.isError())
11621162
diagnose(LAngleLoc, diag::while_parsing_as_left_angle_bracket);
11631163

1164-
SmallVector<TypeLoc, 8> locArgs;
1165-
for (auto ty : args)
1166-
locArgs.push_back(ty);
11671164
SyntaxContext->createNodeInPlace(SyntaxKind::SpecializeExpr);
11681165
Result = makeParserResult(
11691166
Result, UnresolvedSpecializeExpr::create(
1170-
Context, Result.get(), LAngleLoc, locArgs, RAngleLoc));
1167+
Context, Result.get(), LAngleLoc, args, RAngleLoc));
11711168
}
11721169

11731170
continue;
@@ -2312,10 +2309,7 @@ Expr *Parser::parseExprIdentifier() {
23122309
}
23132310

23142311
if (hasGenericArgumentList) {
2315-
SmallVector<TypeLoc, 8> locArgs;
2316-
for (auto ty : args)
2317-
locArgs.push_back(ty);
2318-
E = UnresolvedSpecializeExpr::create(Context, E, LAngleLoc, locArgs,
2312+
E = UnresolvedSpecializeExpr::create(Context, E, LAngleLoc, args,
23192313
RAngleLoc);
23202314
}
23212315
return E;
@@ -2326,14 +2320,14 @@ Expr *Parser::parseExprEditorPlaceholder(Token PlaceholderTok,
23262320
assert(PlaceholderTok.is(tok::identifier));
23272321
assert(PlaceholderId.isEditorPlaceholder());
23282322

2329-
auto parseTypeForPlaceholder = [&](TypeLoc &TyLoc, TypeRepr *&ExpansionTyR) {
2323+
auto parseTypeForPlaceholder = [&]() -> std::pair<TypeRepr *, TypeRepr *> {
23302324
Optional<EditorPlaceholderData> DataOpt =
23312325
swift::parseEditorPlaceholder(PlaceholderTok.getText());
23322326
if (!DataOpt)
2333-
return;
2327+
return {nullptr, nullptr};
23342328
StringRef TypeStr = DataOpt->Type;
23352329
if (TypeStr.empty())
2336-
return;
2330+
return {nullptr, nullptr};
23372331

23382332
// Ensure that we restore the parser state at exit.
23392333
ParserPositionRAII PPR(*this);
@@ -2363,21 +2357,21 @@ Expr *Parser::parseExprEditorPlaceholder(Token PlaceholderTok,
23632357
return parseType().getPtrOrNull();
23642358
};
23652359

2366-
TypeRepr *TyR = parseTypeString(TypeStr);
2367-
TyLoc = TyR;
2360+
TypeRepr *PlaceholderTyR = parseTypeString(TypeStr);
2361+
TypeRepr *ExpansionTyR = nullptr;
23682362
if (DataOpt->TypeForExpansion == TypeStr) {
2369-
ExpansionTyR = TyR;
2363+
ExpansionTyR = PlaceholderTyR;
23702364
} else {
23712365
ExpansionTyR = parseTypeString(DataOpt->TypeForExpansion);
23722366
}
2367+
return {PlaceholderTyR, ExpansionTyR};
23732368
};
23742369

2375-
TypeLoc TyLoc;
2370+
TypeRepr *PlaceholderTyR = nullptr;
23762371
TypeRepr *ExpansionTyR = nullptr;
2377-
parseTypeForPlaceholder(TyLoc, ExpansionTyR);
2378-
return new (Context) EditorPlaceholderExpr(PlaceholderId,
2379-
PlaceholderTok.getLoc(),
2380-
TyLoc, ExpansionTyR);
2372+
std::tie(PlaceholderTyR, ExpansionTyR) = parseTypeForPlaceholder();
2373+
return new (Context) EditorPlaceholderExpr(
2374+
PlaceholderId, PlaceholderTok.getLoc(), PlaceholderTyR, ExpansionTyR);
23812375
}
23822376

23832377
// Extract names of the tuple elements and preserve the structure
@@ -2422,7 +2416,7 @@ parseClosureSignatureIfPresent(SourceRange &bracketRange,
24222416
VarDecl *&capturedSelfDecl,
24232417
ParameterList *&params, SourceLoc &throwsLoc,
24242418
SourceLoc &arrowLoc,
2425-
TypeRepr *&explicitResultType, SourceLoc &inLoc){
2419+
TypeExpr *&explicitResultType, SourceLoc &inLoc){
24262420
// Clear out result parameters.
24272421
bracketRange = SourceRange();
24282422
capturedSelfDecl = nullptr;
@@ -2686,12 +2680,14 @@ parseClosureSignatureIfPresent(SourceRange &bracketRange,
26862680
arrowLoc = consumeToken();
26872681

26882682
// Parse the type.
2689-
explicitResultType =
2683+
auto *explicitResultTypeRepr =
26902684
parseType(diag::expected_closure_result_type).getPtrOrNull();
2691-
if (!explicitResultType) {
2685+
if (!explicitResultTypeRepr) {
26922686
// If we couldn't parse the result type, clear out the arrow location.
26932687
arrowLoc = SourceLoc();
26942688
invalid = true;
2689+
} else {
2690+
explicitResultType = new (Context) TypeExpr(explicitResultTypeRepr);
26952691
}
26962692
}
26972693
}
@@ -2791,7 +2787,7 @@ ParserResult<Expr> Parser::parseExprClosure() {
27912787
ParameterList *params = nullptr;
27922788
SourceLoc throwsLoc;
27932789
SourceLoc arrowLoc;
2794-
TypeRepr *explicitResultType;
2790+
TypeExpr *explicitResultType;
27952791
SourceLoc inLoc;
27962792
parseClosureSignatureIfPresent(bracketRange, captureList,
27972793
capturedSelfDecl, params, throwsLoc,

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7696,6 +7696,10 @@ namespace {
76967696
auto *params = closure->getParameters();
76977697
TypeChecker::coerceParameterListToType(params, closure, fnType);
76987698

7699+
if (closure->hasExplicitResultType()) {
7700+
closure->setExplicitResultType(fnType->getResult());
7701+
}
7702+
76997703
if (auto transform =
77007704
Rewriter.getAppliedBuilderTransform(closure)) {
77017705
// Apply the function builder to the closure. We want to be in the

0 commit comments

Comments
 (0)