Skip to content

initial implementation of the darwin_objc unstable feature #145660

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
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jbatez
Copy link

@jbatez jbatez commented Aug 20, 2025

Tracking issue: #145496

r? @tmandry

@rustbot
Copy link
Collaborator

rustbot commented Aug 20, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-tidy Area: The tidy tool O-apple Operating system: Apple (macOS, iOS, tvOS, visionOS, watchOS) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 20, 2025

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann

@rustbot rustbot assigned tmandry and unassigned lcnr Aug 20, 2025
@jbatez jbatez mentioned this pull request Aug 16, 2025
5 tasks
@rust-log-analyzer

This comment has been minimized.

@jdonszelmann
Copy link
Contributor

I looked through the attribute changes and they look good to me. I can't judge the rest. @tmandry feel free to approve in both our names once you agree with the rest :)

Copy link
Contributor

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked through it, it seems to match what I tried to do with the objc2 macros those years back (which is not to say that it is correct, I had no idea what I was doing back then).

Also, it would be helpful with more comments, and a few codegen tests - I get why you haven't done that yet, it's bothersome while we're still somewhat discussing what the best approach is.


Note: I'm still a bit unsure that this is actually the correct approach going forward, I suspect there might be more value in the future from something like the define_in_every_cgu_used you first proposed?
At the very least we'll also need some way to get protocol references, we might also need metaclass references, and possibly more if we're to support static NSString and support fully statically declared classes.

But I think it's fine to move forwards with this in the current state, then we can always figure that sort of stuff out well before stabilisation.

Comment on lines +592 to +601
let mut classrefs = self.objc_classrefs.borrow_mut();
if let Some(classref) = classrefs.get(&classname).copied() {
return classref;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so we're on the same page: It is basically this part that is the main feature here, right? I.e. that the symbol references are directly present in all translation units, and that they are deduplicated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. This self.objc_classrefs is a per-CGU cache that allows us to deduplicate references within a codegen unit. But since this function ends up getting called in each CGU that references a class, we end up with definitions in all the referencing translations units and let the linker deduplicate across them. I'll add a comment to summarize that.

Comment on lines +614 to +623
let extern_sym = format!("OBJC_CLASS_$_{}", classname.as_str());
let extern_llty = self.get_objc_class_t();
self.declare_global(&extern_sym, extern_llty)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Does this add the unnamed_addr attribute as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, should it? Clang doesn't generate unnamed_addr here, and my understanding is unnamed_addr is only useful for definitions with initializers, not external declarations like this.

});
set_global_alignment(self, g, self.tcx.data_layout.pointer_align().abi);
llvm::set_initializer(g, llval);
llvm::set_linkage(g, llvm::Linkage::PrivateLinkage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: In some places, you use PrivateLinkage, in others InternalLinkage. I assume this is intentional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just try to match what Clang does with these. My guess is they'd all work if they were all the same, but by mimicking Clang I figure it's more likely to continue to work with future linker versions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PrivateLinkage is basically InternalLinkage except that no symbol is generated in the object file.

@jbatez
Copy link
Author

jbatez commented Aug 21, 2025

... At the very least we'll also need some way to get protocol references, we might also need metaclass references, and possibly more if we're to support static NSString and support fully statically declared classes.

But I think it's fine to move forwards with this in the current state, then we can always figure that sort of stuff out well before stabilisation.

NSString constants were next on my list and should be pretty straightforward. I haven't looked into protocols or metaclasses yet, but I did take a quick peek at static class definitions and, wow, that's a lot more complicated; I'm not personally planning to tackle that any time soon.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Aug 21, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@jbatez
Copy link
Author

jbatez commented Aug 21, 2025

I added tests/codegen-llvm/darwin-objc.rs. In the process, I discovered, at some point, OBJC_CLASSLIST_REFERENCES_$_ and OBJC_SELECTOR_REFERENCES_ were getting optimized away and their initial values were getting used directly. I'm pretty sure this wasn't the case when I originally wrote this months ago. Adding them to llvm.compiler.used (which Clang does as well, fwiw) solved the problem. In the process, I had to convert compiler_used_statics to a RefCell, but I think that's fine.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [codegen] tests/codegen-llvm/zip.rs ... ok

failures:

---- [codegen] tests/codegen-llvm/darwin-objc.rs#i686_apple_darwin stdout ----

error in revision `i686_apple_darwin`: compilation failed!
status: exit status: 101
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/codegen-llvm/darwin-objc.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--cfg" "i686_apple_darwin" "--check-cfg" "cfg(test,FALSE,i686_apple_darwin,x86_64_apple_darwin,aarch64_apple_darwin,i386_apple_ios,x86_64_apple_ios,armv7s_apple_ios,aarch64_apple_ios,aarch64_apple_ios_sim)" "-O" "-Cdebug-assertions=no" "-Zcodegen-source-order" "--emit" "llvm-ir" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/codegen-llvm/darwin-objc.i686_apple_darwin/darwin-objc.ll" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "--target" "i686-apple-darwin" "-Cpanic=abort" "-Cforce-unwind-tables=yes" "--extern" "minicore=/checkout/obj/build/aarch64-unknown-linux-gnu/test/codegen-llvm/darwin-objc.i686_apple_darwin/libminicore.rlib"
stdout: none
--- stderr -------------------------------
warning: unused import: `minicore::*`
##[warning]  --> /checkout/tests/codegen-llvm/darwin-objc.rs:33:5
   |
33 | use minicore::*;
   |     ^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default

Global variable initializer type does not match global variable type!
ptr @OBJC_MODULES
rustc-LLVM ERROR: Broken module found, compilation aborted!
------------------------------------------



failures:

@bors
Copy link
Collaborator

bors commented Aug 22, 2025

☔ The latest upstream changes (presumably #145728) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-tidy Area: The tidy tool O-apple Operating system: Apple (macOS, iOS, tvOS, visionOS, watchOS) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants