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.
This commit is contained in:
Nicholas Nethercote 2022-01-25 14:13:38 +11:00
parent 0c2ebbd412
commit e9a0c429c5
145 changed files with 519 additions and 531 deletions

View file

@ -2896,25 +2896,21 @@ impl ClashingExternDeclarations {
(Array(a_ty, a_const), Array(b_ty, b_const)) => {
// For arrays, we also check the constness of the type.
a_const.val == b_const.val
&& structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind)
&& structurally_same_type_impl(seen_types, cx, *a_ty, *b_ty, ckind)
}
(Slice(a_ty), Slice(b_ty)) => {
structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind)
structurally_same_type_impl(seen_types, cx, *a_ty, *b_ty, ckind)
}
(RawPtr(a_tymut), RawPtr(b_tymut)) => {
a_tymut.mutbl == b_tymut.mutbl
&& structurally_same_type_impl(
seen_types,
cx,
&a_tymut.ty,
&b_tymut.ty,
ckind,
seen_types, cx, a_tymut.ty, b_tymut.ty, ckind,
)
}
(Ref(_a_region, a_ty, a_mut), Ref(_b_region, b_ty, b_mut)) => {
// For structural sameness, we don't need the region to be same.
a_mut == b_mut
&& structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind)
&& structurally_same_type_impl(seen_types, cx, *a_ty, *b_ty, ckind)
}
(FnDef(..), FnDef(..)) => {
let a_poly_sig = a.fn_sig(tcx);
@ -2927,7 +2923,7 @@ impl ClashingExternDeclarations {
(a_sig.abi, a_sig.unsafety, a_sig.c_variadic)
== (b_sig.abi, b_sig.unsafety, b_sig.c_variadic)
&& a_sig.inputs().iter().eq_by(b_sig.inputs().iter(), |a, b| {
structurally_same_type_impl(seen_types, cx, a, b, ckind)
structurally_same_type_impl(seen_types, cx, *a, *b, ckind)
})
&& structurally_same_type_impl(
seen_types,

View file

@ -7,9 +7,10 @@ use rustc_middle::ty;
use rustc_span::symbol::sym;
declare_tool_lint! {
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to always be passed by value.
/// This is usually used for types that are thin wrappers around references, so there is no benefit to an extra
/// layer of indirection. (Example: `Ty` which is a reference to a `TyS`)
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to
/// always be passed by value. This is usually used for types that are thin wrappers around
/// references, so there is no benefit to an extra layer of indirection. (Example: `Ty` which
/// is a reference to an `Interned<TyS>`)
pub rustc::PASS_BY_VALUE,
Warn,
"pass by reference of a type flagged as `#[rustc_pass_by_value]`",

View file

@ -249,7 +249,7 @@ fn report_bin_hex_error(
));
}
if let Some(sugg_ty) =
get_type_suggestion(&cx.typeck_results().node_type(expr.hir_id), val, negative)
get_type_suggestion(cx.typeck_results().node_type(expr.hir_id), val, negative)
{
if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
let (sans_suffix, _) = repr_str.split_at(pos);
@ -367,7 +367,7 @@ fn lint_int_literal<'tcx>(
max,
));
if let Some(sugg_ty) =
get_type_suggestion(&cx.typeck_results().node_type(e.hir_id), v, negative)
get_type_suggestion(cx.typeck_results().node_type(e.hir_id), v, negative)
{
err.help(&format!("consider using the type `{}` instead", sugg_ty));
}
@ -1095,7 +1095,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
}
}
for arg in sig.inputs() {
let r = self.check_type_for_ffi(cache, arg);
let r = self.check_type_for_ffi(cache, *arg);
match r {
FfiSafe => {}
_ => {
@ -1257,7 +1257,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
let sig = self.cx.tcx.erase_late_bound_regions(sig);
for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
self.check_type_for_ffi_and_report_errors(input_hir.span, input_ty, false, false);
self.check_type_for_ffi_and_report_errors(input_hir.span, *input_ty, false, false);
}
if let hir::FnRetTy::Return(ref ret_hir) = decl.output {