]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_infer/src/infer/opaque_types.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / compiler / rustc_infer / src / infer / opaque_types.rs
1 use crate::infer::{InferCtxt, InferOk};
2 use crate::traits;
3 use hir::def_id::{DefId, LocalDefId};
4 use hir::{HirId, OpaqueTyOrigin};
5 use rustc_data_structures::sync::Lrc;
6 use rustc_data_structures::vec_map::VecMap;
7 use rustc_hir as hir;
8 use rustc_middle::traits::ObligationCause;
9 use rustc_middle::ty::fold::BottomUpFolder;
10 use rustc_middle::ty::subst::{GenericArgKind, Subst};
11 use rustc_middle::ty::{
12 self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitor,
13 };
14 use rustc_span::Span;
15
16 use std::ops::ControlFlow;
17
18 pub type OpaqueTypeMap<'tcx> = VecMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>;
19
20 mod table;
21
22 pub use table::{OpaqueTypeStorage, OpaqueTypeTable};
23
24 use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
25 use super::InferResult;
26
27 /// Information about the opaque types whose values we
28 /// are inferring in this function (these are the `impl Trait` that
29 /// appear in the return type).
30 #[derive(Clone, Debug)]
31 pub struct OpaqueTypeDecl<'tcx> {
32 /// The hidden types that have been inferred for this opaque type.
33 /// There can be multiple, but they are all `lub`ed together at the end
34 /// to obtain the canonical hidden type.
35 pub hidden_type: OpaqueHiddenType<'tcx>,
36
37 /// The origin of the opaque type.
38 pub origin: hir::OpaqueTyOrigin,
39 }
40
41 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
42 /// This is a backwards compatibility hack to prevent breaking changes from
43 /// lazy TAIT around RPIT handling.
44 pub fn replace_opaque_types_with_inference_vars<T: TypeFoldable<'tcx>>(
45 &self,
46 value: T,
47 body_id: HirId,
48 span: Span,
49 param_env: ty::ParamEnv<'tcx>,
50 ) -> InferOk<'tcx, T> {
51 if !value.has_opaque_types() {
52 return InferOk { value, obligations: vec![] };
53 }
54 let mut obligations = vec![];
55 let value = value.fold_with(&mut ty::fold::BottomUpFolder {
56 tcx: self.tcx,
57 lt_op: |lt| lt,
58 ct_op: |ct| ct,
59 ty_op: |ty| match *ty.kind() {
60 // Closures can't create hidden types for opaque types of their parent, as they
61 // do not have all the outlives information available. Also `type_of` looks for
62 // hidden types in the owner (so the closure's parent), so it would not find these
63 // definitions.
64 ty::Opaque(def_id, _substs)
65 if matches!(
66 self.opaque_type_origin(def_id, span),
67 Some(OpaqueTyOrigin::FnReturn(..))
68 ) =>
69 {
70 let span = if span.is_dummy() { self.tcx.def_span(def_id) } else { span };
71 let cause = ObligationCause::misc(span, body_id);
72 let ty_var = self.next_ty_var(TypeVariableOrigin {
73 kind: TypeVariableOriginKind::TypeInference,
74 span: cause.span,
75 });
76 obligations.extend(
77 self.handle_opaque_type(ty, ty_var, true, &cause, param_env)
78 .unwrap()
79 .obligations,
80 );
81 ty_var
82 }
83 _ => ty,
84 },
85 });
86 InferOk { value, obligations }
87 }
88
89 pub fn handle_opaque_type(
90 &self,
91 a: Ty<'tcx>,
92 b: Ty<'tcx>,
93 a_is_expected: bool,
94 cause: &ObligationCause<'tcx>,
95 param_env: ty::ParamEnv<'tcx>,
96 ) -> InferResult<'tcx, ()> {
97 if a.references_error() || b.references_error() {
98 return Ok(InferOk { value: (), obligations: vec![] });
99 }
100 let (a, b) = if a_is_expected { (a, b) } else { (b, a) };
101 let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() {
102 ty::Opaque(def_id, substs) => {
103 let origin = if self.defining_use_anchor.is_some() {
104 // Check that this is `impl Trait` type is
105 // declared by `parent_def_id` -- i.e., one whose
106 // value we are inferring. At present, this is
107 // always true during the first phase of
108 // type-check, but not always true later on during
109 // NLL. Once we support named opaque types more fully,
110 // this same scenario will be able to arise during all phases.
111 //
112 // Here is an example using type alias `impl Trait`
113 // that indicates the distinction we are checking for:
114 //
115 // ```rust
116 // mod a {
117 // pub type Foo = impl Iterator;
118 // pub fn make_foo() -> Foo { .. }
119 // }
120 //
121 // mod b {
122 // fn foo() -> a::Foo { a::make_foo() }
123 // }
124 // ```
125 //
126 // Here, the return type of `foo` references an
127 // `Opaque` indeed, but not one whose value is
128 // presently being inferred. You can get into a
129 // similar situation with closure return types
130 // today:
131 //
132 // ```rust
133 // fn foo() -> impl Iterator { .. }
134 // fn bar() {
135 // let x = || foo(); // returns the Opaque assoc with `foo`
136 // }
137 // ```
138 self.opaque_type_origin(def_id, cause.span)?
139 } else {
140 self.opaque_ty_origin_unchecked(def_id, cause.span)
141 };
142 if let ty::Opaque(did2, _) = *b.kind() {
143 // We could accept this, but there are various ways to handle this situation, and we don't
144 // want to make a decision on it right now. Likely this case is so super rare anyway, that
145 // no one encounters it in practice.
146 // It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
147 // where it is of no concern, so we only check for TAITs.
148 if let Some(OpaqueTyOrigin::TyAlias) = self.opaque_type_origin(did2, cause.span)
149 {
150 self.tcx
151 .sess
152 .struct_span_err(
153 cause.span,
154 "opaque type's hidden type cannot be another opaque type from the same scope",
155 )
156 .span_label(cause.span, "one of the two opaque types used here has to be outside its defining scope")
157 .span_note(
158 self.tcx.def_span(def_id),
159 "opaque type whose hidden type is being assigned",
160 )
161 .span_note(
162 self.tcx.def_span(did2),
163 "opaque type being used as hidden type",
164 )
165 .emit();
166 }
167 }
168 Some(self.register_hidden_type(
169 OpaqueTypeKey { def_id, substs },
170 cause.clone(),
171 param_env,
172 b,
173 origin,
174 ))
175 }
176 _ => None,
177 };
178 if let Some(res) = process(a, b) {
179 res
180 } else if let Some(res) = process(b, a) {
181 res
182 } else {
183 // Rerun equality check, but this time error out due to
184 // different types.
185 match self.at(cause, param_env).define_opaque_types(false).eq(a, b) {
186 Ok(_) => span_bug!(
187 cause.span,
188 "opaque types are never equal to anything but themselves: {:#?}",
189 (a.kind(), b.kind())
190 ),
191 Err(e) => Err(e),
192 }
193 }
194 }
195
196 /// Given the map `opaque_types` containing the opaque
197 /// `impl Trait` types whose underlying, hidden types are being
198 /// inferred, this method adds constraints to the regions
199 /// appearing in those underlying hidden types to ensure that they
200 /// at least do not refer to random scopes within the current
201 /// function. These constraints are not (quite) sufficient to
202 /// guarantee that the regions are actually legal values; that
203 /// final condition is imposed after region inference is done.
204 ///
205 /// # The Problem
206 ///
207 /// Let's work through an example to explain how it works. Assume
208 /// the current function is as follows:
209 ///
210 /// ```text
211 /// fn foo<'a, 'b>(..) -> (impl Bar<'a>, impl Bar<'b>)
212 /// ```
213 ///
214 /// Here, we have two `impl Trait` types whose values are being
215 /// inferred (the `impl Bar<'a>` and the `impl
216 /// Bar<'b>`). Conceptually, this is sugar for a setup where we
217 /// define underlying opaque types (`Foo1`, `Foo2`) and then, in
218 /// the return type of `foo`, we *reference* those definitions:
219 ///
220 /// ```text
221 /// type Foo1<'x> = impl Bar<'x>;
222 /// type Foo2<'x> = impl Bar<'x>;
223 /// fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
224 /// // ^^^^ ^^
225 /// // | |
226 /// // | substs
227 /// // def_id
228 /// ```
229 ///
230 /// As indicating in the comments above, each of those references
231 /// is (in the compiler) basically a substitution (`substs`)
232 /// applied to the type of a suitable `def_id` (which identifies
233 /// `Foo1` or `Foo2`).
234 ///
235 /// Now, at this point in compilation, what we have done is to
236 /// replace each of the references (`Foo1<'a>`, `Foo2<'b>`) with
237 /// fresh inference variables C1 and C2. We wish to use the values
238 /// of these variables to infer the underlying types of `Foo1` and
239 /// `Foo2`. That is, this gives rise to higher-order (pattern) unification
240 /// constraints like:
241 ///
242 /// ```text
243 /// for<'a> (Foo1<'a> = C1)
244 /// for<'b> (Foo1<'b> = C2)
245 /// ```
246 ///
247 /// For these equation to be satisfiable, the types `C1` and `C2`
248 /// can only refer to a limited set of regions. For example, `C1`
249 /// can only refer to `'static` and `'a`, and `C2` can only refer
250 /// to `'static` and `'b`. The job of this function is to impose that
251 /// constraint.
252 ///
253 /// Up to this point, C1 and C2 are basically just random type
254 /// inference variables, and hence they may contain arbitrary
255 /// regions. In fact, it is fairly likely that they do! Consider
256 /// this possible definition of `foo`:
257 ///
258 /// ```text
259 /// fn foo<'a, 'b>(x: &'a i32, y: &'b i32) -> (impl Bar<'a>, impl Bar<'b>) {
260 /// (&*x, &*y)
261 /// }
262 /// ```
263 ///
264 /// Here, the values for the concrete types of the two impl
265 /// traits will include inference variables:
266 ///
267 /// ```text
268 /// &'0 i32
269 /// &'1 i32
270 /// ```
271 ///
272 /// Ordinarily, the subtyping rules would ensure that these are
273 /// sufficiently large. But since `impl Bar<'a>` isn't a specific
274 /// type per se, we don't get such constraints by default. This
275 /// is where this function comes into play. It adds extra
276 /// constraints to ensure that all the regions which appear in the
277 /// inferred type are regions that could validly appear.
278 ///
279 /// This is actually a bit of a tricky constraint in general. We
280 /// want to say that each variable (e.g., `'0`) can only take on
281 /// values that were supplied as arguments to the opaque type
282 /// (e.g., `'a` for `Foo1<'a>`) or `'static`, which is always in
283 /// scope. We don't have a constraint quite of this kind in the current
284 /// region checker.
285 ///
286 /// # The Solution
287 ///
288 /// We generally prefer to make `<=` constraints, since they
289 /// integrate best into the region solver. To do that, we find the
290 /// "minimum" of all the arguments that appear in the substs: that
291 /// is, some region which is less than all the others. In the case
292 /// of `Foo1<'a>`, that would be `'a` (it's the only choice, after
293 /// all). Then we apply that as a least bound to the variables
294 /// (e.g., `'a <= '0`).
295 ///
296 /// In some cases, there is no minimum. Consider this example:
297 ///
298 /// ```text
299 /// fn baz<'a, 'b>() -> impl Trait<'a, 'b> { ... }
300 /// ```
301 ///
302 /// Here we would report a more complex "in constraint", like `'r
303 /// in ['a, 'b, 'static]` (where `'r` is some region appearing in
304 /// the hidden type).
305 ///
306 /// # Constrain regions, not the hidden concrete type
307 ///
308 /// Note that generating constraints on each region `Rc` is *not*
309 /// the same as generating an outlives constraint on `Tc` itself.
310 /// For example, if we had a function like this:
311 ///
312 /// ```rust
313 /// fn foo<'a, T>(x: &'a u32, y: T) -> impl Foo<'a> {
314 /// (x, y)
315 /// }
316 ///
317 /// // Equivalent to:
318 /// type FooReturn<'a, T> = impl Foo<'a>;
319 /// fn foo<'a, T>(..) -> FooReturn<'a, T> { .. }
320 /// ```
321 ///
322 /// then the hidden type `Tc` would be `(&'0 u32, T)` (where `'0`
323 /// is an inference variable). If we generated a constraint that
324 /// `Tc: 'a`, then this would incorrectly require that `T: 'a` --
325 /// but this is not necessary, because the opaque type we
326 /// create will be allowed to reference `T`. So we only generate a
327 /// constraint that `'0: 'a`.
328 #[instrument(level = "debug", skip(self))]
329 pub fn register_member_constraints(
330 &self,
331 param_env: ty::ParamEnv<'tcx>,
332 opaque_type_key: OpaqueTypeKey<'tcx>,
333 concrete_ty: Ty<'tcx>,
334 span: Span,
335 ) {
336 let def_id = opaque_type_key.def_id;
337
338 let tcx = self.tcx;
339
340 let concrete_ty = self.resolve_vars_if_possible(concrete_ty);
341
342 debug!(?concrete_ty);
343
344 let first_own_region = match self.opaque_ty_origin_unchecked(def_id, span) {
345 hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {
346 // We lower
347 //
348 // fn foo<'l0..'ln>() -> impl Trait<'l0..'lm>
349 //
350 // into
351 //
352 // type foo::<'p0..'pn>::Foo<'q0..'qm>
353 // fn foo<l0..'ln>() -> foo::<'static..'static>::Foo<'l0..'lm>.
354 //
355 // For these types we only iterate over `'l0..lm` below.
356 tcx.generics_of(def_id).parent_count
357 }
358 // These opaque type inherit all lifetime parameters from their
359 // parent, so we have to check them all.
360 hir::OpaqueTyOrigin::TyAlias => 0,
361 };
362
363 // For a case like `impl Foo<'a, 'b>`, we would generate a constraint
364 // `'r in ['a, 'b, 'static]` for each region `'r` that appears in the
365 // hidden type (i.e., it must be equal to `'a`, `'b`, or `'static`).
366 //
367 // `conflict1` and `conflict2` are the two region bounds that we
368 // detected which were unrelated. They are used for diagnostics.
369
370 // Create the set of choice regions: each region in the hidden
371 // type can be equal to any of the region parameters of the
372 // opaque type definition.
373 let choice_regions: Lrc<Vec<ty::Region<'tcx>>> = Lrc::new(
374 opaque_type_key.substs[first_own_region..]
375 .iter()
376 .filter_map(|arg| match arg.unpack() {
377 GenericArgKind::Lifetime(r) => Some(r),
378 GenericArgKind::Type(_) | GenericArgKind::Const(_) => None,
379 })
380 .chain(std::iter::once(self.tcx.lifetimes.re_static))
381 .collect(),
382 );
383
384 concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
385 op: |r| {
386 self.member_constraint(
387 opaque_type_key.def_id,
388 span,
389 concrete_ty,
390 r,
391 &choice_regions,
392 )
393 },
394 });
395 }
396
397 #[instrument(skip(self), level = "trace")]
398 pub fn opaque_type_origin(&self, opaque_def_id: DefId, span: Span) -> Option<OpaqueTyOrigin> {
399 let def_id = opaque_def_id.as_local()?;
400 let opaque_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
401 let parent_def_id = self.defining_use_anchor?;
402 let item_kind = &self.tcx.hir().expect_item(def_id).kind;
403
404 let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item_kind else {
405 span_bug!(
406 span,
407 "weird opaque type: {:#?}, {:#?}",
408 opaque_def_id,
409 item_kind
410 )
411 };
412 let in_definition_scope = match *origin {
413 // Async `impl Trait`
414 hir::OpaqueTyOrigin::AsyncFn(parent) => parent == parent_def_id,
415 // Anonymous `impl Trait`
416 hir::OpaqueTyOrigin::FnReturn(parent) => parent == parent_def_id,
417 // Named `type Foo = impl Bar;`
418 hir::OpaqueTyOrigin::TyAlias => {
419 may_define_opaque_type(self.tcx, parent_def_id, opaque_hir_id)
420 }
421 };
422 trace!(?origin);
423 in_definition_scope.then_some(*origin)
424 }
425
426 #[instrument(skip(self), level = "trace")]
427 fn opaque_ty_origin_unchecked(&self, opaque_def_id: DefId, span: Span) -> OpaqueTyOrigin {
428 let def_id = opaque_def_id.as_local().unwrap();
429 let origin = match self.tcx.hir().expect_item(def_id).kind {
430 hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => origin,
431 ref itemkind => {
432 span_bug!(span, "weird opaque type: {:?}, {:#?}", opaque_def_id, itemkind)
433 }
434 };
435 trace!(?origin);
436 origin
437 }
438 }
439
440 // Visitor that requires that (almost) all regions in the type visited outlive
441 // `least_region`. We cannot use `push_outlives_components` because regions in
442 // closure signatures are not included in their outlives components. We need to
443 // ensure all regions outlive the given bound so that we don't end up with,
444 // say, `ReVar` appearing in a return type and causing ICEs when other
445 // functions end up with region constraints involving regions from other
446 // functions.
447 //
448 // We also cannot use `for_each_free_region` because for closures it includes
449 // the regions parameters from the enclosing item.
450 //
451 // We ignore any type parameters because impl trait values are assumed to
452 // capture all the in-scope type parameters.
453 struct ConstrainOpaqueTypeRegionVisitor<OP> {
454 op: OP,
455 }
456
457 impl<'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<OP>
458 where
459 OP: FnMut(ty::Region<'tcx>),
460 {
461 fn visit_binder<T: TypeFoldable<'tcx>>(
462 &mut self,
463 t: &ty::Binder<'tcx, T>,
464 ) -> ControlFlow<Self::BreakTy> {
465 t.as_ref().skip_binder().visit_with(self);
466 ControlFlow::CONTINUE
467 }
468
469 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
470 match *r {
471 // ignore bound regions, keep visiting
472 ty::ReLateBound(_, _) => ControlFlow::CONTINUE,
473 _ => {
474 (self.op)(r);
475 ControlFlow::CONTINUE
476 }
477 }
478 }
479
480 fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
481 // We're only interested in types involving regions
482 if !ty.flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS) {
483 return ControlFlow::CONTINUE;
484 }
485
486 match ty.kind() {
487 ty::Closure(_, ref substs) => {
488 // Skip lifetime parameters of the enclosing item(s)
489
490 substs.as_closure().tupled_upvars_ty().visit_with(self);
491 substs.as_closure().sig_as_fn_ptr_ty().visit_with(self);
492 }
493
494 ty::Generator(_, ref substs, _) => {
495 // Skip lifetime parameters of the enclosing item(s)
496 // Also skip the witness type, because that has no free regions.
497
498 substs.as_generator().tupled_upvars_ty().visit_with(self);
499 substs.as_generator().return_ty().visit_with(self);
500 substs.as_generator().yield_ty().visit_with(self);
501 substs.as_generator().resume_ty().visit_with(self);
502 }
503 _ => {
504 ty.super_visit_with(self);
505 }
506 }
507
508 ControlFlow::CONTINUE
509 }
510 }
511
512 pub enum UseKind {
513 DefiningUse,
514 OpaqueUse,
515 }
516
517 impl UseKind {
518 pub fn is_defining(self) -> bool {
519 match self {
520 UseKind::DefiningUse => true,
521 UseKind::OpaqueUse => false,
522 }
523 }
524 }
525
526 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
527 #[instrument(skip(self), level = "debug")]
528 pub fn register_hidden_type(
529 &self,
530 opaque_type_key: OpaqueTypeKey<'tcx>,
531 cause: ObligationCause<'tcx>,
532 param_env: ty::ParamEnv<'tcx>,
533 hidden_ty: Ty<'tcx>,
534 origin: hir::OpaqueTyOrigin,
535 ) -> InferResult<'tcx, ()> {
536 let tcx = self.tcx;
537 let OpaqueTypeKey { def_id, substs } = opaque_type_key;
538
539 // Ideally, we'd get the span where *this specific `ty` came
540 // from*, but right now we just use the span from the overall
541 // value being folded. In simple cases like `-> impl Foo`,
542 // these are the same span, but not in cases like `-> (impl
543 // Foo, impl Bar)`.
544 let span = cause.span;
545
546 let mut obligations = vec![];
547 let prev = self.inner.borrow_mut().opaque_types().register(
548 OpaqueTypeKey { def_id, substs },
549 OpaqueHiddenType { ty: hidden_ty, span },
550 origin,
551 );
552 if let Some(prev) = prev {
553 obligations = self.at(&cause, param_env).eq(prev, hidden_ty)?.obligations;
554 }
555
556 let item_bounds = tcx.explicit_item_bounds(def_id);
557
558 for (predicate, _) in item_bounds {
559 debug!(?predicate);
560 let predicate = predicate.subst(tcx, substs);
561
562 let predicate = predicate.fold_with(&mut BottomUpFolder {
563 tcx,
564 ty_op: |ty| match *ty.kind() {
565 // We can't normalize associated types from `rustc_infer`,
566 // but we can eagerly register inference variables for them.
567 ty::Projection(projection_ty) if !projection_ty.has_escaping_bound_vars() => {
568 self.infer_projection(
569 param_env,
570 projection_ty,
571 cause.clone(),
572 0,
573 &mut obligations,
574 )
575 }
576 // Replace all other mentions of the same opaque type with the hidden type,
577 // as the bounds must hold on the hidden type after all.
578 ty::Opaque(def_id2, substs2) if def_id == def_id2 && substs == substs2 => {
579 hidden_ty
580 }
581 _ => ty,
582 },
583 lt_op: |lt| lt,
584 ct_op: |ct| ct,
585 });
586
587 if let ty::PredicateKind::Projection(projection) = predicate.kind().skip_binder() {
588 if projection.term.references_error() {
589 // No point on adding these obligations since there's a type error involved.
590 return Ok(InferOk { value: (), obligations: vec![] });
591 }
592 trace!("{:#?}", projection.term);
593 }
594 // Require that the predicate holds for the concrete type.
595 debug!(?predicate);
596 obligations.push(traits::Obligation::new(cause.clone(), param_env, predicate));
597 }
598 Ok(InferOk { value: (), obligations })
599 }
600 }
601
602 /// Returns `true` if `opaque_hir_id` is a sibling or a child of a sibling of `def_id`.
603 ///
604 /// Example:
605 /// ```rust
606 /// pub mod foo {
607 /// pub mod bar {
608 /// pub trait Bar { .. }
609 ///
610 /// pub type Baz = impl Bar;
611 ///
612 /// fn f1() -> Baz { .. }
613 /// }
614 ///
615 /// fn f2() -> bar::Baz { .. }
616 /// }
617 /// ```
618 ///
619 /// Here, `def_id` is the `LocalDefId` of the defining use of the opaque type (e.g., `f1` or `f2`),
620 /// and `opaque_hir_id` is the `HirId` of the definition of the opaque type `Baz`.
621 /// For the above example, this function returns `true` for `f1` and `false` for `f2`.
622 fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_hir_id: hir::HirId) -> bool {
623 let mut hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
624
625 // Named opaque types can be defined by any siblings or children of siblings.
626 let scope = tcx.hir().get_defining_scope(opaque_hir_id);
627 // We walk up the node tree until we hit the root or the scope of the opaque type.
628 while hir_id != scope && hir_id != hir::CRATE_HIR_ID {
629 hir_id = tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(hir_id));
630 }
631 // Syntactically, we are allowed to define the concrete type if:
632 let res = hir_id == scope;
633 trace!(
634 "may_define_opaque_type(def={:?}, opaque_node={:?}) = {}",
635 tcx.hir().find(hir_id),
636 tcx.hir().get(opaque_hir_id),
637 res
638 );
639 res
640 }