Skip to content

Commit 9b2792d

Browse files
Begin nightly-ifying rustc_type_ir
1 parent cee6db1 commit 9b2792d

File tree

17 files changed

+203
-93
lines changed

17 files changed

+203
-93
lines changed

Diff for: Cargo.lock

+11
Original file line numberDiff line numberDiff line change
@@ -4005,11 +4005,22 @@ name = "rustc_index"
40054005
version = "0.0.0"
40064006
dependencies = [
40074007
"arrayvec",
4008+
"rustc_index_macros",
40084009
"rustc_macros",
40094010
"rustc_serialize",
40104011
"smallvec",
40114012
]
40124013

4014+
[[package]]
4015+
name = "rustc_index_macros"
4016+
version = "0.0.0"
4017+
dependencies = [
4018+
"proc-macro2",
4019+
"quote",
4020+
"syn 2.0.29",
4021+
"synstructure 0.13.0",
4022+
]
4023+
40134024
[[package]]
40144025
name = "rustc_infer"
40154026
version = "0.0.0"

Diff for: compiler/rustc_index/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ edition = "2021"
88
[dependencies]
99
arrayvec = { version = "0.7", default-features = false }
1010
rustc_serialize = { path = "../rustc_serialize", optional = true }
11+
rustc_index_macros = { path = "../rustc_index_macros", default-features = false }
1112
rustc_macros = { path = "../rustc_macros", optional = true }
1213
smallvec = "1.8.1"
1314

1415
[features]
1516
default = ["nightly"]
16-
nightly = ["rustc_serialize", "rustc_macros"]
17+
nightly = ["rustc_serialize", "rustc_macros", "rustc_index_macros/nightly"]

Diff for: compiler/rustc_index/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ mod vec;
2525

2626
pub use {idx::Idx, slice::IndexSlice, vec::IndexVec};
2727

28-
#[cfg(feature = "rustc_macros")]
29-
pub use rustc_macros::newtype_index;
28+
pub use rustc_index_macros::newtype_index;
3029

3130
/// Type size assertion. The first argument is a type and the second argument is its expected size.
3231
///

Diff for: compiler/rustc_index/src/vec/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Allows the macro invocation below to work
44
use crate as rustc_index;
55

6-
rustc_macros::newtype_index! {
6+
crate::newtype_index! {
77
#[max = 0xFFFF_FFFA]
88
struct MyIdx {}
99
}

Diff for: compiler/rustc_index_macros/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "rustc_index_macros"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
synstructure = "0.13.0"
11+
syn = { version = "2.0.9", features = ["full"] }
12+
proc-macro2 = "1"
13+
quote = "1"
14+
15+
[features]
16+
default = ["nightly"]
17+
nightly = []

Diff for: compiler/rustc_index_macros/src/lib.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![cfg_attr(feature = "nightly", feature(allow_internal_unstable))]
2+
#![cfg_attr(feature = "nightly", allow(internal_features))]
3+
4+
use proc_macro::TokenStream;
5+
6+
mod newtype;
7+
8+
/// Creates a struct type `S` that can be used as an index with
9+
/// `IndexVec` and so on.
10+
///
11+
/// There are two ways of interacting with these indices:
12+
///
13+
/// - The `From` impls are the preferred way. So you can do
14+
/// `S::from(v)` with a `usize` or `u32`. And you can convert back
15+
/// to an integer with `u32::from(s)`.
16+
///
17+
/// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
18+
/// to create/return a value.
19+
///
20+
/// Internally, the index uses a u32, so the index must not exceed
21+
/// `u32::MAX`. You can also customize things like the `Debug` impl,
22+
/// what traits are derived, and so forth via the macro.
23+
#[proc_macro]
24+
#[cfg_attr(
25+
feature = "nightly",
26+
allow_internal_unstable(step_trait, rustc_attrs, trusted_step, spec_option_partial_eq)
27+
)]
28+
pub fn newtype_index(input: TokenStream) -> TokenStream {
29+
newtype::newtype(input)
30+
}

