Skip to content

Commit 675141b

Browse files
authored
Merge pull request #26118 from slavapestov/shallow-clone-be-gone
AST: Remove LiteralExpr::shallowClone()
2 parents 277a4dc + 5022d8b commit 675141b

File tree

4 files changed

+3
-124
lines changed

4 files changed

+3
-124
lines changed

include/swift/AST/Expr.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,6 @@ class CodeCompletionExpr : public Expr {
599599
class LiteralExpr : public Expr {
600600
public:
601601
LiteralExpr(ExprKind Kind, bool Implicit) : Expr(Kind, Implicit) {}
602-
603-
// Make an exact copy of this one AST node.
604-
LiteralExpr *
605-
shallowClone(ASTContext &Ctx,
606-
llvm::function_ref<void(Expr *, Type)> setType,
607-
llvm::function_ref<Type(const Expr *)> getType) const;
608602

609603
static bool classof(const Expr *E) {
610604
return E->getKind() >= ExprKind::First_LiteralExpr &&

lib/AST/Expr.cpp

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -773,103 +773,6 @@ llvm::DenseMap<Expr *, unsigned> Expr::getPreorderIndexMap() {
773773
// Support methods for Exprs.
774774
//===----------------------------------------------------------------------===//
775775

776-
static LiteralExpr *
777-
shallowCloneImpl(const NilLiteralExpr *E, ASTContext &Ctx,
778-
llvm::function_ref<Type(const Expr *)> getType) {
779-
return new (Ctx) NilLiteralExpr(E->getLoc());
780-
}
781-
782-
static LiteralExpr *
783-
shallowCloneImpl(const IntegerLiteralExpr *E, ASTContext &Ctx,
784-
llvm::function_ref<Type(const Expr *)> getType) {
785-
auto res = new (Ctx) IntegerLiteralExpr(E->getDigitsText(),
786-
E->getSourceRange().End);
787-
if (E->isNegative())
788-
res->setNegative(E->getSourceRange().Start);
789-
return res;
790-
}
791-
792-
static LiteralExpr *
793-
shallowCloneImpl(const FloatLiteralExpr *E, ASTContext &Ctx,
794-
llvm::function_ref<Type(const Expr *)> getType) {
795-
auto res = new (Ctx) FloatLiteralExpr(E->getDigitsText(),
796-
E->getSourceRange().End);
797-
if (E->isNegative())
798-
res->setNegative(E->getSourceRange().Start);
799-
return res;
800-
}
801-
static LiteralExpr *
802-
shallowCloneImpl(const BooleanLiteralExpr *E, ASTContext &Ctx,
803-
llvm::function_ref<Type(const Expr *)> getType) {
804-
return new (Ctx) BooleanLiteralExpr(E->getValue(), E->getLoc());
805-
}
806-
static LiteralExpr *
807-
shallowCloneImpl(const StringLiteralExpr *E, ASTContext &Ctx,
808-
llvm::function_ref<Type(const Expr *)> getType) {
809-
auto res = new (Ctx) StringLiteralExpr(E->getValue(), E->getSourceRange());
810-
res->setEncoding(E->getEncoding());
811-
return res;
812-
}
813-
814-
static LiteralExpr *
815-
shallowCloneImpl(const InterpolatedStringLiteralExpr *E, ASTContext &Ctx,
816-
llvm::function_ref<Type(const Expr *)> getType) {
817-
auto res = new (Ctx) InterpolatedStringLiteralExpr(E->getLoc(),
818-
E->getTrailingQuoteLoc(),
819-
E->getLiteralCapacity(),
820-
E->getInterpolationCount(),
821-
E->getAppendingExpr());
822-
res->setSemanticExpr(E->getSemanticExpr());
823-
return res;
824-
}
825-
826-
static LiteralExpr *
827-
shallowCloneImpl(const MagicIdentifierLiteralExpr *E, ASTContext &Ctx,
828-
llvm::function_ref<Type(const Expr *)> getType) {
829-
auto res = new (Ctx) MagicIdentifierLiteralExpr(E->getKind(),
830-
E->getSourceRange().End);
831-
if (res->isString())
832-
res->setStringEncoding(E->getStringEncoding());
833-
return res;
834-
}
835-
836-
static LiteralExpr *
837-
shallowCloneImpl(const ObjectLiteralExpr *E, ASTContext &Ctx,
838-
llvm::function_ref<Type(const Expr *)> getType) {
839-
auto res =
840-
ObjectLiteralExpr::create(Ctx, E->getStartLoc(), E->getLiteralKind(),
841-
E->getArg(), E->isImplicit(), getType);
842-
return res;
843-
}
844-
845-
// Make an exact copy of this AST node.
846-
LiteralExpr *LiteralExpr::shallowClone(
847-
ASTContext &Ctx, llvm::function_ref<void(Expr *, Type)> setType,
848-
llvm::function_ref<Type(const Expr *)> getType) const {
849-
LiteralExpr *Result = nullptr;
850-
switch (getKind()) {
851-
default: llvm_unreachable("Unknown literal type!");
852-
#define DISPATCH_CLONE(KIND) \
853-
case ExprKind::KIND: \
854-
Result = shallowCloneImpl(cast<KIND##Expr>(this), Ctx, getType); \
855-
break;
856-
857-
DISPATCH_CLONE(NilLiteral)
858-
DISPATCH_CLONE(IntegerLiteral)
859-
DISPATCH_CLONE(FloatLiteral)
860-
DISPATCH_CLONE(BooleanLiteral)
861-
DISPATCH_CLONE(StringLiteral)
862-
DISPATCH_CLONE(InterpolatedStringLiteral)
863-
DISPATCH_CLONE(ObjectLiteral)
864-
DISPATCH_CLONE(MagicIdentifierLiteral)
865-
#undef DISPATCH_CLONE
866-
}
867-
868-
setType(Result, getType(this));
869-
Result->setImplicit(isImplicit());
870-
return Result;
871-
}
872-
873776
IntegerLiteralExpr * IntegerLiteralExpr::createFromUnsigned(ASTContext &C, unsigned value) {
874777
llvm::SmallString<8> Scratch;
875778
llvm::APInt(sizeof(unsigned)*8, value).toString(Scratch, 10, /*signed*/ false);

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6685,26 +6685,9 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
66856685
DeclName builtinLiteralFuncName,
66866686
Diag<> brokenProtocolDiag,
66876687
Diag<> brokenBuiltinProtocolDiag) {
6688-
auto &tc = cs.getTypeChecker();
6689-
6690-
auto getType = [&](const Expr *E) -> Type {
6691-
return cs.getType(E);
6692-
};
6693-
6694-
auto setType = [&](Expr *E, Type Ty) {
6695-
cs.setType(E, Ty);
6696-
};
6697-
66986688
// If coercing a literal to an unresolved type, we don't try to look up the
66996689
// witness members, just do it.
67006690
if (type->is<UnresolvedType>()) {
6701-
// Instead of updating the literal expr in place, allocate a new node. This
6702-
// avoids issues where Builtin types end up on expr nodes and pollute
6703-
// diagnostics.
6704-
literal = cast<LiteralExpr>(literal)->shallowClone(tc.Context, setType,
6705-
getType);
6706-
6707-
// The literal expression has this type.
67086691
cs.setType(literal, type);
67096692
return literal;
67106693
}
@@ -6753,9 +6736,8 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
67536736
// Dig out the literal type and perform a builtin literal conversion to it.
67546737
if (!literalType.empty()) {
67556738
// Extract the literal type.
6756-
Type builtinLiteralType = tc.getWitnessType(type, protocol, *conformance,
6757-
literalType,
6758-
brokenProtocolDiag);
6739+
Type builtinLiteralType =
6740+
conformance->getTypeWitnessByName(type, literalType);
67596741
if (!builtinLiteralType)
67606742
return nullptr;
67616743

test/Constraints/add_with_nil.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
func test(_ x: Int) -> Int {
44
return x + nil
5-
// expected-error@-1 {{cannot convert value of type 'Int' to expected argument type '_.Stride'}}
5+
// expected-error@-1 {{type of expression is ambiguous without more context}}
66
}

0 commit comments

Comments
 (0)