Skip to content

Commit 4f3da90

Browse files
committedNov 19, 2023
Auto merge of rust-lang#116828 - compiler-errors:nightlyify-rustc_type_ir, r=jackh726
Begin to abstract `rustc_type_ir` for rust-analyzer This adds the "nightly" feature which is used by the compiler, and falls back to more simple implementations when that is not active. r? `@lcnr` or `@jackh726`
2 parents 9a66e44 + 4506681 commit 4f3da90

File tree

19 files changed

+214
-102
lines changed

19 files changed

+214
-102
lines changed
 

‎Cargo.lock

+11
Original file line numberDiff line numberDiff line change
@@ -3970,11 +3970,22 @@ name = "rustc_index"
39703970
version = "0.0.0"
39713971
dependencies = [
39723972
"arrayvec",
3973+
"rustc_index_macros",
39733974
"rustc_macros",
39743975
"rustc_serialize",
39753976
"smallvec",
39763977
]
39773978

3979+
[[package]]
3980+
name = "rustc_index_macros"
3981+
version = "0.0.0"
3982+
dependencies = [
3983+
"proc-macro2",
3984+
"quote",
3985+
"syn 2.0.29",
3986+
"synstructure",
3987+
]
3988+
39783989
[[package]]
39793990
name = "rustc_infer"
39803991
version = "0.0.0"

‎compiler/rustc_index/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
# tidy-alphabetical-start
88
arrayvec = { version = "0.7", default-features = false }
9+
rustc_index_macros = { path = "../rustc_index_macros", default-features = false }
910
rustc_macros = { path = "../rustc_macros", optional = true }
1011
rustc_serialize = { path = "../rustc_serialize", optional = true }
1112
smallvec = "1.8.1"
@@ -14,5 +15,5 @@ smallvec = "1.8.1"
1415
[features]
1516
# tidy-alphabetical-start
1617
default = ["nightly"]
17-
nightly = ["rustc_serialize", "rustc_macros"]
18+
nightly = ["rustc_serialize", "rustc_macros", "rustc_index_macros/nightly"]
1819
# tidy-alphabetical-end

‎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
///

‎compiler/rustc_index/src/vec/tests.rs

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

4-
rustc_macros::newtype_index! {
4+
crate::newtype_index! {
55
#[max = 0xFFFF_FFFA]
66
struct MyIdx {}
77
}
+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 = []
+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+
}

‎compiler/rustc_macros/src/newtype.rs ‎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
}

‎compiler/rustc_macros/src/lib.rs

-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ mod current_version;
1919
mod diagnostics;
2020
mod hash_stable;
2121
mod lift;
22-
mod newtype;
2322
mod query;
2423
mod serialize;
2524
mod symbols;
@@ -44,27 +43,6 @@ pub fn symbols(input: TokenStream) -> TokenStream {
4443
symbols::symbols(input.into()).into()
4544
}
4645

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

‎compiler/rustc_mir_transform/src/gvn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
8888
use rustc_data_structures::graph::dominators::Dominators;
8989
use rustc_hir::def::DefKind;
9090
use rustc_index::bit_set::BitSet;
91+
use rustc_index::newtype_index;
9192
use rustc_index::IndexVec;
92-
use rustc_macros::newtype_index;
9393
use rustc_middle::mir::interpret::GlobalAlloc;
9494
use rustc_middle::mir::visit::*;
9595
use rustc_middle::mir::*;

‎compiler/rustc_type_ir/Cargo.toml

+16-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@ edition = "2021"
77
# tidy-alphabetical-start
88
bitflags = "1.2.1"
99
derivative = "2.2.0"
10-
rustc_data_structures = { path = "../rustc_data_structures" }
11-
rustc_index = { path = "../rustc_index" }
12-
rustc_macros = { path = "../rustc_macros" }
13-
rustc_serialize = { path = "../rustc_serialize" }
14-
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
10+
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
11+
rustc_index = { path = "../rustc_index", default-features = false }
12+
rustc_macros = { path = "../rustc_macros", optional = true }
13+
rustc_serialize = { path = "../rustc_serialize", optional = true }
14+
smallvec = { version = "1.8.1" }
1515
# tidy-alphabetical-end
16+
17+
[features]
18+
default = ["nightly"]
19+
nightly = [
20+
"smallvec/may_dangle",
21+
"smallvec/union",
22+
"rustc_index/nightly",
23+
"rustc_serialize",
24+
"rustc_data_structures",
25+
"rustc_macros",
26+
]