Diff for: compiler/rustc_macros/src/newtype.rs renamed to compiler/rustc_index_macros/src/newtype.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ impl Parse for Newtype {
2424
let mut consts = Vec::new();
2525
let mut encodable = true;
2626
let mut ord = true;
27+
let mut gate_rustc_only = quote! {};
28+
let mut gate_rustc_only_cfg = quote! { all() };
2729

2830
attrs.retain(|attr| match attr.path().get_ident() {
2931
Some(ident) => match &*ident.to_string() {
32+
"gate_rustc_only" => {
33+
gate_rustc_only = quote! { #[cfg(feature = "nightly")] };
34+
gate_rustc_only_cfg = quote! { feature = "nightly" };
35+
false
36+
}
3037
"custom_encodable" => {
3138
encodable = false;
3239
false
@@ -88,11 +95,13 @@ impl Parse for Newtype {
8895

8996
let encodable_impls = if encodable {
9097
quote! {
98+
#gate_rustc_only
9199
impl<D: ::rustc_serialize::Decoder> ::rustc_serialize::Decodable<D> for #name {
92100
fn decode(d: &mut D) -> Self {
93101
Self::from_u32(d.read_u32())
94102
}
95103
}
104+
#gate_rustc_only
96105
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for #name {
97106
fn encode(&self, e: &mut E) {
98107
e.emit_u32(self.private);
@@ -110,6 +119,7 @@ impl Parse for Newtype {
110119

111120
let step = if ord {
112121
quote! {
122+
#gate_rustc_only
113123
impl ::std::iter::Step for #name {
114124
#[inline]
115125
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
@@ -131,6 +141,7 @@ impl Parse for Newtype {
131141
}
132142

133143
// Safety: The implementation of `Step` upholds all invariants.
144+
#gate_rustc_only
134145
unsafe impl ::std::iter::TrustedStep for #name {}
135146
}
136147
} else {
@@ -148,6 +159,7 @@ impl Parse for Newtype {
148159
let spec_partial_eq_impl = if let Lit::Int(max) = &max {
149160
if let Ok(max_val) = max.base10_parse::<u32>() {
150161
quote! {
162+
#gate_rustc_only
151163
impl core::option::SpecOptionPartialEq for #name {
152164
#[inline]
153165
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
@@ -173,8 +185,8 @@ impl Parse for Newtype {
173185
Ok(Self(quote! {
174186
#(#attrs)*
175187
#[derive(Clone, Copy, PartialEq, Eq, Hash, #(#derive_paths),*)]
176-
#[rustc_layout_scalar_valid_range_end(#max)]
177-
#[rustc_pass_by_value]
188+
#[cfg_attr(#gate_rustc_only_cfg, rustc_layout_scalar_valid_range_end(#max))]
189+
#[cfg_attr(#gate_rustc_only_cfg, rustc_pass_by_value)]
178190
#vis struct #name {
179191
private: u32,
180192
}

Diff for: compiler/rustc_macros/src/lib.rs

-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use proc_macro::TokenStream;
1717
mod diagnostics;
1818
mod hash_stable;
1919
mod lift;
20-
mod newtype;
2120
mod query;
2221
mod serialize;
2322
mod symbols;
@@ -34,27 +33,6 @@ pub fn symbols(input: TokenStream) -> TokenStream {
3433
symbols::symbols(input.into()).into()
3534
}
3635

37-
/// Creates a struct type `S` that can be used as an index with
38-
/// `IndexVec` and so on.
39-
///
40-
/// There are two ways of interacting with these indices:
41-
///
42-
/// - The `From` impls are the preferred way. So you can do
43-
/// `S::from(v)` with a `usize` or `u32`. And you can convert back
44-
/// to an integer with `u32::from(s)`.
45-
///
46-
/// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
47-
/// to create/return a value.
48-
///
49-
/// Internally, the index uses a u32, so the index must not exceed
50-
/// `u32::MAX`. You can also customize things like the `Debug` impl,
51-
/// what traits are derived, and so forth via the macro.
52-
#[proc_macro]
53-
#[allow_internal_unstable(step_trait, rustc_attrs, trusted_step, spec_option_partial_eq)]
54-
pub fn newtype_index(input: TokenStream) -> TokenStream {
55-
newtype::newtype(input)
56-
}
57-
5836
decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
5937
decl_derive!(
6038
[HashStable_Generic, attributes(stable_hasher)] =>

Diff for: compiler/rustc_mir_transform/src/gvn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
5757
use rustc_data_structures::graph::dominators::Dominators;
5858
use rustc_index::bit_set::BitSet;
59+
use rustc_index::newtype_index;
5960
use rustc_index::IndexVec;
60-
use rustc_macros::newtype_index;
6161
use rustc_middle::mir::visit::*;
6262
use rustc_middle::mir::*;
6363
use rustc_middle::ty::{self, Ty, TyCtxt};

Diff for: compiler/rustc_type_ir/Cargo.toml

+16-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@ edition = "2021"
77

88
[dependencies]
99
bitflags = "1.2.1"
10-
rustc_index = { path = "../rustc_index" }
11-
rustc_serialize = { path = "../rustc_serialize" }
12-
rustc_data_structures = { path = "../rustc_data_structures" }
13-
rustc_macros = { path = "../rustc_macros" }
14-
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
10+
rustc_index = { path = "../rustc_index", default-features = false }
11+
rustc_serialize = { path = "../rustc_serialize", optional = true }
12+
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
13+
rustc_macros = { path = "../rustc_macros", optional = true }
14+
smallvec = { version = "1.8.1" }
15+
16+
[features]
17+
default = ["nightly"]
18+
nightly = [
19+
"smallvec/may_dangle",
20+
"smallvec/union",
21+
"rustc_index/nightly",
22+
"rustc_serialize",
23+
"rustc_data_structures",
24+
"rustc_macros",
25+
]

Diff for: compiler/rustc_type_ir/src/const_kind.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
#[cfg(feature = "nightly")]
12
use rustc_data_structures::stable_hasher::HashStable;
3+
#[cfg(feature = "nightly")]
24
use rustc_serialize::{Decodable, Decoder, Encodable};
35
use std::cmp::Ordering;
46
use std::fmt;
57
use std::hash;
68

7-
use crate::{
8-
DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, OptWithInfcx,
9-
TyDecoder, TyEncoder,
10-
};
9+
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, Interner, OptWithInfcx};
10+
#[cfg(feature = "nightly")]
11+
use crate::{HashStableContext, TyDecoder, TyEncoder};
1112

1213
use self::ConstKind::*;
1314

@@ -75,6 +76,7 @@ impl<I: Interner> hash::Hash for ConstKind<I> {
7576
}
7677
}
7778

79+
#[cfg(feature = "nightly")]
7880
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
7981
where
8082
I::ParamConst: HashStable<CTX>,
@@ -108,6 +110,7 @@ where
108110
}
109111
}
110112

113+
#[cfg(feature = "nightly")]
111114
impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for ConstKind<I>
112115
where
113116
I::ParamConst: Decodable<D>,
@@ -140,6 +143,7 @@ where
140143
}
141144
}
142145

146+
#[cfg(feature = "nightly")]
143147
impl<I: Interner, E: TyEncoder<I = I>> Encodable<E> for ConstKind<I>
144148
where
145149
I::ParamConst: Encodable<E>,

Diff for: compiler/rustc_type_ir/src/fold.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@
4545
//! - u.fold_with(folder)
4646
//! ```
4747
48-
use rustc_data_structures::sync::Lrc;
4948
use rustc_index::{Idx, IndexVec};
5049
use std::mem;
5150

51+
use crate::Lrc;
5252
use crate::{visit::TypeVisitable, Interner};
5353

54+
#[cfg(feature = "nightly")]
55+
type Never = !;
56+
57+
#[cfg(not(feature = "nightly"))]
58+
type Never = std::convert::Infallible;
59+
5460
/// This trait is implemented for every type that can be folded,
5561
/// providing the skeleton of the traversal.
5662
///
@@ -79,7 +85,10 @@ pub trait TypeFoldable<I: Interner>: TypeVisitable<I> {
7985
/// folders. Do not override this method, to ensure coherence with
8086
/// `try_fold_with`.
8187
fn fold_with<F: TypeFolder<I>>(self, folder: &mut F) -> Self {
82-
self.try_fold_with(folder).into_ok()
88+
match self.try_fold_with(folder) {
89+
Ok(t) => t,
90+
Err(e) => match e {},
91+
}
8392
}
8493
}
8594

@@ -100,7 +109,10 @@ pub trait TypeSuperFoldable<I: Interner>: TypeFoldable<I> {
100109
/// infallible folders. Do not override this method, to ensure coherence
101110
/// with `try_super_fold_with`.
102111
fn super_fold_with<F: TypeFolder<I>>(self, folder: &mut F) -> Self {
103-
self.try_super_fold_with(folder).into_ok()
112+
match self.try_super_fold_with(folder) {
113+
Ok(t) => t,
114+
Err(e) => match e {},
115+
}
104116
}
105117
}
106118

@@ -113,7 +125,7 @@ pub trait TypeSuperFoldable<I: Interner>: TypeFoldable<I> {
113125
/// A blanket implementation of [`FallibleTypeFolder`] will defer to
114126
/// the infallible methods of this trait to ensure that the two APIs
115127
/// are coherent.
116-
pub trait TypeFolder<I: Interner>: FallibleTypeFolder<I, Error = !> {
128+
pub trait TypeFolder<I: Interner>: FallibleTypeFolder<I, Error = Never> {
117129
fn interner(&self) -> I;
118130

119131
fn fold_binder<T>(&mut self, t: I::Binder<T>) -> I::Binder<T>
@@ -208,39 +220,39 @@ impl<I: Interner, F> FallibleTypeFolder<I> for F
208220
where
209221
F: TypeFolder<I>,
210222
{
211-
type Error = !;
223+
type Error = Never;
212224

213225
fn interner(&self) -> I {
214226
TypeFolder::interner(self)
215227
}
216228

217-
fn try_fold_binder<T>(&mut self, t: I::Binder<T>) -> Result<I::Binder<T>, !>
229+
fn try_fold_binder<T>(&mut self, t: I::Binder<T>) -> Result<I::Binder<T>, Never>
218230
where
219231
T: TypeFoldable<I>,
220232
I::Binder<T>: TypeSuperFoldable<I>,
221233
{
222234
Ok(self.fold_binder(t))
223235
}
224236

225-
fn try_fold_ty(&mut self, t: I::Ty) -> Result<I::Ty, !>
237+
fn try_fold_ty(&mut self, t: I::Ty) -> Result<I::Ty, Never>
226238
where
227239
I::Ty: TypeSuperFoldable<I>,
228240
{
229241
Ok(self.fold_ty(t))
230242
}
231243

232-
fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, !> {
244+
fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, Never> {
233245
Ok(self.fold_region(r))
234246
}
235247

236-
fn try_fold_const(&mut self, c: I::Const) -> Result<I::Const, !>
248+
fn try_fold_const(&mut self, c: I::Const) -> Result<I::Const, Never>
237249
where
238250
I::Const: TypeSuperFoldable<I>,
239251
{
240252
Ok(self.fold_const(c))
241253
}
242254

243-
fn try_fold_predicate(&mut self, p: I::Predicate) -> Result<I::Predicate, !>
255+
fn try_fold_predicate(&mut self, p: I::Predicate) -> Result<I::Predicate, Never>
244256
where
245257
I::Predicate: TypeSuperFoldable<I>,
246258
{
@@ -311,7 +323,7 @@ impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Lrc<T> {
311323
// Call to `Lrc::make_mut` above guarantees that `unique` is the
312324
// sole reference to the contained value, so we can avoid doing
313325
// a checked `get_mut` here.
314-
let slot = Lrc::get_mut_unchecked(&mut unique);
326+
let slot = Lrc::get_mut(&mut unique).unwrap_unchecked();
315327

316328
// Semantically move the contained type out from `unique`, fold
317329
// it, then move the folded value back into `unique`. Should

0 commit comments

Comments
 (0)