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

Conversation

Rajveer100
Copy link
Contributor

@Rajveer100 Rajveer100 commented Mar 28, 2023

Part of #61601

Filled out the remaining expression cases for ASTPrinter, which were left out after in the PR #40691.

@Rajveer100
Copy link
Contributor Author

@hamishknight I have implemented one of the functions for you to review if I have done it the right way.

@hamishknight hamishknight self-requested a review March 28, 2023 10:56
Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on!

lib/AST/ASTPrinter.cpp Outdated Show resolved Hide resolved
@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch from 7673bfa to 8231914 Compare March 28, 2023 19:00
@Rajveer100 Rajveer100 requested review from hamishknight and removed request for xedin, slavapestov and hborla March 28, 2023 19:04
@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch from 8231914 to 58e0722 Compare March 29, 2023 18:34
@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch from 58e0722 to ca14635 Compare March 30, 2023 11:36
@Rajveer100
Copy link
Contributor Author

Rajveer100 commented Mar 31, 2023

How would visitTypeJoinExpr, visitClosureExpr, ... be handled?
(Many more which I am figuring...)

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch from ca14635 to b15b531 Compare March 31, 2023 11:08
@Rajveer100 Rajveer100 requested a review from hamishknight March 31, 2023 11:09
@hamishknight
Copy link
Contributor

How would visitTypeJoinExpr, visitClosureExpr, ... be handled?

