Skip to content

Commit f9a506d

Browse files
committed
[NFC] Strip EditorPlaceholderExpr of its TypeLoc
1 parent 19ab68d commit f9a506d

File tree

6 files changed

+43
-57
lines changed

6 files changed

+43
-57
lines changed

include/swift/AST/Expr.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4936,25 +4936,20 @@ class UnresolvedPatternExpr : public Expr {
49364936
class EditorPlaceholderExpr : public Expr {
49374937
Identifier Placeholder;
49384938
SourceLoc Loc;
4939-
TypeLoc PlaceholderTy;
4939+
TypeRepr *PlaceholderTy;
49404940
TypeRepr *ExpansionTyR;
49414941
Expr *SemanticExpr;
49424942

49434943
public:
49444944
EditorPlaceholderExpr(Identifier Placeholder, SourceLoc Loc,
4945-
TypeLoc PlaceholderTy,
4946-
TypeRepr *ExpansionTyR)
4947-
: Expr(ExprKind::EditorPlaceholder, /*Implicit=*/false),
4948-
Placeholder(Placeholder), Loc(Loc),
4949-
PlaceholderTy(PlaceholderTy),
4950-
ExpansionTyR(ExpansionTyR),
4951-
SemanticExpr(nullptr) {
4952-
}
4945+
TypeRepr *PlaceholderTy, TypeRepr *ExpansionTyR)
4946+
: Expr(ExprKind::EditorPlaceholder, /*Implicit=*/false),
4947+
Placeholder(Placeholder), Loc(Loc), PlaceholderTy(PlaceholderTy),
4948+
ExpansionTyR(ExpansionTyR), SemanticExpr(nullptr) {}
49534949

49544950
Identifier getPlaceholder() const { return Placeholder; }
49554951
SourceRange getSourceRange() const { return Loc; }
4956-
TypeLoc &getTypeLoc() { return PlaceholderTy; }
4957-
TypeLoc getTypeLoc() const { return PlaceholderTy; }
4952+
TypeRepr *getPlaceholderTypeRepr() const { return PlaceholderTy; }
49584953
SourceLoc getTrailingAngleBracketLoc() const {
49594954
return Loc.getAdvancedLoc(Placeholder.getLength() - 1);
49604955
}

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2695,7 +2695,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
26952695
}
26962696
}
26972697
OS << '\n';
2698-
auto *TyR = E->getTypeLoc().getTypeRepr();
2698+
auto *TyR = E->getPlaceholderTypeRepr();
26992699
auto *ExpTyR = E->getTypeForExpansion();
27002700
if (TyR)
27012701
printRec(TyR);

