Skip to content
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

Added unimplemented expression cases in the ASTPrinter #64667

Merged
merged 1 commit into from
Feb 5, 2024
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
264 changes: 247 additions & 17 deletions lib/AST/ASTPrinter.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions test/attr/accessibility_print.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ public extension FE_PublicClass {

// CHECK-LABEL: internal func GA_localTypes()
func GA_localTypes() {
// CHECK-SRC: private struct Local {
// CHECK-SRC: struct Local {
struct Local {
// CHECK-SRC: internal let x
let x = 0
}
_ = Local()

// CHECK-SRC: private enum LocalEnum {
// CHECK-SRC: enum LocalEnum {
enum LocalEnum {
// CHECK-SRC: {{^}} case A
case A, B
Expand Down
18 changes: 9 additions & 9 deletions test/decl/enum/derived_hashable_equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ enum Simple: Hashable {
case b

// CHECK: @_implements(Equatable, ==(_:_:)) internal static func __derived_enum_equals(_ a: Simple, _ b: Simple) -> Bool {
// CHECK-NEXT: private var index_a: Int
// CHECK-NEXT: var index_a: Int
// CHECK-NEXT: switch a {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_a = 0
// CHECK-NEXT: case .b:
// CHECK-NEXT: index_a = 1
// CHECK-NEXT: }
// CHECK-NEXT: private var index_b: Int
// CHECK-NEXT: var index_b: Int
// CHECK-NEXT: switch b {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_b = 0
Expand All @@ -26,7 +26,7 @@ enum Simple: Hashable {
// CHECK-NEXT: }

// CHECK: internal func hash(into hasher: inout Hasher) {
// CHECK-NEXT: private var discriminator: Int
// CHECK-NEXT: var discriminator: Int
// CHECK-NEXT: switch self {
// CHECK-NEXT: case .a:
// CHECK-NEXT: discriminator = 0
Expand Down Expand Up @@ -101,14 +101,14 @@ enum HasUnavailableElement: Hashable {
case b

// CHECK: @_implements(Equatable, ==(_:_:)) internal static func __derived_enum_equals(_ a: HasUnavailableElement, _ b: HasUnavailableElement) -> Bool {
// CHECK-NEXT: private var index_a: Int
// CHECK-NEXT: var index_a: Int
// CHECK-NEXT: switch a {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_a = 0
// CHECK-NEXT: case .b:
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
// CHECK-NEXT: }
// CHECK-NEXT: private var index_b: Int
// CHECK-NEXT: var index_b: Int
// CHECK-NEXT: switch b {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_b = 0
Expand All @@ -119,7 +119,7 @@ enum HasUnavailableElement: Hashable {
// CHECK-NEXT: }

// CHECK: internal func hash(into hasher: inout Hasher) {
// CHECK-NEXT: private var discriminator: Int
// CHECK-NEXT: var discriminator: Int
// CHECK-NEXT: switch self {
// CHECK-NEXT: case .a:
// CHECK-NEXT: discriminator = 0
Expand Down Expand Up @@ -185,14 +185,14 @@ enum UnavailableEnum: Hashable {
case b

// CHECK: @_implements(Equatable, ==(_:_:)) internal static func __derived_enum_equals(_ a: UnavailableEnum, _ b: UnavailableEnum) -> Bool {
// CHECK-NEXT: private var index_a: Int
// CHECK-NEXT: var index_a: Int
// CHECK-NEXT: switch a {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_a = 0
// CHECK-NEXT: case .b:
// CHECK-NEXT: index_a = 1
// CHECK-NEXT: }
// CHECK-NEXT: private var index_b: Int
// CHECK-NEXT: var index_b: Int
// CHECK-NEXT: switch b {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_b = 0
Expand All @@ -203,7 +203,7 @@ enum UnavailableEnum: Hashable {
// CHECK-NEXT: }

// CHECK: internal func hash(into hasher: inout Hasher) {
// CHECK-NEXT: private var discriminator: Int
// CHECK-NEXT: var discriminator: Int
// CHECK-NEXT: switch self {
// CHECK-NEXT: case .a:
// CHECK-NEXT: discriminator = 0
Expand Down
6 changes: 3 additions & 3 deletions test/decl/enum/derived_hashable_equatable_macos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum HasElementsWithAvailability: Hashable {
case introduced10_50

// CHECK: @_implements(Equatable, ==(_:_:)) internal static func __derived_enum_equals(_ a: HasElementsWithAvailability, _ b: HasElementsWithAvailability) -> Bool {
// CHECK-NEXT: private var index_a: Int
// CHECK-NEXT: var index_a: Int
// CHECK-NEXT: switch a {
// CHECK-NEXT: case .alwaysAvailable:
// CHECK-NEXT: index_a = 0
Expand All @@ -40,7 +40,7 @@ enum HasElementsWithAvailability: Hashable {
// CHECK-NEXT: case .introduced10_50:
// CHECK-NEXT: index_a = 2
// CHECK-NEXT: }
// CHECK-NEXT: private var index_b: Int
// CHECK-NEXT: var index_b: Int
// CHECK-NEXT: switch b {
// CHECK-NEXT: case .alwaysAvailable:
// CHECK-NEXT: index_b = 0
Expand All @@ -59,7 +59,7 @@ enum HasElementsWithAvailability: Hashable {
// CHECK-NEXT: }

// CHECK: internal func hash(into hasher: inout Hasher) {
// CHECK-NEXT: private var discriminator: Int
// CHECK-NEXT: var discriminator: Int
// CHECK-NEXT: switch self {
// CHECK-NEXT: case .alwaysAvailable:
// CHECK-NEXT: discriminator = 0
Expand Down
20 changes: 20 additions & 0 deletions test/expr/print/conditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ repeat {
// CHECK: repeat {
// CHECK: b += 1
// CHECK: } while b < 10

var p = (17 > 7 ? true : false)
// CHECK: @_hasInitialValue internal var p: (Bool) = (17 > 7 ? true : false)

var x: Int = 3
var y: Bool = x is Int
// CHECK: @_hasInitialValue internal var y: Bool = x is Int

enum SomeError: Error {
case errorType
}

func someThrowingFunc() throws -> SomeError {
throw SomeError.errorType
}

var tryExpr = try? someThrowingFunc()
// CHECK: @_hasInitialValue internal var tryExpr: SomeError? = try? someThrowingFunc()

var tryForceExpr = try! someThrowingFunc()
55 changes: 55 additions & 0 deletions test/expr/print/func_closures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// RUN: %target-swift-frontend -print-ast -disable-availability-checking %s 2>&1 | %FileCheck %s

func fetch() async throws -> String {
}
// CHECK: internal func fetch() async throws -> String {
// CHECK: }

let fn = { (x: Int) in
}
Rajveer100 marked this conversation as resolved.
Show resolved Hide resolved
// CHECK: @_hasInitialValue internal let fn: (_ x: Int) -> () = { (x: Int) in
// CHECK: }

let fn1 = {}
// CHECK: @_hasInitialValue internal let fn1: () -> () = { () in
// CHECK: }

let fn2: (Int) -> Void = { x in }
// CHECK: @_hasInitialValue internal let fn2: (Int) -> Void = { (x: Int) in
// CHECK: }

let fn3: (Int, Int) -> Void = { x, y in }
// CHECK: @_hasInitialValue internal let fn3: (Int, Int) -> Void = { (x: Int, y: Int) in
// CHECK: }

let fn4: (Int, Int) -> Void = { (x, y) in }
// CHECK: @_hasInitialValue internal let fn4: (Int, Int) -> Void = { (x: Int, y: Int) in
// CHECK: }

let fn5 = { (x: String, y: Int) in }
// CHECK: @_hasInitialValue internal let fn5: (_ x: String, _ y: Int) -> () = { (x: String, y: Int) in
// CHECK: }

let fn6: (Int) -> Int = { x -> Int in x }
// CHECK: @_hasInitialValue internal let fn6: (Int) -> Int = { (x: Int) -> Int in
// CHECK: return x
// CHECK: }

let fn7: (Int, Int) -> Int = { (x, y) -> Int in x + y }
// CHECK: @_hasInitialValue internal let fn7: (Int, Int) -> Int = { (x: Int, y: Int) -> Int in
// CHECK: return x + y
// CHECK: }

let fn8 = { (x: Int, y: Int) -> Int in x + y }
// CHECK: @_hasInitialValue internal let fn8: (_ x: Int, _ y: Int) -> Int = { (x: Int, y: Int) -> Int in
// CHECK: return x + y
// CHECK: }

let fn9 = { () -> Int in 0 }
// CHECK: @_hasInitialValue internal let fn9: () -> Int = { () -> Int in
// CHECK: return 0
// CHECK: }

let fn10 = { () in }
// CHECK: @_hasInitialValue internal let fn10: () -> () = { () in
// CHECK: }
140 changes: 140 additions & 0 deletions test/expr/print/misc_expr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// RUN: %target-swift-frontend -print-ast -disable-objc-attr-requires-foundation-module -enable-objc-interop %s 2>&1 | %FileCheck %s

var x = 0.0

type(of: x)
// CHECK: type(of: x)

class A {
var x: Int = 3
init() {
}
}

class B: A {
override init() {
super.init()
super.x = 2;
}
}
// CHECK: @_inheritsConvenienceInitializers internal class B : A {
// CHECK: override internal init() {
// CHECK: super.init()
// CHECK: super.x = 2
// CHECK: }
// CHECK: @objc deinit {
// CHECK: }
// CHECK: }

var a: [Int] = [1, 3, 2];

for i in 0...2 {
a[i] = i;
}
// CHECK: for i in 0 ... 2 {
// CHECK: a[i] = i
// CHECK: }

let y: Double = 0 as Double
// CHECK: @_hasInitialValue internal let y: Double = 0 as Double

var stringToInt: Int? = Int("1");

var forceInt: Int = stringToInt!
// CHECK: @_hasInitialValue internal var forceInt: Int = stringToInt!

var prefixUnaryExpr: Int = -a[1];
// CHECK: @_hasInitialValue internal var prefixUnaryExpr: Int = -a[1]
var postfixUnaryExpr: Int = stringToInt!
// CHECK: @_hasInitialValue internal var postfixUnaryExpr: Int = stringToInt!

class MyID {
var num = 1
}

class SomeJSON {
var id: MyID?
}

let cnt = SomeJSON().id?.num
// CHECK: @_hasInitialValue internal let cnt: Int? = SomeJSON().id?.num

struct User {
let name: String
let email: String
let address: Address?
let role: Role
}

struct Address {
let street: String
}

enum Role {
case admin
case member
case guest

var permissions: [Permission] {
switch self {
case .admin:
return [.create, .read, .update, .delete]
case .member:
return [.create, .read]
case .guest:
return [.read]
}
}
}

enum Permission {
case create
case read
case update
case delete
}

var user = User(
name: "Swift",
email: "http://www.swift.org",
address: nil,
role: .admin
)


let userRoleKeyPath = \User.role
// CHECK: @_hasInitialValue internal let userRoleKeyPath: KeyPath<User, Role> = \User
Comment on lines +105 to +106
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be checking for \User.role, remember we need to be printing out the original code.

Some other key path tests you could add are e.g:

// Multiple members
\X.y.z

// Optional chaining
\X.y?.z

// Optional force
\X.y!
\X.y!.z

// Dot self
\X.self

// Subscript
\X[0]
\X.[0]
\X.y[0]

// Tuple
\(Int, Int).0
\(x: Int, y: Int).x


let role = user[keyPath: userRoleKeyPath]
// CHECK: @_hasInitialValue internal let role: Role = user[keyPath: userRoleKeyPath]

struct FakeColor: _ExpressibleByColorLiteral {
init(_colorLiteralRed: Float, green: Float, blue: Float, alpha: Float) {}
}
typealias _ColorLiteralType = FakeColor

let myColor = #colorLiteral(red: 0.292, green: 0.081, blue: 0.6, alpha: 255)
// CHECK: @_hasInitialValue internal let myColor: FakeColor = #colorLiteral(red: 0.292, green: 0.081, blue: 0.6, alpha: 255)

enum FileNames {
static let main = FileNames.file(#file)
// CHECK: @_hasInitialValue internal static let main: FileNames = FileNames.file(#file)
case file(String)
}

class C {
@objc
var foo = 0
}

func foo(_ x: AnyObject) {
let y = x.foo
}
// CHECK: internal func foo(_ x: AnyObject) {
// CHECK: @_hasInitialValue let y: Int? = x.foo
// CHECK: }

struct S {
func foo() {}
}
let fn = S.foo
4 changes: 2 additions & 2 deletions test/expr/print/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func cast(_ a: Any) {
let forced = a as! Archivable
}
// CHECK: internal func cast(_ a: Any) {
// CHECK: @_hasInitialValue private let conditional: (any Archivable)? = a as? any Archivable
// CHECK: @_hasInitialValue private let forced: any Archivable = a as! any Archivable
// CHECK: @_hasInitialValue let conditional: (any Archivable)? = a as? any Archivable
// CHECK: @_hasInitialValue let forced: any Archivable = a as! any Archivable
// CHECK: }