Skip to content

Commit 1bc8681

Browse files
committed
Split out the stable part of smir into its own crate to prevent accidental usage of forever unstable things
1 parent 5ea306e commit 1bc8681

File tree

20 files changed

+115
-117
lines changed

20 files changed

+115
-117
lines changed

Cargo.lock

+10-1
Original file line numberDiff line numberDiff line change
@@ -3194,6 +3194,7 @@ dependencies = [
31943194
"rustc_driver",
31953195
"rustc_driver_impl",
31963196
"rustc_smir",
3197+
"stable_mir",
31973198
]
31983199

31993200
[[package]]
@@ -4358,7 +4359,7 @@ dependencies = [
43584359
"rustc_session",
43594360
"rustc_span",
43604361
"rustc_target",
4361-
"scoped-tls",
4362+
"stable_mir",
43624363
"tracing",
43634364
]
43644365

@@ -4909,6 +4910,14 @@ version = "1.2.0"
49094910
source = "registry+https://github.com/rust-lang/crates.io-index"
49104911
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
49114912

4913+
[[package]]
4914+
name = "stable_mir"
4915+
version = "0.1.0-preview"
4916+
dependencies = [
4917+
"scoped-tls",
4918+
"tracing",
4919+
]
4920+
49124921
[[package]]
49134922
name = "stacker"
49144923
version = "0.1.15"

compiler/rustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
1313
# Make sure rustc_smir ends up in the sysroot, because this
1414
# crate is intended to be used by stable MIR consumers, which are not in-tree
1515
rustc_smir = { path = "../rustc_smir" }
16+
stable_mir = { path = "../stable_mir" }
1617

1718
[dependencies.jemalloc-sys]
1819
version = "0.5.0"

compiler/rustc_smir/Cargo.toml

+8-18
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,14 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7-
# Use optional dependencies for rustc_* in order to support building this crate separately.
8-
rustc_hir = { path = "../rustc_hir", optional = true }
9-
rustc_middle = { path = "../rustc_middle", optional = true }
10-
rustc_span = { path = "../rustc_span", optional = true }
11-
rustc_target = { path = "../rustc_target", optional = true }
12-
rustc_driver = { path = "../rustc_driver", optional = true }
13-
rustc_interface = { path = "../rustc_interface", optional = true}
14-
rustc_session = {path = "../rustc_session", optional = true}
7+
rustc_hir = { path = "../rustc_hir" }
8+
rustc_middle = { path = "../rustc_middle" }
9+
rustc_span = { path = "../rustc_span" }
10+
rustc_target = { path = "../rustc_target" }
11+
rustc_driver = { path = "../rustc_driver" }
12+
rustc_interface = { path = "../rustc_interface" }
13+
rustc_session = {path = "../rustc_session" }
1514
tracing = "0.1"
16-
scoped-tls = "1.0"
15+
stable_mir = {path = "../stable_mir" }
1716

1817
[features]
19-
default = [
20-
"rustc_hir",
21-
"rustc_middle",
22-
"rustc_span",
23-
"rustc_target",
24-
"rustc_driver",
25-
"rustc_interface",
26-
"rustc_session",
27-
]

compiler/rustc_smir/src/lib.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,12 @@
1010
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
1111
test(attr(allow(unused_variables), deny(warnings)))
1212
)]
13-
#![cfg_attr(not(feature = "default"), feature(rustc_private))]
13+
#![feature(rustc_private)]
1414
#![feature(ptr_metadata)]
1515
#![feature(type_alias_impl_trait)] // Used to define opaque types.
1616
#![feature(intra_doc_pointers)]
1717

18-
// Declare extern rustc_* crates to enable building this crate separately from the compiler.
19-
#[cfg(not(feature = "default"))]
20-
extern crate rustc_hir;
21-
#[cfg(not(feature = "default"))]
22-
extern crate rustc_middle;
23-
#[cfg(not(feature = "default"))]
24-
extern crate rustc_span;
25-
#[cfg(not(feature = "default"))]
26-
extern crate rustc_target;
27-
2818
pub mod rustc_internal;
29-
pub mod stable_mir;
3019

3120
// Make this module private for now since external users should not call these directly.
3221
mod rustc_smir;
33-
34-
#[macro_use]
35-
extern crate scoped_tls;

compiler/rustc_smir/src/rustc_internal/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
use std::ops::{ControlFlow, Index};
77

88
use crate::rustc_internal;
9-
use crate::stable_mir::CompilerError;
10-
use crate::{rustc_smir::Tables, stable_mir};
9+
use crate::rustc_smir::Tables;
1110
use rustc_driver::{Callbacks, Compilation, RunCompiler};
1211
use rustc_interface::{interface, Queries};
1312
use rustc_middle::mir::interpret::AllocId;
1413
use rustc_middle::ty::TyCtxt;
1514
pub use rustc_span::def_id::{CrateNum, DefId};
1615
use rustc_span::Span;
16+
use stable_mir::CompilerError;
1717

1818
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
1919
type Output = DefId;
@@ -129,7 +129,7 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
129129
}
130130

131131
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
132-
crate::stable_mir::run(
132+
stable_mir::run(
133133
Tables { tcx, def_ids: vec![], alloc_ids: vec![], spans: vec![], types: vec![] },
134134
f,
135135
);

compiler/rustc_smir/src/rustc_smir/alloc.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use rustc_middle::mir::interpret::{alloc_range, AllocRange, ConstValue, Pointer};
22

3-
use crate::{
4-
rustc_smir::{Stable, Tables},
5-
stable_mir::mir::Mutability,
6-
stable_mir::ty::{Allocation, ProvenanceMap},
7-
};
3+
use crate::rustc_smir::{Stable, Tables};
4+
use stable_mir::mir::Mutability;
5+
use stable_mir::ty::{Allocation, ProvenanceMap};
86

97
/// Creates new empty `Allocation` from given `Align`.
108
fn new_empty_allocation(align: rustc_target::abi::Align) -> Allocation {

compiler/rustc_smir/src/rustc_smir/mod.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
11-
use crate::stable_mir::ty::{
12-
FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy,
13-
};
14-
use crate::stable_mir::{self, opaque, CompilerError, Context};
1510
use hir::def::DefKind;
1611
use rustc_hir as hir;
1712
use rustc_middle::mir::interpret::{alloc_range, AllocId};
1813
use rustc_middle::mir::{self, ConstantKind};
1914
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
2015
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
21-
use rustc_span::ErrorGuaranteed;
2216
use rustc_target::abi::FieldIdx;
17+
use stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
18+
use stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy};
19+
use stable_mir::{self, opaque, Context};
2320
use tracing::debug;
2421

2522
mod alloc;
@@ -112,7 +109,7 @@ impl<'tcx> Context for Tables<'tcx> {
112109
}
113110
}
114111

115-
fn ty_kind(&mut self, ty: crate::stable_mir::ty::Ty) -> TyKind {
112+
fn ty_kind(&mut self, ty: stable_mir::ty::Ty) -> TyKind {
116113
self.types[ty.0].clone().stable(self)
117114
}
118115

@@ -1523,12 +1520,6 @@ impl<'tcx> Stable<'tcx> for rustc_span::Span {
15231520
}
15241521
}
15251522

