Skip to content

Potential linker issue with optional parameters. #67276

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

Open
plotfi opened this issue Jul 13, 2023 · 1 comment
Open

Potential linker issue with optional parameters. #67276

plotfi opened this issue Jul 13, 2023 · 1 comment
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++

Comments

@plotfi
Copy link
Contributor

plotfi commented Jul 13, 2023

@drodriguez some time ago noticed some issues around C++-Interop and linking with the use of default parameters in Swift. After some investigation on #66501, which looks like something similar to what we encountered, I tried to setup a similar case described below. Now, this actually repros with or without C++-Interop, but I dont understand why.

Description

Module Map:

module CXX {
  header "CXX.h"
}

Code:

///---------------------------------------------------------------------------------------------------------------///
/// CXX.h
/// ObjC++ Module:

#include <Foundation/Foundation.h>

typedef NS_OPTIONS(NSUInteger, FooBarCenteringOptions) {
  FooBarCenteringNone NS_SWIFT_NAME(None) = 0,
  FooBarCenteringX NS_SWIFT_NAME(x) = 1,
  FooBarCenteringY NS_SWIFT_NAME(y) = 2,
  FooBarCenteringXY NS_SWIFT_NAME(xy) = 3
} NS_SWIFT_NAME(FooBarComponent.CenteringOptions);

typedef NS_OPTIONS(NSUInteger, FooBarSizingOptions) {
  FooBarSizingOptionDefault,
  FooBarSizingOptionMinimumX = 1,
  FooBarSizingOptionMinimumY = 2,
  FooBarSizingOptionMinimumXY = 3
} NS_SWIFT_NAME(FooBarComponent.SizingOptions);


@interface FooBarLayoutComponent : NSObject
@end

NS_SWIFT_NAME(FooBarComponent)
@interface FooBarComponent : FooBarLayoutComponent
@end
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
/// CxxSwiftModule.swift
/// Swift Module, Uses C++:


import Foundation
import CXX

public
func center(centeringOptions: FooBarComponent.CenteringOptions = .xy,
            sizingOptions: FooBarComponent.SizingOptions = [])
  -> FooBarComponent.SizingOptions {
  sizingOptions
}

///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
/// CxxSwiftModuleUser.swift
/// Swift User of C++ using Swift Module:

import Foundation
import CXX
import CxxSwiftModule

public func f() -> FooBarComponent.SizingOptions {
  center()
}

@main
class C {
  static func main() {
    let c = center()
    if c == FooBarComponent.SizingOptions.minimumXY {
      print("foo")
    } else {
      print("bar")
    }
  }
}
///---------------------------------------------------------------------------------------------------------------///

Steps to reproduce


/Users/plotfi/opt/dev/S/build/RelWithDebInfo/BinaryCache/toolchain/bin/swift-frontend \
  -frontend -c \
  -sdk  /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
  -enable-objc-interop \
  -cxx-interoperability-mode=default \ ### <- This line is optional
  -disable-autolinking-runtime-compatibility-concurrency \
  -disable-autolinking-runtime-compatibility-dynamic-replacements \
  -disable-autolinking-runtime-compatibility \
  -o CxxSwiftModule.o  \
  CxxSwiftModule.swift -I./ -emit-module -parse-as-library


/Users/plotfi/opt/dev/S/build/RelWithDebInfo/BinaryCache/toolchain/bin/swift-frontend \
  -frontend -c \
  -sdk  /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
  -enable-objc-interop \
  -disable-autolinking-runtime-compatibility-concurrency \
  -disable-autolinking-runtime-compatibility-dynamic-replacements \
  -disable-autolinking-runtime-compatibility \
  -o CxxSwiftModuleUser.o  \
  CxxSwiftModuleUser.swift -disable-cxx-interop-requirement-at-import -I./ -parse-as-library


 /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld \
  -syslibroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
-o a.out \
-L/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx \
-L/usr/lib/swift \
CxxSwiftModuleUser.o CxxSwiftModuleUser.o

Expected behavior

Linker spits out:

Undefined symbols for architecture arm64:
  "_$s14CxxSwiftModule6center16centeringOptions06sizingF0So012FooBarSizingF0VSo0hi9CenteringF0V_AFtF", referenced from:
      _$s18CxxSwiftModuleUser1fSo19FooBarSizingOptionsVyF in CxxSwiftModuleUser.o
      _$s18CxxSwiftModuleUser1CC4mainyyFZ in CxxSwiftModuleUser.o
      _$s18CxxSwiftModuleUser1fSo19FooBarSizingOptionsVyF in CxxSwiftModuleUser.o
      _$s18CxxSwiftModuleUser1CC4mainyyFZ in CxxSwiftModuleUser.o
     (maybe you meant: _$s14CxxSwiftModule6center16centeringOptions06sizingF0So012FooBarSizingF0VSo0hi9CenteringF0V_AFtFfA0_, _$s14CxxSwiftModule6center16centeringOptions06sizingF0So012FooBarSizingF0VSo0hi9CenteringF0V_AFtFfA_ )
ld: symbol(s) not found for architecture arm64

Demangled:

ld: symbol(s) not found for architecture arm64Undefined symbols for architecture arm64:
  "CxxSwiftModule.center(centeringOptions: __C.FooBarCenteringOptions, sizingOptions: __C.FooBarSizingOptions) -> __C.FooBarSizingOptions", referenced from:
      CxxSwiftModuleUser.f() -> __C.FooBarSizingOptions in CxxSwiftModuleUser.o
      static CxxSwiftModuleUser.C.main() -> () in CxxSwiftModuleUser.o
      CxxSwiftModuleUser.f() -> __C.FooBarSizingOptions in CxxSwiftModuleUser.o
      static CxxSwiftModuleUser.C.main() -> () in CxxSwiftModuleUser.o
     (maybe you meant: default argument 1 of CxxSwiftModule.center(centeringOptions: __C.FooBarCenteringOptions, sizingOptions: __C.FooBarSizingOptions) -> __C.FooBarSizingOptions, default argument 0 of CxxSwiftModule.center(centeringOptions: __C.FooBarCenteringOptions, sizingOptions: __C.FooBarSizingOptions) -> __C.FooBarSizingOptions )

Now I am not sure why this is the case here.

@plotfi plotfi added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 13, 2023
@plotfi plotfi self-assigned this Jul 13, 2023
@plotfi plotfi added the c++ interop Feature: Interoperability with C++ label Jul 13, 2023
@plotfi
Copy link
Contributor Author

plotfi commented Jul 13, 2023

One thing to note, this is work-aroundable. Just drop the default params. But still want to understand if the linker is right here and why.

@hborla hborla removed the triage needed This issue needs more specific labels label Apr 27, 2024
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. c++ interop Feature: Interoperability with C++
Projects
None yet
Development

No branches or pull requests

2 participants