Skip to content

Conversation

@aDotInTheVoid
Copy link
Member

@aDotInTheVoid aDotInTheVoid commented Oct 29, 2025

These types are Id's to a table stored in TLS, so using them from another tread will either panic, or give wrong results.

Therefor, I've added a ReferencesTlsThreadLocalIndex marker type, which ensures types arn't Send/Sync.

This is a breaking change for users of the rustc_public crate.

It also changes how DefId and similar are Serialized. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these. EDIT: Not anymore: #148261 (comment)

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171

@rustbot
Copy link
Collaborator

rustbot commented Oct 29, 2025

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 29, 2025

r? @celinval

rustbot has assigned @celinval.
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

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

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

It's too bad we cannot use negative trait here. KMIR actually uses the serialization. See: https://github.com/runtimeverification/stable-mir-json. They use IDs to map components and correlate then AFAIK.

So can you please make sure you skip the serialization of the new field in the structs you added them?

View changes since this review

}

#[derive(Clone, Copy, Hash, PartialEq, Eq, Default)]
#[derive(serde::Serialize)] // TODO: Don't.
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree... Please don't.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 97e6df4. It's sadly not as simple as using #[serde(skip)], as that still moves serde from serializing just the single field, to a list containing the single field: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b4aafe4ff5312c4ddedd5e17ee361d5a

Is there somewhere that tests for this could go?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest adding unit tests for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added tests: 7bdbf8e

pub type Symbol = String;

/// The number that identifies a crate.
// FIXME: Make this a newtype, so it can have a `ReferencesTls`
Copy link
Contributor

Choose a reason for hiding this comment

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

why not include in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to keep this PR not doing too much at once. But I've added it in 8b3ee9f

@rust-log-analyzer

This comment has been minimized.

@aDotInTheVoid
Copy link
Member Author

I'd like to squash this after approval but before merging.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Nov 5, 2025

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

/// The number that identifies a crate.
pub type CrateNum = usize;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CrateNum(pub(crate) usize, ThreadLocalIndex);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I just remembered that CrateNum can be sync + send. So there's no reason to change 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.

How so? I don't think it can.

Otherwise, rustc_public::Crate would be send+sync. But Crate has a load of methods that can only be run on the thread with the TLVs:

impl Crate {
/// The list of foreign modules in this crate.
pub fn foreign_modules(&self) -> Vec<ForeignModuleDef> {
with(|cx| cx.foreign_modules(self.id))
}

Copy link
Contributor

Choose a reason for hiding this comment

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

But I don't think any of them require data from the TLV table. They will panic if you don't initialize the context, but that's the case for any function that doesn't take any argument.

Copy link
Member Author

Choose a reason for hiding this comment

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

Even so, letting you move a Crate/CrateNum across threads lets get wrong results:

let (tx, rx) = std::sync::mpsc::sync_channel(0);

let t1 = std::thread::spawn(move || {
    rustc_public::run!(&["rustc".to_owned(), "./demo/foo.rs".to_owned()], || {
        let this_crate = rustc_public::local_crate();
        print_crate_info(&this_crate);

        tx.send(this_crate).unwrap();

        ControlFlow::<()>::Continue(())
    })
    .unwrap();
});

let t2 = std::thread::spawn(move || {
    rustc_public::run!(
        &["rustc".to_owned(), "./demo/bar.rs".to_owned()],
        move || {
            let this_crate = rustc_public::local_crate();
            print_crate_info(&this_crate);

            let that_crate = rx.recv().unwrap();
            print_crate_info(&that_crate);

            ControlFlow::<()>::Continue(())
        }
    )
    .unwrap();
});

t1.join().unwrap();
t2.join().unwrap();

fn print_crate_info(krate: &rustc_public::Crate) {
    let tid = std::thread::current().id();

    for func in krate.fn_defs() {
        println!(
            "{tid:?} -- crate {} has function {}",
            krate.name,
            func.name()
        )
    }
}

This prints:

ThreadId(5) -- crate foo has function foo_1
ThreadId(5) -- crate foo has function foo_2
ThreadId(4) -- crate bar has function bar_1
ThreadId(4) -- crate bar has function bar_2
ThreadId(4) -- crate foo has function bar_1
ThreadId(4) -- crate foo has function bar_2

When we move crate foo to a different thread, it gets index'd into a different TLV, so gets results for a different crate. Full code: https://github.com/aDotInTheVoid/rust-148261

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see. You are building different rust files in each thread.

pub type CrateNum = usize;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CrateNum(pub(crate) usize, ThreadLocalIndex);
serialize_index_impl!(CrateNum);
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, I don't think we need a separate macro either

@aDotInTheVoid
Copy link
Member Author

Does anything else need doing before this can be approved?

@celinval
Copy link
Contributor

I'd like to squash this after approval but before merging.

Can you do this please. Thanks!
@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Nov 18, 2025

📌 Commit 7bdbf8e has been approved by celinval

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2025
@aDotInTheVoid
Copy link
Member Author

aDotInTheVoid commented Nov 18, 2025

@bors r-, I’ll squash & re-approve when i’m at a laptop.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 18, 2025
These types are Id's to a table stored in TLS, so using them from
another tread will either panic, or give wrong results.

Therefor, I've added a `ThreadLocalIndex` marker type, which ensures types
arn't `Send`/`Sync`.

This is a breaking change for users of the `rustc_public` crate.

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171
@aDotInTheVoid
Copy link
Member Author

@bors r=celinval rollup

@bors
Copy link
Collaborator

bors commented Nov 19, 2025

📌 Commit e7b8460 has been approved by celinval

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 19, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 20, 2025
…celinval

rustc_public: Make Id types !Send / !Sync

These types are Id's to a table stored in TLS, so using them from another tread will either panic, or give wrong results.

Therefor, I've added a ~~`ReferencesTls`~~`ThreadLocalIndex`  marker type, which ensures types arn't `Send`/`Sync`.

This is a breaking change for users of the `rustc_public` crate.

~~It also changes how `DefId` and similar are `Serialize`d. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these.~~ EDIT: Not anymore: rust-lang#148261 (comment)

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171
bors added a commit that referenced this pull request Nov 20, 2025
Rollup of 9 pull requests

Successful merges:

