Skip to content

Commit 90bfc16

Browse files
slavapestovhamishknight
authored andcommitted
Sema: Remove some unreachable code from CSApply
I believe these code paths could only be reached by re-typechecking invalid code in the old CSDiag implementation.
1 parent a7c0460 commit 90bfc16

File tree

8 files changed

+25
-132
lines changed

8 files changed

+25
-132
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,6 @@ ERROR(cannot_apply_lvalue_binop_to_subelement,none,
254254
ERROR(cannot_apply_lvalue_binop_to_rvalue,none,
255255
"left side of mutating operator has immutable type %0", (Type))
256256

257-
ERROR(cannot_subscript_base,none,
258-
"cannot subscript a value of type %0",
259-
(Type))
260-
261-
ERROR(cannot_subscript_ambiguous_base,none,
262-
"cannot subscript a value of incorrect or ambiguous type", ())
263-
264257
ERROR(cannot_subscript_nil_literal,none,
265258
"cannot subscript a nil literal value", ())
266259
ERROR(conditional_cast_from_nil,none,

include/swift/AST/Expr.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,24 +3348,9 @@ class ABISafeConversionExpr : public ImplicitConversionExpr {
33483348
}
33493349
};
33503350

3351-
/// This is a conversion from an expression of UnresolvedType to an arbitrary
3352-
/// other type, and from an arbitrary type to UnresolvedType. This node does
3353-
/// not appear in valid code, only in code involving diagnostics.
3354-
class UnresolvedTypeConversionExpr : public ImplicitConversionExpr {
3355-
public:
3356-
UnresolvedTypeConversionExpr(Expr *subExpr, Type type)
3357-
: ImplicitConversionExpr(ExprKind::UnresolvedTypeConversion, subExpr, type) {}
3358-
3359-
static bool classof(const Expr *E) {
3360-
return E->getKind() == ExprKind::UnresolvedTypeConversion;
3361-
}
3362-
};
3363-
33643351
/// FunctionConversionExpr - Convert a function to another function type,
33653352
/// which might involve renaming the parameters or handling substitutions
33663353
/// of subtypes (in the return) or supertypes (in the input).
3367-
///
3368-
/// FIXME: This should be a CapturingExpr.
33693354
class FunctionConversionExpr : public ImplicitConversionExpr {
33703355
public:
33713356
FunctionConversionExpr(Expr *subExpr, Type type)

include/swift/AST/ExprNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ ABSTRACT_EXPR(ImplicitConversion, Expr)
161161
EXPR(Load, ImplicitConversionExpr)
162162
EXPR(ABISafeConversion, ImplicitConversionExpr)
163163
EXPR(DestructureTuple, ImplicitConversionExpr)
164-
EXPR(UnresolvedTypeConversion, ImplicitConversionExpr)
165164
EXPR(FunctionConversion, ImplicitConversionExpr)
166165
EXPR(CovariantFunctionConversion, ImplicitConversionExpr)
167166
EXPR(CovariantReturnConversion, ImplicitConversionExpr)

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3817,12 +3817,6 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
38173817

38183818
printFoot();
38193819
}
3820-
void visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *E,
3821-
Label label) {
3822-
printCommon(E, "unresolvedtype_conversion_expr", label);
3823-
printRec(E->getSubExpr(), Label::optional("sub_expr"));
3824-
printFoot();
3825-
}
38263820
void visitFunctionConversionExpr(FunctionConversionExpr *E, Label label) {
38273821
printCommon(E, "function_conversion_expr", label);
38283822
printRec(E->getSubExpr(), Label::optional("sub_expr"));

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5558,9 +5558,6 @@ void PrintAST::visitMakeTemporarilyEscapableExpr(MakeTemporarilyEscapableExpr *e
55585558
void PrintAST::visitProtocolMetatypeToObjectExpr(ProtocolMetatypeToObjectExpr *expr) {
55595559
}
55605560

5561-
void PrintAST::visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *expr) {
5562-
}
5563-
55645561
void PrintAST::visitConditionalBridgeFromObjCExpr(ConditionalBridgeFromObjCExpr *expr) {
55655562
}
55665563

lib/AST/Expr.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
414414

415415
PASS_THROUGH_REFERENCE(Load, getSubExpr);
416416
NO_REFERENCE(DestructureTuple);
417-
NO_REFERENCE(UnresolvedTypeConversion);
418417
PASS_THROUGH_REFERENCE(ABISafeConversion, getSubExpr);
419418
PASS_THROUGH_REFERENCE(FunctionConversion, getSubExpr);
420419
PASS_THROUGH_REFERENCE(CovariantFunctionConversion, getSubExpr);
@@ -783,7 +782,6 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
783782
case ExprKind::Load:
784783
case ExprKind::ABISafeConversion:
785784
case ExprKind::DestructureTuple:
786-
case ExprKind::UnresolvedTypeConversion:
787785
case ExprKind::FunctionConversion:
788786
case ExprKind::CovariantFunctionConversion:
789787
case ExprKind::CovariantReturnConversion:
@@ -993,7 +991,6 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
993991
case ExprKind::Binary:
994992
case ExprKind::Load:
995993
case ExprKind::DestructureTuple:
996-
case ExprKind::UnresolvedTypeConversion:
997994
case ExprKind::ABISafeConversion:
998995
case ExprKind::FunctionConversion:
999996
case ExprKind::CovariantFunctionConversion:

lib/SILGen/SILGenExpr.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,6 @@ namespace {
481481
RValue visitConditionalBridgeFromObjCExpr(ConditionalBridgeFromObjCExpr *E,
482482
SGFContext C);
483483
RValue visitArchetypeToSuperExpr(ArchetypeToSuperExpr *E, SGFContext C);
484-
RValue visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *E,
485-
SGFContext C);
486484
RValue visitABISafeConversionExpr(ABISafeConversionExpr *E, SGFContext C) {
487485
llvm_unreachable("cannot appear in rvalue");
488486
}
@@ -1035,12 +1033,6 @@ RValue RValueEmitter::visitSuperRefExpr(SuperRefExpr *E, SGFContext C) {
10351033
return RValue(SGF, E, result);
10361034
}
10371035

1038-
RValue RValueEmitter::
1039-
visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *E,
1040-
SGFContext C) {
1041-
llvm_unreachable("invalid code made its way into SILGen");
1042-
}
1043-
10441036
RValue RValueEmitter::visitOtherConstructorDeclRefExpr(
10451037
OtherConstructorDeclRefExpr *E, SGFContext C) {
10461038
// This should always be a child of an ApplyExpr and so will be emitted by

lib/Sema/CSApply.cpp

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,8 +3268,7 @@ namespace {
32683268
auto type = simplifyType(openedType);
32693269
cs.setType(expr, type);
32703270

3271-
if (type->is<UnresolvedType>())
3272-
return expr;
3271+
assert(!type->is<UnresolvedType>());
32733272

32743273
Type conformingType = type;
32753274
if (auto baseType = conformingType->getOptionalObjectType()) {
@@ -3288,14 +3287,11 @@ namespace {
32883287

32893288
ConcreteDeclRef witness = conformance.getWitnessByName(constrName);
32903289

3291-
auto selectedOverload = solution.getOverloadChoiceIfAvailable(
3290+
auto selectedOverload = solution.getOverloadChoice(
32923291
cs.getConstraintLocator(expr, ConstraintLocator::ConstructorMember));
32933292

3294-
if (!selectedOverload)
3295-
return nullptr;
3296-
3297-
auto fnType =
3298-
simplifyType(selectedOverload->adjustedOpenedType)->castTo<FunctionType>();
3293+
auto fnType = simplifyType(selectedOverload.adjustedOpenedType)
3294+
->castTo<FunctionType>();
32993295

33003296
auto newArgs = coerceCallArguments(
33013297
expr->getArgs(), fnType, witness, /*applyExpr=*/nullptr,
@@ -3628,18 +3624,8 @@ namespace {
36283624
// Determine the declaration selected for this overloaded reference.
36293625
auto memberLocator = cs.getConstraintLocator(expr,
36303626
ConstraintLocator::Member);
3631-
auto selectedElt = solution.getOverloadChoiceIfAvailable(memberLocator);
3632-
3633-
if (!selectedElt) {
3634-
// If constraint solving resolved this to an UnresolvedType, then we're
3635-
// in an ambiguity tolerant mode used for diagnostic generation. Just
3636-
// leave this as whatever type of member reference it already is.
3637-
Type resultTy = simplifyType(cs.getType(expr));
3638-
cs.setType(expr, resultTy);
3639-
return expr;
3640-
}
3627+
auto selected = solution.getOverloadChoice(memberLocator);
36413628

3642-
auto selected = *selectedElt;
36433629
if (!selected.choice.getBaseType()) {
36443630
// This is one of the "outer alternatives", meaning the innermost
36453631
// methods didn't work out.
@@ -3958,39 +3944,17 @@ namespace {
39583944
Expr *visitSubscriptExpr(SubscriptExpr *expr) {
39593945
auto *memberLocator =
39603946
cs.getConstraintLocator(expr, ConstraintLocator::SubscriptMember);
3961-
auto overload = solution.getOverloadChoiceIfAvailable(memberLocator);
3962-
3963-
// Handles situation where there was a solution available but it didn't
3964-
// have a proper overload selected from subscript call, might be because
3965-
// solver was allowed to return free or unresolved types, which can
3966-
// happen while running diagnostics on one of the expressions.
3967-
if (!overload) {
3968-
auto *base = expr->getBase();
3969-
auto &de = ctx.Diags;
3970-
auto baseType = cs.getType(base);
3971-
3972-
if (auto errorType = baseType->getAs<ErrorType>()) {
3973-
de.diagnose(base->getLoc(), diag::cannot_subscript_base,
3974-
errorType->getOriginalType())
3975-
.highlight(base->getSourceRange());
3976-
} else {
3977-
de.diagnose(base->getLoc(), diag::cannot_subscript_ambiguous_base)
3978-
.highlight(base->getSourceRange());
3979-
}
3980-
3981-
return nullptr;
3982-
}
3983-
3984-
if (overload->choice.isKeyPathDynamicMemberLookup()) {
3947+
auto overload = solution.getOverloadChoice(memberLocator);
3948+
if (overload.choice.isKeyPathDynamicMemberLookup()) {
39853949
return buildDynamicMemberLookupRef(
39863950
expr, expr->getBase(), expr->getArgs()->getStartLoc(), SourceLoc(),
3987-
*overload, memberLocator);
3951+
overload, memberLocator);
39883952
}
39893953

39903954
return buildSubscript(expr->getBase(), expr->getArgs(),
39913955
cs.getConstraintLocator(expr), memberLocator,
39923956
expr->isImplicit(), expr->getAccessSemantics(),
3993-
*overload);
3957+
overload);
39943958
}
39953959

39963960
/// "Finish" an array expression by filling in the semantic expression.
@@ -5321,15 +5285,8 @@ namespace {
53215285
// If this is an unresolved link, make sure we resolved it.
53225286
if (kind == KeyPathExpr::Component::Kind::UnresolvedMember ||
53235287
kind == KeyPathExpr::Component::Kind::UnresolvedSubscript) {
5324-
auto foundDecl = solution.getOverloadChoiceIfAvailable(calleeLoc);
5325-
if (!foundDecl) {
5326-
// If we couldn't resolve the component, leave it alone.
5327-
resolvedComponents.push_back(origComponent);
5328-
componentTy = origComponent.getComponentType();
5329-
continue;
5330-
}
5331-
5332-
isDynamicMember = foundDecl->choice.isAnyDynamicMemberLookup();
5288+
auto foundDecl = solution.getOverloadChoice(calleeLoc);
5289+
isDynamicMember = foundDecl.choice.isAnyDynamicMemberLookup();
53335290

53345291
// If this was a @dynamicMemberLookup property, then we actually
53355292
// form a subscript reference, so switch the kind.
@@ -5368,11 +5325,9 @@ namespace {
53685325
case KeyPathExpr::Component::Kind::OptionalChain: {
53695326
didOptionalChain = true;
53705327
// Chaining always forces the element to be an rvalue.
5328+
assert(!componentTy->hasUnresolvedType());
53715329
auto objectTy =
53725330
componentTy->getWithoutSpecifierType()->getOptionalObjectType();
5373-
if (componentTy->hasUnresolvedType() && !objectTy) {
5374-
objectTy = componentTy;
5375-
}
53765331
assert(objectTy);
53775332

53785333
auto loc = origComponent.getLoc();
@@ -5422,8 +5377,8 @@ namespace {
54225377
}
54235378

54245379
// Wrap a non-optional result if there was chaining involved.
5380+
assert(!componentTy->hasUnresolvedType());
54255381
if (didOptionalChain && componentTy &&
5426-
!componentTy->hasUnresolvedType() &&
54275382
!componentTy->getWithoutSpecifierType()->isEqual(leafTy)) {
54285383
auto component = KeyPathExpr::Component::forOptionalWrap(leafTy);
54295384
resolvedComponents.push_back(component);
@@ -5549,18 +5504,13 @@ namespace {
55495504

55505505
// Unwrap the last component type, preserving @lvalue-ness.
55515506
auto optionalTy = components.back().getComponentType();
5507+
assert(!optionalTy->hasUnresolvedType());
55525508
Type objectTy;
55535509
if (auto lvalue = optionalTy->getAs<LValueType>()) {
55545510
objectTy = lvalue->getObjectType()->getOptionalObjectType();
5555-
if (optionalTy->hasUnresolvedType() && !objectTy) {
5556-
objectTy = optionalTy;
5557-
}
55585511
objectTy = LValueType::get(objectTy);
55595512
} else {
55605513
objectTy = optionalTy->getOptionalObjectType();
5561-
if (optionalTy->hasUnresolvedType() && !objectTy) {
5562-
objectTy = optionalTy;
5563-
}
55645514
}
55655515
assert(objectTy);
55665516

@@ -7160,12 +7110,10 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType,
71607110
Type openedFromInstanceType = openedFromType;
71617111

71627112
// Look through metatypes.
7163-
while ((fromInstanceType->is<UnresolvedType>() ||
7164-
fromInstanceType->is<AnyMetatypeType>()) &&
7113+
while (fromInstanceType->is<AnyMetatypeType>() &&
71657114
toInstanceType->is<ExistentialMetatypeType>()) {
7166-
if (!fromInstanceType->is<UnresolvedType>())
7167-
fromInstanceType = fromInstanceType->getMetatypeInstanceType();
7168-
if (openedFromInstanceType && !openedFromInstanceType->is<UnresolvedType>())
7115+
fromInstanceType = fromInstanceType->getMetatypeInstanceType();
7116+
if (openedFromInstanceType)
71697117
openedFromInstanceType = openedFromInstanceType->getMetatypeInstanceType();
71707118
toInstanceType = toInstanceType->getMetatypeInstanceType();
71717119
}
@@ -7297,8 +7245,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
72977245
if (knownRestriction != solution.ConstraintRestrictions.end()) {
72987246
switch (knownRestriction->second) {
72997247
case ConversionRestrictionKind::DeepEquality: {
7300-
if (toType->hasUnresolvedType())
7301-
break;
7248+
assert(!toType->hasUnresolvedType());
73027249

73037250
// HACK: Fix problem related to Swift 4 mode (with assertions),
73047251
// since Swift 4 mode allows passing arguments with extra parens
@@ -8172,9 +8119,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
81728119
}
81738120
}
81748121

8175-
// Unresolved types come up in diagnostics for lvalue and inout types.
8176-
if (fromType->hasUnresolvedType() || toType->hasUnresolvedType())
8177-
return cs.cacheType(new (ctx) UnresolvedTypeConversionExpr(expr, toType));
8122+
assert(!fromType->hasUnresolvedType());
8123+
assert(!toType->hasUnresolvedType());
81788124

81798125
ABORT([&](auto &out) {
81808126
out << "Unhandled coercion:\n";
@@ -8723,11 +8669,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
87238669

87248670
// FIXME: Handle unwrapping everywhere else.
87258671

8726-
// If this is an UnresolvedType in the system, preserve it.
8727-
if (cs.getType(fn)->is<UnresolvedType>()) {
8728-
cs.setType(apply, cs.getType(fn));
8729-
return apply;
8730-
}
8672+
assert(!cs.getType(fn)->is<UnresolvedType>());
87318673

87328674
// We have a type constructor.
87338675
auto metaTy = cs.getType(fn)->castTo<AnyMetatypeType>();
@@ -8754,22 +8696,16 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
87548696
auto *ctorLocator =
87558697
cs.getConstraintLocator(locator, {ConstraintLocator::ApplyFunction,
87568698
ConstraintLocator::ConstructorMember});
8757-
auto selected = solution.getOverloadChoiceIfAvailable(ctorLocator);
8758-
if (!selected) {
8759-
assert(ty->hasError() || ty->hasUnresolvedType());
8760-
cs.setType(apply, ty);
8761-
return apply;
8762-
}
8699+
auto selected = solution.getOverloadChoice(ctorLocator);
87638700

87648701
assert(ty->getNominalOrBoundGenericNominal() || ty->is<DynamicSelfType>() ||
87658702
ty->isExistentialType() || ty->is<ArchetypeType>());
87668703

87678704
// Consider the constructor decl reference expr 'implicit', but the
87688705
// constructor call expr itself has the apply's 'implicitness'.
8769-
Expr *declRef = buildMemberRef(fn, /*dotLoc=*/SourceLoc(), *selected,
8770-
DeclNameLoc(fn->getEndLoc()), locator,
8771-
ctorLocator, /*implicit=*/true,
8772-
AccessSemantics::Ordinary);
8706+
Expr *declRef = buildMemberRef(
8707+
fn, /*dotLoc=*/SourceLoc(), selected, DeclNameLoc(fn->getEndLoc()),
8708+
locator, ctorLocator, /*implicit=*/true, AccessSemantics::Ordinary);
87738709
if (!declRef)
87748710
return nullptr;
87758711

0 commit comments

Comments
 (0)