Uploaded image for project: 'Swift'
  1. Swift
  2. SR-6913

Default arguments are not evaluated correctly

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reopened
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

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

      macOS X 10.13.3 (17D47)

      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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                davedelong Dave DeLong
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated: