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

[SR-12934] How can I forward #filePath for Swift 5.2 & 5.3 without just silencing the warning #55380

Open
weissi opened this issue Jun 4, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@weissi
Copy link
Member

weissi commented Jun 4, 2020

Previous ID SR-12934
Radar rdar://problem/63974145
Original Reporter @weissi
Type Bug

Attachment: Download

Environment

today's snapshot Swift version 5.3-dev (LLVM 5b8bad5c6b, Swift d992e04717)

Additional Detail from JIRA
Votes 1
Component/s
Labels Bug
Assignee None
Priority Medium

md5: b8e8326821df35d900d7831aa50a46da

Issue Description:

Let's assume I have a method today that is

func assertFoo(filePath: String: #file, line: Int = #line) { ... }

One way to make this work for Swift 5.2 & 5.3 is this

#if compiler(>=5.3)
func assertFoo(filePath: String: #filePath, line: Int = #line) { ... }
#else
func assertFoo(filePath: String: #file, line: Int = #line) { ... }
#endif

but that means duplicating the whole method body which is usually unacceptable.

A cute hack is to do something like

#if compiler(>=5.3)
func _filePath(_ filePath: String = #filePath) { return filePath }
#else
func _filePath(_ filePath: String = #file) { return filePath }
#endif

func assertFoo(filePath: String = _filePath(), line: Int = #line) { ... }

but this doesn't seem to work reliably.

I've written the attached program (attention, it's a multiple modules in one file Swift file that you need to run with /path/to/swift-nio/dev/make-single-file-spm test.swift run or you just download the attached tarball which is a SwiftPM project.

import Other
import Other2

print("==== from main ====")
print("EXPECTED: file=\(#file), line=\(#line + 1)")
f()
print("==== from Other2 ====")
doItInOther2()
print("==== from Other2 only internal functions ====")
doItInOther2Internally()

// MODULE: Other
@inline(__always)
public func idString(_ x: String = #file) -> String { return x }
@inline(__always)
public func idStaticString(_ x: StaticString = #file) -> StaticString { return x }
@inline(__always)
public func idInt(_ x: Int = #line) -> Int { return x }
@inline(__always)
public func idUInt(_ x: UInt = #line) -> UInt { return x }

// MODULE: Other2
import Other

@inline(__always)
public func internalIdString(_ x: String = #file) -> String { return x }
@inline(__always)
public func internalIdStaticString(_ x: StaticString = #file) -> StaticString { return x }
@inline(__always)
public func internalIdInt(_ x: Int = #line) -> Int { return x }
@inline(__always)
public func internalIdUInt(_ x: UInt = #line) -> UInt { return x }

func internalOnlyIdString(_ x: String = #file) -> String { return x }
func internalOnlyIdStaticString(_ x: StaticString = #file) -> StaticString { return x }
func internalOnlyIdInt(_ x: Int = #line) -> Int { return x }
func internalOnlyIdUInt(_ x: UInt = #line) -> UInt { return x }

public func f(
    directFileString                                    :                    String       = #file,
    directFileStaticString                              :                    StaticString = #file,
    autoClosureFileString                               : @autoclosure () -> String       = #file,
    autoClosureFileStaticString                         : @autoclosure () -> StaticString = #file,
    computedDirectFileString                            :                    String       = idString(#file),
    computedDirectFileStaticString                      :                    StaticString = idStaticString(#file),
    computedAutoClosureFileString                       : @autoclosure () -> String       = idString(#file),
    computedAutoClosureFileStaticString                 : @autoclosure () -> StaticString = idStaticString(#file),
    computedForwardedDirectFileString                   :                    String       = idString(),
    computedForwardedDirectFileStaticString             :                    StaticString = idStaticString(),
    computedForwardedAutoClosureFileString              : @autoclosure () -> String       = idString(),
    computedForwardedAutoClosureFileStaticString        : @autoclosure () -> StaticString = idStaticString(),
    internalComputedDirectFileString                    :                    String       = internalIdString(#file),
    internalComputedDirectFileStaticString              :                    StaticString = internalIdStaticString(#file),
    internalComputedAutoClosureFileString               : @autoclosure () -> String       = internalIdString(#file),
    internalComputedAutoClosureFileStaticString         : @autoclosure () -> StaticString = internalIdStaticString(#file),
    internalComputedForwardedDirectFileString           :                    String       = internalIdString(),
    internalComputedForwardedDirectFileStaticString     :                    StaticString = internalIdStaticString(),
    internalComputedForwardedAutoClosureFileString      : @autoclosure () -> String       = internalIdString(),
    internalComputedForwardedAutoClosureFileStaticString: @autoclosure () -> StaticString = internalIdStaticString(),
    directLineInt                                       :                    Int          = #line,
    directLineUInt                                      :                    UInt         = #line,
    autoClosureLineInt                                  : @autoclosure () -> Int          = #line,
    autoClosureLineUInt                                 : @autoclosure () -> UInt         = #line,
    computedDirectLineInt                               :                    Int          = idInt(#line),
    computedDirectLineUInt                              :                    UInt         = idUInt(#line),
    computedAutoClosureLineInt                          : @autoclosure () -> Int          = idInt(#line),
    computedAutoClosureLineUInt                         : @autoclosure () -> UInt         = idUInt(#line),
    computedForwardedDirectLineInt                      :                    Int          = idInt(),
    computedForwardedDirectLineUInt                     :                    UInt         = idUInt(),
    computedForwardedAutoClosureLineInt                 : @autoclosure () -> Int          = idInt(),
    computedForwardedAutoClosureLineUInt                : @autoclosure () -> UInt         = idUInt(),
    internalComputedDirectLineInt                       :                    Int          = internalIdInt(#line),
    internalComputedDirectLineUInt                      :                    UInt         = internalIdUInt(#line),
    internalComputedAutoClosureLineInt                  : @autoclosure () -> Int          = internalIdInt(#line),
    internalComputedAutoClosureLineUInt                 : @autoclosure () -> UInt         = internalIdUInt(#line),
    internalComputedForwardedDirectLineInt              :                    Int          = internalIdInt(),
    internalComputedForwardedDirectLineUInt             :                    UInt         = internalIdUInt(),
    internalComputedForwardedAutoClosureLineInt         : @autoclosure () -> Int          = internalIdInt(),
    internalComputedForwardedAutoClosureLineUInt        : @autoclosure () -> UInt         = internalIdUInt()
    ) {

    print(
          directFileString,
          directFileStaticString,
          autoClosureFileString(),
          autoClosureFileStaticString(),
          computedDirectFileString,
          computedDirectFileStaticString,
          computedAutoClosureFileString(),
          computedAutoClosureFileStaticString(),
          computedForwardedDirectFileString,
          computedForwardedDirectFileStaticString,
          computedForwardedAutoClosureFileString(),
          computedForwardedAutoClosureFileStaticString(),
          internalComputedDirectFileString,
          internalComputedDirectFileStaticString,
          internalComputedAutoClosureFileString(),
          internalComputedAutoClosureFileStaticString(),
          internalComputedForwardedDirectFileString,
          internalComputedForwardedDirectFileStaticString,
          internalComputedForwardedAutoClosureFileString(),
          internalComputedForwardedAutoClosureFileStaticString(),
          directLineInt,
          directLineUInt,
          autoClosureLineInt(),
          autoClosureLineUInt(),
          computedDirectLineInt,
          computedDirectLineUInt,
          computedAutoClosureLineInt(),
          computedAutoClosureLineUInt(),
          computedForwardedDirectLineInt,
          computedForwardedDirectLineUInt,
          computedForwardedAutoClosureLineInt(),
          computedForwardedAutoClosureLineUInt(),
          internalComputedDirectLineInt,
          internalComputedDirectLineUInt,
          internalComputedAutoClosureLineInt(),
          internalComputedAutoClosureLineUInt(),
          internalComputedForwardedDirectLineInt,
          internalComputedForwardedDirectLineUInt,
          internalComputedForwardedAutoClosureLineInt(),
          internalComputedForwardedAutoClosureLineUInt(),
          separator: "\n")
}

internal func fInternal(
    directFileString                                        :                    String       = #file,
    directFileStaticString                                  :                    StaticString = #file,
    autoClosureFileString                                   : @autoclosure () -> String       = #file,
    autoClosureFileStaticString                             : @autoclosure () -> StaticString = #file,
    internalOnlyComputedDirectFileString                    :                    String       = internalOnlyIdString(#file),
    internalOnlyComputedDirectFileStaticString              :                    StaticString = internalOnlyIdStaticString(#file),
    internalOnlyComputedAutoClosureFileString               : @autoclosure () -> String       = internalOnlyIdString(#file),
    internalOnlyComputedAutoClosureFileStaticString         : @autoclosure () -> StaticString = internalOnlyIdStaticString(#file),
    internalOnlyComputedForwardedDirectFileString           :                    String       = internalOnlyIdString(),
    internalOnlyComputedForwardedDirectFileStaticString     :                    StaticString = internalOnlyIdStaticString(),
    internalOnlyComputedForwardedAutoClosureFileString      : @autoclosure () -> String       = internalOnlyIdString(),
    internalOnlyComputedForwardedAutoClosureFileStaticString: @autoclosure () -> StaticString = internalOnlyIdStaticString(),
    directLineInt                                           :                    Int          = #line,
    directLineUInt                                          :                    UInt         = #line,
    autoClosureLineInt                                      : @autoclosure () -> Int          = #line,
    autoClosureLineUInt                                     : @autoclosure () -> UInt         = #line,
    internalOnlyComputedDirectLineInt                       :                    Int          = internalOnlyIdInt(#line),
    internalOnlyComputedDirectLineUInt                      :                    UInt         = internalOnlyIdUInt(#line),
    internalOnlyComputedAutoClosureLineInt                  : @autoclosure () -> Int          = internalOnlyIdInt(#line),
    internalOnlyComputedAutoClosureLineUInt                 : @autoclosure () -> UInt         = internalOnlyIdUInt(#line),
    internalOnlyComputedForwardedDirectLineInt              :                    Int          = internalOnlyIdInt(),
    internalOnlyComputedForwardedDirectLineUInt             :                    UInt         = internalOnlyIdUInt(),
    internalOnlyComputedForwardedAutoClosureLineInt         : @autoclosure () -> Int          = internalOnlyIdInt(),
    internalOnlyComputedForwardedAutoClosureLineUInt        : @autoclosure () -> UInt         = internalOnlyIdUInt()
    ) {

    print(
          directFileString,
          directFileStaticString,
          autoClosureFileString(),
          autoClosureFileStaticString(),
          internalOnlyComputedDirectFileString,
          internalOnlyComputedDirectFileStaticString,
          internalOnlyComputedAutoClosureFileString(),
          internalOnlyComputedAutoClosureFileStaticString(),
          internalOnlyComputedForwardedDirectFileString,
          internalOnlyComputedForwardedDirectFileStaticString,
          internalOnlyComputedForwardedAutoClosureFileString(),
          internalOnlyComputedForwardedAutoClosureFileStaticString(),
          directLineInt,
          directLineUInt,
          autoClosureLineInt(),
          autoClosureLineUInt(),
          internalOnlyComputedDirectLineInt,
          internalOnlyComputedDirectLineUInt,
          internalOnlyComputedAutoClosureLineInt(),
          internalOnlyComputedAutoClosureLineUInt(),
          internalOnlyComputedForwardedDirectLineInt,
          internalOnlyComputedForwardedDirectLineUInt,
          internalOnlyComputedForwardedAutoClosureLineInt(),
          internalOnlyComputedForwardedAutoClosureLineUInt(),
          separator: "\n")
}

public func doItInOther2() {
    print("EXPECTED: file=\(#file), line=\(#line + 1)")
    f()
}

public func doItInOther2Internally() {
    print("EXPECTED: file=\(#file), line=\(#line + 1)")
    fInternal()
}

For the above hack to work, this program would need to continuously print the EXPECTED values but it does not. The output is:

$ ~/devel/swift-nio/dev/make-single-file-spm test.swift run
all extra modules: Other Other2
SwiftPM package in:
  /tmp/test_package_5RrXdF
  /tmp/test_package_5RrXdF/.build/release/TestApp
[4/4] Linking TestApp
==== from main ====
EXPECTED: file=/private/tmp/test_package_5RrXdF/Sources/TestApp/main.swift, line=6
/private/tmp/test_package_5RrXdF/Sources/TestApp/main.swift
/private/tmp/test_package_5RrXdF/Sources/TestApp/main.swift
/private/tmp/test_package_5RrXdF/Sources/TestApp/main.swift
/private/tmp/test_package_5RrXdF/Sources/TestApp/main.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
6
6
6
6
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
==== from Other2 ====
EXPECTED: file=/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift, line=183
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
183
183
183
183
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
==== from Other2 only internal functions ====
EXPECTED: file=/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift, line=188
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
/private/tmp/test_package_5RrXdF/Sources/Other2/module.swift
188
188
188
188
143
144
145
146
147
148
149
150

So it seems that the only thing that resolves correctly is = #file and = #line without any computations being allowed.

@weissi
Copy link
Member Author

weissi commented Jun 4, 2020

@swift-ci create

@theblixguy
Copy link
Collaborator

cc brentdax (JIRA User)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

2 participants