|  | 
| 1 | 1 | use rustc_hir as hir; | 
| 2 |  | -use rustc_hir::def_id::DefId; | 
| 3 |  | -use rustc_infer::infer::at::ToTrace; | 
| 4 | 2 | use rustc_infer::infer::canonical::{Canonical, QueryResponse}; | 
| 5 | 3 | use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt}; | 
| 6 | 4 | use rustc_infer::traits::ObligationCauseCode; | 
| @@ -57,122 +55,67 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>( | 
| 57 | 55 |         "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}", | 
| 58 | 56 |         mir_ty, def_id, user_substs | 
| 59 | 57 |     ); | 
| 60 |  | -    let cx = AscribeUserTypeCx { ocx, param_env, span: span.unwrap_or(DUMMY_SP) }; | 
| 61 |  | -    cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?; | 
| 62 |  | -    Ok(()) | 
| 63 |  | -} | 
|  | 58 | +    let span = span.unwrap_or(DUMMY_SP); | 
| 64 | 59 | 
 | 
| 65 |  | -struct AscribeUserTypeCx<'me, 'tcx> { | 
| 66 |  | -    ocx: &'me ObligationCtxt<'me, 'tcx>, | 
| 67 |  | -    param_env: ty::ParamEnv<'tcx>, | 
| 68 |  | -    span: Span, | 
| 69 |  | -} | 
|  | 60 | +    let UserSubsts { user_self_ty, substs } = user_substs; | 
|  | 61 | +    let tcx = ocx.infcx.tcx; | 
|  | 62 | +    let cause = ObligationCause::dummy_with_span(span); | 
| 70 | 63 | 
 | 
| 71 |  | -impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> { | 
| 72 |  | -    fn normalize<T>(&self, value: T) -> T | 
| 73 |  | -    where | 
| 74 |  | -        T: TypeFoldable<'tcx>, | 
| 75 |  | -    { | 
| 76 |  | -        self.normalize_with_cause(value, ObligationCause::misc(self.span, hir::CRATE_HIR_ID)) | 
| 77 |  | -    } | 
|  | 64 | +    let ty = tcx.bound_type_of(def_id).subst(tcx, substs); | 
|  | 65 | +    let ty = ocx.normalize(cause.clone(), param_env, ty); | 
|  | 66 | +    debug!("relate_type_and_user_type: ty of def-id is {:?}", ty); | 
| 78 | 67 | 
 | 
| 79 |  | -    fn normalize_with_cause<T>(&self, value: T, cause: ObligationCause<'tcx>) -> T | 
| 80 |  | -    where | 
| 81 |  | -        T: TypeFoldable<'tcx>, | 
| 82 |  | -    { | 
| 83 |  | -        self.ocx.normalize(cause, self.param_env, value) | 
| 84 |  | -    } | 
|  | 68 | +    ocx.eq(&cause, param_env, mir_ty, ty)?; | 
| 85 | 69 | 
 | 
| 86 |  | -    fn eq<T>(&self, a: T, b: T) -> Result<(), NoSolution> | 
| 87 |  | -    where | 
| 88 |  | -        T: ToTrace<'tcx>, | 
| 89 |  | -    { | 
| 90 |  | -        Ok(self.ocx.eq(&ObligationCause::dummy_with_span(self.span), self.param_env, a, b)?) | 
| 91 |  | -    } | 
|  | 70 | +    // Prove the predicates coming along with `def_id`. | 
|  | 71 | +    // | 
|  | 72 | +    // Also, normalize the `instantiated_predicates` | 
|  | 73 | +    // because otherwise we wind up with duplicate "type | 
|  | 74 | +    // outlives" error messages. | 
|  | 75 | +    let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs); | 
| 92 | 76 | 
 | 
| 93 |  | -    fn prove_predicate(&self, predicate: Predicate<'tcx>, cause: ObligationCause<'tcx>) { | 
| 94 |  | -        self.ocx.register_obligation(Obligation::new( | 
| 95 |  | -            self.ocx.infcx.tcx, | 
| 96 |  | -            cause, | 
| 97 |  | -            self.param_env, | 
| 98 |  | -            predicate, | 
| 99 |  | -        )); | 
| 100 |  | -    } | 
|  | 77 | +    debug!(?instantiated_predicates); | 
|  | 78 | +    for (instantiated_predicate, predicate_span) in | 
|  | 79 | +        zip(instantiated_predicates.predicates, instantiated_predicates.spans) | 
|  | 80 | +    { | 
|  | 81 | +        let span = if span == DUMMY_SP { predicate_span } else { span }; | 
|  | 82 | +        let cause = ObligationCause::new( | 
|  | 83 | +            span, | 
|  | 84 | +            hir::CRATE_HIR_ID, | 
|  | 85 | +            ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span), | 
|  | 86 | +        ); | 
|  | 87 | +        let instantiated_predicate = | 
|  | 88 | +            ocx.normalize(cause.clone(), param_env, instantiated_predicate); | 
| 101 | 89 | 
 | 
| 102 |  | -    fn tcx(&self) -> TyCtxt<'tcx> { | 
| 103 |  | -        self.ocx.infcx.tcx | 
|  | 90 | +        ocx.register_obligation(Obligation::new(tcx, cause, param_env, instantiated_predicate)); | 
| 104 | 91 |     } | 
| 105 | 92 | 
 | 
| 106 |  | -    #[instrument(level = "debug", skip(self))] | 
| 107 |  | -    fn relate_mir_and_user_ty( | 
| 108 |  | -        &self, | 
| 109 |  | -        mir_ty: Ty<'tcx>, | 
| 110 |  | -        def_id: DefId, | 
| 111 |  | -        user_substs: UserSubsts<'tcx>, | 
| 112 |  | -    ) -> Result<(), NoSolution> { | 
| 113 |  | -        let UserSubsts { user_self_ty, substs } = user_substs; | 
| 114 |  | -        let tcx = self.tcx(); | 
| 115 |  | - | 
| 116 |  | -        let ty = tcx.bound_type_of(def_id).subst(tcx, substs); | 
| 117 |  | -        let ty = self.normalize(ty); | 
| 118 |  | -        debug!("relate_type_and_user_type: ty of def-id is {:?}", ty); | 
| 119 |  | - | 
| 120 |  | -        self.eq(mir_ty, ty)?; | 
| 121 |  | - | 
| 122 |  | -        // Prove the predicates coming along with `def_id`. | 
| 123 |  | -        // | 
| 124 |  | -        // Also, normalize the `instantiated_predicates` | 
| 125 |  | -        // because otherwise we wind up with duplicate "type | 
| 126 |  | -        // outlives" error messages. | 
| 127 |  | -        let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs); | 
| 128 |  | - | 
| 129 |  | -        let cause = ObligationCause::dummy_with_span(self.span); | 
| 130 |  | - | 
| 131 |  | -        debug!(?instantiated_predicates); | 
| 132 |  | -        for (instantiated_predicate, predicate_span) in | 
| 133 |  | -            zip(instantiated_predicates.predicates, instantiated_predicates.spans) | 
| 134 |  | -        { | 
| 135 |  | -            let span = if self.span == DUMMY_SP { predicate_span } else { self.span }; | 
| 136 |  | -            let cause = ObligationCause::new( | 
| 137 |  | -                span, | 
| 138 |  | -                hir::CRATE_HIR_ID, | 
| 139 |  | -                ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span), | 
| 140 |  | -            ); | 
| 141 |  | -            let instantiated_predicate = | 
| 142 |  | -                self.normalize_with_cause(instantiated_predicate, cause.clone()); | 
| 143 |  | -            self.prove_predicate(instantiated_predicate, cause); | 
| 144 |  | -        } | 
|  | 93 | +    if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { | 
|  | 94 | +        let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs); | 
|  | 95 | +        let impl_self_ty = ocx.normalize(cause.clone(), param_env, impl_self_ty); | 
| 145 | 96 | 
 | 
| 146 |  | -        if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { | 
| 147 |  | -            let impl_self_ty = tcx.bound_type_of(impl_def_id).subst(tcx, substs); | 
| 148 |  | -            let impl_self_ty = self.normalize(impl_self_ty); | 
|  | 97 | +        ocx.eq(&cause, param_env, self_ty, impl_self_ty)?; | 
| 149 | 98 | 
 | 
| 150 |  | -            self.eq(self_ty, impl_self_ty)?; | 
| 151 |  | - | 
| 152 |  | -            self.prove_predicate( | 
| 153 |  | -                ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())) | 
| 154 |  | -                    .to_predicate(tcx), | 
| 155 |  | -                cause.clone(), | 
| 156 |  | -            ); | 
| 157 |  | -        } | 
| 158 |  | - | 
| 159 |  | -        // In addition to proving the predicates, we have to | 
| 160 |  | -        // prove that `ty` is well-formed -- this is because | 
| 161 |  | -        // the WF of `ty` is predicated on the substs being | 
| 162 |  | -        // well-formed, and we haven't proven *that*. We don't | 
| 163 |  | -        // want to prove the WF of types from  `substs` directly because they | 
| 164 |  | -        // haven't been normalized. | 
| 165 |  | -        // | 
| 166 |  | -        // FIXME(nmatsakis): Well, perhaps we should normalize | 
| 167 |  | -        // them?  This would only be relevant if some input | 
| 168 |  | -        // type were ill-formed but did not appear in `ty`, | 
| 169 |  | -        // which...could happen with normalization... | 
| 170 |  | -        self.prove_predicate( | 
| 171 |  | -            ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(tcx), | 
| 172 |  | -            cause, | 
| 173 |  | -        ); | 
| 174 |  | -        Ok(()) | 
|  | 99 | +        let predicate: Predicate<'tcx> = | 
|  | 100 | +            ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())).to_predicate(tcx); | 
|  | 101 | +        ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate)); | 
| 175 | 102 |     } | 
|  | 103 | + | 
|  | 104 | +    // In addition to proving the predicates, we have to | 
|  | 105 | +    // prove that `ty` is well-formed -- this is because | 
|  | 106 | +    // the WF of `ty` is predicated on the substs being | 
|  | 107 | +    // well-formed, and we haven't proven *that*. We don't | 
|  | 108 | +    // want to prove the WF of types from  `substs` directly because they | 
|  | 109 | +    // haven't been normalized. | 
|  | 110 | +    // | 
|  | 111 | +    // FIXME(nmatsakis): Well, perhaps we should normalize | 
|  | 112 | +    // them?  This would only be relevant if some input | 
|  | 113 | +    // type were ill-formed but did not appear in `ty`, | 
|  | 114 | +    // which...could happen with normalization... | 
|  | 115 | +    let predicate: Predicate<'tcx> = | 
|  | 116 | +        ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into())).to_predicate(tcx); | 
|  | 117 | +    ocx.register_obligation(Obligation::new(tcx, cause, param_env, predicate)); | 
|  | 118 | +    Ok(()) | 
| 176 | 119 | } | 
| 177 | 120 | 
 | 
| 178 | 121 | fn type_op_eq<'tcx>( | 
|  | 
0 commit comments