Skip to content

Commit 5638d92

Browse files
committed
Escalate the warning about the "== Self" type soundness hole to an error.
Swift 3 had a type soundness hole in protocol conformance checking where the requirement contained an "== Self" constraint and the witness was a member of a non-final class. We previously closed the type soundness hole in PR #9830, but left it as a warning in Swift 3 compatibility mode. Escalate that warning to an error. The optimizers break due to this type soundness hole, and of course it can lead to other runtime breakage because it violates the type system. Fixes rdar://problem/28601761. (cherry picked from commit 8b80681)
1 parent ab8ab48 commit 5638d92

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,11 +1416,6 @@ ERROR(witness_self_same_type,none,
14161416
" (in protocol %5) due to same-type requirement involving 'Self'",
14171417
(DescriptiveDeclKind, DeclName, Type, DescriptiveDeclKind,
14181418
DeclName, Type))
1419-
WARNING(witness_self_same_type_warn,none,
1420-
"%0 %1 in non-final class %2 cannot be used to satisfy requirement %3 %4"
1421-
" (in protocol %5) due to same-type requirement involving 'Self'",
1422-
(DescriptiveDeclKind, DeclName, Type, DescriptiveDeclKind,
1423-
DeclName, Type))
14241419
NOTE(witness_self_weaken_same_type,none,
14251420
"consider weakening the same-type requirement %0 == %1 to a superclass "
14261421
"requirement", (Type, Type))

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,9 +3121,7 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
31213121
auto proto = Conformance->getProtocol();
31223122
auto &diags = proto->getASTContext().Diags;
31233123
diags.diagnose(witness->getLoc(),
3124-
proto->getASTContext().LangOpts.isSwiftVersion3()
3125-
? diag::witness_self_same_type_warn
3126-
: diag::witness_self_same_type,
3124+
diag::witness_self_same_type,
31273125
witness->getDescriptiveKind(),
31283126
witness->getFullName(),
31293127
Conformance->getType(),

test/Compatibility/self_same_type.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// RUN: %target-typecheck-verify-swift -swift-version 3
22

3+
// Note: while Swift 3.2 originally intended to provide backward
4+
// compatibility here, the type-soundness issue was considered so severe
5+
// (due to it breaking the optimizer) that that we escalated it to an
6+
// error.
7+
38
protocol P {
49
associatedtype T
510
}
@@ -9,7 +14,7 @@ protocol Q {
914
}
1015

1116
class C1: Q {
12-
func foo<T: P>(_: T, _: C1) where T.T == C1 {} // expected-warning{{instance method 'foo' in non-final class 'C1' cannot be used to satisfy requirement instance method 'foo' (in protocol 'Q') due to same-type requirement involving 'Self'}}}}
17+
func foo<T: P>(_: T, _: C1) where T.T == C1 {} // expected-error{{instance method 'foo' in non-final class 'C1' cannot be used to satisfy requirement instance method 'foo' (in protocol 'Q') due to same-type requirement involving 'Self'}}}}
1318
// expected-note@-1{{consider weakening the same-type requirement 'T.T' == 'C1' to a superclass requirement}}{{41-43=:}}
1419
}
1520

0 commit comments

Comments
 (0)