From 1b4b9bca94e9e11dd8f6ab2182533ee3bf11c843 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 18 Jun 2025 23:34:39 +0200 Subject: [PATCH] Linking fails on operator method imported as `@public` #2224. --- releasenotes.md | 1 + src/compiler/sema_expr.c | 1 + .../test_suite/abi/link_operator_overload.c3t | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 test/test_suite/abi/link_operator_overload.c3t diff --git a/releasenotes.md b/releasenotes.md index c8f2de309..254205393 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -52,6 +52,7 @@ - `x++` and `x--` works on pointer vectors #2222. - `x += 1` and `x -= 1` works propertly on pointer vectors #2222. - Fixes to `x += { 1, 1 }` for enum and pointer vectors #2222. +- Linking fails on operator method imported as `@public` #2224. ### Stdlib changes - Deprecate `String.is_zstr` and `String.quick_zstr` #2188. diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index d53284371..0836af350 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -11339,6 +11339,7 @@ bool sema_insert_method_call(SemaContext *context, Expr *method_call, Decl *meth } } ASSERT_SPAN(method_call, parent && parent->type && first == parent->type->canonical); + unit_register_external_symbol(context, method_decl); if (!sema_expr_analyse_general_call(context, method_call, method_decl, parent, false, NULL)) return expr_poison(method_call); method_call->resolve_status = RESOLVE_DONE; diff --git a/test/test_suite/abi/link_operator_overload.c3t b/test/test_suite/abi/link_operator_overload.c3t new file mode 100644 index 000000000..da4ce1f1a --- /dev/null +++ b/test/test_suite/abi/link_operator_overload.c3t @@ -0,0 +1,34 @@ +// #target: macos-x64 +module test; +import foo @public; + +fn int main() +{ + Pos p1 = (Pos){} + 1; + return 0; +} + +module foo @private; + +typedef Pos = inline uint; +fn Pos Pos.add(pos, uint rhs) @operator(+) => (Pos)((uint)pos + rhs); + +/* #expect: test.ll + +define i32 @main() #0 { +entry: + %p1 = alloca i32, align 4 + %0 = call i32 @foo.Pos.add(i32 0, i32 1) + store i32 %0, ptr %p1, align 4 + ret i32 0 +} + +declare i32 @foo.Pos.add(i32, i32) #0 + +/* #expect: foo.ll + +define i32 @foo.Pos.add(i32 %0, i32 %1) #0 { +entry: + %add = add i32 %0, %1 + ret i32 %add +} \ No newline at end of file