Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions compiler/rustc_public/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use serde::Serialize;
use crate::compiler_interface::with;
use crate::mir::FieldIdx;
use crate::target::{MachineInfo, MachineSize as Size};
use crate::ty::{Align, Ty, VariantIdx};
use crate::{Error, Opaque, error};
use crate::ty::{Align, Ty, VariantIdx, index_impl};
use crate::{Error, Opaque, ThreadLocalIndex, error};

/// A function ABI definition.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
Expand Down Expand Up @@ -109,24 +109,16 @@ impl LayoutShape {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct Layout(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Layout(usize, ThreadLocalIndex);
index_impl!(Layout);

impl Layout {
pub fn shape(self) -> LayoutShape {
with(|cx| cx.layout_shape(self))
}
}

impl crate::IndexedVal for Layout {
fn to_val(index: usize) -> Self {
Layout(index)
}
fn to_index(&self) -> usize {
self.0
}
}

/// Describes how the fields of a type are shaped in memory.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub enum FieldsShape {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_public/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::ty::{
use crate::unstable::{RustcInternal, Stable, new_item_kind};
use crate::{
AssocItems, Crate, CrateDef, CrateItem, CrateItems, CrateNum, DefId, Error, Filename,
ImplTraitDecls, ItemKind, Symbol, TraitDecls, alloc, mir,
ImplTraitDecls, ItemKind, Symbol, ThreadLocalIndex, TraitDecls, alloc, mir,
};

pub struct BridgeTys;
Expand Down Expand Up @@ -1093,7 +1093,7 @@ fn smir_crate<'tcx>(
) -> Crate {
let name = cx.crate_name(crate_num);
let is_local = cx.crate_is_local(crate_num);
let id = cx.crate_num_id(crate_num);
let id = CrateNum(cx.crate_num_id(crate_num), ThreadLocalIndex);
debug!(?name, ?crate_num, "smir_crate");
Crate { id, name, is_local }
}
11 changes: 5 additions & 6 deletions compiler/rustc_public/src/crate_def.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Module that define a common trait for things that represent a crate definition,
//! such as, a function, a trait, an enum, and any other definitions.

use serde::Serialize;

use crate::ty::{GenericArgs, Span, Ty};
use crate::{AssocItems, Crate, Symbol, with};
use crate::ty::{GenericArgs, Span, Ty, index_impl};
use crate::{AssocItems, Crate, Symbol, ThreadLocalIndex, with};

/// A unique identification number for each item accessible for the current compilation unit.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
pub struct DefId(pub(crate) usize);
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DefId(pub(crate) usize, ThreadLocalIndex);
index_impl!(DefId);

impl DefId {
/// Return fully qualified name of this definition
Expand Down
42 changes: 30 additions & 12 deletions compiler/rustc_public/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -56,24 +60,16 @@ pub mod visitor;
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);

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>;

Expand Down Expand Up @@ -300,3 +296,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()
}
}
18 changes: 5 additions & 13 deletions compiler/rustc_public/src/mir/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use serde::Serialize;

use crate::mir::mono::{Instance, StaticDef};
use crate::target::{Endian, MachineInfo};
use crate::ty::{Allocation, Binder, ExistentialTraitRef, Ty};
use crate::{Error, IndexedVal, with};
use crate::ty::{Allocation, Binder, ExistentialTraitRef, Ty, index_impl};
use crate::{Error, ThreadLocalIndex, with};

/// An allocation in the rustc_public's IR global memory can be either a function pointer,
/// a static, or a "real" allocation with some data in it.
Expand Down Expand Up @@ -47,17 +47,9 @@ impl GlobalAlloc {
}

/// A unique identification number for each provenance
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize)]
pub struct AllocId(usize);

