-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
rustc_public: Make Id types !Send / !Sync #148261
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
Conversation
There was a problem hiding this 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?
compiler/rustc_public/src/lib.rs
Outdated
| } | ||
|
|
||
| #[derive(Clone, Copy, Hash, PartialEq, Eq, Default)] | ||
| #[derive(serde::Serialize)] // TODO: Don't. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests: 7bdbf8e
compiler/rustc_public/src/lib.rs
Outdated
| pub type Symbol = String; | ||
|
|
||
| /// The number that identifies a crate. | ||
| // FIXME: Make this a newtype, so it can have a `ReferencesTls` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
|
I'd like to squash this after approval but before merging. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
These commits modify the If this was unintentional then you should revert the changes before this PR is merged. |
| /// The number that identifies a crate. | ||
| pub type CrateNum = usize; | ||
| #[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| pub struct CrateNum(pub(crate) usize, ThreadLocalIndex); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
rust/compiler/rustc_public/src/lib.rs
Lines 94 to 98 in 055d0d6
| impl Crate { | |
| /// The list of foreign modules in this crate. | |
| pub fn foreign_modules(&self) -> Vec<ForeignModuleDef> { | |
| with(|cx| cx.foreign_modules(self.id)) | |
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
|
Does anything else need doing before this can be approved? |
Can you do this please. Thanks! |
|
@bors r-, I’ll squash & re-approve when i’m at a laptop. |
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
7bdbf8e to
e7b8460
Compare
|
@bors r=celinval rollup |
…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
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
…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
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
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
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
Relevant upstream PR: - rust-lang/rust#148261 (rustc_public: Make Id types !Send / !Sync) Resolves: model-checking#4485
Relevant upstream PR: - rust-lang/rust#148261 (rustc_public: Make Id types !Send / !Sync) Resolves: model-checking#4485
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
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
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
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
…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
…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
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
ReferencesTlsThreadLocalIndexmarker type, which ensures types arn'tSend/Sync.This is a breaking change for users of the
rustc_publiccrate.It also changes howEDIT: Not anymore: #148261 (comment)DefIdand similar areSerialized. It would be possible to preserve the old behavior if that's needed, but I couldn't see any tests for these.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