Skip to content

Commit a04bd9b

Browse files
committed
group ty::opaque in one file
1 parent 2c556f5 commit a04bd9b

File tree

4 files changed

+97
-107
lines changed

4 files changed

+97
-107
lines changed

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ mod infer_var_info;
7878
mod instance;
7979
mod list;
8080
mod main_definition;
81-
mod opaque_hidden_type;
82-
mod opaque_type_key;
83-
mod opaque_types;
81+
mod opaque;
8482
mod param_env;
8583
mod parameterized;
8684
mod placeholder;
@@ -145,8 +143,7 @@ pub use self::infer_var_info::InferVarInfo;
145143
pub use self::instance::{Instance, InstanceDef, ShortInstance, UnusedGenericParams};
146144
pub use self::list::List;
147145
pub use self::main_definition::MainDefinition;
148-
pub use self::opaque_hidden_type::OpaqueHiddenType;
149-
pub use self::opaque_type_key::OpaqueTypeKey;
146+
pub use self::opaque::{OpaqueHiddenType, OpaqueTypeKey};
150147
pub use self::param_env::{ParamEnv, ParamEnvAnd};
151148
pub use self::parameterized::ParameterizedOverTcx;
152149
pub use self::placeholder::{Placeholder, PlaceholderConst, PlaceholderRegion, PlaceholderType};

compiler/rustc_middle/src/ty/opaque_types.rs renamed to compiler/rustc_middle/src/ty/opaque.rs

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
1+
use rustc_data_structures::fx::FxHashMap;
2+
use rustc_errors::ErrorGuaranteed;
3+
use rustc_span::def_id::{DefId, LocalDefId};
4+
use rustc_span::Span;
5+
16
use crate::error::ConstNotUsedTraitAlias;
7+
use crate::error::OpaqueHiddenTypeMismatch;
28
use crate::ty::fold::{TypeFolder, TypeSuperFoldable};
39
use crate::ty::subst::{GenericArg, GenericArgKind};
4-
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
5-
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_span::def_id::DefId;
7-
use rustc_span::Span;
10+
use crate::ty::{self, InternalSubsts, SubstsRef, Ty, TyCtxt, TypeFoldable, TypeMismatchReason};
11+
12+
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
13+
pub struct OpaqueHiddenType<'tcx> {
14+
/// The span of this particular definition of the opaque type. So
15+
/// for example:
16+
///
17+
/// ```ignore (incomplete snippet)
18+
/// type Foo = impl Baz;
19+
/// fn bar() -> Foo {
20+
/// // ^^^ This is the span we are looking for!
21+
/// }
22+
/// ```
23+
///
24+
/// In cases where the fn returns `(impl Trait, impl Trait)` or
25+
/// other such combinations, the result is currently
26+
/// over-approximated, but better than nothing.
27+
pub span: Span,
28+
29+
/// The type variable that represents the value of the opaque type
30+
/// that we require. In other words, after we compile this function,
31+
/// we will be created a constraint like:
32+
/// ```ignore (pseudo-rust)
33+
/// Foo<'a, T> = ?C
34+
/// ```
35+
/// where `?C` is the value of this type variable. =) It may
36+
/// naturally refer to the type and lifetime parameters in scope
37+
/// in this function, though ultimately it should only reference
38+
/// those that are arguments to `Foo` in the constraint above. (In
39+
/// other words, `?C` should not include `'b`, even though it's a
40+
/// lifetime parameter on `foo`.)
41+
pub ty: Ty<'tcx>,
42+
}
43+
44+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable, Lift)]
45+
#[derive(TypeFoldable, TypeVisitable)]
46+
pub struct OpaqueTypeKey<'tcx> {
47+
pub def_id: LocalDefId,
48+
pub substs: SubstsRef<'tcx>,
49+
}
850

951
/// Converts generic params of a TypeFoldable from one
1052
/// item's generics to another. Usually from a function's generics
1153
/// list to the opaque type's own generics.
12-
pub(super) struct ReverseMapper<'tcx> {
54+
struct ReverseMapper<'tcx> {
1355
tcx: TyCtxt<'tcx>,
1456
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
1557
/// see call sites to fold_kind_no_missing_regions_error
@@ -26,6 +68,54 @@ pub(super) struct ReverseMapper<'tcx> {
2668
span: Span,
2769
}
2870

71+
impl<'tcx> OpaqueHiddenType<'tcx> {
72+
pub fn report_mismatch(&self, other: &Self, tcx: TyCtxt<'tcx>) -> ErrorGuaranteed {
73+
// Found different concrete types for the opaque type.
74+
let sub_diag = if self.span == other.span {
75+
TypeMismatchReason::ConflictType { span: self.span }
76+
} else {
77+
TypeMismatchReason::PreviousUse { span: self.span }
78+
};
79+
tcx.sess.emit_err(OpaqueHiddenTypeMismatch {
80+
self_ty: self.ty,
81+
other_ty: other.ty,
82+
other_span: other.span,
83+
sub: sub_diag,
84+
})
85+
}
86+
87+
#[instrument(level = "debug", skip(tcx), ret)]
88+
pub fn remap_generic_params_to_declaration_params(
89+
self,
90+
opaque_type_key: OpaqueTypeKey<'tcx>,
91+
tcx: TyCtxt<'tcx>,
92+
// typeck errors have subpar spans for opaque types, so delay error reporting until borrowck.
93+
ignore_errors: bool,
94+
) -> Self {
95+
let OpaqueTypeKey { def_id, substs } = opaque_type_key;
96+
97+
// Use substs to build up a reverse map from regions to their
98+
// identity mappings. This is necessary because of `impl
99+
// Trait` lifetimes are computed by replacing existing
100+
// lifetimes with 'static and remapping only those used in the
101+
// `impl Trait` return type, resulting in the parameters
102+
// shifting.
103+
let id_substs = InternalSubsts::identity_for_item(tcx, def_id);
104+
debug!(?id_substs);
105+
106+
// This zip may have several times the same lifetime in `substs` paired with a different
107+
// lifetime from `id_substs`. Simply `collect`ing the iterator is the correct behaviour:
108+
// it will pick the last one, which is the one we introduced in the impl-trait desugaring.
109+
let map = substs.iter().zip(id_substs).collect();
110+
debug!("map = {:#?}", map);
111+
112+
// Convert the type from the function into a type valid outside
113+
// the function, by replacing invalid regions with 'static,
114+
// after producing an error for each of them.
115+
self.fold_with(&mut ReverseMapper::new(tcx, map, self.span, ignore_errors))
116+
}
117+
}
118+
29119
impl<'tcx> ReverseMapper<'tcx> {
30120
pub(super) fn new(
31121
tcx: TyCtxt<'tcx>,

compiler/rustc_middle/src/ty/opaque_hidden_type.rs

Lines changed: 0 additions & 87 deletions
This file was deleted.

compiler/rustc_middle/src/ty/opaque_type_key.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)