Skip to content

Support weak definitions #4414

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

Merged
merged 1 commit into from
Aug 19, 2025
Merged

Support weak definitions #4414

merged 1 commit into from
Aug 19, 2025

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Jun 26, 2025

When a symbol only has a weak definition, this definition will be picked. When a symbol has both a weak and a regular definition, the regular definition will be picked instead.

In the future the allocator shim may be replaced by weak definitions. This already adds support to simplify things in the future.

@bjorn3
Copy link
Member Author

bjorn3 commented Jun 26, 2025

This has been extracted out of rust-lang/rust#134522.

@bjorn3 bjorn3 force-pushed the weak_defs branch 3 times, most recently from 5a433c4 to 2402b40 Compare June 27, 2025 10:27
Comment on lines 215 to 237
if let Some((instance, _, _)) = instance_and_crate {
if !matches!(tcx.def_kind(instance.def_id()), DefKind::Fn | DefKind::AssocFn) {
throw_ub_format!(
"attempt to call an exported symbol that is not defined as a function"
);
}
}

Copy link
Member

Choose a reason for hiding this comment

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

I guess this logic can live here as well, but is there a specific reason you moved it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not that I recall.

Copy link
Member

Choose a reason for hiding this comment

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

It will make a difference if there's a weak symbol that's not a function and a "strong" symbol that is. That's probably fine?

@RalfJung
Copy link
Member

@rustbot author

@rustbot rustbot added the S-waiting-on-author Status: Waiting for the PR author to address review comments label Jun 28, 2025
@jdonszelmann
Copy link
Contributor

what's the status of this one @bjorn3? (I expect just little time which is very fair <3)

@bjorn3
Copy link
Member Author

bjorn3 commented Aug 6, 2025

Somehow I didn't notice there were outstanding review comments.

@bjorn3 bjorn3 force-pushed the weak_defs branch 2 times, most recently from 59a946e to de4a356 Compare August 6, 2025 17:48
@bjorn3
Copy link
Member Author

bjorn3 commented Aug 6, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Aug 6, 2025
@bjorn3 bjorn3 requested a review from RalfJung August 13, 2025 14:19
Comment on lines 167 to 171
if let Some(SymbolTarget {
instance: original_instance,
cnum: original_cnum,
is_weak: original_is_weak,
}) = symbol_target
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if let Some(SymbolTarget {
instance: original_instance,
cnum: original_cnum,
is_weak: original_is_weak,
}) = symbol_target
if let Some(original) = symbol_target

seems more concise and more clear

Comment on lines 215 to 237
if let Some((instance, _, _)) = instance_and_crate {
if !matches!(tcx.def_kind(instance.def_id()), DefKind::Fn | DefKind::AssocFn) {
throw_ub_format!(
"attempt to call an exported symbol that is not defined as a function"
);
}
}

Copy link
Member

Choose a reason for hiding this comment

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

It will make a difference if there's a weak symbol that's not a function and a "strong" symbol that is. That's probably fine?

}
interp_ok(())
})?;

e.insert(instance_and_crate.map(|ic| ic.0))
if let Some(SymbolTarget { instance, .. }) = symbol_target {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if let Some(SymbolTarget { instance, .. }) = symbol_target {
// Once we identified the instance corresponding to the symbol, ensure
// it is a function. It is okay to encounter non-functions in the search above
// as long as the final instance we arrive at is a function.
if let Some(SymbolTarget { instance, .. }) = symbol_target {

@RalfJung
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Aug 19, 2025
@bjorn3
Copy link
Member Author

bjorn3 commented Aug 19, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Aug 19, 2025
@RalfJung
Copy link
Member

Thanks!
This is mostly ready, but CI isn't happy, and also please squash the commits.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Aug 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 19, 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.

When a symbol only has a weak definition, this definition will be
picked. When a symbol has both a weak and a regular definition, the
regular definition will be picked instead.
@bjorn3
Copy link
Member Author

bjorn3 commented Aug 19, 2025

Rebased and squashed.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Aug 19, 2025
@RalfJung RalfJung added this pull request to the merge queue Aug 19, 2025
Merged via the queue into rust-lang:master with commit c304485 Aug 19, 2025
14 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Waiting for a review to complete label Aug 19, 2025
@bjorn3 bjorn3 deleted the weak_defs branch August 19, 2025 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants