Skip to content

Commit 058e021

Browse files
committed
Use Lift derive proc-macro in librustc_traits.
1 parent 2baefd3 commit 058e021

File tree

4 files changed

+23
-239
lines changed

4 files changed

+23
-239
lines changed

src/librustc_middle/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum ErrorHandled {
2727
TooGeneric,
2828
}
2929

30-
CloneTypeFoldableImpls! {
30+
CloneTypeFoldableAndLiftImpls! {
3131
ErrorHandled,
3232
}
3333

src/librustc_middle/traits/mod.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub type ChalkCanonicalGoal<'tcx> = Canonical<'tcx, ChalkEnvironmentAndGoal<'tcx
3030

3131
pub use self::ImplSource::*;
3232
pub use self::ObligationCauseCode::*;
33-
pub use self::SelectionError::*;
3433

3534
pub use self::chalk::{
3635
ChalkEnvironmentAndGoal, ChalkEnvironmentClause, RustInterner as ChalkRustInterner,
@@ -86,7 +85,7 @@ pub enum Reveal {
8685
///
8786
/// We do not want to intern this as there are a lot of obligation causes which
8887
/// only live for a short period of time.
89-
#[derive(Clone, PartialEq, Eq, Hash)]
88+
#[derive(Clone, PartialEq, Eq, Hash, Lift)]
9089
pub struct ObligationCause<'tcx> {
9190
/// `None` for `ObligationCause::dummy`, `Some` otherwise.
9291
data: Option<Rc<ObligationCauseData<'tcx>>>,
@@ -111,7 +110,7 @@ impl Deref for ObligationCause<'tcx> {
111110
}
112111
}
113112

114-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
113+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
115114
pub struct ObligationCauseData<'tcx> {
116115
pub span: Span,
117116

@@ -169,14 +168,14 @@ impl<'tcx> ObligationCause<'tcx> {
169168
}
170169
}
171170

172-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
171+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
173172
pub struct UnifyReceiverContext<'tcx> {
174173
pub assoc_item: ty::AssocItem,
175174
pub param_env: ty::ParamEnv<'tcx>,
176175
pub substs: SubstsRef<'tcx>,
177176
}
178177

179-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
178+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
180179
pub enum ObligationCauseCode<'tcx> {
181180
/// Not well classified or should be obvious from the span.
182181
MiscObligation,
@@ -343,7 +342,7 @@ impl ObligationCauseCode<'_> {
343342
#[cfg(target_arch = "x86_64")]
344343
static_assert_size!(ObligationCauseCode<'_>, 32);
345344

346-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
345+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
347346
pub struct MatchExpressionArmCause<'tcx> {
348347
pub arm_span: Span,
349348
pub source: hir::MatchSource,
@@ -359,7 +358,7 @@ pub struct IfExpressionCause {
359358
pub semicolon: Option<Span>,
360359
}
361360

362-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
361+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
363362
pub struct DerivedObligationCause<'tcx> {
364363
/// The trait reference of the parent obligation that led to the
365364
/// current obligation. Note that only trait obligations lead to
@@ -371,7 +370,7 @@ pub struct DerivedObligationCause<'tcx> {
371370
pub parent_code: Rc<ObligationCauseCode<'tcx>>,
372371
}
373372

374-
#[derive(Clone, Debug, TypeFoldable)]
373+
#[derive(Clone, Debug, TypeFoldable, Lift)]
375374
pub enum SelectionError<'tcx> {
376375
Unimplemented,
377376
OutputTypeParameterMismatch(
@@ -427,7 +426,7 @@ pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>;
427426
/// ### The type parameter `N`
428427
///
429428
/// See explanation on `ImplSourceUserDefinedData`.
430-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
429+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
431430
pub enum ImplSource<'tcx, N> {
432431
/// ImplSource identifying a particular impl.
433432
ImplSourceUserDefined(ImplSourceUserDefinedData<'tcx, N>),
@@ -558,14 +557,14 @@ impl<'tcx, N> ImplSource<'tcx, N> {
558557
/// is `Obligation`, as one might expect. During codegen, however, this
559558
/// is `()`, because codegen only requires a shallow resolution of an
560559
/// impl, and nested obligations are satisfied later.
561-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
560+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
562561
pub struct ImplSourceUserDefinedData<'tcx, N> {
563562
pub impl_def_id: DefId,
564563
pub substs: SubstsRef<'tcx>,
565564
pub nested: Vec<N>,
566565
}
567566

568-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
567+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
569568
pub struct ImplSourceGeneratorData<'tcx, N> {
570569
pub generator_def_id: DefId,
571570
pub substs: SubstsRef<'tcx>,
@@ -574,7 +573,7 @@ pub struct ImplSourceGeneratorData<'tcx, N> {
574573
pub nested: Vec<N>,
575574
}
576575

577-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
576+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
578577
pub struct ImplSourceClosureData<'tcx, N> {
579578
pub closure_def_id: DefId,
580579
pub substs: SubstsRef<'tcx>,
@@ -583,18 +582,18 @@ pub struct ImplSourceClosureData<'tcx, N> {
583582
pub nested: Vec<N>,
584583
}
585584

586-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
585+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
587586
pub struct ImplSourceAutoImplData<N> {
588587
pub trait_def_id: DefId,
589588
pub nested: Vec<N>,
590589
}
591590

592-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
591+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
593592
pub struct ImplSourceBuiltinData<N> {
594593
pub nested: Vec<N>,
595594
}
596595

597-
#[derive(PartialEq, Eq, Clone, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
596+
#[derive(PartialEq, Eq, Clone, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
598597
pub struct ImplSourceObjectData<'tcx, N> {
599598
/// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`.
600599
pub upcast_trait_ref: ty::PolyTraitRef<'tcx>,
@@ -607,17 +606,17 @@ pub struct ImplSourceObjectData<'tcx, N> {
607606
pub nested: Vec<N>,
608607
}
609608

610-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
609+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
611610
pub struct ImplSourceFnPointerData<'tcx, N> {
612611
pub fn_ty: Ty<'tcx>,
613612
pub nested: Vec<N>,
614613
}
615614

616615
// FIXME(@lcnr): This should be refactored and merged with other builtin vtables.
617-
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
616+
#[derive(Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
618617
pub struct ImplSourceDiscriminantKindData;
619618

620-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
619+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable, Lift)]
621620
pub struct ImplSourceTraitAliasData<'tcx, N> {
622621
pub alias_def_id: DefId,
623622
pub substs: SubstsRef<'tcx>,
+3-220
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::traits;
2-
use crate::ty::{Lift, TyCtxt};
32

43
use std::fmt;
5-
use std::rc::Rc;
64

75
// Structural impls for the structs in `traits`.
86

@@ -107,222 +105,7 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitAliasData<'tcx,
107105
///////////////////////////////////////////////////////////////////////////
108106
// Lift implementations
109107

110-
impl<'a, 'tcx> Lift<'tcx> for traits::SelectionError<'a> {
111-
type Lifted = traits::SelectionError<'tcx>;
112-
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
113-
match *self {
114-
super::Unimplemented => Some(super::Unimplemented),
115-
super::OutputTypeParameterMismatch(a, b, ref err) => {
116-
tcx.lift(&(a, b)).and_then(|(a, b)| {
117-
tcx.lift(err).map(|err| super::OutputTypeParameterMismatch(a, b, err))
118-
})
119-
}
120-
super::TraitNotObjectSafe(def_id) => Some(super::TraitNotObjectSafe(def_id)),
121-
super::ConstEvalFailure(err) => Some(super::ConstEvalFailure(err)),
122-
super::Overflow => Some(super::Overflow),
123-
}
124-
}
125-
}
126-
127-
impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
128-
type Lifted = traits::ObligationCauseCode<'tcx>;
129-
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
130-
match *self {
131-
super::ReturnNoExpression => Some(super::ReturnNoExpression),
132-
super::MiscObligation => Some(super::MiscObligation),
133-
super::SliceOrArrayElem => Some(super::SliceOrArrayElem),
134-
super::TupleElem => Some(super::TupleElem),
135-
super::ProjectionWf(proj) => tcx.lift(&proj).map(super::ProjectionWf),
136-
super::ItemObligation(def_id) => Some(super::ItemObligation(def_id)),
137-
super::BindingObligation(def_id, span) => Some(super::BindingObligation(def_id, span)),
138-
super::ReferenceOutlivesReferent(ty) => {
139-
tcx.lift(&ty).map(super::ReferenceOutlivesReferent)
140-
}
141-
super::ObjectTypeBound(ty, r) => {
142-
tcx.lift(&ty).and_then(|ty| tcx.lift(&r).map(|r| super::ObjectTypeBound(ty, r)))
143-
}
144-
super::ObjectCastObligation(ty) => tcx.lift(&ty).map(super::ObjectCastObligation),
145-
super::Coercion { source, target } => {
146-
Some(super::Coercion { source: tcx.lift(&source)?, target: tcx.lift(&target)? })
147-
}
148-
super::AssignmentLhsSized => Some(super::AssignmentLhsSized),
149-
super::TupleInitializerSized => Some(super::TupleInitializerSized),
150-
super::StructInitializerSized => Some(super::StructInitializerSized),
151-
super::VariableType(id) => Some(super::VariableType(id)),
152-
super::ReturnValue(id) => Some(super::ReturnValue(id)),
153-
super::ReturnType => Some(super::ReturnType),
154-
super::SizedArgumentType(sp) => Some(super::SizedArgumentType(sp)),
155-
super::SizedReturnType => Some(super::SizedReturnType),
156-
super::SizedYieldType => Some(super::SizedYieldType),
157-
super::InlineAsmSized => Some(super::InlineAsmSized),
158-
super::RepeatVec(suggest_flag) => Some(super::RepeatVec(suggest_flag)),
159-
super::FieldSized { adt_kind, span, last } => {
160-
Some(super::FieldSized { adt_kind, span, last })
161-
}
162-
super::ConstSized => Some(super::ConstSized),
163-
super::ConstPatternStructural => Some(super::ConstPatternStructural),
164-
super::SharedStatic => Some(super::SharedStatic),
165-
super::BuiltinDerivedObligation(ref cause) => {
166-
tcx.lift(cause).map(super::BuiltinDerivedObligation)
167-
}
168-
super::ImplDerivedObligation(ref cause) => {
169-
tcx.lift(cause).map(super::ImplDerivedObligation)
170-
}
171-
super::DerivedObligation(ref cause) => tcx.lift(cause).map(super::DerivedObligation),
172-
super::CompareImplConstObligation => Some(super::CompareImplConstObligation),
173-
super::CompareImplMethodObligation {
174-
item_name,
175-
impl_item_def_id,
176-
trait_item_def_id,
177-
} => Some(super::CompareImplMethodObligation {
178-
item_name,
179-
impl_item_def_id,
180-
trait_item_def_id,
181-
}),
182-
super::CompareImplTypeObligation { item_name, impl_item_def_id, trait_item_def_id } => {
183-
Some(super::CompareImplTypeObligation {
184-
item_name,
185-
impl_item_def_id,
186-
trait_item_def_id,
187-
})
188-
}
189-
super::ExprAssignable => Some(super::ExprAssignable),
190-
super::MatchExpressionArm(box super::MatchExpressionArmCause {
191-
arm_span,
192-
source,
193-
ref prior_arms,
194-
last_ty,
195-
scrut_hir_id,
196-
}) => tcx.lift(&last_ty).map(|last_ty| {
197-
super::MatchExpressionArm(box super::MatchExpressionArmCause {
198-
arm_span,
199-
source,
200-
prior_arms: prior_arms.clone(),
201-
last_ty,
202-
scrut_hir_id,
203-
})
204-
}),
205-
super::Pattern { span, root_ty, origin_expr } => {
206-
tcx.lift(&root_ty).map(|root_ty| super::Pattern { span, root_ty, origin_expr })
207-
}
208-
super::IfExpression(box super::IfExpressionCause { then, outer, semicolon }) => {
209-
Some(super::IfExpression(box super::IfExpressionCause { then, outer, semicolon }))
210-
}
211-
super::IfExpressionWithNoElse => Some(super::IfExpressionWithNoElse),
212-
super::MainFunctionType => Some(super::MainFunctionType),
213-
super::StartFunctionType => Some(super::StartFunctionType),
214-
super::IntrinsicType => Some(super::IntrinsicType),
215-
super::MethodReceiver => Some(super::MethodReceiver),
216-
super::UnifyReceiver(ref ctxt) => tcx.lift(ctxt).map(|ctxt| super::UnifyReceiver(ctxt)),
217-
super::BlockTailExpression(id) => Some(super::BlockTailExpression(id)),
218-
super::TrivialBound => Some(super::TrivialBound),
219-
}
220-
}
221-
}
222-
223-
impl<'a, 'tcx> Lift<'tcx> for traits::UnifyReceiverContext<'a> {
224-
type Lifted = traits::UnifyReceiverContext<'tcx>;
225-
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
226-
tcx.lift(&self.param_env).and_then(|param_env| {
227-
tcx.lift(&self.substs).map(|substs| traits::UnifyReceiverContext {
228-
assoc_item: self.assoc_item,
229-
param_env,
230-
substs,
231-
})
232-
})
233-
}
234-
}
235-
236-
impl<'a, 'tcx> Lift<'tcx> for traits::DerivedObligationCause<'a> {
237-
type Lifted = traits::DerivedObligationCause<'tcx>;
238-
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
239-
tcx.lift(&self.parent_trait_ref).and_then(|trait_ref| {
240-
tcx.lift(&*self.parent_code).map(|code| traits::DerivedObligationCause {
241-
parent_trait_ref: trait_ref,
242-
parent_code: Rc::new(code),
243-
})
244-
})
245-
}
246-
}
247-
248-
impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCause<'a> {
249-
type Lifted = traits::ObligationCause<'tcx>;
250-
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
251-
tcx.lift(&self.code).map(|code| traits::ObligationCause::new(self.span, self.body_id, code))
252-
}
253-
}
254-
255-
// For codegen only.
256-
impl<'a, 'tcx> Lift<'tcx> for traits::ImplSource<'a, ()> {
257-
type Lifted = traits::ImplSource<'tcx, ()>;
258-
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
259-
match self.clone() {
260-
traits::ImplSourceUserDefined(traits::ImplSourceUserDefinedData {
261-
impl_def_id,
262-
substs,
263-
nested,
264-
}) => tcx.lift(&substs).map(|substs| {
265-
traits::ImplSourceUserDefined(traits::ImplSourceUserDefinedData {
266-
impl_def_id,
267-
substs,
268-
nested,
269-
})
270-
}),
271-
traits::ImplSourceAutoImpl(t) => Some(traits::ImplSourceAutoImpl(t)),
272-
traits::ImplSourceGenerator(traits::ImplSourceGeneratorData {
273-
generator_def_id,
274-
substs,
275-
nested,
276-
}) => tcx.lift(&substs).map(|substs| {
277-
traits::ImplSourceGenerator(traits::ImplSourceGeneratorData {
278-
generator_def_id,
279-
substs,
280-
nested,
281-
})
282-
}),
283-
traits::ImplSourceClosure(traits::ImplSourceClosureData {
284-
closure_def_id,
285-
substs,
286-
nested,
287-
}) => tcx.lift(&substs).map(|substs| {
288-
traits::ImplSourceClosure(traits::ImplSourceClosureData {
289-
closure_def_id,
290-
substs,
291-
nested,
292-
})
293-
}),
294-
traits::ImplSourceFnPointer(traits::ImplSourceFnPointerData { fn_ty, nested }) => {
295-
tcx.lift(&fn_ty).map(|fn_ty| {
296-
traits::ImplSourceFnPointer(traits::ImplSourceFnPointerData { fn_ty, nested })
297-
})
298-
}
299-
traits::ImplSourceDiscriminantKind(traits::ImplSourceDiscriminantKindData) => {
300-
Some(traits::ImplSourceDiscriminantKind(traits::ImplSourceDiscriminantKindData))
301-
}
302-
traits::ImplSourceParam(n) => Some(traits::ImplSourceParam(n)),
303-
traits::ImplSourceBuiltin(n) => Some(traits::ImplSourceBuiltin(n)),
304-
traits::ImplSourceObject(traits::ImplSourceObjectData {
305-
upcast_trait_ref,
306-
vtable_base,
307-
nested,
308-
}) => tcx.lift(&upcast_trait_ref).map(|trait_ref| {
309-
traits::ImplSourceObject(traits::ImplSourceObjectData {
310-
upcast_trait_ref: trait_ref,
311-
vtable_base,
312-
nested,
313-
})
314-
}),
315-
traits::ImplSourceTraitAlias(traits::ImplSourceTraitAliasData {
316-
alias_def_id,
317-
substs,
318-
nested,
319-
}) => tcx.lift(&substs).map(|substs| {
320-
traits::ImplSourceTraitAlias(traits::ImplSourceTraitAliasData {
321-
alias_def_id,
322-
substs,
323-
nested,
324-
})
325-
}),
326-
}
327-
}
108+
CloneTypeFoldableAndLiftImpls! {
109+
super::IfExpressionCause,
110+
super::ImplSourceDiscriminantKindData,
328111
}

0 commit comments

Comments
 (0)