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

Swift does not export operator overloads to C++ #79433

Open
nikeokoronkwo opened this issue Feb 17, 2025 · 0 comments
Open

Swift does not export operator overloads to C++ #79433

nikeokoronkwo opened this issue Feb 17, 2025 · 0 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ swift to c++ Feature → c++ interop: swift to c++

Comments

@nikeokoronkwo
Copy link

Description

While compiling a swift module to a C++ library and header file, operator overloads defined on public structs were not exported to C++.

Given the fact that C++ has support for operator overloading similar to how it's done in Swift, I was expecting it would simply be mapped to the C++ header, just like other functionality is. However, that was not the case.

Reproduction

  1. Write the following swift code
public struct Vector2D: Vector, TwoDimensional {
    public var x: Float = 0.0, y: Float = 0.0

    public init(_ x: Float, _ y: Float) {
        self.x = x; self.y = y;
    }

    public init(start: Point2D, end: Point2D) {
        self.x = end.0 - start.0
        self.y = end.1 - start.1
    }

    public static func + (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(left.x + right.x, left.y + right.y)
    }
}
  1. Compile the swift code into a clang C++ library
swiftc vector.swift -parse-as-library -module-name Vector -cxx-interoperability-mode=default -emit-clang-header-path Vector.h
  1. Use the swift clang code in C++
#include "Vector.h"
#include <iostream>

int main(){
    Vector::Vector2D v = Vector::Vector2D::init(1,2);
    Vector::Vector2D z = Vector::Vector2D::init(2,3);

    Vector::Vector2D x = v + z;
    
    std::cout << x.getX() << std::endl;
}
  1. Compile and run the C++ code
clang++ main.cc -L Vector.lib -lVector -std=c++14 -o vector # If on windows add .exe

The final command failed with the following error:

main.cc:8:28: error: invalid operands to binary expression ('Vector::Vector2D' and 'Vector::Vector2D')                             
    8 |     Vector::Vector2D x = v + z;
      |                          ~ ^ ~
1 error generated.

Expected behavior

The following code should work and provide a value of 3

Environment

Swift version 6.0.2 (swift-6.0.2-RELEASE)

Additional information

No response

@nikeokoronkwo nikeokoronkwo added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Feb 17, 2025
@hborla hborla added c++ interop Feature: Interoperability with C++ swift to c++ Feature → c++ interop: swift to c++ and removed triage needed This issue needs more specific labels labels Feb 20, 2025
@egorzhdan egorzhdan self-assigned this Feb 21, 2025
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++ swift to c++ Feature → c++ interop: swift to c++
Projects
None yet
Development

No branches or pull requests

3 participants