‎compiler/rustc_type_ir/src/canonical.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ use std::fmt;
22
use std::hash::Hash;
33
use std::ops::ControlFlow;
44

5+
#[cfg(feature = "nightly")]
56
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
67

78
use crate::fold::{FallibleTypeFolder, TypeFoldable};
89
use crate::visit::{TypeVisitable, TypeVisitor};
9-
use crate::{HashStableContext, Interner, UniverseIndex};
10+
use crate::{Interner, UniverseIndex};
1011

1112
/// A "canonicalized" type `V` is one where all free inference
1213
/// variables have been rewritten to "canonical vars". These are
1314
/// numbered starting from 0 in order of first appearance.
1415
#[derive(derivative::Derivative)]
1516
#[derivative(Clone(bound = "V: Clone"), Hash(bound = "V: Hash"))]
16-
#[derive(TyEncodable, TyDecodable)]
17+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable))]
1718
pub struct Canonical<I: Interner, V> {
1819
pub value: V,
1920
pub max_universe: UniverseIndex,
@@ -60,7 +61,9 @@ impl<I: Interner, V> Canonical<I, V> {
6061
}
6162
}
6263

63-
impl<CTX: HashStableContext, I: Interner, V: HashStable<CTX>> HashStable<CTX> for Canonical<I, V>
64+
#[cfg(feature = "nightly")]
65+
impl<CTX: crate::HashStableContext, I: Interner, V: HashStable<CTX>> HashStable<CTX>
66+
for Canonical<I, V>
6467
where
6568
I::CanonicalVars: HashStable<CTX>,
6669
{

‎compiler/rustc_type_ir/src/const_kind.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use rustc_data_structures::stable_hasher::HashStable;
2-
use rustc_data_structures::stable_hasher::StableHasher;
1+
#[cfg(feature = "nightly")]
2+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33
use std::fmt;
44

5-
use crate::{DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, WithInfcx};
5+
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, Interner, WithInfcx};
66

77
use self::ConstKind::*;
88

@@ -16,7 +16,7 @@ use self::ConstKind::*;
1616
Ord = "feature_allow_slow_enum",
1717
Hash(bound = "")
1818
)]
19-
#[derive(TyEncodable, TyDecodable)]
19+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable))]
2020
pub enum ConstKind<I: Interner> {
2121
/// A const generic parameter.
2222
Param(I::ParamConst),
@@ -47,6 +47,7 @@ pub enum ConstKind<I: Interner> {
4747
Expr(I::ExprConst),
4848
}
4949

50+
#[cfg(feature = "nightly")]
5051
const fn const_kind_discriminant<I: Interner>(value: &ConstKind<I>) -> usize {
5152
match value {
5253
Param(_) => 0,
@@ -60,7 +61,8 @@ const fn const_kind_discriminant<I: Interner>(value: &ConstKind<I>) -> usize {
6061
}
6162
}
6263

63-
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
64+
#[cfg(feature = "nightly")]
65+
impl<CTX: crate::HashStableContext, I: Interner> HashStable<CTX> for ConstKind<I>
6466
where
6567
I::ParamConst: HashStable<CTX>,
6668
I::InferConst: HashStable<CTX>,

‎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

‎compiler/rustc_type_ir/src/lib.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
#![feature(associated_type_defaults)]
2-
#![feature(fmt_helpers_for_derive)]
3-
#![feature(get_mut_unchecked)]
4-
#![feature(min_specialization)]
5-
#![feature(never_type)]
6-
#![feature(new_uninit)]
7-
#![feature(rustc_attrs)]
8-
#![feature(unwrap_infallible)]
1+
#![cfg_attr(
2+
feature = "nightly",
3+
feature(associated_type_defaults, min_specialization, never_type, rustc_attrs)
4+
)]
95
#![deny(rustc::untranslatable_diagnostic)]
106
#![deny(rustc::diagnostic_outside_of_impl)]
11-
#![allow(internal_features)]
7+
#![cfg_attr(feature = "nightly", allow(internal_features))]
128