 - #146925 (Add doc for va_list APIs)
 - #147035 (alloc: fix `Debug` implementation of `ExtractIf`)
 - #147173 (Add support for hexagon-unknown-qurt target)
 - #148261 (rustc_public: Make Id types !Send / !Sync)
 - #149041 (ignore unsized types in mips64 and sparc64 callconvs)
 - #149043 ( rustdoc-json: add rlib path to ExternalCrate to enable robust crate resolution)
 - #149056 (fix the fragment_in_dst_padding_gets_overwritten test on s390x)
 - #149095 (rustc-dev-guide subtree update)
 - #149108 ([AIX][ppc64le-linux-gnu] Add Amy Kwan to target maintainers)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 20, 2025
…celinval

rustc_public: Make Id types !Send / !Sync

These types are Id's to a table stored in TLS, so using them from another tread will either panic, or give wrong results.

Therefor, I've added a ~~`ReferencesTls`~~`ThreadLocalIndex`  marker type, which ensures types arn't `Send`/`Sync`.

This is a breaking change for users of the `rustc_public` crate.

~~It also changes how `DefId` and similar are `Serialize`d. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these.~~ EDIT: Not anymore: rust-lang#148261 (comment)

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171
bors added a commit that referenced this pull request Nov 20, 2025
Rollup of 10 pull requests

Successful merges:

 - #146925 (Add doc for va_list APIs)
 - #147035 (alloc: fix `Debug` implementation of `ExtractIf`)
 - #147173 (Add support for hexagon-unknown-qurt target)
 - #148261 (rustc_public: Make Id types !Send / !Sync)
 - #148831 (Bump compiler dependencies)
 - #149041 (ignore unsized types in mips64 and sparc64 callconvs)
 - #149056 (fix the fragment_in_dst_padding_gets_overwritten test on s390x)
 - #149071 (Add test scaffolding for the `remote-test-client`)
 - #149095 (rustc-dev-guide subtree update)
 - #149108 ([AIX][ppc64le-linux-gnu] Add Amy Kwan to target maintainers)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Nov 20, 2025
Rollup of 9 pull requests

Successful merges:

 - #146925 (Add doc for va_list APIs)
 - #147035 (alloc: fix `Debug` implementation of `ExtractIf`)
 - #147173 (Add support for hexagon-unknown-qurt target)
 - #148261 (rustc_public: Make Id types !Send / !Sync)
 - #149041 (ignore unsized types in mips64 and sparc64 callconvs)
 - #149056 (fix the fragment_in_dst_padding_gets_overwritten test on s390x)
 - #149071 (Add test scaffolding for the `remote-test-client`)
 - #149095 (rustc-dev-guide subtree update)
 - #149108 ([AIX][ppc64le-linux-gnu] Add Amy Kwan to target maintainers)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 34b3c9d into rust-lang:main Nov 20, 2025
11 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 20, 2025
rust-timer added a commit that referenced this pull request Nov 20, 2025
Rollup merge of #148261 - aDotInTheVoid:no-sync-for-smir, r=celinval

rustc_public: Make Id types !Send / !Sync

These types are Id's to a table stored in TLS, so using them from another tread will either panic, or give wrong results.

Therefor, I've added a ~~`ReferencesTls`~~`ThreadLocalIndex`  marker type, which ensures types arn't `Send`/`Sync`.

This is a breaking change for users of the `rustc_public` crate.

~~It also changes how `DefId` and similar are `Serialize`d. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these.~~ EDIT: Not anymore: #148261 (comment)

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171
tautschnig pushed a commit to tautschnig/kani that referenced this pull request Nov 21, 2025
Relevant upstream PR:
- rust-lang/rust#148261 (rustc_public: Make Id
  types !Send / !Sync)

Resolves: model-checking#4485
tautschnig added a commit to tautschnig/kani that referenced this pull request Nov 21, 2025
Relevant upstream PR:
- rust-lang/rust#148261 (rustc_public: Make Id
  types !Send / !Sync)

Resolves: model-checking#4485
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Nov 24, 2025
rustc_public: Make Id types !Send / !Sync

These types are Id's to a table stored in TLS, so using them from another tread will either panic, or give wrong results.

Therefor, I've added a ~~`ReferencesTls`~~`ThreadLocalIndex`  marker type, which ensures types arn't `Send`/`Sync`.

This is a breaking change for users of the `rustc_public` crate.

~~It also changes how `DefId` and similar are `Serialize`d. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these.~~ EDIT: Not anymore: rust-lang/rust#148261 (comment)

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Nov 24, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#146925 (Add doc for va_list APIs)
 - rust-lang/rust#147035 (alloc: fix `Debug` implementation of `ExtractIf`)
 - rust-lang/rust#147173 (Add support for hexagon-unknown-qurt target)
 - rust-lang/rust#148261 (rustc_public: Make Id types !Send / !Sync)
 - rust-lang/rust#149041 (ignore unsized types in mips64 and sparc64 callconvs)
 - rust-lang/rust#149056 (fix the fragment_in_dst_padding_gets_overwritten test on s390x)
 - rust-lang/rust#149071 (Add test scaffolding for the `remote-test-client`)
 - rust-lang/rust#149095 (rustc-dev-guide subtree update)
 - rust-lang/rust#149108 ([AIX][ppc64le-linux-gnu] Add Amy Kwan to target maintainers)

r? `@ghost`
`@rustbot` modify labels: rollup
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Dec 10, 2025
rustc_public: Make Id types !Send / !Sync

These types are Id's to a table stored in TLS, so using them from another tread will either panic, or give wrong results.

Therefor, I've added a ~~`ReferencesTls`~~`ThreadLocalIndex`  marker type, which ensures types arn't `Send`/`Sync`.

This is a breaking change for users of the `rustc_public` crate.

~~It also changes how `DefId` and similar are `Serialize`d. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these.~~ EDIT: Not anymore: rust-lang/rust#148261 (comment)

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Dec 10, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#146925 (Add doc for va_list APIs)
 - rust-lang/rust#147035 (alloc: fix `Debug` implementation of `ExtractIf`)
 - rust-lang/rust#147173 (Add support for hexagon-unknown-qurt target)
 - rust-lang/rust#148261 (rustc_public: Make Id types !Send / !Sync)
 - rust-lang/rust#149041 (ignore unsized types in mips64 and sparc64 callconvs)
 - rust-lang/rust#149056 (fix the fragment_in_dst_padding_gets_overwritten test on s390x)
 - rust-lang/rust#149071 (Add test scaffolding for the `remote-test-client`)
 - rust-lang/rust#149095 (rustc-dev-guide subtree update)
 - rust-lang/rust#149108 ([AIX][ppc64le-linux-gnu] Add Amy Kwan to target maintainers)

r? `@ghost`
`@rustbot` modify labels: rollup
makai410 pushed a commit to makai410/rust that referenced this pull request Dec 10, 2025
…celinval

rustc_public: Make Id types !Send / !Sync

These types are Id's to a table stored in TLS, so using them from another tread will either panic, or give wrong results.

Therefor, I've added a ~~`ReferencesTls`~~`ThreadLocalIndex`  marker type, which ensures types arn't `Send`/`Sync`.

This is a breaking change for users of the `rustc_public` crate.

~~It also changes how `DefId` and similar are `Serialize`d. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these.~~ EDIT: Not anymore: rust-lang#148261 (comment)

Zulip Discussion: https://rust-lang.zulipchat.com/#narrow/channel/320896-project-stable-mir/topic/WDYM.20.22should.20not.20.20be.20shared.20across.20threads.22/with/547374171
makai410 pushed a commit to makai410/rust that referenced this pull request Dec 10, 2025
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#146925 (Add doc for va_list APIs)
 - rust-lang#147035 (alloc: fix `Debug` implementation of `ExtractIf`)
 - rust-lang#147173 (Add support for hexagon-unknown-qurt target)
 - rust-lang#148261 (rustc_public: Make Id types !Send / !Sync)
 - rust-lang#149041 (ignore unsized types in mips64 and sparc64 callconvs)
 - rust-lang#149056 (fix the fragment_in_dst_padding_gets_overwritten test on s390x)
 - rust-lang#149071 (Add test scaffolding for the `remote-test-client`)
 - rust-lang#149095 (rustc-dev-guide subtree update)
 - rust-lang#149108 ([AIX][ppc64le-linux-gnu] Add Amy Kwan to target maintainers)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants