Skip to content

Commit 59d2d95

Browse files
authored
Merge pull request swiftlang#32234 from LucianoPAlmeida/SR-12946-cast-warning-metatype
2 parents 4fe0c69 + eb0b2bb commit 59d2d95

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,9 +3706,11 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
37063706
else
37073707
fromRequiresClass = fromType->mayHaveSuperclass();
37083708

3709-
// Casts between protocol metatypes only succeed if the type is existential.
3709+
// Casts between protocol metatypes only succeed if the type is existential
3710+
// or if it involves generic types because they may be protocol conformances
3711+
// we can't know at compile time.
37103712
if (metatypeCast) {
3711-
if (toExistential || fromExistential)
3713+
if ((toExistential || fromExistential) && !(fromArchetype || toArchetype))
37123714
return failed();
37133715
}
37143716

test/expr/cast/metatype_casts.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ use(C.self as P.Type) // expected-error{{cannot convert value of type 'C.Type' t
4949

5050
use(E.self as P.Protocol) // expected-error{{cannot convert value of type 'E.Type' to type 'P.Protocol' in coercion}}
5151
use(E.self as P.Type)
52+
53+
// SR-12946
54+
func SR12946<T>(_ e: T) {
55+
_ = AnyObject.self is T.Type // OK
56+
}
57+
58+
SR12946(1 as AnyObject)

0 commit comments

Comments
 (0)