From 3e0c1c8e0cdeab8871f96b1703f6b8a852d51568 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 25 May 2022 07:30:04 +0000
Subject: [PATCH 01/11] Add WIP stable MIR crate

---
 Cargo.toml           | 10 ++++++++++
 src/lib.rs           | 10 ++++++++++
 src/mir.rs           | 10 ++++++++++
 src/very_unstable.rs |  9 +++++++++
 4 files changed, 39 insertions(+)
 create mode 100644 Cargo.toml
 create mode 100644 src/lib.rs
 create mode 100644 src/mir.rs
 create mode 100644 src/very_unstable.rs

diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000000000..0c5a19d4034aa
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "rustc_smir"
+version = "0.0.0"
+edition = "2021"
+
+[dependencies]
+rustc_middle = { path = "../rustc_middle" }
+rustc_driver = { path = "../rustc_driver" }
+rustc_borrowck = { path = "../rustc_borrowck" }
+rustc_interface = { path = "../rustc_interface" }
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000000000..405ee1388ddca
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,10 @@
+//! The WIP stable interface to rustc internals.
+
+#![doc(
+    html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
+    test(attr(allow(unused_variables), deny(warnings)))
+)]
+
+pub mod mir;
+
+pub mod very_unstable;
diff --git a/src/mir.rs b/src/mir.rs
new file mode 100644
index 0000000000000..97969be669dc6
--- /dev/null
+++ b/src/mir.rs
@@ -0,0 +1,10 @@
+pub use rustc_middle::mir::{
+    visit::MutVisitor, AggregateKind, AssertKind, BasicBlock, BasicBlockData, BinOp, BindingForm,
+    BlockTailInfo, Body, BorrowKind, CastKind, ClearCrossCrate, Constant, ConstantKind,
+    CopyNonOverlapping, Coverage, FakeReadCause, Field, GeneratorInfo, ImplicitSelfKind,
+    InlineAsmOperand, Local, LocalDecl, LocalInfo, LocalKind, Location, MirPhase, MirSource,
+    NullOp, Operand, Place, PlaceRef, ProjectionElem, ProjectionKind, Promoted, RetagKind, Rvalue,
+    Safety, SourceInfo, SourceScope, SourceScopeData, SourceScopeLocalData, Statement,
+    StatementKind, UnOp, UserTypeProjection, UserTypeProjections, VarBindingForm, VarDebugInfo,
+    VarDebugInfoContents,
+};
diff --git a/src/very_unstable.rs b/src/very_unstable.rs
new file mode 100644
index 0000000000000..8ba0251629d7f
--- /dev/null
+++ b/src/very_unstable.rs
@@ -0,0 +1,9 @@
+//! This module reexports various crates and modules from unstable rustc APIs.
+//! Add anything you need here and it will get slowly transferred to a stable API.
+//! Only use rustc_smir in your dependencies and use the reexports here instead of
+//! directly referring to the unstable crates.
+
+pub use rustc_borrowck as borrowck;
+pub use rustc_driver as driver;
+pub use rustc_interface as interface;
+pub use rustc_middle as middle;

From d9a3f5cfebd189044fe294ec8398a6cac6ee613d Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 25 May 2022 08:41:50 +0000
Subject: [PATCH 02/11] Add instructions

---
 README.md | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 README.md

diff --git a/README.md b/README.md
new file mode 100644
index 0000000000000..de358941b786c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,33 @@
+This crate is regularly synced with its mirror in the rustc repo at `compiler/rustc_smir`.
+
+We use `git subtree` for this to preserve commits and allow the rustc repo to
+edit these crates without having to touch this repo. This keeps the crates compiling
+while allowing us to independently work on them here. The effort of keeping them in
+sync is pushed entirely onto us, without affecting rustc workflows negatively.
+This may change in the future, but changes to policy should only be done via a
+compiler team MCP.
+
+## Instructions for syncing
+
+### Updating this repository
+
+In the rustc repo, execute
+
+```
+git subtree push --prefix=compiler/rustc_smir url_to_your_fork_of_project_stable_mir some_feature_branch
+```
+
+and then open a PR of your `some_feature_branch` against https://github.com/rust-lang/project-stable-mir
+
+### Updating the rustc librar
+
+
+In the rustc repo, execute
+
+```
+git subtree pull --prefix=compiler/rustc_smir https://github.com/rust-lang/project-stable-mir smir
+```
+
+Note: only ever sync to rustc from the project-stable-mir's `smir` branch. Do not sync with your own forks.
+
+Then open a PR against rustc just like a regular PR.

From fe76f0324c67c65b4a54097e874998f73f366b5f Mon Sep 17 00:00:00 2001
From: Xavier Denis <xldenis@gmail.com>
Date: Wed, 25 May 2022 13:20:15 +0200
Subject: [PATCH 03/11] Add additional crates to `very_unstable

Adds some additional crates used by Creusot.
---
 src/very_unstable.rs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/very_unstable.rs b/src/very_unstable.rs
index 8ba0251629d7f..dacdabf1ccb23 100644
--- a/src/very_unstable.rs
+++ b/src/very_unstable.rs
@@ -5,5 +5,10 @@
 
 pub use rustc_borrowck as borrowck;
 pub use rustc_driver as driver;
+pub use rustc_hir as hir;
 pub use rustc_interface as interface;
 pub use rustc_middle as middle;
+pub use rustc_mir_dataflow as dataflow;
+pub use rustc_mir_transform as transform;
+pub use rustc_serialize as serialize;
+pub use rustc_trait_selection as trait_selection;

From e5b824c535242866ee004e2c6ef8da5ad162b1ad Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 25 May 2022 08:48:14 +0000
Subject: [PATCH 04/11] Documentation never hurts

---
 src/lib.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/lib.rs b/src/lib.rs
index 405ee1388ddca..a867520fc6810 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,10 @@
 //! The WIP stable interface to rustc internals.
+//! 
+//! For more information see https://github.com/rust-lang/project-stable-mir
+//! 
+//! # Note
+//!
+//! This API is still completely unstable and subject to change.
 
 #![doc(
     html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",

From 36f3c03421513d6b14a26538f2971f75d1e9f5bb Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 25 May 2022 09:06:12 +0000
Subject: [PATCH 05/11] Update instructions

---
 README.md | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index de358941b786c..92eb926f5058e 100644
--- a/README.md
+++ b/README.md
@@ -19,8 +19,38 @@ git subtree push --prefix=compiler/rustc_smir url_to_your_fork_of_project_stable
 
 and then open a PR of your `some_feature_branch` against https://github.com/rust-lang/project-stable-mir
 
-### Updating the rustc librar
+### Updating the rustc library
 
+First we need to bump our stack limit, as the rustc repo otherwise quickly hits that:
+
+```
+ulimit -s 60000
+```
+
+#### Maximum function recursion depth (1000) reached
+
+Then we need to disable `dash` as the default shell for sh scripts, as otherwise we run into a
+hard limit of a recursion depth of 1000:
+
+```
+sudo dpkg-reconfigure dash
+```
+
+and then select `No` to disable dash.
+
+
+#### Patching your `git worktree`
+
+The regular git worktree does not scale to repos of the size of the rustc repo.
+So download the `git-subtree.sh` from https://github.com/gitgitgadget/git/pull/493/files and run
+
+```
+sudo cp --backup /path/to/patched/git-subtree.sh /usr/lib/git-core/git-subtree
+sudo chmod --reference=/usr/lib/git-core/git-subtree~ /usr/lib/git-core/git-subtree
+sudo chown --reference=/usr/lib/git-core/git-subtree~ /usr/lib/git-core/git-subtree
+```
+
+#### Actually doing a sync
 
 In the rustc repo, execute
 

From 9da5e13294177621854dfd6a249863286c482fb1 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 25 May 2022 09:11:09 +0000
Subject: [PATCH 06/11] Rustfmt

---
 src/lib.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index a867520fc6810..2b1c2bb3d2b84 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,7 +1,7 @@
 //! The WIP stable interface to rustc internals.
-//! 
+//!
 //! For more information see https://github.com/rust-lang/project-stable-mir
-//! 
+//!
 //! # Note
 //!
 //! This API is still completely unstable and subject to change.

From 615f8c5dbbd59b9ec734efaeb36b0e619c9e3e38 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Thu, 2 Jun 2022 09:54:57 +0000
Subject: [PATCH 07/11] record which nightly we support

---
 rust-toolchain.toml | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 rust-toolchain.toml

diff --git a/rust-toolchain.toml b/rust-toolchain.toml
new file mode 100644
index 0000000000000..7b696fc1f5cec
--- /dev/null
+++ b/rust-toolchain.toml
@@ -0,0 +1,3 @@
+[toolchain]
+channel = "nightly-2022-06-01"
+components = [ "rustfmt", "rustc-dev" ]

From 9abcb5c7b574cf316eb23d3f469187bb86ba3019 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Thu, 2 Jun 2022 09:55:54 +0000
Subject: [PATCH 08/11] Make the crate work both in rustc and locally

---
 .gitignore           |  1 +
 Cargo.toml           | 11 +++++++----
 README.md            | 12 ++++++++++++
 src/lib.rs           |  2 ++
 src/mir.rs           |  2 +-
 src/very_unstable.rs | 31 ++++++++++++++++++++++---------
 6 files changed, 45 insertions(+), 14 deletions(-)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000000..eb5a316cbd195
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+target
diff --git a/Cargo.toml b/Cargo.toml
index 0c5a19d4034aa..bc0cac5a0bcef 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,7 +4,10 @@ version = "0.0.0"
 edition = "2021"
 
 [dependencies]
-rustc_middle = { path = "../rustc_middle" }
-rustc_driver = { path = "../rustc_driver" }
-rustc_borrowck = { path = "../rustc_borrowck" }
-rustc_interface = { path = "../rustc_interface" }
+rustc_middle = { path = "../rustc_middle", optional = true }
+rustc_driver = { path = "../rustc_driver", optional = true }
+rustc_borrowck = { path = "../rustc_borrowck", optional = true }
+rustc_interface = { path = "../rustc_interface", optional = true }
+
+[features]
+default = ["rustc_middle", "rustc_driver", "rustc_borrowck", "rustc_interface"]
diff --git a/README.md b/README.md
index 92eb926f5058e..ae49098dd0ce6 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,18 @@ sync is pushed entirely onto us, without affecting rustc workflows negatively.
 This may change in the future, but changes to policy should only be done via a
 compiler team MCP.
 
+## Instructions for working on this crate locally
+
+Since the crate is the same in the rustc repo and here, the dependencies on rustc_* crates
+will only either work here or there, but never in both places at the same time. Thus we use
+optional dependencies on the rustc_* crates, requiring local development to use
+
+```
+cargo build --no-default-features -Zavoid-dev-deps
+```
+
+in order to compile successfully.
+
 ## Instructions for syncing
 
 ### Updating this repository
diff --git a/src/lib.rs b/src/lib.rs
index 2b1c2bb3d2b84..decdae953d95b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -11,6 +11,8 @@
     test(attr(allow(unused_variables), deny(warnings)))
 )]
 
+#![cfg_attr(not(feature = "default"), feature(rustc_private))]
+
 pub mod mir;
 
 pub mod very_unstable;
diff --git a/src/mir.rs b/src/mir.rs
index 97969be669dc6..855605b1a4f9d 100644
--- a/src/mir.rs
+++ b/src/mir.rs
@@ -1,4 +1,4 @@
-pub use rustc_middle::mir::{
+pub use crate::very_unstable::middle::mir::{
     visit::MutVisitor, AggregateKind, AssertKind, BasicBlock, BasicBlockData, BinOp, BindingForm,
     BlockTailInfo, Body, BorrowKind, CastKind, ClearCrossCrate, Constant, ConstantKind,
     CopyNonOverlapping, Coverage, FakeReadCause, Field, GeneratorInfo, ImplicitSelfKind,
diff --git a/src/very_unstable.rs b/src/very_unstable.rs
index dacdabf1ccb23..12ba133dbb169 100644
--- a/src/very_unstable.rs
+++ b/src/very_unstable.rs
@@ -3,12 +3,25 @@
 //! Only use rustc_smir in your dependencies and use the reexports here instead of
 //! directly referring to the unstable crates.
 
-pub use rustc_borrowck as borrowck;
-pub use rustc_driver as driver;
-pub use rustc_hir as hir;
-pub use rustc_interface as interface;
-pub use rustc_middle as middle;
-pub use rustc_mir_dataflow as dataflow;
-pub use rustc_mir_transform as transform;
-pub use rustc_serialize as serialize;
-pub use rustc_trait_selection as trait_selection;
+macro_rules! crates {
+    ($($rustc_name:ident -> $name:ident,)*) => {
+        $(
+            #[cfg(not(feature = "default"))]
+            pub extern crate $rustc_name as $name;
+            #[cfg(feature = "default")]
+            pub use $rustc_name as $name;
+        )*
+    }
+}
+
+crates! {
+    rustc_borrowck -> borrowck,
+    rustc_driver -> driver,
+    rustc_hir -> hir,
+    rustc_interface -> interface,
+    rustc_middle -> middle,
+    rustc_mir_dataflow -> dataflow,
+    rustc_mir_transform -> transform,
+    rustc_serialize -> serialize,
+    rustc_trait_selection -> trait_selection,
+}

From 9960cc1f084694f096d6876e1afd698cce1222c8 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Thu, 2 Jun 2022 10:08:38 +0000
Subject: [PATCH 09/11] Ship rustc_smir with rustc

---
 Cargo.lock                | 11 +++++++++++
 compiler/rustc/Cargo.toml |  3 +++
 2 files changed, 14 insertions(+)

diff --git a/Cargo.lock b/Cargo.lock
index 7ed327e9f4ccb..e5908889dce6c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3410,6 +3410,7 @@ dependencies = [
  "jemalloc-sys",
  "rustc_codegen_ssa",
  "rustc_driver",
+ "rustc_smir",
 ]
 
 [[package]]
@@ -4401,6 +4402,16 @@ dependencies = [
  "tracing",
 ]
 
+[[package]]
+name = "rustc_smir"
+version = "0.0.0"
+dependencies = [
+ "rustc_borrowck",
+ "rustc_driver",
+ "rustc_interface",
+ "rustc_middle",
+]
+
 [[package]]
 name = "rustc_span"
 version = "0.0.0"
diff --git a/compiler/rustc/Cargo.toml b/compiler/rustc/Cargo.toml
index 5e0bb1a7f95d1..27ee3dd2aeafc 100644
--- a/compiler/rustc/Cargo.toml
+++ b/compiler/rustc/Cargo.toml
@@ -9,6 +9,9 @@ rustc_driver = { path = "../rustc_driver" }
 # Make sure rustc_codegen_ssa ends up in the sysroot, because this
 # crate is intended to be used by codegen backends, which may not be in-tree.
 rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
+# Make sure rustc_smir ends up in the sysroot, because this
+# crate is intended to be used by stable MIR consumers, which are not in-tree
+rustc_smir = { path = "../rustc_smir" }
 
 [dependencies.jemalloc-sys]
 version = "0.5.0"

From 0324ac8ae014afc3eaa1bb85f169a025274e54bc Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Thu, 2 Jun 2022 10:15:07 +0000
Subject: [PATCH 10/11] List all crates used in crate source in Cargo.toml

---
 Cargo.lock                     |  5 +++++
 compiler/rustc_smir/Cargo.toml | 21 ++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index e5908889dce6c..75eedc90dbc7c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4408,8 +4408,13 @@ version = "0.0.0"
 dependencies = [
  "rustc_borrowck",
  "rustc_driver",
+ "rustc_hir",
  "rustc_interface",
  "rustc_middle",
+ "rustc_mir_dataflow",
+ "rustc_mir_transform",
+ "rustc_serialize",
+ "rustc_trait_selection",
 ]
 
 [[package]]
diff --git a/compiler/rustc_smir/Cargo.toml b/compiler/rustc_smir/Cargo.toml
index bc0cac5a0bcef..5e0d1f369a6a2 100644
--- a/compiler/rustc_smir/Cargo.toml
+++ b/compiler/rustc_smir/Cargo.toml
@@ -4,10 +4,25 @@ version = "0.0.0"
 edition = "2021"
 
 [dependencies]
-rustc_middle = { path = "../rustc_middle", optional = true }
-rustc_driver = { path = "../rustc_driver", optional = true }
 rustc_borrowck = { path = "../rustc_borrowck", optional = true }
+rustc_driver = { path = "../rustc_driver", optional = true }
+rustc_hir = { path = "../rustc_hir", optional = true }
 rustc_interface = { path = "../rustc_interface", optional = true }
+rustc_middle = { path = "../rustc_middle", optional = true }
+rustc_mir_dataflow = { path = "../rustc_mir_dataflow", optional = true }
+rustc_mir_transform = { path = "../rustc_mir_transform", optional = true }
+rustc_serialize = { path = "../rustc_serialize", optional = true }
+rustc_trait_selection = { path = "../rustc_trait_selection", optional = true }
 
 [features]
-default = ["rustc_middle", "rustc_driver", "rustc_borrowck", "rustc_interface"]
+default = [
+    "rustc_borrowck",
+    "rustc_driver",
+    "rustc_hir",
+    "rustc_interface",
+    "rustc_middle",
+    "rustc_mir_dataflow",
+    "rustc_mir_transform",
+    "rustc_serialize",
+    "rustc_trait_selection",
+]

From 7cba9ed4f7089774f10243b8b4825dd5c34759da Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Thu, 2 Jun 2022 10:29:00 +0000
Subject: [PATCH 11/11] Rustfmt

---
 compiler/rustc_smir/src/lib.rs | 1 -
 1 file changed, 1 deletion(-)

diff --git a/compiler/rustc_smir/src/lib.rs b/compiler/rustc_smir/src/lib.rs
index decdae953d95b..5c7aaf35b9032 100644
--- a/compiler/rustc_smir/src/lib.rs
+++ b/compiler/rustc_smir/src/lib.rs
@@ -10,7 +10,6 @@
     html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
     test(attr(allow(unused_variables), deny(warnings)))
 )]
-
 #![cfg_attr(not(feature = "default"), feature(rustc_private))]
 
 pub mod mir;