Skip to content

Commit 4f3da90

Browse files
committed
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

Diff for: 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"

Diff for: 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

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
@@ -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
}

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
@@ -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)] =>

Diff for: 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::*;

Diff for: 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+
]

Diff for: 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
{

Diff for: 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>,

0 commit comments

Comments
 (0)