9+
#[cfg(feature = "nightly")]
1310
extern crate self as rustc_type_ir;
1411

1512
#[macro_use]
1613
extern crate bitflags;
14+
#[cfg(feature = "nightly")]
1715
#[macro_use]
1816
extern crate rustc_macros;
1917

18+
#[cfg(feature = "nightly")]
19+
use rustc_data_structures::sync::Lrc;
2020
use std::fmt;
2121
use std::hash::Hash;
22+
#[cfg(not(feature = "nightly"))]
23+
use std::sync::Arc as Lrc;
2224

25+
#[cfg(feature = "nightly")]
2326
pub mod codec;
2427
pub mod fold;
2528
pub mod ty_info;
@@ -37,6 +40,7 @@ mod predicate_kind;
3740
mod region_kind;
3841

3942
pub use canonical::*;
43+
#[cfg(feature = "nightly")]
4044
pub use codec::*;
4145
pub use const_kind::*;
4246
pub use debug::{DebugWithInfcx, InferCtxtLike, WithInfcx};
@@ -90,8 +94,9 @@ rustc_index::newtype_index! {
9094
/// is the outer fn.
9195
///
9296
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
93-
#[derive(HashStable_Generic)]
97+
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
9498
#[debug_format = "DebruijnIndex({})"]
99+
#[gate_rustc_only]
95100
pub struct DebruijnIndex {
96101
const INNERMOST = 0;
97102
}
@@ -173,8 +178,9 @@ pub fn debug_bound_var<T: std::fmt::Write>(
173178
}
174179
}
175180

