Navigation Menu

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-6386] Note incorrect pass-through of variadic arguments #48936

Open
swift-ci opened this issue Nov 15, 2017 · 1 comment
Open

[SR-6386] Note incorrect pass-through of variadic arguments #48936

swift-ci opened this issue Nov 15, 2017 · 1 comment
Labels
compiler The Swift compiler in itself improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-6386
Radar None
Original Reporter rmann (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: c1e78ccbc6e54d3f8578b6e6c8b37666

relates to:

  • SR-8047 compiler should warn when implicitly casting Array to CVarArg

Issue Description:

I've had a long-working debugLog() method that looks like this:

func
debugLog<T>(_ inMsg: T, file inFile : String = #file, line inLine : Int = #line)
{
    let file = (inFile as NSString).lastPathComponent
     let s = "\(file):\(inLine)    \(inMsg)"
     print(s)
}

I wanted to add a version that works like String(format:):

func
debugLog(format inFormat: String, file inFile : String = #file, line inLine : Int = #line, _ inArgs: CVarArg...)
{
     let s = String(format: inFormat, inArgs)
     debugLog(s, file: inFile, line: inLine)
}

While this compiles and executes, all of the values are zero for this example:

let xc = CGFloat(1.0)
let yc = CGFloat(0.0)
let len = CGFloat(282.1364917907643)

debugLog(format: "Pixel %f, %f length too far %f", xc, yc, len)

Output:

FisheyeImageRenderer.swift:108    Pixel 0.000000, 0.000000 length too far 0.000000

Something is being misinterpreted in the passing of inArgs: CVarArg.... The solution is to call

let s = String(format: inFormat, arguments: inArgs)

The compiler, perhaps, should warn of this mistake.

@swift-ci
Copy link
Collaborator Author

Comment by Federico Cappelli (JIRA)

I don't think this is a bug.

When you pass a `CVarArg` to `String(format: inFormat, inArgs)` at that point `inArgs` is already an array.

If you check the Swift implementation of os_log(...) in https://github.com/apple/swift/blob/6e7051eb1e38e743a514555d09256d12d3fec750/stdlib/public/Darwin/os/os_log.swift you can see how this is done:

public func os_log(
  _ message: StaticString,
  dso: UnsafeRawPointer? = #dsohandle,
  log: OSLog = .default,
  type: OSLogType = .default,
  _ args: CVarArg...)
{
  guard log.isEnabled(type: type) else { return }
  let ra = _swift_os_log_return_address()


  message.withUTF8Buffer { (buf: UnsafeBufferPointer<UInt8>) in
    // Since dladdr is in libc, it is safe to unsafeBitCast
    // the cstring argument type.
    buf.baseAddress!.withMemoryRebound(
      to: CChar.self, capacity: buf.count
    ) { str in
      withVaList(args) { valist in                           <<<<<<<<HERE
        _swift_os_log(dso, ra, log, type, str, valist)
      }
    }
  }
}

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

No branches or pull requests

1 participant