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-13041] (#file) to silence the #file to #filePath warning only works on call site not on declaration #55487

Open
weissi opened this issue Jun 18, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@weissi
Copy link
Member

weissi commented Jun 18, 2020

Previous ID SR-13041
Radar rdar://problem/64517052
Original Reporter @weissi
Type Bug

Attachment: Download

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

md5: ce9ac6b944a9a11dee601f23700669b9

Issue Description:

In the Swift 5.3 and master branches, when you forward #file to a function that is defaulted to #filePath, you get the following warning:

/private/tmp/test_package_VdXUpS/Sources/Other/module.swift:7:20: warning: parameter 'file' with default argument '#file' passed to parameter 'file', whose default argument is '#filePath'
    whatever(file: file)
                   ^
/private/tmp/test_package_VdXUpS/Sources/Other/module.swift:6:46: note: did you mean for parameter 'file' to default to '#filePath'?
public func printMyFile(file: StaticString = #file) {
                                             ^~~~~
                                             #filePath
/private/tmp/test_package_VdXUpS/Sources/Other/module.swift:7:20: note: add parentheses to silence this warning
    whatever(file: file)
                   ^
                   (   )
/private/tmp/test_package_VdXUpS/Sources/Other/module.swift:10:15: note: 'file' declared here
func whatever(file: StaticString = #filePath) {
              ^

Using #filePath is only possible when using Swift 5.3 so not available to most Swift packages. Fortunately, the compiler offers

/private/tmp/test_package_VdXUpS/Sources/Other/module.swift:7:20: note: add parentheses to silence this warning
    whatever(file: file)
                   ^
                   (   )

but doing so only works IFF applied to the call's parameter (and not in the function decl file: StaticString = (#file) which silences the warning too). That's super confusing and you'll end up getting the wrong file path. The worst is that this is primarily used in tests so the problem remains hidden until you get a test failure (which will then show up in the wrong file). See attached example project.

I can't really believe this but because of SR-12934 the absolutely necessary forwarding of #file to #filePath seems impossible (when writing the issue I believed that (#file) actually works).
Whilst implementing this change, despite it very clearly being backwards compatibility changing, the Swift project failed to up the -swift-version number (SR-12936). This is so very disappointing :'(.

This needs fixing ASAP before Swift 5.3 is out, either by fixing SR-12934 or SR-12936.

Repro:

import Other

print("The following two lines should read equally\n")
print("\(#file)")
printMyFile()

// MODULE: Other
public func printMyFile(file: StaticString = (#file)) {
    whatever(file: file)
}

func whatever(file: StaticString = #filePath) {
    print(file)
}

prints

The following two lines should read equally

/tmp/test_package_j8xPzy/Sources/TestApp/main.swift
/tmp/test_package_j8xPzy/Sources/Other/module.swift
@weissi
Copy link
Member Author

weissi commented Jun 18, 2020

@swift-ci create

@weissi
Copy link
Member Author

weissi commented Jun 18, 2020

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. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant