Skip to content

Remove support for -swift-version 3 from the expression type checker. #17691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,6 @@ ERROR(err_noescape_param_call,none,
"parameter %0 to a call to a non-escaping function parameter can allow "
"re-entrant modification of a variable",
(DeclName, unsigned))
WARNING(warn_noescape_param_call,none,
"passing a %select{|closure which captures a }1non-escaping function "
"parameter %0 to a call to a non-escaping function parameter can allow "
"re-entrant modification of a variable and will be illegal in Swift 4",
(DeclName, unsigned))

ERROR(cannot_call_function_value,none,
"cannot invoke value of function type with argument list '%0'",
(StringRef))
Expand Down Expand Up @@ -913,10 +907,6 @@ ERROR(missing_unwrap_optional_try,none,
ERROR(missing_forced_downcast,none,
"%0 is not convertible to %1; "
"did you mean to use 'as!' to force downcast?", (Type, Type))
WARNING(missing_forced_downcast_swift3_compat_warning,none,
"bridging %0 to %1 may fail at runtime and will become a checked "
"cast in Swift 4; did you mean to use 'as!' to force downcast?",
(Type, Type))
ERROR(missing_explicit_conversion,none,
"%0 is not implicitly convertible to %1; "
"did you mean to use 'as' to explicitly convert?", (Type, Type))
Expand Down Expand Up @@ -2855,18 +2845,11 @@ WARNING(store_in_willset,none,
"attempting to store to property %0 within its own willSet, which is "
"about to be overwritten by the new value", (Identifier))

ERROR(tuple_splat_use,none,
"passing %0 arguments to a callee as a single tuple value has been removed in Swift 3",
(unsigned))


ERROR(value_of_module_type,none,
"expected module member name after module name", ())

ERROR(value_of_metatype_type,none,
"expected member name or constructor call after type name", ())
WARNING(warn_value_of_metatype_missing_self,none,
"missing '.self' for reference to metatype of type %0", (Type))

NOTE(add_parens_to_type,none,
"add arguments after the type to construct a value of the type", ())
Expand Down
11 changes: 2 additions & 9 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -93,15 +93,8 @@ enum class CheckedCastKind : unsigned {
SetDowncast,
/// A bridging conversion that always succeeds.
BridgingCoercion,
/// A bridging conversion that may fail, because there are multiple Swift
/// value types that bridge to the same Cocoa object type.
///
/// This kind is only used for Swift 3 compatibility diagnostics and is
/// treated the same as 'BridgingCoercion' otherwise. In Swift 4 or later,
/// any conversions with this kind show up as ValueCasts.
Swift3BridgingDowncast,

Last_CheckedCastKind = Swift3BridgingDowncast,
Last_CheckedCastKind = BridgingCoercion,
};

/// What are the high-level semantics of this access?
Expand Down
4 changes: 1 addition & 3 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -4009,8 +4009,6 @@ StringRef swift::getCheckedCastKindName(CheckedCastKind kind) {
return "set_downcast";
case CheckedCastKind::BridgingCoercion:
return "bridging_coercion";
case CheckedCastKind::Swift3BridgingDowncast:
return "bridging_downcast";
}
llvm_unreachable("bad checked cast name");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ FunctionSignatureTransformDescriptor::createOptimizedSILFunctionName() {
Mangler.setReturnValueOwnedToUnowned();
}

SILModule &M = F->getModule();
auto MangledName = Mangler.mangle();
assert(!M.hasFunction(MangledName));
assert(!F->getModule().hasFunction(MangledName));

return MangledName;
}
Expand Down
69 changes: 4 additions & 65 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3299,7 +3299,6 @@ namespace {
}
expr->setCastKind(castKind);
break;
case CheckedCastKind::Swift3BridgingDowncast:
case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
Expand Down Expand Up @@ -3658,19 +3657,6 @@ namespace {
Expr *sub = handleOptionalBindings(expr->getSubExpr(), toType,
OptionalBindingsCastKind::Bridged,
[&](Expr *sub, Type toInstanceType) {
// Warn about NSNumber and NSValue bridging coercions we accepted in
// Swift 3 but which can fail at runtime.
if (tc.Context.LangOpts.isSwiftVersion3()
&& tc.typeCheckCheckedCast(cs.getType(sub), toInstanceType,
CheckedCastContextKind::None,
dc, SourceLoc(), sub, SourceRange())
== CheckedCastKind::Swift3BridgingDowncast) {
tc.diagnose(expr->getLoc(),
diag::missing_forced_downcast_swift3_compat_warning,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "missing_forced_downcast_swift3_compat_warning"

cs.getType(sub), toInstanceType)
.fixItReplace(expr->getAsLoc(), "as!");
}

return buildObjCBridgeExpr(sub, toInstanceType, locator);
});

Expand Down Expand Up @@ -3736,7 +3722,6 @@ namespace {
}

// Valid casts.
case CheckedCastKind::Swift3BridgingDowncast:
case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
Expand Down Expand Up @@ -3823,7 +3808,6 @@ namespace {
}

// Valid casts.
case CheckedCastKind::Swift3BridgingDowncast:
case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
Expand Down Expand Up @@ -5703,34 +5687,6 @@ Expr *ExprRewriter::coerceCallArguments(
return param.getType()->hasUnresolvedType();
});

// If you value your sanity, ignore the body of this 'if' statement.
if (cs.getASTContext().isSwiftVersion3() && params.size() == 1) {
const auto &param = params.front();
auto paramType = param.getType();

// Total hack: In Swift 3 mode, we can end up with an arity mismatch due to
// loss of ParenType sugar.
if (isa<TupleExpr>(arg)) {
auto *tupleType = paramType->getAs<TupleType>();
if (!param.hasLabel() && !param.isVariadic() && tupleType) {
// Rebuild the function type.
funcType = FunctionType::get(tupleType, funcType->getResult(),
funcType->getExtInfo());
params = funcType->getParams();
}
}

// Total hack: In Swift 3 mode, argument labels are ignored when calling
// function type with a single Any parameter.
if (params.size() == 1 && params.front().getType()->isAny()) {
auto argType = cs.getType(arg);
if (auto *tupleArgType = dyn_cast<TupleType>(argType.getPointer())) {
if (tupleArgType->getNumElements() == 1)
matchCanFail = true;
}
}
}

auto paramType = AnyFunctionType::composeInput(tc.Context, params, false);
bool allParamsMatch = cs.getType(arg)->isEqual(paramType);

Expand Down Expand Up @@ -5781,6 +5737,7 @@ Expr *ExprRewriter::coerceCallArguments(

assert((matchCanFail || !failed) && "Call arguments did not match up?");
(void)failed;
(void)matchCanFail;

// We should either have parentheses or a tuple.
auto *argTuple = dyn_cast<TupleExpr>(arg);
Expand Down Expand Up @@ -6526,8 +6483,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
if (toType->hasUnresolvedType())
break;

// HACK: Fix problem related to Swift 3 mode (with assertions),
// since Swift 3 mode allows passing arguments with extra parens
// HACK: Fix problem related to Swift 4 mode (with assertions),
// since Swift 4 mode allows passing arguments with extra parens
// to parameters which don't expect them, it should be supported
// by "deep equality" type - Optional<T> e.g.
// ```swift
Expand All @@ -6537,7 +6494,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
// ```
//
// See also: https://bugs.swift.org/browse/SR-6796
if (cs.getASTContext().isSwiftVersionAtLeast(3) &&
if (cs.getASTContext().isSwiftVersionAtLeast(4) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t the first part always true? It should suffice to just check if you’re < 5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was intentional. If there are tests passing -swift-version 3 explicitly they should break if they are relying on this behavior. You might have removed all of those with your PR, though.

!cs.getASTContext().isSwiftVersionAtLeast(5)) {
auto obj1 = fromType->getOptionalObjectType();
auto obj2 = toType->getOptionalObjectType();
Expand Down Expand Up @@ -8067,24 +8024,6 @@ bool ConstraintSystem::applySolutionFix(Expr *expr,
llvm_unreachable("Coercions handled in other disjunction branch");

// Valid casts.
case CheckedCastKind::Swift3BridgingDowncast: {
// Swift 3 accepted coercions from NSNumber and NSValue to Swift
// value types, even though there are multiple Swift types that
// bridge to those classes, and the bridging operation back into Swift
// is type-checked. For compatibility, downgrade to a warning.
assert(TC.Context.LangOpts.isSwiftVersion3()
&& "should only appear in Swift 3 compat mode");

TC.diagnose(coerceExpr->getLoc(),
diag::missing_forced_downcast_swift3_compat_warning,
fromType, toType)
.highlight(coerceExpr->getSourceRange())
.fixItReplace(coerceExpr->getLoc(), "as!");

// This is just a warning, so allow the expression to type-check.
return false;
}

case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
Expand Down
12 changes: 5 additions & 7 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -613,12 +613,10 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,

// If they both have trailing closures, compare those separately.
bool compareTrailingClosureParamsSeparately = false;
if (!tc.getLangOpts().isSwiftVersion3()) {
if (numParams1 > 0 && numParams2 > 0 &&
params1.back().getType()->is<AnyFunctionType>() &&
params2.back().getType()->is<AnyFunctionType>()) {
compareTrailingClosureParamsSeparately = true;
}
if (numParams1 > 0 && numParams2 > 0 &&
params1.back().getType()->is<AnyFunctionType>() &&
params2.back().getType()->is<AnyFunctionType>()) {
compareTrailingClosureParamsSeparately = true;
}

auto maybeAddSubtypeConstraint =
Expand Down
43 changes: 8 additions & 35 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,8 @@ matchCallArguments(ConstraintSystem &cs, ConstraintKind kind,
// Check if exactly one argument was passed to this function, otherwise
// we obviously have a mismatch.
if (auto tupleArgType = dyn_cast<TupleType>(argType.getPointer())) {
// Total hack: In Swift 3 mode, argument labels are ignored when calling
// function type with a single Any parameter.
if (tupleArgType->getNumElements() != 1 ||
(!cs.getASTContext().isSwiftVersion3() &&
tupleArgType->getElement(0).hasName())) {
tupleArgType->getElement(0).hasName()) {
return cs.getTypeMatchFailure(locator);
}
}
Expand Down Expand Up @@ -704,21 +701,6 @@ matchCallArguments(ConstraintSystem &cs, ConstraintKind kind,
llvm::SmallBitVector defaultMap =
computeDefaultMap(params, callee, calleeLevel);

if (callee && cs.getASTContext().isSwiftVersion3()
&& argType->is<TupleType>()) {
// Hack: In Swift 3 mode, accept `foo(x, y)` for `foo((x, y))` when the
// callee is a function-typed property or an enum constructor whose
// argument is a single unlabeled type parameter.
if (auto *prop = dyn_cast<VarDecl>(callee)) {
auto *fnType = prop->getInterfaceType()->getAs<AnyFunctionType>();
if (fnType && fnType->getInput()->isTypeParameter())
argType = ParenType::get(cs.getASTContext(), argType);
} else if (auto *enumCtor = dyn_cast<EnumElementDecl>(callee)) {
if (enumCtor->getArgumentInterfaceType()->isTypeParameter())
argType = ParenType::get(cs.getASTContext(), argType);
}
}

// Extract the arguments.
auto args = decomposeArgType(argType, argLabels);

Expand Down Expand Up @@ -1154,7 +1136,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
// arity).
auto func1Input = func1->getInput();
auto func2Input = func2->getInput();
if (!getASTContext().isSwiftVersion3()) {
{
SmallVector<LocatorPathElt, 4> path;
locator.getLocatorParts(path);

Expand Down Expand Up @@ -1573,13 +1555,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// types of two function types, we have to be careful to preserve
// ParenType sugar.
bool isArgumentTupleMatch = isArgumentTupleConversion;
bool isSwiftVersion3 = getASTContext().isSwiftVersion3();

// ... but not in Swift 3 mode, where this behavior was broken.
if (!isSwiftVersion3)
if (auto elt = locator.last())
if (elt->getKind() == ConstraintLocator::FunctionArgument)
isArgumentTupleMatch = true;
if (auto elt = locator.last())
if (elt->getKind() == ConstraintLocator::FunctionArgument)
isArgumentTupleMatch = true;

// If we have type variables that have been bound to fixed types, look through
// to the fixed type.
Expand All @@ -1591,8 +1570,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
auto desugar1 = type1->getDesugaredType();
auto desugar2 = type2->getDesugaredType();
TypeVariableType *typeVar1, *typeVar2;
if (isArgumentTupleMatch &&
!isSwiftVersion3) {
if (isArgumentTupleMatch) {
typeVar1 = dyn_cast<TypeVariableType>(type1.getPointer());
typeVar2 = dyn_cast<TypeVariableType>(type2.getPointer());

Expand Down Expand Up @@ -1777,8 +1755,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
return formUnsolvedResult();
}

if (isArgumentTupleMatch &&
!isSwiftVersion3) {
if (isArgumentTupleMatch) {
if (!typeVar1 && !typeVar2) {
if (type1->hasParenSugar() != type2->hasParenSugar()) {
return getTypeMatchFailure(locator);
Expand Down Expand Up @@ -2853,7 +2830,6 @@ ConstraintSystem::simplifyCheckedCastConstraint(

case CheckedCastKind::Coercion:
case CheckedCastKind::BridgingCoercion:
case CheckedCastKind::Swift3BridgingDowncast:
case CheckedCastKind::Unresolved:
llvm_unreachable("Not a valid result");
}
Expand Down Expand Up @@ -3765,10 +3741,7 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
// Bridging NSNumber to NSValue is one-way, since there are multiple Swift
// value types that bridge to those object types. It requires a checked
// cast to get back.
// We accepted these coercions in Swift 3 mode, so we have to live with
// them (but give a warning) in that language mode.
if (!TC.Context.LangOpts.isSwiftVersion3()
&& TC.Context.isObjCClassWithMultipleSwiftBridgedTypes(objcClass))
if (TC.Context.isObjCClassWithMultipleSwiftBridgedTypes(objcClass))
return SolutionKind::Error;

// If the bridged value type is generic, the generic arguments
Expand Down
6 changes: 2 additions & 4 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,8 @@ static unsigned getNumRemovedArgumentLabels(TypeChecker &TC, ValueDecl *decl,
// bar.map(E.foo)
//
// `E.foo` has to act as a regular function type passed as a value.
if (!TC.getLangOpts().isSwiftVersion3()) {
if (auto *EED = dyn_cast<EnumElementDecl>(decl)) {
numParameterLists = EED->hasAssociatedValues() ? 2 : 1;
}
if (auto *EED = dyn_cast<EnumElementDecl>(decl)) {
numParameterLists = EED->hasAssociatedValues() ? 2 : 1;
}

// Only applicable to functions. Nothing else should have argument labels in
Expand Down
Loading