Skip to content

Commit 29a7901

Browse files
committed
Move ty::OpaqueHiddenType to its own little module (cute)
1 parent a409b98 commit 29a7901

File tree

2 files changed

+90
-82
lines changed

2 files changed

+90
-82
lines changed

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::AssocItemContainer::*;
1717
pub use self::BorrowKind::*;
1818
pub use self::IntVarValue::*;
1919
pub use self::Variance::*;
20-
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};
20+
use crate::error::TypeMismatchReason;
2121
use crate::metadata::ModChild;
2222
use crate::middle::privacy::EffectiveVisibilities;
2323
use crate::mir::{Body, GeneratorLayout};
@@ -34,7 +34,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3434
use rustc_data_structures::intern::Interned;
3535
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3636
use rustc_data_structures::steal::Steal;
37-
use rustc_errors::ErrorGuaranteed;
3837
use rustc_hir as hir;
3938
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
4039
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap};
@@ -144,6 +143,7 @@ mod typeck_results;
144143
mod alias_relation_direction;
145144
mod bound_constness;
146145
mod impl_polarity;
146+
mod opaque_hidden_type;
147147
mod param_env;
148148
mod predicate;
149149
mod term;
@@ -153,6 +153,7 @@ mod visibility;
153153
pub use alias_relation_direction::AliasRelationDirection;
154154
pub use bound_constness::BoundConstness;
155155
pub use impl_polarity::ImplPolarity;
156+
pub use opaque_hidden_type::OpaqueHiddenType;
156157
pub use param_env::{ParamEnv, ParamEnvAnd};
157158
pub use predicate::{
158159
CoercePredicate, InstantiatedPredicates, OutlivesPredicate, PolyCoercePredicate,
@@ -412,86 +413,6 @@ pub struct OpaqueTypeKey<'tcx> {
412413
pub substs: SubstsRef<'tcx>,
413414
}
414415

415-
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
416-
pub struct OpaqueHiddenType<'tcx> {
417-
/// The span of this particular definition of the opaque type. So
418-
/// for example:
419-
///
420-
/// ```ignore (incomplete snippet)
421-
/// type Foo = impl Baz;
422-
/// fn bar() -> Foo {
423-
/// // ^^^ This is the span we are looking for!
424-
/// }
425-
/// ```
426-
///
427-
/// In cases where the fn returns `(impl Trait, impl Trait)` or
428-
/// other such combinations, the result is currently
429-
/// over-approximated, but better than nothing.
430-
pub span: Span,
431-
432-
/// The type variable that represents the value of the opaque type
433-
/// that we require. In other words, after we compile this function,
434-
/// we will be created a constraint like:
435-
/// ```ignore (pseudo-rust)
436-
/// Foo<'a, T> = ?C
437-
/// ```
438-
/// where `?C` is the value of this type variable. =) It may
439-
/// naturally refer to the type and lifetime parameters in scope
440-
/// in this function, though ultimately it should only reference
441-
/// those that are arguments to `Foo` in the constraint above. (In
442-
/// other words, `?C` should not include `'b`, even though it's a
443-
/// lifetime parameter on `foo`.)
444-
pub ty: Ty<'tcx>,
445-
}
446-
447-
impl<'tcx> OpaqueHiddenType<'tcx> {
448-
pub fn report_mismatch(&self, other: &Self, tcx: TyCtxt<'tcx>) -> ErrorGuaranteed {
449-
// Found different concrete types for the opaque type.
450-
let sub_diag = if self.span == other.span {
451-
TypeMismatchReason::ConflictType { span: self.span }
452-
} else {
453-
TypeMismatchReason::PreviousUse { span: self.span }
454-
};
455-
tcx.sess.emit_err(OpaqueHiddenTypeMismatch {
456-
self_ty: self.ty,
457-
other_ty: other.ty,
458-
other_span: other.span,
459-
sub: sub_diag,
460-
})
461-
}
462-
463-
#[instrument(level = "debug", skip(tcx), ret)]
464-
pub fn remap_generic_params_to_declaration_params(
465-
self,
466-
opaque_type_key: OpaqueTypeKey<'tcx>,
467-
tcx: TyCtxt<'tcx>,
468-
// typeck errors have subpar spans for opaque types, so delay error reporting until borrowck.
469-
ignore_errors: bool,
470-
) -> Self {
471-
let OpaqueTypeKey { def_id, substs } = opaque_type_key;
472-
473-
// Use substs to build up a reverse map from regions to their
474-
// identity mappings. This is necessary because of `impl
475-
// Trait` lifetimes are computed by replacing existing
476-
// lifetimes with 'static and remapping only those used in the
477-
// `impl Trait` return type, resulting in the parameters
478-
// shifting.
479-
let id_substs = InternalSubsts::identity_for_item(tcx, def_id);
480-
debug!(?id_substs);
481-
482-
// This zip may have several times the same lifetime in `substs` paired with a different
483-
// lifetime from `id_substs`. Simply `collect`ing the iterator is the correct behaviour:
484-
// it will pick the last one, which is the one we introduced in the impl-trait desugaring.
485-
let map = substs.iter().zip(id_substs).collect();
486-
debug!("map = {:#?}", map);
487-
488-
// Convert the type from the function into a type valid outside
489-
// the function, by replacing invalid regions with 'static,
490-
// after producing an error for each of them.
491-
self.fold_with(&mut opaque_types::ReverseMapper::new(tcx, map, self.span, ignore_errors))
492-
}
493-
}
494-
495416
/// The "placeholder index" fully defines a placeholder region, type, or const. Placeholders are
496417
/// identified by both a universe, as well as a name residing within that universe. Distinct bound
497418
/// regions/types/consts within the same universe simply have an unknown relationship to one
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use rustc_errors::ErrorGuaranteed;
2+
use rustc_span::Span;
3+
4+
use crate::error::OpaqueHiddenTypeMismatch;
5+
use crate::ty::{
6+
opaque_types, InternalSubsts, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeMismatchReason,
7+
};
8+
9+
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
10+
pub struct OpaqueHiddenType<'tcx> {
11+
/// The span of this particular definition of the opaque type. So
12+
/// for example:
13+
///
14+
/// ```ignore (incomplete snippet)
15+
/// type Foo = impl Baz;
16+
/// fn bar() -> Foo {
17+
/// // ^^^ This is the span we are looking for!
18+
/// }
19+
/// ```
20+
///
21+
/// In cases where the fn returns `(impl Trait, impl Trait)` or
22+
/// other such combinations, the result is currently
23+
/// over-approximated, but better than nothing.
24+
pub span: Span,
25+
26+
/// The type variable that represents the value of the opaque type
27+
/// that we require. In other words, after we compile this function,
28+
/// we will be created a constraint like:
29+
/// ```ignore (pseudo-rust)
30+
/// Foo<'a, T> = ?C
31+
/// ```
32+
/// where `?C` is the value of this type variable. =) It may
33+
/// naturally refer to the type and lifetime parameters in scope
34+
/// in this function, though ultimately it should only reference
35+
/// those that are arguments to `Foo` in the constraint above. (In
36+
/// other words, `?C` should not include `'b`, even though it's a
37+
/// lifetime parameter on `foo`.)
38+
pub ty: Ty<'tcx>,
39+
}
40+
41+
impl<'tcx> OpaqueHiddenType<'tcx> {
42+
pub fn report_mismatch(&self, other: &Self, tcx: TyCtxt<'tcx>) -> ErrorGuaranteed {
43+
// Found different concrete types for the opaque type.
44+
let sub_diag = if self.span == other.span {
45+
TypeMismatchReason::ConflictType { span: self.span }
46+
} else {
47+
TypeMismatchReason::PreviousUse { span: self.span }
48+
};
49+
tcx.sess.emit_err(OpaqueHiddenTypeMismatch {
50+
self_ty: self.ty,
51+
other_ty: other.ty,
52+
other_span: other.span,
53+
sub: sub_diag,
54+
})
55+
}
56+
57+
#[instrument(level = "debug", skip(tcx), ret)]
58+
pub fn remap_generic_params_to_declaration_params(
59+
self,
60+
opaque_type_key: OpaqueTypeKey<'tcx>,
61+
tcx: TyCtxt<'tcx>,
62+
// typeck errors have subpar spans for opaque types, so delay error reporting until borrowck.
63+
ignore_errors: bool,
64+
) -> Self {
65+
let OpaqueTypeKey { def_id, substs } = opaque_type_key;
66+
67+
// Use substs to build up a reverse map from regions to their
68+
// identity mappings. This is necessary because of `impl
69+
// Trait` lifetimes are computed by replacing existing
70+
// lifetimes with 'static and remapping only those used in the
71+
// `impl Trait` return type, resulting in the parameters
72+
// shifting.
73+
let id_substs = InternalSubsts::identity_for_item(tcx, def_id);
74+
debug!(?id_substs);
75+
76+
// This zip may have several times the same lifetime in `substs` paired with a different
77+
// lifetime from `id_substs`. Simply `collect`ing the iterator is the correct behaviour:
78+
// it will pick the last one, which is the one we introduced in the impl-trait desugaring.
79+
let map = substs.iter().zip(id_substs).collect();
80+
debug!("map = {:#?}", map);
81+
82+
// Convert the type from the function into a type valid outside
83+
// the function, by replacing invalid regions with 'static,
84+
// after producing an error for each of them.
85+
self.fold_with(&mut opaque_types::ReverseMapper::new(tcx, map, self.span, ignore_errors))
86+
}
87+
}

0 commit comments

Comments
 (0)