2019-03-12 19:27:06 +00:00
|
|
|
use crate::ty::error::TypeError;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
|
|
|
|
|
use crate::ty::{self, InferConst, Ty, TyCtxt};
|
2015-03-20 08:17:09 -04:00
|
|
|
|
|
|
|
|
/// A type "A" *matches* "B" if the fresh types in B could be
|
|
|
|
|
/// substituted with values so as to make it equal to A. Matching is
|
|
|
|
|
/// intended to be used only on freshened types, and it basically
|
|
|
|
|
/// indicates if the non-freshened versions of A and B could have been
|
|
|
|
|
/// unified.
|
|
|
|
|
///
|
|
|
|
|
/// It is only an approximation. If it yields false, unification would
|
|
|
|
|
/// definitely fail, but a true result doesn't mean unification would
|
|
|
|
|
/// succeed. This is because we don't track the "side-constraints" on
|
|
|
|
|
/// type variables, nor do we track if the same freshened type appears
|
|
|
|
|
/// more than once. To some extent these approximations could be
|
|
|
|
|
/// fixed, given effort.
|
|
|
|
|
///
|
|
|
|
|
/// Like subtyping, matching is really a binary relation, so the only
|
|
|
|
|
/// important thing about the result is Ok/Err. Also, matching never
|
|
|
|
|
/// affects any type variables or unification state.
|
2019-06-14 00:48:52 +03:00
|
|
|
pub struct Match<'tcx> {
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-03-26 00:13:09 +01:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2015-03-20 08:17:09 -04:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> Match<'tcx> {
|
2019-03-26 00:13:09 +01:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Match<'tcx> {
|
|
|
|
|
Match { tcx, param_env }
|
2015-03-20 08:17:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn tag(&self) -> &'static str {
|
|
|
|
|
"Match"
|
|
|
|
|
}
|
|
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
|
self.tcx
|
|
|
|
|
}
|
|
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
|
|
|
self.param_env
|
|
|
|
|
}
|
|
|
|
|
fn a_is_expected(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
} // irrelevant
|
|
|
|
|
|
|
|
|
|
fn relate_with_variance<T: Relate<'tcx>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
_: ty::Variance,
|
2021-05-13 09:30:14 -04:00
|
|
|
_: ty::VarianceDiagInfo<'tcx>,
|
2020-06-24 23:14:18 +02:00
|
|
|
a: T,
|
|
|
|
|
b: T,
|
2019-12-22 17:42:04 -05:00
|
|
|
) -> RelateResult<'tcx, T> {
|
2015-03-20 08:17:09 -04:00
|
|
|
self.relate(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn regions(
|
|
|
|
|
&mut self,
|
|
|
|
|
a: ty::Region<'tcx>,
|
|
|
|
|
b: ty::Region<'tcx>,
|
|
|
|
|
) -> RelateResult<'tcx, ty::Region<'tcx>> {
|
2022-02-11 07:18:06 +00:00
|
|
|
debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
|
2015-03-20 08:17:09 -04:00
|
|
|
Ok(a)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
|
2022-02-11 07:18:06 +00:00
|
|
|
debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
|
2019-12-22 17:42:04 -05:00
|
|
|
if a == b {
|
|
|
|
|
return Ok(a);
|
|
|
|
|
}
|
2015-03-20 08:17:09 -04:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match (a.kind(), b.kind()) {
|
2020-04-16 17:38:52 -07:00
|
|
|
(
|
|
|
|
|
_,
|
|
|
|
|
&ty::Infer(ty::FreshTy(_))
|
|
|
|
|
| &ty::Infer(ty::FreshIntTy(_))
|
|
|
|
|
| &ty::Infer(ty::FreshFloatTy(_)),
|
|
|
|
|
) => Ok(a),
|
2015-03-20 08:17:09 -04:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
Err(TypeError::Sorts(relate::expected_found(self, a, b)))
|
2015-03-20 08:17:09 -04:00
|
|
|
}
|
|
|
|
|
|
2020-05-05 23:02:09 -05:00
|
|
|
(&ty::Error(_), _) | (_, &ty::Error(_)) => Ok(self.tcx().ty_error()),
|
2015-03-20 08:17:09 -04:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => relate::super_relate_tys(self, a, b),
|
2015-03-20 08:17:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 01:19:13 +00:00
|
|
|
fn consts(
|
|
|
|
|
&mut self,
|
2022-02-02 14:24:45 +11:00
|
|
|
a: ty::Const<'tcx>,
|
|
|
|
|
b: ty::Const<'tcx>,
|
|
|
|
|
) -> RelateResult<'tcx, ty::Const<'tcx>> {
|
2019-03-08 01:19:13 +00:00
|
|
|
debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
|
|
|
|
|
if a == b {
|
|
|
|
|
return Ok(a);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
match (a.val(), b.val()) {
|
2019-11-08 21:32:56 +01:00
|
|
|
(_, ty::ConstKind::Infer(InferConst::Fresh(_))) => {
|
2019-03-18 20:55:19 +00:00
|
|
|
return Ok(a);
|
|
|
|
|
}
|
2019-03-08 01:19:13 +00:00
|
|
|
|
2019-11-08 21:32:56 +01:00
|
|
|
(ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
|
2022-02-02 14:24:45 +11:00
|
|
|
return Err(TypeError::ConstMismatch(relate::expected_found(self, a, b)));
|
2019-03-08 01:19:13 +00:00
|
|
|
}
|
2019-03-18 20:55:19 +00:00
|
|
|
|
|
|
|
|
_ => {}
|
2019-03-08 01:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
relate::super_relate_consts(self, a, b)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn binders<T>(
|
|
|
|
|
&mut self,
|
2020-10-05 16:51:33 -04:00
|
|
|
a: ty::Binder<'tcx, T>,
|
|
|
|
|
b: ty::Binder<'tcx, T>,
|
|
|
|
|
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: Relate<'tcx>,
|
2015-03-20 08:17:09 -04:00
|
|
|
{
|
2020-12-16 22:36:14 -05:00
|
|
|
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
|
2015-03-20 08:17:09 -04:00
|
|
|
}
|
|
|
|
|
}
|