Sorry, I shouldn't have included TypeJoinExpr and OneWayExpr in the list, they only exist during constraint solving, so I don't think we need any printing logic for them (TypeJoinExpr isn't even really representable as source code anyway).

ClosureExpr should print a closure e.g { <body> } if there are no params, { <param>, ... in <body> } if there are params, { (<param>, ...) -> <Result> in <body>} if there is an explicit result type, { (<param>: <Type>, ...) in } if there is an explicit param type. Note you'll also need to handle CaptureListExpr, which also prints a closure, but with a capture list e.g { [<capture>, ...] <param>, ... in }.

lib/AST/ASTPrinter.cpp Outdated Show resolved Hide resolved
lib/AST/ASTPrinter.cpp Outdated Show resolved Hide resolved
@Rajveer100
Copy link
Contributor Author

How would visitTypeJoinExpr, visitClosureExpr, ... be handled?

Sorry, I shouldn't have included TypeJoinExpr and OneWayExpr in the list, they only exist during constraint solving, so I don't think we need any printing logic for them (TypeJoinExpr isn't even really representable as source code anyway).

ClosureExpr should print a closure e.g { <body> } if there are no params, { <param>, ... in <body> } if there are params, { (<param>, ...) -> <Result> in <body>} if there is an explicit result type, { (<param>: <Type>, ...) in } if there is an explicit param type. Note you'll also need to handle CaptureListExpr, which also prints a closure, but with a capture list e.g { [<capture>, ...] <param>, ... in }.

Alright! If I correctly understand, the capture in closures is similar to lambda capture in C++, just that in Swift it's handled by weak self?

Also after the recent update in Swift, I presume we no longer have to use self keyword inside the body (to reference a capture variable) once it's guarded.

@hamishknight
Copy link
Contributor

Alright! If I correctly understand, the capture in closures is similar to lambda capture in C++, just that in Swift it's handled by weak self?

Yes they are similar to lambda captures (specifically captures by value). weak and unowned can be used as ownership modifiers in a capture list, yes, but they can be applied to any capture, e.g:

class C {}
var c = C()
let fn = { [weak c] in }

And without them, the capture is by a strong reference:

class C {}
var c = C()
let fn = { [c] in }

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch from b15b531 to 1b5c7cf Compare April 8, 2023 10:01
@Rajveer100 Rajveer100 requested a review from tshortli as a code owner April 8, 2023 10:01
@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch 2 times, most recently from 6bb3422 to 8da1c75 Compare April 13, 2023 11:30
@Rajveer100 Rajveer100 requested a review from hamishknight April 13, 2023 11:30
@hamishknight
Copy link
Contributor

Note you will also need to fixup test/expr/print/protocol.swift to drop the access modifiers from the local vars:

  // 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: }

@Rajveer100
Copy link
Contributor Author

Rajveer100 commented Jan 25, 2024

KeyPathExpr is complete right, or are there some significant changes to do still?

@hamishknight
Copy link
Contributor

KeyPathExpr is complete right, or are there some significant changes to do still?

See #64667 (comment), the components still need printing

@Rajveer100
Copy link
Contributor Author

KeyPathExpr is complete right, or are there some significant changes to do still?

See #64667 (comment), the components still need printing

Under printKeyPathComponents, which components are missing?

@hamishknight
Copy link
Contributor

Under printKeyPathComponents, which components are missing?

The individual components are good, but you're not calling into printKeyPathComponents for regular key paths, and you're missing the dots between components.

@Rajveer100
Copy link
Contributor Author

The individual components are good, but you're not calling into printKeyPathComponents for regular key paths, and you're missing the dots between components.

IIUC, we need to call this irrespective of any of the conditions (like ObjC or if root exists or not)?

@hamishknight
Copy link
Contributor

IIUC, we need to call this irrespective of any of the conditions (like ObjC or if root exists or not)?

Yes, that's correct. FWIW it looks like the getParsedRoot() path is also missing the leading \. I would do the expr->isObjC() check as an early return, then print the \, then have the if statement where you print the root, then print the components. Happy for this to be done in a follow-up though if you leave it as a FIXME.

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch 2 times, most recently from d494003 to 135333f Compare January 29, 2024 09:14
@hamishknight
Copy link
Contributor

@swift-ci please test

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch from 135333f to dac72b2 Compare January 29, 2024 18:46
@Rajveer100
Copy link
Contributor Author

Rajveer100 commented Jan 29, 2024

Tests pass locally, I have pushed again, maybe a rebase should solve this, as there are few other tests not part of my changes that also fail?

Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

Looks like you need to make a couple of other changes (the change to not print access modifiers for local declarations has affected a couple of other tests):

In test/attr/accessibility_print.swift:

@@ -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

In decl/enum/derived_hashable_equatable.swift:

@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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

test/expr/print/misc_expr.swift Outdated Show resolved Hide resolved
@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch 3 times, most recently from dac72b2 to ffc7673 Compare January 30, 2024 18:54
@hamishknight
Copy link
Contributor

@swift-ci please test

@Rajveer100
Copy link
Contributor Author

Rajveer100 commented Jan 31, 2024

Regressions macOS Platform:

Failed Tests (2):
  Swift(macosx-x86_64) :: decl/enum/derived_hashable_equatable_macos.swift
  Swift(macosx-x86_64) :: expr/print/func_closures.swift

CI

@hamishknight
Copy link
Contributor

@swift-ci please test macOS

@hamishknight
Copy link
Contributor

Looks like a couple more test tweaks are needed:

test/decl/enum/derived_hashable_equatable_macos.swift:

@@ -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
@@ -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
@@ -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

test/expr/print/func_closures.swift:

@@ -1,4 +1,4 @@
-// RUN: %target-swift-frontend -print-ast %s 2>&1 | %FileCheck %s
+// RUN: %target-swift-frontend -print-ast -disable-availability-checking %s 2>&1 | %FileCheck %s
 
 func fetch() async throws -> String {
 }

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-61601 branch from ffc7673 to 48847da Compare February 2, 2024 08:21
@hamishknight
Copy link
Contributor

@swift-ci please test

@Rajveer100
Copy link
Contributor Author

CI looks good!

Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

Thank you!

@hamishknight hamishknight merged commit 658be5b into swiftlang:main Feb 5, 2024
5 checks passed
@Rajveer100
Copy link
Contributor Author

Welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants