|
1 | | -use rustc_data_structures::fx::FxHashMap; |
2 | 1 | use rustc_data_structures::vec_map::VecMap; |
3 | 2 | use rustc_hir::def_id::DefId; |
4 | 3 | use rustc_hir::OpaqueTyOrigin; |
5 | 4 | use rustc_infer::infer::InferCtxt; |
6 | | -use rustc_middle::ty::subst::GenericArgKind; |
7 | 5 | use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, TyCtxt, TypeFoldable}; |
8 | | -use rustc_span::Span; |
9 | 6 | use rustc_trait_selection::opaque_types::InferCtxtExt; |
10 | 7 |
|
11 | 8 | use super::RegionInferenceContext; |
@@ -107,21 +104,11 @@ impl<'tcx> RegionInferenceContext<'tcx> { |
107 | 104 |
|
108 | 105 | let opaque_type_key = |
109 | 106 | OpaqueTypeKey { def_id: opaque_type_key.def_id, substs: universal_substs }; |
110 | | - let remapped_type = infcx.infer_opaque_definition_from_instantiation( |
| 107 | + let ty = infcx.infer_opaque_definition_from_instantiation( |
111 | 108 | opaque_type_key, |
112 | 109 | universal_concrete_type, |
113 | 110 | origin, |
114 | 111 | ); |
115 | | - let ty = if check_opaque_type_parameter_valid( |
116 | | - infcx.tcx, |
117 | | - opaque_type_key, |
118 | | - origin, |
119 | | - concrete_type.span, |
120 | | - ) { |
121 | | - remapped_type |
122 | | - } else { |
123 | | - infcx.tcx.ty_error() |
124 | | - }; |
125 | 112 | // Sometimes two opaque types are the same only after we remap the generic parameters |
126 | 113 | // back to the opaque type definition. E.g. we may have `OpaqueType<X, Y>` mapped to `(X, Y)` |
127 | 114 | // and `OpaqueType<Y, X>` mapped to `(Y, X)`, and those are the same, but we only know that |
@@ -184,95 +171,3 @@ impl<'tcx> RegionInferenceContext<'tcx> { |
184 | 171 | }) |
185 | 172 | } |
186 | 173 | } |
187 | | - |
188 | | -fn check_opaque_type_parameter_valid( |
189 | | - tcx: TyCtxt<'_>, |
190 | | - opaque_type_key: OpaqueTypeKey<'_>, |
191 | | - origin: OpaqueTyOrigin, |
192 | | - span: Span, |
193 | | -) -> bool { |
194 | | - match origin { |
195 | | - // No need to check return position impl trait (RPIT) |
196 | | - // because for type and const parameters they are correct |
197 | | - // by construction: we convert |
198 | | - // |
199 | | - // fn foo<P0..Pn>() -> impl Trait |
200 | | - // |
201 | | - // into |
202 | | - // |
203 | | - // type Foo<P0...Pn> |
204 | | - // fn foo<P0..Pn>() -> Foo<P0...Pn>. |
205 | | - // |
206 | | - // For lifetime parameters we convert |
207 | | - // |
208 | | - // fn foo<'l0..'ln>() -> impl Trait<'l0..'lm> |
209 | | - // |
210 | | - // into |
211 | | - // |
212 | | - // type foo::<'p0..'pn>::Foo<'q0..'qm> |
213 | | - // fn foo<l0..'ln>() -> foo::<'static..'static>::Foo<'l0..'lm>. |
214 | | - // |
215 | | - // which would error here on all of the `'static` args. |
216 | | - OpaqueTyOrigin::FnReturn(..) | OpaqueTyOrigin::AsyncFn(..) => return true, |
217 | | - // Check these |
218 | | - OpaqueTyOrigin::TyAlias => {} |
219 | | - } |
220 | | - let opaque_generics = tcx.generics_of(opaque_type_key.def_id); |
221 | | - let mut seen_params: FxHashMap<_, Vec<_>> = FxHashMap::default(); |
222 | | - for (i, arg) in opaque_type_key.substs.iter().enumerate() { |
223 | | - let arg_is_param = match arg.unpack() { |
224 | | - GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)), |
225 | | - GenericArgKind::Lifetime(lt) if lt.is_static() => { |
226 | | - tcx.sess |
227 | | - .struct_span_err(span, "non-defining opaque type use in defining scope") |
228 | | - .span_label( |
229 | | - tcx.def_span(opaque_generics.param_at(i, tcx).def_id), |
230 | | - "cannot use static lifetime; use a bound lifetime \ |
231 | | - instead or remove the lifetime parameter from the \ |
232 | | - opaque type", |
233 | | - ) |
234 | | - .emit(); |
235 | | - return false; |
236 | | - } |
237 | | - GenericArgKind::Lifetime(lt) => { |
238 | | - matches!(*lt, ty::ReEarlyBound(_) | ty::ReFree(_)) |
239 | | - } |
240 | | - GenericArgKind::Const(ct) => matches!(ct.val(), ty::ConstKind::Param(_)), |
241 | | - }; |
242 | | - |
243 | | - if arg_is_param { |
244 | | - seen_params.entry(arg).or_default().push(i); |
245 | | - } else { |
246 | | - // Prevent `fn foo() -> Foo<u32>` from being defining. |
247 | | - let opaque_param = opaque_generics.param_at(i, tcx); |
248 | | - tcx.sess |
249 | | - .struct_span_err(span, "non-defining opaque type use in defining scope") |
250 | | - .span_note( |
251 | | - tcx.def_span(opaque_param.def_id), |
252 | | - &format!( |
253 | | - "used non-generic {} `{}` for generic parameter", |
254 | | - opaque_param.kind.descr(), |
255 | | - arg, |
256 | | - ), |
257 | | - ) |
258 | | - .emit(); |
259 | | - return false; |
260 | | - } |
261 | | - } |
262 | | - |
263 | | - for (_, indices) in seen_params { |
264 | | - if indices.len() > 1 { |
265 | | - let descr = opaque_generics.param_at(indices[0], tcx).kind.descr(); |
266 | | - let spans: Vec<_> = indices |
267 | | - .into_iter() |
268 | | - .map(|i| tcx.def_span(opaque_generics.param_at(i, tcx).def_id)) |
269 | | - .collect(); |
270 | | - tcx.sess |
271 | | - .struct_span_err(span, "non-defining opaque type use in defining scope") |
272 | | - .span_note(spans, &format!("{} used multiple times", descr)) |
273 | | - .emit(); |
274 | | - return false; |
275 | | - } |
276 | | - } |
277 | | - true |
278 | | -} |
0 commit comments