Skip to content

Commit d801458

Browse files
committed
Auto merge of #66384 - cjgillot:typefoldable, r=Zoxc
Derive TypeFoldable using a proc-macro A new proc macro is added in librustc_macros. It is used to derive TypeFoldable inside librustc and librustc_traits. For now, the macro uses the `'tcx` lifetime implicitly, and does not allow for a more robust selection of the adequate lifetime. The Clone-based TypeFoldable implementations are not migrated. Closes #65674
2 parents 4b6cef1 + cb46c35 commit d801458

34 files changed

+143
-758
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3866,6 +3866,7 @@ dependencies = [
38663866
"log",
38673867
"rustc",
38683868
"rustc_data_structures",
3869+
"rustc_macros",
38693870
"rustc_target",
38703871
"smallvec 1.0.0",
38713872
"syntax",

src/librustc/infer/canonical/mod.rs

+6-30
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ mod substitute;
4343
/// A "canonicalized" type `V` is one where all free inference
4444
/// variables have been rewritten to "canonical vars". These are
4545
/// numbered starting from 0 in order of first appearance.
46-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcDecodable, RustcEncodable, HashStable)]
46+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcDecodable, RustcEncodable)]
47+
#[derive(HashStable, TypeFoldable)]
4748
pub struct Canonical<'tcx, V> {
4849
pub max_universe: ty::UniverseIndex,
4950
pub variables: CanonicalVarInfos<'tcx>,
@@ -63,7 +64,8 @@ impl<'tcx> UseSpecializedDecodable for CanonicalVarInfos<'tcx> {}
6364
/// vectors with the original values that were replaced by canonical
6465
/// variables. You will need to supply it later to instantiate the
6566
/// canonicalized query response.
66-
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcDecodable, RustcEncodable, HashStable)]
67+
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcDecodable, RustcEncodable)]
68+
#[derive(HashStable, TypeFoldable)]
6769
pub struct CanonicalVarValues<'tcx> {
6870
pub var_values: IndexVec<BoundVar, GenericArg<'tcx>>,
6971
}
@@ -186,15 +188,15 @@ pub enum CanonicalTyVarKind {
186188
/// After we execute a query with a canonicalized key, we get back a
187189
/// `Canonical<QueryResponse<..>>`. You can use
188190
/// `instantiate_query_result` to access the data in this result.
189-
#[derive(Clone, Debug, HashStable)]
191+
#[derive(Clone, Debug, HashStable, TypeFoldable)]
190192
pub struct QueryResponse<'tcx, R> {
191193
pub var_values: CanonicalVarValues<'tcx>,
192194
pub region_constraints: QueryRegionConstraints<'tcx>,
193195
pub certainty: Certainty,
194196
pub value: R,
195197
}
196198

197-
#[derive(Clone, Debug, Default, HashStable)]
199+
#[derive(Clone, Debug, Default, HashStable, TypeFoldable)]
198200
pub struct QueryRegionConstraints<'tcx> {
199201
pub outlives: Vec<QueryOutlivesConstraint<'tcx>>,
200202
pub member_constraints: Vec<MemberConstraint<'tcx>>,
@@ -467,14 +469,6 @@ CloneTypeFoldableImpls! {
467469
}
468470
}
469471

470-
BraceStructTypeFoldableImpl! {
471-
impl<'tcx, C> TypeFoldable<'tcx> for Canonical<'tcx, C> {
472-
max_universe,
473-
variables,
474-
value,
475-
} where C: TypeFoldable<'tcx>
476-
}
477-
478472
BraceStructLiftImpl! {
479473
impl<'a, 'tcx, T> Lift<'tcx> for Canonical<'a, T> {
480474
type Lifted = Canonical<'tcx, T::Lifted>;
@@ -534,31 +528,13 @@ BraceStructLiftImpl! {
534528
}
535529
}
536530

537-
BraceStructTypeFoldableImpl! {
538-
impl<'tcx> TypeFoldable<'tcx> for CanonicalVarValues<'tcx> {
539-
var_values,
540-
}
541-
}
542-
543-
BraceStructTypeFoldableImpl! {
544-
impl<'tcx, R> TypeFoldable<'tcx> for QueryResponse<'tcx, R> {
545-
var_values, region_constraints, certainty, value
546-
} where R: TypeFoldable<'tcx>,
547-
}
548-
549531
BraceStructLiftImpl! {
550532
impl<'a, 'tcx, R> Lift<'tcx> for QueryResponse<'a, R> {
551533
type Lifted = QueryResponse<'tcx, R::Lifted>;
552534
var_values, region_constraints, certainty, value
553535
} where R: Lift<'tcx>
554536
}
555537

556-
BraceStructTypeFoldableImpl! {
557-
impl<'tcx> TypeFoldable<'tcx> for QueryRegionConstraints<'tcx> {
558-
outlives, member_constraints
559-
}
560-
}
561-
562538
BraceStructLiftImpl! {
563539
impl<'a, 'tcx> Lift<'tcx> for QueryRegionConstraints<'a> {
564540
type Lifted = QueryRegionConstraints<'tcx>;

src/librustc/infer/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub struct InferCtxt<'a, 'tcx> {
232232
pub type PlaceholderMap<'tcx> = BTreeMap<ty::BoundRegion, ty::Region<'tcx>>;
233233

234234
/// See the `error_reporting` module for more details.
235-
#[derive(Clone, Debug, PartialEq, Eq)]
235+
#[derive(Clone, Debug, PartialEq, Eq, TypeFoldable)]
236236
pub enum ValuePairs<'tcx> {
237237
Types(ExpectedFound<Ty<'tcx>>),
238238
Regions(ExpectedFound<ty::Region<'tcx>>),
@@ -1781,16 +1781,6 @@ impl RegionVariableOrigin {
17811781
}
17821782
}
17831783

1784-
EnumTypeFoldableImpl! {
1785-
impl<'tcx> TypeFoldable<'tcx> for ValuePairs<'tcx> {
1786-
(ValuePairs::Types)(a),
1787-
(ValuePairs::Regions)(a),
1788-
(ValuePairs::Consts)(a),
1789-
(ValuePairs::TraitRefs)(a),
1790-
(ValuePairs::PolyTraitRefs)(a),
1791-
}
1792-
}
1793-
17941784
impl<'tcx> fmt::Debug for RegionObligation<'tcx> {
17951785
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17961786
write!(

src/librustc/infer/region_constraints/mod.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl Constraint<'_> {
151151
/// ```
152152
/// R0 member of [O1..On]
153153
/// ```
154-
#[derive(Debug, Clone, HashStable)]
154+
#[derive(Debug, Clone, HashStable, TypeFoldable)]
155155
pub struct MemberConstraint<'tcx> {
156156
/// The `DefId` of the opaque type causing this constraint: used for error reporting.
157157
pub opaque_type_def_id: DefId,
@@ -169,12 +169,6 @@ pub struct MemberConstraint<'tcx> {
169169
pub choice_regions: Lrc<Vec<Region<'tcx>>>,
170170
}
171171

172-
BraceStructTypeFoldableImpl! {
173-
impl<'tcx> TypeFoldable<'tcx> for MemberConstraint<'tcx> {
174-
opaque_type_def_id, definition_span, hidden_ty, member_region, choice_regions
175-
}
176-
}
177-
178172
BraceStructLiftImpl! {
179173
impl<'a, 'tcx> Lift<'tcx> for MemberConstraint<'a> {
180174
type Lifted = MemberConstraint<'tcx>;
@@ -195,19 +189,12 @@ pub struct Verify<'tcx> {
195189
pub bound: VerifyBound<'tcx>,
196190
}
197191

198-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
192+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TypeFoldable)]
199193
pub enum GenericKind<'tcx> {
200194
Param(ty::ParamTy),
201195
Projection(ty::ProjectionTy<'tcx>),
202196
}
203197

204-
EnumTypeFoldableImpl! {
205-
impl<'tcx> TypeFoldable<'tcx> for GenericKind<'tcx> {
206-
(GenericKind::Param)(a),
207-
(GenericKind::Projection)(a),
208-
}
209-
}
210-
211198
/// Describes the things that some `GenericKind` value `G` is known to
212199
/// outlive. Each variant of `VerifyBound` can be thought of as a
213200
/// function:

src/librustc/macros.rs

-54
Original file line numberDiff line numberDiff line change
@@ -324,60 +324,6 @@ macro_rules! EnumLiftImpl {
324324
};
325325
}
326326

327-
#[macro_export]
328-
macro_rules! BraceStructTypeFoldableImpl {
329-
(impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
330-
$($field:ident),* $(,)?
331-
} $(where $($wc:tt)*)*) => {
332-
impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
333-
$(where $($wc)*)*
334-
{
335-
fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
336-
&self,
337-
folder: &mut V,
338-
) -> Self {
339-
let $s { $($field,)* } = self;
340-
$s { $($field: $crate::ty::fold::TypeFoldable::fold_with($field, folder),)* }
341-
}
342-
343-
fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
344-
&self,
345-
visitor: &mut V,
346-
) -> bool {
347-
let $s { $($field,)* } = self;
348-
false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
349-
}
350-
}
351-
};
352-
}
353-
354-
#[macro_export]
355-
macro_rules! TupleStructTypeFoldableImpl {
356-
(impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {
357-
$($field:ident),* $(,)?
358-
} $(where $($wc:tt)*)*) => {
359-
impl<$($p),*> $crate::ty::fold::TypeFoldable<$tcx> for $s
360-
$(where $($wc)*)*
361-
{
362-
fn super_fold_with<V: $crate::ty::fold::TypeFolder<$tcx>>(
363-
&self,
364-
folder: &mut V,
365-
) -> Self {
366-
let $s($($field,)*)= self;
367-
$s($($crate::ty::fold::TypeFoldable::fold_with($field, folder),)*)
368-
}
369-
370-
fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
371-
&self,
372-
visitor: &mut V,
373-
) -> bool {
374-
let $s($($field,)*) = self;
375-
false $(|| $crate::ty::fold::TypeFoldable::visit_with($field, visitor))*
376-
}
377-
}
378-
};
379-
}
380-
381327
#[macro_export]
382328
macro_rules! EnumTypeFoldableImpl {
383329
(impl<$($p:tt),*> TypeFoldable<$tcx:tt> for $s:path {

0 commit comments

Comments
 (0)