mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
It turns out the query to check the reference semantics of a type had the side effect of importing some functions/types. This could introduce circular reference errors with our queries. This PR removes this side effect from the query and updates the test files. Since we do the same work later on in another query, the main change is just the wording of the diagnostics (and we now can also infer immortal references based on the base types). This PR also reorders some operations, specifically now we mark base classes as imported before we attempt to import template arguments. After these changes it is possible to remove the last feature check for strict memory safe mode. We want to mark types as @unsafe in both language modes so we can diagnose redundant unsafe markers even when the feature is off.
158 lines
6.2 KiB
Swift
158 lines
6.2 KiB
Swift
// REQUIRES: executable_test
|
||
// RUN: %target-run-simple-swift(-cxx-interoperability-mode=default -Xfrontend -disable-availability-checking -I %S/Inputs)
|
||
|
||
// TODO: Fix this lit test failure on windows rdar://145218056
|
||
// XFAIL: OS=windows-msvc
|
||
|
||
import Inheritance
|
||
import StdlibUnittest
|
||
|
||
// A function whose explicit type annotations specializes the cxxCast function.
|
||
//
|
||
// In Swift, the generic type parameters of cxxCast I and O should be respectively instantiated to SubT and BaseT.
|
||
// However, since these are foreign reference types, this instantiates (and calls) cxxCast<SubT *, BaseT *> in C++.
|
||
func cast(_ s: SubT) -> BaseT {
|
||
return cxxCast(s)
|
||
}
|
||
|
||
var TemplatingTestSuite = TestSuite("Foreign references work with templates")
|
||
|
||
TemplatingTestSuite.test("SubT") {
|
||
let s: SubT = SubT.getSubT()
|
||
expectFalse(s.isBase)
|
||
let sc: BaseT = cast(s)
|
||
expectFalse(sc.isBase)
|
||
let sx: BaseT = cxxCast(s) // should instantiate I to SubT and O to BaseT
|
||
expectFalse(sc.isBase)
|
||
}
|
||
|
||
TemplatingTestSuite.test("BaseT") {
|
||
let b: BaseT = BaseT.getBaseT()
|
||
expectTrue(b.isBase)
|
||
let bc: BaseT = cxxCast(b) // should instantiate I and O both to BaseT
|
||
expectTrue(bc.isBase)
|
||
}
|
||
|
||
TemplatingTestSuite.test("DerivedOutOfOrder") {
|
||
let d = DerivedOutOfOrder.getInstance()
|
||
expectEqual(123, d.baseField)
|
||
expectEqual(456, d.derivedField)
|
||
expectEqual(789, d.leafField)
|
||
}
|
||
|
||
var FrtInheritanceTestSuite = TestSuite("Foreign references in C++ inheritance")
|
||
|
||
FrtInheritanceTestSuite.test("ParentChild") {
|
||
let immortalRefType = ImmortalRefereceExample.returnImmortalRefType()
|
||
expectTrue(
|
||
type(of: immortalRefType) is AnyObject.Type,
|
||
"Expected immortalRefType to be a reference type, but it’s a value type")
|
||
|
||
let derivedFromImmortalRefType = ImmortalRefereceExample.returnDerivedFromImmortalRefType()
|
||
expectTrue(
|
||
type(of: derivedFromImmortalRefType) is AnyObject.Type,
|
||
"Expected derivedFromImmortalRefType to be a value type, but it’s a reference type")
|
||
|
||
let valType = ExplicitAnnotationHasPrecedence1.returnValueType()
|
||
expectTrue(
|
||
!(type(of: valType) is AnyObject.Type),
|
||
"Expected valType to be a value type, but it’s a reference type")
|
||
|
||
let referenceType = ExplicitAnnotationHasPrecedence1.returnRefType()
|
||
expectTrue(
|
||
type(of: referenceType) is AnyObject.Type,
|
||
"Expected referenceType to be a reference type, but it’s a value type")
|
||
|
||
let derivedFromValType = ExplicitAnnotationHasPrecedence1.returnDerivedFromValueType()
|
||
expectTrue(
|
||
!(type(of: derivedFromValType) is AnyObject.Type),
|
||
"Expected derivedFromValType to be a value type, but it’s a reference type")
|
||
|
||
let derivedFromRefTypeAAndB = ExplicitAnnotationHasPrecedence2.returnDerivedFromRefTypeAAndB()
|
||
expectTrue(
|
||
!(type(of: derivedFromRefTypeAAndB) is AnyObject.Type),
|
||
"Expected derivedFromRefTypeAAndB to be a value type, but it’s a reference type")
|
||
|
||
let derivedFromRefTypeAAndBAnnotated =
|
||
ExplicitAnnotationHasPrecedence2.returnDerivedFromRefTypeAAndBAnnotated()
|
||
expectTrue(
|
||
type(of: derivedFromRefTypeAAndBAnnotated) is AnyObject.Type,
|
||
"Expected derivedFromRefTypeAAndBAnnotated to be a reference type, but it’s a value type")
|
||
|
||
let derivedFromValueTypeAndAnnotated =
|
||
ExplicitAnnotationHasPrecedence1.returnDerivedFromValueTypeAndAnnotated()
|
||
expectTrue(
|
||
type(of: derivedFromValueTypeAndAnnotated) is AnyObject.Type,
|
||
"Expected derivedFromValueTypeAndAnnotated to be a reference type, but it’s a value type")
|
||
|
||
let derivedFromReferenceType = ExplicitAnnotationHasPrecedence1.returnDerivedFromRefType()
|
||
expectTrue(
|
||
type(of: derivedFromReferenceType) is AnyObject.Type,
|
||
"Expected derivedFromReferenceType to be a reference type, but it’s a value type")
|
||
|
||
let derivedFromReferenceTypeAndAnnotated =
|
||
ExplicitAnnotationHasPrecedence1.returnDerivedFromRefTypeAndAnnotated()
|
||
expectTrue(
|
||
type(of: derivedFromReferenceTypeAndAnnotated) is AnyObject.Type,
|
||
"Expected derivedFromReferenceTypeAndAnnotated to be a reference type, but it’s a value type"
|
||
)
|
||
|
||
let valueType = BasicInheritanceExample.returnValueType()
|
||
expectTrue(
|
||
!(type(of: valueType) is AnyObject.Type),
|
||
"Expected valueType to be a value type, but it’s a reference type")
|
||
|
||
let refType = BasicInheritanceExample.returnRefType()
|
||
expectTrue(
|
||
type(of: refType) is AnyObject.Type,
|
||
"Expected refType to be a reference type, but it’s a value type")
|
||
|
||
let derivedFromRefType = BasicInheritanceExample.returnDerivedFromRefType()
|
||
expectTrue(
|
||
type(of: derivedFromRefType) is AnyObject.Type,
|
||
"Expected derivedFromRefType to be a reference type, but it’s a value type")
|
||
|
||
let derivedFromBaseRef1AndBaseRef2 =
|
||
MultipleInheritanceExample1.returnDerivedFromBaseRef1AndBaseRef2()
|
||
expectTrue(
|
||
!(type(of: derivedFromBaseRef1AndBaseRef2) is AnyObject.Type),
|
||
"Expected derivedFromBaseRef1AndBaseRef2 to be a value type, but it’s a reference type")
|
||
|
||
let derivedFromBaseRef3 = MultipleInheritanceExample1.returnDerivedFromBaseRef3()
|
||
expectTrue(
|
||
type(of: derivedFromBaseRef3) is AnyObject.Type,
|
||
"Expected derivedFromBaseRef3 to be a reference type, but it’s a value type")
|
||
|
||
let d2 = MultipleInheritanceExample2.returnD()
|
||
expectTrue(
|
||
!(type(of: d2) is AnyObject.Type),
|
||
"Expected d2 to be a value type, but it’s a reference type")
|
||
|
||
let d3 = MultipleInheritanceExample2.returnD()
|
||
expectTrue(
|
||
!(type(of: d3) is AnyObject.Type),
|
||
"Expected d3 to be a value type, but it’s a reference type")
|
||
|
||
let refTypeDiamond = RefTypeDiamondInheritance.returnDiamond()
|
||
expectTrue(
|
||
!(type(of: refTypeDiamond) is AnyObject.Type),
|
||
"Expected refTypeDiamond to be a value type, but it’s a reference type")
|
||
|
||
let virtualDiamond = RefTypeDiamondInheritance.returnVirtualDiamond()
|
||
expectTrue(
|
||
type(of: virtualDiamond) is AnyObject.Type,
|
||
"Expected virtualDiamond to be a reference type, but it’s a value type")
|
||
|
||
let nonRefTypeDiamond = NonRefTypeDiamondInheritance.returnDiamond()
|
||
expectTrue(
|
||
type(of: nonRefTypeDiamond) is AnyObject.Type,
|
||
"Expected nonRefTypeDiamond to be a reference type, but it’s a value type")
|
||
|
||
let forest = InheritingTemplatedRefType.returnForest()
|
||
expectTrue(
|
||
type(of: forest) is AnyObject.Type,
|
||
"Expected forest to be a reference type, but it’s a value type")
|
||
}
|
||
|
||
runAllTests()
|