impl IndexedVal for AllocId {
fn to_val(index: usize) -> Self {
AllocId(index)
}
fn to_index(&self) -> usize {
self.0
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct AllocId(usize, ThreadLocalIndex);
index_impl!(AllocId);

/// Utility function used to read an allocation data into a unassigned integer.
pub(crate) fn read_target_uint(mut bytes: &[u8]) -> Result<u128, Error> {
Expand Down
18 changes: 5 additions & 13 deletions compiler/rustc_public/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use serde::Serialize;
use crate::abi::FnAbi;
use crate::crate_def::CrateDef;
use crate::mir::Body;
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, Ty};
use crate::{CrateItem, DefId, Error, IndexedVal, ItemKind, Opaque, Symbol, with};
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, Ty, index_impl};
use crate::{CrateItem, DefId, Error, ItemKind, Opaque, Symbol, ThreadLocalIndex, with};

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub enum MonoItem {
Expand Down Expand Up @@ -241,8 +241,9 @@ impl From<StaticDef> for CrateItem {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct InstanceDef(usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct InstanceDef(usize, ThreadLocalIndex);
index_impl!(InstanceDef);

impl CrateDef for InstanceDef {
fn def_id(&self) -> DefId {
Expand Down Expand Up @@ -294,12 +295,3 @@ impl StaticDef {
with(|cx| cx.eval_static_initializer(*self))
}
}

impl IndexedVal for InstanceDef {
fn to_val(index: usize) -> Self {
InstanceDef(index)
}
fn to_index(&self) -> usize {
self.0
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_public/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ where
}

pub fn crate_num(item: &crate::Crate) -> CrateNum {
item.id.into()
item.id.0.into()
}

// A thread local variable that stores a pointer to the tables mapping between TyCtxt
Expand Down
39 changes: 27 additions & 12 deletions compiler/rustc_public/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use crate::crate_def::{CrateDef, CrateDefItems, CrateDefType};
use crate::mir::alloc::{AllocId, read_target_int, read_target_uint};
use crate::mir::mono::StaticDef;
use crate::target::MachineInfo;
use crate::{Filename, IndexedVal, Opaque};
use crate::{Filename, IndexedVal, Opaque, ThreadLocalIndex};

#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)]
pub struct Ty(usize);
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct Ty(usize, ThreadLocalIndex);

impl Debug for Ty {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -151,8 +151,8 @@ pub enum TyConstKind {
ZSTValue(Ty),
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)]
pub struct TyConstId(usize);
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct TyConstId(usize, ThreadLocalIndex);

/// Represents a constant in MIR
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
Expand Down Expand Up @@ -212,8 +212,8 @@ impl MirConst {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct MirConstId(usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MirConstId(usize, ThreadLocalIndex);

type Ident = Opaque;

Expand Down Expand Up @@ -255,8 +255,8 @@ pub struct Placeholder<T> {
pub bound: T,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
pub struct Span(usize);
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Span(usize, ThreadLocalIndex);

impl Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -1560,14 +1560,29 @@ macro_rules! index_impl {
($name:ident) => {
impl crate::IndexedVal for $name {
fn to_val(index: usize) -> Self {
$name(index)
$name(index, $crate::ThreadLocalIndex)
}
fn to_index(&self) -> usize {
self.0
}
}
$crate::ty::serialize_index_impl!($name);
};
}
macro_rules! serialize_index_impl {
($name:ident) => {
impl ::serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
let n: usize = self.0; // Make sure we're serializing an int.
::serde::Serialize::serialize(&n, serializer)
}
}
};
}
pub(crate) use {index_impl, serialize_index_impl};

index_impl!(TyConstId);
index_impl!(MirConstId);
Expand All @@ -1587,8 +1602,8 @@ index_impl!(Span);
/// `a` is in the variant with the `VariantIdx` of `0`,
/// `c` is in the variant with the `VariantIdx` of `1`, and
/// `g` is in the variant with the `VariantIdx` of `0`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct VariantIdx(usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct VariantIdx(usize, ThreadLocalIndex);

index_impl!(VariantIdx);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_public/src/unstable/convert/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl RustcInternal for CrateNum {
_tables: &mut Tables<'_, BridgeTys>,
_tcx: impl InternalCx<'tcx>,
) -> Self::T<'tcx> {
rustc_span::def_id::CrateNum::from_usize(*self)
rustc_span::def_id::CrateNum::from_usize(self.0)
}
}

Expand Down
Loading