lib/Parse/ParseExpr.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,14 +2320,14 @@ Expr *Parser::parseExprEditorPlaceholder(Token PlaceholderTok,
23202320
assert(PlaceholderTok.is(tok::identifier));
23212321
assert(PlaceholderId.isEditorPlaceholder());
23222322

2323-
auto parseTypeForPlaceholder = [&](TypeLoc &TyLoc, TypeRepr *&ExpansionTyR) {
2323+
auto parseTypeForPlaceholder = [&]() -> std::pair<TypeRepr *, TypeRepr *> {
23242324
Optional<EditorPlaceholderData> DataOpt =
23252325
swift::parseEditorPlaceholder(PlaceholderTok.getText());
23262326
if (!DataOpt)
2327-
return;
2327+
return {nullptr, nullptr};
23282328
StringRef TypeStr = DataOpt->Type;
23292329
if (TypeStr.empty())
2330-
return;
2330+
return {nullptr, nullptr};
23312331

23322332
// Ensure that we restore the parser state at exit.
23332333
ParserPositionRAII PPR(*this);
@@ -2357,21 +2357,21 @@ Expr *Parser::parseExprEditorPlaceholder(Token PlaceholderTok,
23572357
return parseType().getPtrOrNull();
23582358
};
23592359

2360-
TypeRepr *TyR = parseTypeString(TypeStr);
2361-
TyLoc = TyR;
2360+
TypeRepr *PlaceholderTyR = parseTypeString(TypeStr);
2361+
TypeRepr *ExpansionTyR = nullptr;
23622362
if (DataOpt->TypeForExpansion == TypeStr) {
2363-
ExpansionTyR = TyR;
2363+
ExpansionTyR = PlaceholderTyR;
23642364
} else {
23652365
ExpansionTyR = parseTypeString(DataOpt->TypeForExpansion);
23662366
}
2367+
return {PlaceholderTyR, ExpansionTyR};
23672368
};
23682369

2369-
TypeLoc TyLoc;
2370+
TypeRepr *PlaceholderTyR = nullptr;
23702371
TypeRepr *ExpansionTyR = nullptr;
2371-
parseTypeForPlaceholder(TyLoc, ExpansionTyR);
2372-
return new (Context) EditorPlaceholderExpr(PlaceholderId,
2373-
PlaceholderTok.getLoc(),
2374-
TyLoc, ExpansionTyR);
2372+
std::tie(PlaceholderTyR, ExpansionTyR) = parseTypeForPlaceholder();
2373+
return new (Context) EditorPlaceholderExpr(
2374+
PlaceholderId, PlaceholderTok.getLoc(), PlaceholderTyR, ExpansionTyR);
23752375
}
23762376

23772377
// Extract names of the tuple elements and preserve the structure

lib/Sema/CSGen.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,33 +3301,34 @@ namespace {
33013301
}
33023302

33033303
Type visitEditorPlaceholderExpr(EditorPlaceholderExpr *E) {
3304-
if (E->getTypeLoc().isNull()) {
3305-
auto locator = CS.getConstraintLocator(E);
3306-
3307-
// A placeholder may have any type, but default to Void type if
3308-
// otherwise unconstrained.
3309-
auto &placeholderTy
3310-
= editorPlaceholderVariables[currentEditorPlaceholderVariable];
3311-
if (!placeholderTy) {
3312-
placeholderTy = CS.createTypeVariable(locator, TVO_CanBindToNoEscape);
3313-
3314-
CS.addConstraint(ConstraintKind::Defaultable,
3315-
placeholderTy,
3316-
TupleType::getEmpty(CS.getASTContext()),
3317-
locator);
3318-
}
3304+
if (auto *placeholderRepr = E->getPlaceholderTypeRepr()) {
3305+
// Just resolve the referenced type.
3306+
// FIXME: The type reference needs to be opened into context.
3307+
return resolveTypeReferenceInExpression(placeholderRepr);
3308+
}
3309+
3310+
auto locator = CS.getConstraintLocator(E);
33193311

3320-
// Move to the next placeholder variable.
3321-
currentEditorPlaceholderVariable
3322-
= (currentEditorPlaceholderVariable + 1) %
3323-
numEditorPlaceholderVariables;
3312+
// A placeholder may have any type, but default to Void type if
3313+
// otherwise unconstrained.
3314+
auto &placeholderTy
3315+
= editorPlaceholderVariables[currentEditorPlaceholderVariable];
3316+
if (!placeholderTy) {
3317+
placeholderTy = CS.createTypeVariable(locator, TVO_CanBindToNoEscape);
33243318

3325-
return placeholderTy;
3319+
CS.addConstraint(ConstraintKind::Defaultable,
3320+
placeholderTy,
3321+
TupleType::getEmpty(CS.getASTContext()),
3322+
locator);
33263323
}
33273324

3328-
// NOTE: The type loc may be there but have failed to validate, in which
3329-
// case we return the null type.
3330-
return E->getType();
3325+
// Move to the next placeholder variable.
3326+
// FIXME: Cycling type variables like this is unsound.
3327+
currentEditorPlaceholderVariable
3328+
= (currentEditorPlaceholderVariable + 1) %
3329+
numEditorPlaceholderVariables;
3330+
3331+
return placeholderTy;
33313332
}
33323333

33333334
Type visitObjCSelectorExpr(ObjCSelectorExpr *E) {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,16 +1096,6 @@ namespace {
10961096
return finish(true, TypeChecker::resolveDeclRefExpr(unresolved, DC));
10971097
}
10981098

1099-
if (auto PlaceholderE = dyn_cast<EditorPlaceholderExpr>(expr)) {
1100-
if (!PlaceholderE->getTypeLoc().isNull()) {
1101-
if (!TypeChecker::validateType(
1102-
getASTContext(), PlaceholderE->getTypeLoc(),
1103-
TypeResolution::forContextual(DC), None))
1104-
expr->setType(PlaceholderE->getTypeLoc().getType());
1105-
}
1106-
return finish(true, expr);
1107-
}
1108-
11091099
// Let's try to figure out if `InOutExpr` is out of place early
11101100
// otherwise there is a risk of producing solutions which can't
11111101
// be later applied to AST and would result in the crash in some

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ class PlaceholderExpansionScanner {
15621562
PlaceholderFinder Finder(E->getStartLoc(), Found);
15631563
E->walk(Finder);
15641564
if (Found) {
1565-
if (auto TR = Found->getTypeLoc().getTypeRepr()) {
1565+
if (auto TR = Found->getPlaceholderTypeRepr()) {
15661566
TR->walk(ClosureWalker);
15671567
return ClosureWalker.FoundFunctionTypeRepr;
15681568
}

0 commit comments

Comments
 (0)