1526-
impl<T> From<ErrorGuaranteed> for CompilerError<T> {
1527-
fn from(_error: ErrorGuaranteed) -> Self {
1528-
CompilerError::CompilationFailed
1529-
}
1530-
}
1531-
15321523
impl<'tcx> Stable<'tcx> for DefKind {
15331524
type T = stable_mir::DefKind;
15341525

compiler/stable_mir/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "stable_mir"
3+
version = "0.1.0-preview"
4+
edition = "2021"
5+
6+
[dependencies]
7+
tracing = "0.1"
8+
scoped-tls = "1.0"
File renamed without changes.

compiler/rustc_smir/src/stable_mir/fold.rs renamed to compiler/stable_mir/src/fold.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::ops::ControlFlow;
22

3-
use super::{
4-
ty::{
5-
Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig,
6-
GenericArgKind, GenericArgs, Promoted, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst,
7-
},
8-
Opaque,
3+
use crate::Opaque;
4+
5+
use super::ty::{
6+
Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind,
7+
GenericArgs, Promoted, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst,
98
};
109

1110
pub trait Folder: Sized {

compiler/rustc_smir/src/stable_mir/mod.rs renamed to compiler/stable_mir/src/lib.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
//! Module that implements the public interface to the Stable MIR.
1+
//! The WIP stable interface to rustc internals.
22
//!
3-
//! This module shall contain all type definitions and APIs that we expect third-party tools to invoke to
4-
//! interact with the compiler.
3+
//! For more information see <https://github.com/rust-lang/project-stable-mir>
54
//!
6-
//! The goal is to eventually move this module to its own crate which shall be published on
7-
//! [crates.io](https://crates.io).
5+
//! # Note
6+
//!
7+
//! This API is still completely unstable and subject to change.
8+
9+
#![doc(
10+
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
11+
test(attr(allow(unused_variables), deny(warnings)))
12+
)]
813
//!
9-
//! ## Note:
14+
//! This crate shall contain all type definitions and APIs that we expect third-party tools to invoke to
15+
//! interact with the compiler.
1016
//!
11-
//! There shouldn't be any direct references to internal compiler constructs in this module.
12-
//! If you need an internal construct, consider using `rustc_internal` or `rustc_smir`.
17+
//! The goal is to eventually be published on
18+
//! [crates.io](https://crates.io).
1319
1420
use std::cell::Cell;
1521
use std::fmt;
@@ -19,6 +25,9 @@ use self::ty::{
1925
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
2026
};
2127

28+
#[macro_use]
29+
extern crate scoped_tls;
30+
2231
pub mod fold;
2332
pub mod mir;
2433
pub mod ty;
@@ -32,11 +41,11 @@ pub type CrateNum = usize;
3241

3342
/// A unique identification number for each item accessible for the current compilation unit.
3443
#[derive(Clone, Copy, PartialEq, Eq)]
35-
pub struct DefId(pub(crate) usize);
44+
pub struct DefId(pub usize);
3645

3746
impl Debug for DefId {
3847
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39-
f.debug_struct("DefId:")
48+
f.debug_struct("DefId")
4049
.field("id", &self.0)
4150
.field("name", &with(|cx| cx.name_of_def_id(*self)))
4251
.finish()
@@ -45,7 +54,7 @@ impl Debug for DefId {
4554

4655
/// A unique identification number for each provenance
4756
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
48-
pub struct AllocId(pub(crate) usize);
57+
pub struct AllocId(pub usize);
4958

5059
/// A list of crate items.
5160
pub type CrateItems = Vec<CrateItem>;
@@ -73,7 +82,7 @@ pub enum CompilerError<T> {
7382
/// Holds information about a crate.
7483
#[derive(Clone, PartialEq, Eq, Debug)]
7584
pub struct Crate {
76-
pub(crate) id: CrateNum,
85+
pub id: CrateNum,
7786
pub name: Symbol,
7887
pub is_local: bool,
7988
}
@@ -84,7 +93,7 @@ pub type DefKind = Opaque;
8493
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
8594
/// use this item.
8695
#[derive(Clone, PartialEq, Eq, Debug)]
87-
pub struct CrateItem(pub(crate) DefId);
96+
pub struct CrateItem(pub DefId);
8897

8998
impl CrateItem {
9099
pub fn body(&self) -> mir::Body {
@@ -170,9 +179,12 @@ pub trait Context {
170179
/// Prints the name of given `DefId`
171180
fn name_of_def_id(&self, def_id: DefId) -> String;
172181

182+
/// Prints a human readable form of `Span`
173183
fn print_span(&self, span: Span) -> String;
174184

185+
/// Prints the kind of given `DefId`
175186
fn def_kind(&mut self, def_id: DefId) -> DefKind;
187+
176188
/// `Span` of an item
177189
fn span_of_an_item(&mut self, def_id: DefId) -> Span;
178190

@@ -200,7 +212,7 @@ pub fn run(mut context: impl Context, f: impl FnOnce()) {
200212

201213
/// Loads the current context and calls a function with it.
202214
/// Do not nest these, as that will ICE.
203-
pub(crate) fn with<R>(f: impl FnOnce(&mut dyn Context) -> R) -> R {
215+
pub fn with<R>(f: impl FnOnce(&mut dyn Context) -> R) -> R {
204216
assert!(TLV.is_set());
205217
TLV.with(|tlv| {
206218
let ptr = tlv.get();
@@ -225,6 +237,6 @@ impl std::fmt::Debug for Opaque {
225237
}
226238
}
227239

228-
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
240+
pub fn opaque<T: Debug>(value: &T) -> Opaque {
229241
Opaque(format!("{value:?}"))
230242
}

compiler/rustc_smir/src/stable_mir/mir/body.rs renamed to compiler/stable_mir/src/mir/body.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use crate::stable_mir::ty::{
2-
AdtDef, ClosureDef, Const, GeneratorDef, GenericArgs, Movability, Region,
3-
};
4-
use crate::stable_mir::Opaque;
5-
use crate::stable_mir::{self, ty::Ty, Span};
1+
use crate::ty::{AdtDef, ClosureDef, Const, GeneratorDef, GenericArgs, Movability, Region};
2+
use crate::Opaque;
3+
use crate::{ty::Ty, Span};
64

75
#[derive(Clone, Debug)]
86
pub struct Body {
@@ -135,7 +133,7 @@ pub enum AsyncGeneratorKind {
135133
}
136134

137135
pub(crate) type LocalDefId = Opaque;
138-
/// [`rustc_middle::mir::Coverage`] is heavily tied to internal details of the
136+
/// The rustc coverage data structures are heavily tied to internal details of the
139137
/// coverage implementation that are likely to change, and are unlikely to be
140138
/// useful to third-party tools for the foreseeable future.
141139
pub(crate) type Coverage = Opaque;
@@ -215,7 +213,7 @@ pub enum Rvalue {
215213
/// generator lowering, `Generator` aggregate kinds are disallowed too.
216214
Aggregate(AggregateKind, Vec<Operand>),
217215

218-
/// * `Offset` has the same semantics as [`offset`](pointer::offset), except that the second
216+
/// * `Offset` has the same semantics as `<*const T>::offset`, except that the second
219217
/// parameter may be a `usize` as well.
220218
/// * The comparison operations accept `bool`s, `char`s, signed or unsigned integers, floats,
221219
/// raw pointers, or function pointers and return a `bool`. The types of the operands must be
@@ -245,16 +243,14 @@ pub enum Rvalue {
245243
/// deref operation, immediately followed by one or more projections.
246244
CopyForDeref(Place),
247245

248-
/// Computes the discriminant of the place, returning it as an integer of type
249-
/// [`discriminant_ty`]. Returns zero for types without discriminant.
246+
/// Computes the discriminant of the place, returning it as an integer.
247+
/// Returns zero for types without discriminant.
250248
///
251249
/// The validity requirements for the underlying value are undecided for this rvalue, see
252250
/// [#91095]. Note too that the value of the discriminant is not the same thing as the
253-
/// variant index; use [`discriminant_for_variant`] to convert.
251+
/// variant index;
254252
///
255-
/// [`discriminant_ty`]: rustc_middle::ty::Ty::discriminant_ty
256253
/// [#91095]: https://github.com/rust-lang/rust/issues/91095
257-
/// [`discriminant_for_variant`]: rustc_middle::ty::Ty::discriminant_for_variant
258254
Discriminant(Place),
259255

260256
/// Yields the length of the place, as a `usize`.
@@ -295,7 +291,7 @@ pub enum Rvalue {
295291
///
296292
/// **Needs clarification**: Are there weird additional semantics here related to the runtime
297293
/// nature of this operation?
298-
ThreadLocalRef(stable_mir::CrateItem),
294+
ThreadLocalRef(crate::CrateItem),
299295

300296
/// Computes a value as described by the operation.
301297
NullaryOp(NullOp, Ty),

0 commit comments

Comments
 (0)