Skip to content

Commit 9d3c8ca

Browse files
committed
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 d3f624f commit 9d3c8ca

File tree

7 files changed

+26
-134
lines changed

7 files changed

+26
-134
lines changed

include/swift/AST/DiagnosticsSema.def

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

228-
ERROR(cannot_subscript_base,none,
229-
"cannot subscript a value of type %0",
230-
(Type))
231-
232-
ERROR(cannot_subscript_ambiguous_base,none,
233-
"cannot subscript a value of incorrect or ambiguous type", ())
234-
235228
ERROR(cannot_subscript_nil_literal,none,
236229
"cannot subscript a nil literal value", ())
237230
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
@@ -3122,24 +3122,9 @@ class LoadExpr : public ImplicitConversionExpr {
31223122
static bool classof(const Expr *E) { return E->getKind() == ExprKind::Load; }
31233123
};
31243124

3125-
/// This is a conversion from an expression of UnresolvedType to an arbitrary
3126-
/// other type, and from an arbitrary type to UnresolvedType. This node does
3127-
/// not appear in valid code, only in code involving diagnostics.
3128-
class UnresolvedTypeConversionExpr : public ImplicitConversionExpr {
3129-
public:
3130-
UnresolvedTypeConversionExpr(Expr *subExpr, Type type)
3131-
: ImplicitConversionExpr(ExprKind::UnresolvedTypeConversion, subExpr, type) {}
3132-
3133-
static bool classof(const Expr *E) {
3134-
return E->getKind() == ExprKind::UnresolvedTypeConversion;
3135-
}
3136-
};
3137-
31383125
/// FunctionConversionExpr - Convert a function to another function type,
31393126
/// which might involve renaming the parameters or handling substitutions
31403127
/// of subtypes (in the return) or supertypes (in the input).
3141-
///
3142-
/// FIXME: This should be a CapturingExpr.
31433128
class FunctionConversionExpr : public ImplicitConversionExpr {
31443129
public:
31453130
FunctionConversionExpr(Expr *subExpr, Type type)

include/swift/AST/ExprNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ ABSTRACT_EXPR(Apply, Expr)
150150
ABSTRACT_EXPR(ImplicitConversion, Expr)
151151
EXPR(Load, ImplicitConversionExpr)
152152
EXPR(DestructureTuple, ImplicitConversionExpr)
153-
EXPR(UnresolvedTypeConversion, ImplicitConversionExpr)
154153
EXPR(FunctionConversion, ImplicitConversionExpr)
155154
EXPR(CovariantFunctionConversion, ImplicitConversionExpr)
156155
EXPR(CovariantReturnConversion, ImplicitConversionExpr)

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,11 +2267,6 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
22672267
printRec(E->getResultExpr());
22682268
PrintWithColorRAII(OS, ParenthesisColor) << ')';
22692269
}
2270-
void visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *E) {
2271-
printCommon(E, "unresolvedtype_conversion_expr") << '\n';
2272-
printRec(E->getSubExpr());
2273-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
2274-
}
22752270
void visitFunctionConversionExpr(FunctionConversionExpr *E) {
22762271
printCommon(E, "function_conversion_expr") << '\n';
22772272
printRec(E->getSubExpr());

lib/AST/Expr.cpp

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

338338
PASS_THROUGH_REFERENCE(Load, getSubExpr);
339339
NO_REFERENCE(DestructureTuple);
340-
NO_REFERENCE(UnresolvedTypeConversion);
341340
PASS_THROUGH_REFERENCE(FunctionConversion, getSubExpr);
342341
PASS_THROUGH_REFERENCE(CovariantFunctionConversion, getSubExpr);
343342
PASS_THROUGH_REFERENCE(CovariantReturnConversion, getSubExpr);
@@ -661,7 +660,6 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
661660

662661
case ExprKind::Load:
663662
case ExprKind::DestructureTuple:
664-
case ExprKind::UnresolvedTypeConversion:
665663
case ExprKind::FunctionConversion:
666664
case ExprKind::CovariantFunctionConversion:
667665
case ExprKind::CovariantReturnConversion:

lib/SILGen/SILGenExpr.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,6 @@ namespace {
447447
RValue visitConditionalBridgeFromObjCExpr(ConditionalBridgeFromObjCExpr *E,
448448
SGFContext C);
449449
RValue visitArchetypeToSuperExpr(ArchetypeToSuperExpr *E, SGFContext C);
450-
RValue visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *E,
451-
SGFContext C);
452450
RValue visitFunctionConversionExpr(FunctionConversionExpr *E,
453451
SGFContext C);
454452
RValue visitCovariantFunctionConversionExpr(
@@ -957,12 +955,6 @@ RValue RValueEmitter::visitSuperRefExpr(SuperRefExpr *E, SGFContext C) {
957955
return RValue(SGF, E, result);
958956
}
959957

960-
RValue RValueEmitter::
961-
visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *E,
962-
SGFContext C) {
963-
llvm_unreachable("invalid code made its way into SILGen");
964-
}
965-
966958
RValue RValueEmitter::visitOtherConstructorDeclRefExpr(
967959
OtherConstructorDeclRefExpr *E, SGFContext C) {
968960
// This should always be a child of an ApplyExpr and so will be emitted by

lib/Sema/CSApply.cpp

Lines changed: 26 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,8 +2679,7 @@ namespace {
26792679
auto type = simplifyType(openedType);
26802680
cs.setType(expr, type);
26812681

2682-
if (type->is<UnresolvedType>())
2683-
return expr;
2682+
assert(!type->is<UnresolvedType>());
26842683

26852684
auto &ctx = cs.getASTContext();
26862685

@@ -2703,14 +2702,11 @@ namespace {
27032702
ConcreteDeclRef witness = conformance.getWitnessByName(
27042703
conformingType->getRValueType(), constrName);
27052704

2706-
auto selectedOverload = solution.getOverloadChoiceIfAvailable(
2705+
auto selectedOverload = solution.getOverloadChoice(
27072706
cs.getConstraintLocator(expr, ConstraintLocator::ConstructorMember));
27082707

2709-
if (!selectedOverload)
2710-
return nullptr;
2711-
27122708
auto fnType =
2713-
simplifyType(selectedOverload->openedType)->castTo<FunctionType>();
2709+
simplifyType(selectedOverload.openedType)->castTo<FunctionType>();
27142710

27152711
auto newArg = coerceCallArguments(
27162712
expr->getArg(), fnType, witness,
@@ -3009,18 +3005,8 @@ namespace {
30093005
// Determine the declaration selected for this overloaded reference.
30103006
auto memberLocator = cs.getConstraintLocator(expr,
30113007
ConstraintLocator::Member);
3012-
auto selectedElt = solution.getOverloadChoiceIfAvailable(memberLocator);
3013-
3014-
if (!selectedElt) {
3015-
// If constraint solving resolved this to an UnresolvedType, then we're
3016-
// in an ambiguity tolerant mode used for diagnostic generation. Just
3017-
// leave this as whatever type of member reference it already is.
3018-
Type resultTy = simplifyType(cs.getType(expr));
3019-
cs.setType(expr, resultTy);
3020-
return expr;
3021-
}
3008+
auto selected = solution.getOverloadChoice(memberLocator);
30223009

3023-
auto selected = *selectedElt;
30243010
if (!selected.choice.getBaseType()) {
30253011
// This is one of the "outer alternatives", meaning the innermost
30263012
// methods didn't work out.
@@ -3252,40 +3238,19 @@ namespace {
32523238
Expr *visitSubscriptExpr(SubscriptExpr *expr) {
32533239
auto *memberLocator =
32543240
cs.getConstraintLocator(expr, ConstraintLocator::SubscriptMember);
3255-
auto overload = solution.getOverloadChoiceIfAvailable(memberLocator);
3256-
3257-
// Handles situation where there was a solution available but it didn't
3258-
// have a proper overload selected from subscript call, might be because
3259-
// solver was allowed to return free or unresolved types, which can
3260-
// happen while running diagnostics on one of the expressions.
3261-
if (!overload) {
3262-
auto *base = expr->getBase();
3263-
auto &de = cs.getASTContext().Diags;
3264-
auto baseType = cs.getType(base);
3265-
3266-
if (auto errorType = baseType->getAs<ErrorType>()) {
3267-
de.diagnose(base->getLoc(), diag::cannot_subscript_base,
3268-
errorType->getOriginalType())
3269-
.highlight(base->getSourceRange());
3270-
} else {
3271-
de.diagnose(base->getLoc(), diag::cannot_subscript_ambiguous_base)
3272-
.highlight(base->getSourceRange());
3273-
}
3274-
3275-
return nullptr;
3276-
}
3241+
auto overload = solution.getOverloadChoice(memberLocator);
32773242

3278-
if (overload->choice.getKind() ==
3243+
if (overload.choice.getKind() ==
32793244
OverloadChoiceKind::KeyPathDynamicMemberLookup) {
32803245
return buildDynamicMemberLookupRef(
32813246
expr, expr->getBase(), expr->getIndex()->getStartLoc(), SourceLoc(),
3282-
*overload, memberLocator);
3247+
overload, memberLocator);
32833248
}
32843249

32853250
return buildSubscript(
32863251
expr->getBase(), expr->getIndex(), expr->getArgumentLabels(),
32873252
expr->hasTrailingClosure(), cs.getConstraintLocator(expr),
3288-
expr->isImplicit(), expr->getAccessSemantics(), *overload);
3253+
expr->isImplicit(), expr->getAccessSemantics(), overload);
32893254
}
32903255

32913256
/// "Finish" an array expression by filling in the semantic expression.
@@ -4146,8 +4111,7 @@ namespace {
41464111
expr->getSubPattern()->forEachVariable([](VarDecl *VD) {
41474112
VD->setInvalid();
41484113
});
4149-
if (!SuppressDiagnostics
4150-
&& !cs.getType(simplified)->is<UnresolvedType>()) {
4114+
if (!SuppressDiagnostics) {
41514115
auto &de = cs.getASTContext().Diags;
41524116
de.diagnose(simplified->getLoc(), diag::pattern_in_expr,
41534117
expr->getSubPattern()->getKind());
@@ -4626,18 +4590,12 @@ namespace {
46264590
// If this is an unresolved link, make sure we resolved it.
46274591
if (kind == KeyPathExpr::Component::Kind::UnresolvedProperty ||
46284592
kind == KeyPathExpr::Component::Kind::UnresolvedSubscript) {
4629-
auto foundDecl = solution.getOverloadChoiceIfAvailable(locator);
4630-
if (!foundDecl) {
4631-
// If we couldn't resolve the component, leave it alone.
4632-
resolvedComponents.push_back(origComponent);
4633-
componentTy = origComponent.getComponentType();
4634-
continue;
4635-
}
4593+
auto foundDecl = solution.getOverloadChoice(locator);
46364594

46374595
isDynamicMember =
4638-
foundDecl->choice.getKind() ==
4596+
foundDecl.choice.getKind() ==
46394597
OverloadChoiceKind::DynamicMemberLookup ||
4640-
foundDecl->choice.getKind() ==
4598+
foundDecl.choice.getKind() ==
46414599
OverloadChoiceKind::KeyPathDynamicMemberLookup;
46424600

46434601
// If this was a @dynamicMemberLookup property, then we actually
@@ -4668,11 +4626,9 @@ namespace {
46684626
case KeyPathExpr::Component::Kind::OptionalChain: {
46694627
didOptionalChain = true;
46704628
// Chaining always forces the element to be an rvalue.
4629+
assert(!componentTy->hasUnresolvedType());
46714630
auto objectTy =
46724631
componentTy->getWithoutSpecifierType()->getOptionalObjectType();
4673-
if (componentTy->hasUnresolvedType() && !objectTy) {
4674-
objectTy = componentTy;
4675-
}
46764632
assert(objectTy);
46774633

46784634
auto loc = origComponent.getLoc();
@@ -4724,8 +4680,8 @@ namespace {
47244680
}
47254681

47264682
// Wrap a non-optional result if there was chaining involved.
4683+
assert(!componentTy->hasUnresolvedType());
47274684
if (didOptionalChain && componentTy &&
4728-
!componentTy->hasUnresolvedType() &&
47294685
!componentTy->getWithoutSpecifierType()->isEqual(leafTy)) {
47304686
assert(leafTy->getOptionalObjectType()->isEqual(
47314687
componentTy->getWithoutSpecifierType()));
@@ -4744,8 +4700,8 @@ namespace {
47444700

47454701
// The final component type ought to line up with the leaf type of the
47464702
// key path.
4747-
assert(!componentTy || componentTy->hasUnresolvedType()
4748-
|| componentTy->getWithoutSpecifierType()->isEqual(leafTy));
4703+
assert(!componentTy->hasUnresolvedType());
4704+
assert(componentTy->getWithoutSpecifierType()->isEqual(leafTy));
47494705

47504706
if (!isFunctionType)
47514707
return E;
@@ -4850,18 +4806,13 @@ namespace {
48504806

48514807
// Unwrap the last component type, preserving @lvalue-ness.
48524808
auto optionalTy = components.back().getComponentType();
4809+
assert(!optionalTy->hasUnresolvedType());
48534810
Type objectTy;
48544811
if (auto lvalue = optionalTy->getAs<LValueType>()) {
48554812
objectTy = lvalue->getObjectType()->getOptionalObjectType();
4856-
if (optionalTy->hasUnresolvedType() && !objectTy) {
4857-
objectTy = optionalTy;
4858-
}
48594813
objectTy = LValueType::get(objectTy);
48604814
} else {
48614815
objectTy = optionalTy->getOptionalObjectType();
4862-
if (optionalTy->hasUnresolvedType() && !objectTy) {
4863-
objectTy = optionalTy;
4864-
}
48654816
}
48664817
assert(objectTy);
48674818

@@ -5290,11 +5241,9 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType) {
52905241
Type toInstanceType = toType;
52915242

52925243
// Look through metatypes
5293-
while ((fromInstanceType->is<UnresolvedType>() ||
5294-
fromInstanceType->is<AnyMetatypeType>()) &&
5244+
while (fromInstanceType->is<AnyMetatypeType>() &&
52955245
toInstanceType->is<ExistentialMetatypeType>()) {
5296-
if (!fromInstanceType->is<UnresolvedType>())
5297-
fromInstanceType = fromInstanceType->castTo<AnyMetatypeType>()->getInstanceType();
5246+
fromInstanceType = fromInstanceType->castTo<AnyMetatypeType>()->getInstanceType();
52985247
toInstanceType = toInstanceType->castTo<ExistentialMetatypeType>()->getInstanceType();
52995248
}
53005249

@@ -5492,11 +5441,6 @@ Expr *ExprRewriter::coerceCallArguments(
54925441
LocatorPathElt::ApplyArgToParam(argIdx, paramIdx, flags));
54935442
};
54945443

5495-
bool matchCanFail =
5496-
llvm::any_of(params, [](const AnyFunctionType::Param &param) {
5497-
return param.getPlainType()->hasUnresolvedType();
5498-
});
5499-
55005444
// Determine whether this application has curried self.
55015445
bool skipCurriedSelf = apply ? hasCurriedSelf(cs, callee, apply) : true;
55025446
// Determine the parameter bindings.
@@ -5554,9 +5498,7 @@ Expr *ExprRewriter::coerceCallArguments(
55545498
args, params, paramInfo, unlabeledTrailingClosureIndex,
55555499
/*allowFixes=*/false, listener, trailingClosureMatching);
55565500

5557-
assert((matchCanFail || callArgumentMatch) &&
5558-
"Call arguments did not match up?");
5559-
(void)matchCanFail;
5501+
assert(callArgumentMatch && "Call arguments did not match up?");
55605502

55615503
auto parameterBindings = std::move(callArgumentMatch->parameterBindings);
55625504

@@ -6279,8 +6221,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
62796221
if (knownRestriction != solution.ConstraintRestrictions.end()) {
62806222
switch (knownRestriction->second) {
62816223
case ConversionRestrictionKind::DeepEquality: {
6282-
if (toType->hasUnresolvedType())
6283-
break;
6224+
assert(!toType->hasUnresolvedType());
62846225

62856226
// HACK: Fix problem related to Swift 4 mode (with assertions),
62866227
// since Swift 4 mode allows passing arguments with extra parens
@@ -6826,9 +6767,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
68266767
break;
68276768
}
68286769

6829-
// Unresolved types come up in diagnostics for lvalue and inout types.
6830-
if (fromType->hasUnresolvedType() || toType->hasUnresolvedType())
6831-
return cs.cacheType(new (ctx) UnresolvedTypeConversionExpr(expr, toType));
6770+
assert(!fromType->hasUnresolvedType());
6771+
assert(!toType->hasUnresolvedType());
68326772

68336773
// Use an opaque type to abstract a value of the underlying concrete type.
68346774
if (toType->getAs<OpaqueTypeArchetypeType>()) {
@@ -7401,19 +7341,14 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
74017341
// FIXME: handle unwrapping everywhere else
74027342
assert(!unwrapResult);
74037343

7404-
// If this is an UnresolvedType in the system, preserve it.
7405-
if (cs.getType(fn)->is<UnresolvedType>()) {
7406-
cs.setType(apply, cs.getType(fn));
7407-
return apply;
7408-
}
7344+
assert(!cs.getType(fn)->is<UnresolvedType>());
74097345

74107346
// We have a type constructor.
74117347
auto metaTy = cs.getType(fn)->castTo<AnyMetatypeType>();
74127348
auto ty = metaTy->getInstanceType();
74137349

74147350
// If we're "constructing" a tuple type, it's simply a conversion.
74157351
if (auto tupleTy = ty->getAs<TupleType>()) {
7416-
// FIXME: Need an AST to represent this properly.
74177352
return coerceToType(apply->getArg(), tupleTy, locator);
74187353
}
74197354

@@ -7422,19 +7357,14 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
74227357
auto *ctorLocator =
74237358
cs.getConstraintLocator(locator, {ConstraintLocator::ApplyFunction,
74247359
ConstraintLocator::ConstructorMember});
7425-
auto selected = solution.getOverloadChoiceIfAvailable(ctorLocator);
7426-
if (!selected) {
7427-
assert(ty->hasError() || ty->hasUnresolvedType());
7428-
cs.setType(apply, ty);
7429-
return apply;
7430-
}
7360+
auto selected = solution.getOverloadChoice(ctorLocator);
74317361

74327362
assert(ty->getNominalOrBoundGenericNominal() || ty->is<DynamicSelfType>() ||
74337363
ty->isExistentialType() || ty->is<ArchetypeType>());
74347364

74357365
// Consider the constructor decl reference expr 'implicit', but the
74367366
// constructor call expr itself has the apply's 'implicitness'.
7437-
Expr *declRef = buildMemberRef(fn, /*dotLoc=*/SourceLoc(), *selected,
7367+
Expr *declRef = buildMemberRef(fn, /*dotLoc=*/SourceLoc(), selected,
74387368
DeclNameLoc(fn->getEndLoc()), locator,
74397369
ctorLocator, /*implicit=*/true,
74407370
AccessSemantics::Ordinary);

0 commit comments

Comments
 (0)