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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Support weak definitions #4414

wants to merge 2 commits into from

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.

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 Jun 26, 2025

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

@bjorn3 bjorn3 force-pushed the weak_defs branch 2 times, most recently from 9284d4c to 5a433c4 Compare June 27, 2025 10:16
@@ -138,7 +139,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
// Find it if it was not cached.
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum, bool)> = None;
Copy link
Member

Choose a reason for hiding this comment

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

Please explain what the bool means, or use a self-explaining type. It might be worth using a struct instead of a tuple.

if let Some((original_instance, original_cnum, original_is_weak)) =
instance_and_crate
{
match (is_weak, original_is_weak) {
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
match (is_weak, original_is_weak) {
// There is more than one definition with this name. What we do now
// depends on whether one or both definitions are weak.
match (is_weak, original_is_weak) {

(true, true) | (false, false) => {
// Either both definitions are non-weak or both are weak. In
// either case return an error. For weak definitions we error
// because it is undefined which definition would have been
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
// because it is undefined which definition would have been
// because it is unspecified which definition would have been

Comment on lines +215 to +222
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

Choose a reason for hiding this comment

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

Please rename this to exported_symbol_weak for better test grouping.

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: Waiting for the PR author to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants