@@ -63,13 +63,16 @@ fn relate_mir_and_user_ty<'tcx>(
63
63
user_ty : Ty < ' tcx > ,
64
64
) -> Result < ( ) , NoSolution > {
65
65
let cause = ObligationCause :: dummy_with_span ( span) ;
66
+ ocx. register_obligation ( Obligation :: new (
67
+ ocx. infcx . tcx ,
68
+ cause. clone ( ) ,
69
+ param_env,
70
+ ty:: ClauseKind :: WellFormed ( user_ty. into ( ) ) ,
71
+ ) ) ;
72
+
66
73
let user_ty = ocx. normalize ( & cause, param_env, user_ty) ;
67
74
ocx. eq ( & cause, param_env, mir_ty, user_ty) ?;
68
75
69
- // FIXME(#104764): We should check well-formedness before normalization.
70
- let predicate =
71
- ty:: Binder :: dummy ( ty:: PredicateKind :: Clause ( ty:: ClauseKind :: WellFormed ( user_ty. into ( ) ) ) ) ;
72
- ocx. register_obligation ( Obligation :: new ( ocx. infcx . tcx , cause, param_env, predicate) ) ;
73
76
Ok ( ( ) )
74
77
}
75
78
@@ -113,31 +116,38 @@ fn relate_mir_and_user_args<'tcx>(
113
116
ocx. register_obligation ( Obligation :: new ( tcx, cause, param_env, instantiated_predicate) ) ;
114
117
}
115
118
119
+ // Now prove the well-formedness of `def_id` with `substs`.
120
+ // Note for some items, proving the WF of `ty` is not sufficient because the
121
+ // well-formedness of an item may depend on the WF of gneneric args not present in the
122
+ // item's type. Currently this is true for associated consts, e.g.:
123
+ // ```rust
124
+ // impl<T> MyTy<T> {
125
+ // const CONST: () = { /* arbitrary code that depends on T being WF */ };
126
+ // }
127
+ // ```
128
+ for arg in args {
129
+ ocx. register_obligation ( Obligation :: new (
130
+ tcx,
131
+ cause. clone ( ) ,
132
+ param_env,
133
+ ty:: ClauseKind :: WellFormed ( arg) ,
134
+ ) ) ;
135
+ }
136
+
116
137
if let Some ( UserSelfTy { impl_def_id, self_ty } ) = user_self_ty {
138
+ ocx. register_obligation ( Obligation :: new (
139
+ tcx,
140
+ cause. clone ( ) ,
141
+ param_env,
142
+ ty:: ClauseKind :: WellFormed ( self_ty. into ( ) ) ,
143
+ ) ) ;
144
+
117
145
let self_ty = ocx. normalize ( & cause, param_env, self_ty) ;
118
146
let impl_self_ty = tcx. type_of ( impl_def_id) . instantiate ( tcx, args) ;
119
147
let impl_self_ty = ocx. normalize ( & cause, param_env, impl_self_ty) ;
120
148
121
149
ocx. eq ( & cause, param_env, self_ty, impl_self_ty) ?;
122
- let predicate = ty:: Binder :: dummy ( ty:: PredicateKind :: Clause ( ty:: ClauseKind :: WellFormed (
123
- impl_self_ty. into ( ) ,
124
- ) ) ) ;
125
- ocx. register_obligation ( Obligation :: new ( tcx, cause. clone ( ) , param_env, predicate) ) ;
126
150
}
127
151
128
- // In addition to proving the predicates, we have to
129
- // prove that `ty` is well-formed -- this is because
130
- // the WF of `ty` is predicated on the args being
131
- // well-formed, and we haven't proven *that*. We don't
132
- // want to prove the WF of types from `args` directly because they
133
- // haven't been normalized.
134
- //
135
- // FIXME(nmatsakis): Well, perhaps we should normalize
136
- // them? This would only be relevant if some input
137
- // type were ill-formed but did not appear in `ty`,
138
- // which...could happen with normalization...
139
- let predicate =
140
- ty:: Binder :: dummy ( ty:: PredicateKind :: Clause ( ty:: ClauseKind :: WellFormed ( ty. into ( ) ) ) ) ;
141
- ocx. register_obligation ( Obligation :: new ( tcx, cause, param_env, predicate) ) ;
142
152
Ok ( ( ) )
143
153
}
0 commit comments