176-
#[derive(Copy, Clone, PartialEq, Eq, Decodable, Encodable, Hash, HashStable_Generic)]
177-
#[rustc_pass_by_value]
181+
#[derive(Copy, Clone, PartialEq, Eq)]
182+
#[cfg_attr(feature = "nightly", derive(Decodable, Encodable, Hash, HashStable_Generic))]
183+
#[cfg_attr(feature = "nightly", rustc_pass_by_value)]
178184
pub enum Variance {
179185
Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
180186
Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
@@ -289,8 +295,9 @@ rustc_index::newtype_index! {
289295
/// declared, but a type name in a non-zero universe is a placeholder
290296
/// type -- an idealized representative of "types in general" that we
291297
/// use for checking generic functions.
292-
#[derive(HashStable_Generic)]
298+
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
293299
#[debug_format = "U{}"]
300+
#[gate_rustc_only]
294301
pub struct UniverseIndex {}
295302
}
296303

‎compiler/rustc_type_ir/src/predicate_kind.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
#[cfg(feature = "nightly")]
12
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
23
use std::fmt;
34
use std::ops::ControlFlow;
45

56
use crate::fold::{FallibleTypeFolder, TypeFoldable};
67
use crate::visit::{TypeVisitable, TypeVisitor};
7-
use crate::{HashStableContext, Interner};
8+
use crate::Interner;
89

910
/// A clause is something that can appear in where bounds or be inferred
1011
/// by implied bounds.
1112
#[derive(derivative::Derivative)]
1213
#[derivative(Clone(bound = ""), Hash(bound = ""))]
13-
#[derive(TyEncodable, TyDecodable)]
14+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable))]
1415
pub enum ClauseKind<I: Interner> {
1516
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
1617
/// the `Self` type of the trait reference and `A`, `B`, and `C`
@@ -67,6 +68,7 @@ impl<I: Interner> PartialEq for ClauseKind<I> {
6768

6869
impl<I: Interner> Eq for ClauseKind<I> {}
6970

71+
#[cfg(feature = "nightly")]
7072
fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
7173
match value {
7274
ClauseKind::Trait(_) => 0,
@@ -79,7 +81,8 @@ fn clause_kind_discriminant<I: Interner>(value: &ClauseKind<I>) -> usize {
7981
}
8082
}
8183

82-
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for ClauseKind<I>
84+
#[cfg(feature = "nightly")]
85+
impl<CTX: crate::HashStableContext, I: Interner> HashStable<CTX> for ClauseKind<I>
8386
where
8487
I::Ty: HashStable<CTX>,
8588
I::Const: HashStable<CTX>,
@@ -161,7 +164,7 @@ where
161164

162165
#[derive(derivative::Derivative)]
163166
#[derivative(Clone(bound = ""), Hash(bound = ""))]
164-
#[derive(TyEncodable, TyDecodable)]
167+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable))]
165168
pub enum PredicateKind<I: Interner> {
166169
/// Prove a clause
167170
Clause(ClauseKind<I>),
@@ -239,6 +242,7 @@ impl<I: Interner> PartialEq for PredicateKind<I> {
239242

240243
impl<I: Interner> Eq for PredicateKind<I> {}
241244

245+
#[cfg(feature = "nightly")]
242246
fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
243247
match value {
244248
PredicateKind::Clause(_) => 0,
@@ -252,7 +256,8 @@ fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
252256
}
253257
}
254258

255-
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for PredicateKind<I>
259+
#[cfg(feature = "nightly")]
260+
impl<CTX: crate::HashStableContext, I: Interner> HashStable<CTX> for PredicateKind<I>
256261
where
257262
I::DefId: HashStable<CTX>,
258263
I::Const: HashStable<CTX>,
@@ -361,7 +366,7 @@ where
361366
}
362367

363368
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
364-
#[derive(HashStable_Generic, Encodable, Decodable)]
369+
#[cfg_attr(feature = "nightly", derive(HashStable_Generic, Encodable, Decodable))]
365370
pub enum AliasRelationDirection {
366371
Equate,
367372
Subtype,

‎compiler/rustc_type_ir/src/region_kind.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use rustc_data_structures::stable_hasher::HashStable;
2-
use rustc_data_structures::stable_hasher::StableHasher;
1+
#[cfg(feature = "nightly")]
2+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33
use std::fmt;
44

5-
use crate::{DebruijnIndex, DebugWithInfcx, HashStableContext, InferCtxtLike, Interner, WithInfcx};
5+
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, Interner, WithInfcx};
66

77
use self::RegionKind::*;
88

@@ -121,7 +121,7 @@ use self::RegionKind::*;
121121
Ord = "feature_allow_slow_enum",
122122
Hash(bound = "")
123123
)]
124-
#[derive(TyEncodable, TyDecodable)]
124+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable))]
125125
pub enum RegionKind<I: Interner> {
126126
/// A region parameter; for example `'a` in `impl<'a> Trait for &'a ()`.
127127
///
@@ -261,8 +261,9 @@ impl<I: Interner> fmt::Debug for RegionKind<I> {
261261
}
262262
}
263263

264+
#[cfg(feature = "nightly")]
264265
// This is not a derived impl because a derive would require `I: HashStable`
265-
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for RegionKind<I>
266+
impl<CTX: crate::HashStableContext, I: Interner> HashStable<CTX> for RegionKind<I>
266267
where
267268
I::EarlyParamRegion: HashStable<CTX>,
268269
I::BoundRegion: HashStable<CTX>,

‎compiler/rustc_type_ir/src/ty_info.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#[cfg(feature = "nightly")]
12
use rustc_data_structures::fingerprint::Fingerprint;
3+
#[cfg(feature = "nightly")]
24
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
35
use std::cmp::Ordering;
46
use std::hash::{Hash, Hasher};
@@ -16,6 +18,8 @@ use crate::{DebruijnIndex, TypeFlags};
1618
#[derive(Copy, Clone)]
1719
pub struct WithCachedTypeInfo<T> {
1820
pub internee: T,
21+
22+
#[cfg(feature = "nightly")]
1923
pub stable_hash: Fingerprint,
2024

2125
/// This field provides fast access to information that is also contained
@@ -81,14 +85,16 @@ impl<T> Deref for WithCachedTypeInfo<T> {
8185
impl<T: Hash> Hash for WithCachedTypeInfo<T> {
8286
#[inline]
8387
fn hash<H: Hasher>(&self, s: &mut H) {
88+
#[cfg(feature = "nightly")]
8489
if self.stable_hash != Fingerprint::ZERO {
85-
self.stable_hash.hash(s)
86-
} else {
87-
self.internee.hash(s)
90+
return self.stable_hash.hash(s);
8891
}
92+
93+
self.internee.hash(s)
8994
}
9095
}
9196

97+
#[cfg(feature = "nightly")]
9298
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for WithCachedTypeInfo<T> {
9399
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
94100
if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) {

‎compiler/rustc_type_ir/src/ty_kind.rs

+34-21
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#![allow(rustc::usage_of_ty_tykind)]
22

3+
#[cfg(feature = "nightly")]
34
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5+
#[cfg(feature = "nightly")]
46
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
57
use std::fmt;
6-
use std::mem::discriminant;
78

8-
use crate::HashStableContext;
99
use crate::Interner;
1010
use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, WithInfcx};
1111

1212
use self::TyKind::*;
1313

1414
/// The movability of a coroutine / closure literal:
1515
/// whether a coroutine contains self-references, causing it to be `!Unpin`.
16-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable, Debug, Copy)]
17-
#[derive(HashStable_Generic)]
16+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
17+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
1818
pub enum Movability {
1919
/// May contain self-references, `!Unpin`.
2020
Static,
@@ -23,7 +23,7 @@ pub enum Movability {
2323
}
2424

2525
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
26-
#[derive(HashStable_Generic, Encodable, Decodable)]
26+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
2727
pub enum Mutability {
2828
// N.B. Order is deliberate, so that Not < Mut
2929
Not,
@@ -75,7 +75,7 @@ impl Mutability {
7575

7676
/// Specifies how a trait object is represented.
7777
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
78-
#[derive(Encodable, Decodable, HashStable_Generic)]
78+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
7979
pub enum DynKind {
8080
/// An unsized `dyn Trait` object
8181
Dyn,
@@ -89,7 +89,7 @@ pub enum DynKind {
8989
}
9090

9191
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
92-
#[derive(Encodable, Decodable, HashStable_Generic)]
92+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
9393
pub enum AliasKind {
9494
/// A projection `<Type as Trait>::AssocType`.
9595
/// Can get normalized away if monomorphic enough.
@@ -109,7 +109,7 @@ pub enum AliasKind {
109109
///
110110
/// Types written by the user start out as `hir::TyKind` and get
111111
/// converted to this representation using `AstConv::ast_ty_to_ty`.
112-
#[rustc_diagnostic_item = "IrTyKind"]
112+
#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "IrTyKind")]
113113
#[derive(derivative::Derivative)]
114114
#[derivative(
115115
Clone(bound = ""),
@@ -119,7 +119,7 @@ pub enum AliasKind {
119119
Ord = "feature_allow_slow_enum",
120120
Hash(bound = "")
121121
)]
122-
#[derive(TyEncodable, TyDecodable)]
122+
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable))]
123123
pub enum TyKind<I: Interner> {
124124
/// The primitive boolean type. Written as `bool`.
125125
Bool,
@@ -407,7 +407,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
407407

408408
write!(f, ">")
409409
}
410-
Foreign(d) => f.debug_tuple_field1_finish("Foreign", d),
410+
Foreign(d) => f.debug_tuple("Foreign").field(d).finish(),
411411
Str => write!(f, "str"),
412412
Array(t, c) => write!(f, "[{:?}; {:?}]", &this.wrap(t), &this.wrap(c)),
413413
Slice(t) => write!(f, "[{:?}]", &this.wrap(t)),
@@ -423,18 +423,20 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
423423
Mutability::Mut => write!(f, "&{:?} mut {:?}", &this.wrap(r), &this.wrap(t)),
424424
Mutability::Not => write!(f, "&{:?} {:?}", &this.wrap(r), &this.wrap(t)),
425425
},
426-
FnDef(d, s) => f.debug_tuple_field2_finish("FnDef", d, &this.wrap(s)),
426+
FnDef(d, s) => f.debug_tuple("FnDef").field(d).field(&this.wrap(s)).finish(),
427427
FnPtr(s) => write!(f, "{:?}", &this.wrap(s)),
428428
Dynamic(p, r, repr) => match repr {
429429
DynKind::Dyn => write!(f, "dyn {:?} + {:?}", &this.wrap(p), &this.wrap(r)),
430430
DynKind::DynStar => {
431431
write!(f, "dyn* {:?} + {:?}", &this.wrap(p), &this.wrap(r))
432432
}
433433
},
434-
Closure(d, s) => f.debug_tuple_field2_finish("Closure", d, &this.wrap(s)),
435-
Coroutine(d, s, m) => f.debug_tuple_field3_finish("Coroutine", d, &this.wrap(s), m),
434+
Closure(d, s) => f.debug_tuple("Closure").field(d).field(&this.wrap(s)).finish(),
435+
Coroutine(d, s, m) => {
436+
f.debug_tuple("Coroutine").field(d).field(&this.wrap(s)).field(m).finish()
437+
}
436438
CoroutineWitness(d, s) => {
437-
f.debug_tuple_field2_finish("CoroutineWitness", d, &this.wrap(s))
439+
f.debug_tuple("CoroutineWitness").field(d).field(&this.wrap(s)).finish()
438440
}
439441
Never => write!(f, "!"),
440442
Tuple(t) => {
@@ -453,7 +455,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
453455
}
454456
write!(f, ")")
455457
}
456-
Alias(i, a) => f.debug_tuple_field2_finish("Alias", i, &this.wrap(a)),
458+
Alias(i, a) => f.debug_tuple("Alias").field(i).field(&this.wrap(a)).finish(),
457459
Param(p) => write!(f, "{p:?}"),
458460
Bound(d, b) => crate::debug_bound_var(f, *d, b),
459461
Placeholder(p) => write!(f, "{p:?}"),
@@ -471,8 +473,9 @@ impl<I: Interner> fmt::Debug for TyKind<I> {
471473
}
472474

473475
// This is not a derived impl because a derive would require `I: HashStable`
476+
#[cfg(feature = "nightly")]
474477
#[allow(rustc::usage_of_ty_tykind)]
475-
impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for TyKind<I>
478+
impl<CTX: crate::HashStableContext, I: Interner> HashStable<CTX> for TyKind<I>
476479
where
477480
I::AdtDef: HashStable<CTX>,
478481
I::DefId: HashStable<CTX>,
@@ -583,7 +586,7 @@ where
583586
}
584587

585588
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
586-
#[derive(Encodable, Decodable, HashStable_Generic)]
589+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
587590
pub enum IntTy {
588591
Isize,
589592
I8,
@@ -641,7 +644,7 @@ impl IntTy {
641644
}
642645

643646
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
644-
#[derive(Encodable, Decodable, HashStable_Generic)]
647+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
645648
pub enum UintTy {
646649
Usize,
647650
U8,
@@ -699,7 +702,7 @@ impl UintTy {
699702
}
700703

701704
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
702-
#[derive(Encodable, Decodable, HashStable_Generic)]
705+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
703706
pub enum FloatTy {
704707
F32,
705708
F64,
@@ -733,18 +736,21 @@ pub struct FloatVarValue(pub FloatTy);
733736
rustc_index::newtype_index! {
734737
/// A **ty**pe **v**ariable **ID**.
735738
#[debug_format = "?{}t"]
739+
#[gate_rustc_only]
736740
pub struct TyVid {}
737741
}
738742

739743
rustc_index::newtype_index! {
740744
/// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
741745
#[debug_format = "?{}i"]
746+
#[gate_rustc_only]
742747
pub struct IntVid {}
743748
}
744749

745750
rustc_index::newtype_index! {
746751
/// A **float**ing-point (`f32` or `f64`) type **v**ariable **ID**.
747752
#[debug_format = "?{}f"]
753+
#[gate_rustc_only]
748754
pub struct FloatVid {}
749755
}
750756

@@ -753,7 +759,8 @@ rustc_index::newtype_index! {
753759
/// E.g., if we have an empty array (`[]`), then we create a fresh
754760
/// type variable for the element type since we won't know until it's
755761
/// used what the element type is supposed to be.
756-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
762+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
763+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable))]
757764
pub enum InferTy {
758765
/// A type variable.
759766
TyVar(TyVid),
@@ -786,6 +793,7 @@ pub enum InferTy {
786793

787794
/// Raw `TyVid` are used as the unification key for `sub_relations`;
788795
/// they carry no values.
796+
#[cfg(feature = "nightly")]
789797
impl UnifyKey for TyVid {
790798
type Value = ();
791799
#[inline]
@@ -801,8 +809,10 @@ impl UnifyKey for TyVid {
801809
}
802810
}
803811

812+
#[cfg(feature = "nightly")]
804813
impl EqUnifyValue for IntVarValue {}
805814

815+
#[cfg(feature = "nightly")]
806816
impl UnifyKey for IntVid {
807817
type Value = Option<IntVarValue>;
808818
#[inline] // make this function eligible for inlining - it is quite hot.
@@ -818,8 +828,10 @@ impl UnifyKey for IntVid {
818828
}
819829
}
820830

831+
#[cfg(feature = "nightly")]
821832
impl EqUnifyValue for FloatVarValue {}
822833

834+
#[cfg(feature = "nightly")]
823835
impl UnifyKey for FloatVid {
824836
type Value = Option<FloatVarValue>;
825837
#[inline]
@@ -835,10 +847,11 @@ impl UnifyKey for FloatVid {
835847
}
836848
}
837849

850+
#[cfg(feature = "nightly")]
838851
impl<CTX> HashStable<CTX> for InferTy {
839852
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
840853
use InferTy::*;
841-
discriminant(self).hash_stable(ctx, hasher);
854+
std::mem::discriminant(self).hash_stable(ctx, hasher);
842855
match self {
843856
TyVar(_) | IntVar(_) | FloatVar(_) => {
844857
panic!("type variables should not be hashed: {self:?}")

‎compiler/rustc_type_ir/src/visit.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
//! - u.visit_with(visitor)
4242
//! ```
4343
44-
use rustc_data_structures::sync::Lrc;
4544
use rustc_index::{Idx, IndexVec};
4645
use std::fmt;
4746
use std::ops::ControlFlow;
4847

4948
use crate::Interner;
49+
use crate::Lrc;
5050

5151
/// This trait is implemented for every type that can be visited,
5252
/// providing the skeleton of the traversal.
@@ -82,8 +82,12 @@ pub trait TypeSuperVisitable<I: Interner>: TypeVisitable<I> {
8282
/// method defined for every type of interest. Each such method has a default
8383
/// that recurses into the type's fields in a non-custom fashion.
8484
pub trait TypeVisitor<I: Interner>: Sized {
85+
#[cfg(feature = "nightly")]
8586
type BreakTy = !;
8687

88+
#[cfg(not(feature = "nightly"))]
89+
type BreakTy;
90+
8791
fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &I::Binder<T>) -> ControlFlow<Self::BreakTy>
8892
where
8993
I::Binder<T>: TypeSuperVisitable<I>,

0 commit comments

Comments
 (0)
Please sign in to comment.