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-6913] Default arguments are not evaluated correctly in larger expression #49462

Open
davedelong opened this issue Feb 3, 2018 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@davedelong
Copy link

Previous ID SR-6913
Radar rdar://problem/47307090
Original Reporter @davedelong
Type Bug
Status Reopened
Resolution

Attachment: Download

Environment

Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)

macOS X 10.13.3 (17D47)

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

md5: cfb72e303e35076200e38bc5b71119cc

duplicates:

  • SR-3258 #file keyword doesn't work inside interpolated default argument

is duplicated by:

  • SR-7184 #file doesn't match up with caller when in a larger expression
  • SR-10106 Move execution of default parameters to external context.
  • SR-10951 Value of #file/#line/#column/#function change when used from expression
  • SR-11222 Magic literal default args don't use caller info if nested in another expression

Issue Description:

See attached `defaultArgs` file; Save it to your computer and execute it with `./defaultArgs`

Here are its contents:

#!/usr/bin/xcrun --toolchain default swift

struct Context {
  let file: StaticString
  let line: UInt

  init(file: StaticString = #file, line: UInt = #line) {
    self.file = file
    self.line = line
  }

}

func fooWithImplicitContext(context: Context = Context()) {
  print("\(#function): \(context.file): \(context.line)")
}

func fooWithExplicitContext(context: Context = Context(file: #file, line: #line)) {
  print("\(#function): \(context.file): \(context.line)")
}

func fooWithIndirectContext(file: StaticString = #file, line: UInt = #line) {
  let context = Context(file: file, line: line)
  print("\(#function): \(context.file): \(context.line)")
}

fooWithIndirectContext(); fooWithImplicitContext(); fooWithExplicitContext()

Since the three `foo` functions are all executed on the same line, I would expect that all of them would log the same `#file` and `#line` values.

However, that is not the case. Only the `fooWithIndirectContext()` function does what is expected:

$ ./defaultArgs 
fooWithIndirectContext(file:line:): ./defaultArgs: 29
fooWithImplicitContext(context:): : 0
fooWithExplicitContext(context:): : 0

The other two fail to capture the `#file` and `#line` entirely.

@belkadan
Copy link
Contributor

belkadan commented Feb 5, 2018

Fixed in Swift 4.1!

@davedelong
Copy link
Author

Sadly, this is not fixed in Swift 4.1, although it is better.

When I run the code in a playground, I get this output:

fooWithIndirectContext(file:line:): MyPlayground.playground: 27
fooWithImplicitContext(context:): MyPlayground.playground: 14
fooWithExplicitContext(context:): MyPlayground.playground: 18

As stated in the bug, since all the foo invocations are happening on the same line, I am expecting them all to resolve to the same #line value.

@belkadan
Copy link
Contributor

belkadan commented Feb 5, 2018

Oops, sorry, I just noticed they weren't 0, but not that they should all be 27. I can understand why the current behavior is the way it is, but it's also clearly not what's desired. (Although things could get complicated if your default argument value itself uses something that has default arguments…)

@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

2 participants