-
-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4440,6 +4440,7 @@ dependencies = [ | |
| "rustc_target", | ||
| "scoped-tls", | ||
| "serde", | ||
| "serde_json", | ||
| "tracing", | ||
| ] | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| //! [crates.io](https://crates.io). | ||
|
|
||
| use std::fmt::Debug; | ||
| use std::marker::PhantomData; | ||
| use std::{fmt, io}; | ||
|
|
||
| pub(crate) use rustc_public_bridge::IndexedVal; | ||
|
|
@@ -36,7 +37,10 @@ pub use crate::crate_def::{CrateDef, CrateDefItems, CrateDefType, DefId}; | |
| pub use crate::error::*; | ||
| use crate::mir::mono::StaticDef; | ||
| use crate::mir::{Body, Mutability}; | ||
| use crate::ty::{AssocItem, FnDef, ForeignModuleDef, ImplDef, ProvenanceMap, Span, TraitDef, Ty}; | ||
| use crate::ty::{ | ||
| AssocItem, FnDef, ForeignModuleDef, ImplDef, ProvenanceMap, Span, TraitDef, Ty, | ||
| serialize_index_impl, | ||
| }; | ||
| use crate::unstable::Stable; | ||
|
|
||
| pub mod abi; | ||
|
|
@@ -49,31 +53,25 @@ pub mod compiler_interface; | |
| pub mod error; | ||
| pub mod mir; | ||
| pub mod target; | ||
| #[cfg(test)] | ||
| mod tests; | ||
| pub mod ty; | ||
| pub mod visitor; | ||
|
|
||
| /// Use String for now but we should replace it. | ||
| pub type Symbol = String; | ||
|
|
||
| /// The number that identifies a crate. | ||
| pub type CrateNum = usize; | ||
| #[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| pub struct CrateNum(pub(crate) usize, ThreadLocalIndex); | ||
| serialize_index_impl!(CrateNum); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| impl Debug for DefId { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("DefId").field("id", &self.0).field("name", &self.name()).finish() | ||
| } | ||
| } | ||
|
|
||
| impl IndexedVal for DefId { | ||
| fn to_val(index: usize) -> Self { | ||
| DefId(index) | ||
| } | ||
|
|
||
| fn to_index(&self) -> usize { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| /// A list of crate items. | ||
| pub type CrateItems = Vec<CrateItem>; | ||
|
|
||
|
|
@@ -300,3 +298,25 @@ impl rustc_public_bridge::bridge::Allocation<compiler_interface::BridgeTys> | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Hash, PartialEq, Eq, Default)] | ||
| /// Marker type for indexes into thread local structures. | ||
| /// | ||
| /// Makes things `!Send`/`!Sync`, so users don't move `rustc_public` types to | ||
| /// thread with no (or worse, different) `rustc_public` pointer. | ||
| /// | ||
| /// Note. This doesn't make it impossible to confuse TLS. You could return a | ||
| /// `DefId` from one `run!` invocation, and then use it inside a different | ||
| /// `run!` invocation with different tables. | ||
| pub(crate) struct ThreadLocalIndex { | ||
| _phantom: PhantomData<*const ()>, | ||
| } | ||
| #[expect(non_upper_case_globals)] | ||
| /// Emulating unit struct `struct ThreadLocalIndex`; | ||
| pub(crate) const ThreadLocalIndex: ThreadLocalIndex = ThreadLocalIndex { _phantom: PhantomData }; | ||
|
|
||
| impl fmt::Debug for ThreadLocalIndex { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_tuple("ThreadLocalIndex").finish() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| use rustc_public_bridge::IndexedVal; | ||
|
|
||
| use crate::abi::Layout; | ||
| use crate::mir::alloc::AllocId; | ||
| use crate::mir::mono::InstanceDef; | ||
| use crate::ty::{MirConstId, TyConstId, VariantIdx}; | ||
| use crate::{CrateNum, DefId, Span, ThreadLocalIndex, Ty}; | ||
|
|
||
| #[track_caller] | ||
| fn check_serialize<T: serde::Serialize>(value: T, expected_json: &str) { | ||
| let got_json = serde_json::to_string(&value).unwrap(); | ||
| assert_eq!(got_json, expected_json, "didn't get expected json for serializing"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_cratenum() { | ||
| check_serialize(CrateNum(1, ThreadLocalIndex), "1"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_defid() { | ||
| check_serialize(DefId::to_val(2), "2"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_layout() { | ||
| check_serialize(Layout::to_val(3), "3"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_allocid() { | ||
| check_serialize(AllocId::to_val(4), "4"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_ty() { | ||
| check_serialize(Ty::to_val(5), "5"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_tyconstid() { | ||
| check_serialize(TyConstId::to_val(6), "6"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_mirconstid() { | ||
| check_serialize(MirConstId::to_val(7), "7"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_span() { | ||
| check_serialize(Span::to_val(8), "8"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_variantidx() { | ||
| check_serialize(VariantIdx::to_val(9), "9"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize_instancedef() { | ||
| check_serialize(InstanceDef::to_val(10), "10"); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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::Cratewould be send+sync. ButCratehas 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
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/CrateNumacross threads lets get wrong results:This prints:
When we move crate
footo 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-148261There 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.