Skip to content
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
175 changes: 112 additions & 63 deletions test/Inputs/lazy_typecheck.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
// This source file contains intentional type checking errors that should be
// avoided when compiling with -experimental-lazy-typecheck and emitting module
// outputs since all the errors occur in regions of the AST that do not
// need to be type checked in order to emit a module or module interface.

struct NoTypecheck {
static let int: Int = 0
static func fatalError() -> Never { Swift.fatalError() }
}

protocol NoTypecheckProto {}

extension NoTypecheck: PublicProto {
func req() -> Int { return 0 }
}

// MARK: - Global functions

public func publicFunc() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}

public func publicFuncWithDefaultArg(_ x: Int = 1) -> Int {
return doesNotExist() // expected-error {{cannot find 'doesNotExist' in scope}}
return NoTypecheck.int
}

package func packageFunc() -> Int {
return false // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}

func internalFunc() -> DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
return 1
func internalFunc() -> NoTypecheck {
return NoTypecheck()
}

@inlinable func inlinableFunc() -> Int {
return 1
}

private func privateFunc() -> DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
return 1
private func privateFunc() -> NoTypecheck {
return NoTypecheck()
}

public func constrainedGenericPublicFunction<T>(_ t: T) where T: PublicProto {
doesNotExist() // expected-error {{cannot find 'doesNotExist' in scope}}
_ = NoTypecheck()
}

@_specialize(exported: true, where T == PublicProto)
public func publicSpecializedFunc<T>(_ t: T) -> T {
return doesNotExist() // expected-error {{cannot find 'doesNotExist' in scope}}
_ = NoTypecheck()
return t
}

@available(SwiftStdlib 5.1, *)
public func publicFuncWithOpaqueReturnType() -> some PublicProto { // expected-note {{opaque return type declared here}}
return 1 // expected-error {{return type of global function 'publicFuncWithOpaqueReturnType()' requires that 'Int' conform to 'PublicProto'}}
public func publicFuncWithOpaqueReturnType() -> some PublicProto {
return NoTypecheck()
}

@available(SwiftStdlib 5.1, *)
Expand All @@ -58,38 +66,45 @@ public func publicFuncWithOpaqueReturnType() -> some PublicProto { // expected-n
public struct PublicWrapper<T> {
public var wrappedValue: T {
get {
_ = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
NoTypecheck.fatalError()
}
set {
_ = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
NoTypecheck.fatalError()
}
}

public var projectedValue: PublicWrapper { self }

public init(wrappedValue value: T) {
_ = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
NoTypecheck.fatalError()
}
}

@propertyWrapper
struct InternalWrapper<T> {} // expected-error {{property wrapper type 'InternalWrapper' does not contain a non-static property named 'wrappedValue'}}
struct InternalWrapper {
var wrappedValue: NoTypecheck
}

// MARK: - Global vars

public var publicGlobalVar: Int = 0
public var publicGlobalVarInferredType = ""
public var (publicGlobalVarInferredTuplePatX, publicGlobalVarInferredTuplePatY) = (0, 1)

var internalGlobalVar: DoesNotExist // expected-error {{cannot find type 'DoesNotExist' in scope}}
var internalGlobalVarInferredType = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
var internalGlobalVar: NoTypecheck = NoTypecheck()
var internalGlobalVarInferredType = NoTypecheck()

// MARK: - Nominal types

public protocol EmptyPublicProto {}

public protocol PublicProto {
func req() -> Int // expected-note 2 {{protocol requires function 'req()' with type '() -> Int'; add a stub for conformance}}
func req() -> Int
}

public protocol PublicProtoWithAssociatedType {
associatedtype A
func req() -> A
}

@rethrows public protocol PublicRethrowsProto {
Expand All @@ -100,13 +115,13 @@ public protocol PublicProto {
func req() throws -> Int
}

protocol InternalProto {
func goodReq() -> Int // expected-note 2 {{protocol requires function 'goodReq()' with type '() -> Int'; add a stub for conformance}}
func badReq() -> DoesNotExist // expected-error {{cannot find type 'DoesNotExist' in scope}}
protocol InternalProtoWithAssociatedType {
associatedtype A
func internalReq() -> A
}

protocol InternalProtoConformingToPublicProto: PublicProto {
func internalReq() -> DoesNotExist // expected-error {{cannot find type 'DoesNotExist' in scope}}
func internalReq() -> NoTypecheck
}

public struct PublicStruct {
Expand All @@ -115,89 +130,104 @@ public struct PublicStruct {
@PublicWrapper public var publicWrappedProperty = 3.14

public init(x: Int) {
_ = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
self.publicProperty = 1
_ = NoTypecheck()
}

public func publicMethod() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}

@MainActor public func publicMainActorMethod() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}

public static func publicStaticMethod() {
_ = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
_ = NoTypecheck()
}

func internalMethod() -> DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
return 1
func internalMethod() -> NoTypecheck {
return NoTypecheck()
}

static func internalStaticMethod() -> DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
return 1
static func internalStaticMethod() -> NoTypecheck {
return NoTypecheck()
}
}

public struct PublicGenericStruct<T> {
var t: T

public func publicMethod() -> T {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'T'}}
_ = NoTypecheck()
return t
}
}

struct InternalStruct: DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
var x: DoesNotExist // expected-error {{cannot find type 'DoesNotExist' in scope}}
struct InternalStruct: NoTypecheckProto {
var x: NoTypecheck

func f(_ x: DoesNotExist) {} // expected-error {{cannot find type 'DoesNotExist' in scope}}
func f(_ x: NoTypecheck) {}
}

public class PublicClass {
public var publicProperty: Int
public var publicPropertyInferredType = ""

public init(x: Int) {
_ = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
self.publicProperty = x
_ = NoTypecheck()
}

// FIXME: TBDGen causes this constructor to be type checked
// init(_ x: DoesNotExist) {}
convenience init(_ x: NoTypecheck) {
NoTypecheck.fatalError()
}

public func publicMethod() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}

public class func publicClassMethod() {
_ = DoesNotExist() // expected-error {{cannot find 'DoesNotExist' in scope}}
_ = NoTypecheck()
}

// FIXME: TBDGen causes these methods to be type checked
// func internalMethod() -> DoesNotExist {}
// class func internalClassMethod() -> DoesNotExist {}
public static func publicStaticMethod() {
_ = NoTypecheck()
}

func internalMethod() -> NoTypecheck {
return NoTypecheck()
}

class func internalClassMethod() -> NoTypecheck {
return NoTypecheck()
}
}

public class PublicDerivedClass: PublicClass {}

class InternalClass: DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
init(x: DoesNotExist) {} // expected-error {{cannot find type 'DoesNotExist' in scope}}
class InternalClass: NoTypecheckProto {
init(x: NoTypecheck) {}
}

public enum PublicEnum {
case a
case b(x: Int)

public func publicMethod() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}

public var publicComputedVar: Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}
}

enum InternalEnum {
case bad(DoesNotExist) // expected-error {{cannot find type 'DoesNotExist' in scope}}
case bad(NoTypecheck)

func method() -> DoesNotExist { // expected-error {{cannot find type 'DoesNotExist' in scope}}
func method() -> NoTypecheck {
return NoTypecheck()
}
}

Expand All @@ -206,47 +236,66 @@ enum InternalEnum {
public struct PublicStructConformingToPublicProto: PublicProto {
public init() {}
public func req() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}
}

public struct PublicStructIndirectlyConformingToPublicProto: InternalProtoConformingToPublicProto {
public init() {}
public func req() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}

func internalReq() -> NoTypecheck {
return NoTypecheck()
}
}

public class PublicClassConformingToPublicProto: PublicProto {
public init() {}
public func req() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}
}

public class PublicClassInheritingConformanceToPublicProto: PublicClassConformingToPublicProto {}

extension String: PublicProto {
public func req() -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}
}

extension String: InternalProto {} // expected-error {{type 'String' does not conform to protocol 'InternalProto'}}
extension String: InternalProtoWithAssociatedType {
func internalReq() -> NoTypecheck {
return NoTypecheck()
}
}

extension Int: PublicRethrowsProto {
public func req() throws -> Int {
return true // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
return NoTypecheck.int
}
}

struct InternalStructConformingToPublicProto: PublicProto { // expected-error {{type 'InternalStructConformingToPublicProto' does not conform to protocol 'PublicProto'}}
struct InternalStructConformingToPublicProto: PublicProtoWithAssociatedType {
typealias A = NoTypecheck
func req() -> A {
return NoTypecheck()
}
}

extension InternalStruct: PublicProto { // expected-error {{type 'InternalStruct' does not conform to protocol 'PublicProto'}}
extension InternalStruct: PublicProtoWithAssociatedType {
typealias A = NoTypecheck
func req() -> A {
return NoTypecheck()
}
}

struct InternalStructConformingToInternalProto: InternalProto { // expected-error {{type 'InternalStructConformingToInternalProto' does not conform to protocol 'InternalProto'}}
struct InternalStructConformingToInternalProto: InternalProtoWithAssociatedType {
func internalReq() -> NoTypecheck {
return NoTypecheck()
}
}

struct InternalStructForConstraint {}
Expand All @@ -258,15 +307,15 @@ extension PublicGenericStruct: EmptyPublicProto where T == InternalStructForCons
// MARK: - Type aliases

public typealias PublicStructAlias = PublicStruct
typealias InternalTypeAlias = DoesNotExist // expected-error {{cannot find type 'DoesNotExist' in scope}}
typealias InternalTypeAlias = NoTypecheck

// MARK: - Compiler directives

extension PublicStruct {
#if FLAG
public static func activeMethod() {}
#else
public static func inactiveMethod() -> DoesNotExist {}
public static func inactiveMethod() -> NoTypecheck {}
#endif
}

Expand Down
Loading