Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul interning. #93148

Merged
merged 8 commits into from
Feb 15, 2022
Merged

Overhaul interning. #93148

merged 8 commits into from
Feb 15, 2022

Commits on Feb 15, 2022

  1. Rename Interned as InternedInSet.

    This will let us introduce a more widely-used `Interned` type in the
    next commit.
    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    028e57b View commit details
    Browse the repository at this point in the history
  2. Rename PtrKey as Interned and improve it.

    In particular, there's now more protection against incorrect usage,
    because you can only create one via `Interned::new_unchecked`, which
    makes it more obvious that you must be careful.
    
    There are also some tests.
    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    0c2ebbd View commit details
    Browse the repository at this point in the history
  3. 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.
    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    e9a0c42 View commit details
    Browse the repository at this point in the history
  4. Overhaul PredicateInner and Predicate.

    Specifically, change `Ty` from this:
    ```
    pub struct Predicate<'tcx> { inner: &'tcx PredicateInner<'tcx> }
    ```
    to this:
    ```
    pub struct Predicate<'tcx>(&'tcx Interned<PredicateS<'tcx>>)
    ```
    where `PredicateInner` is renamed as `PredicateS`.
    
     This (plus a few other minor changes) makes the parallels with `Ty` and
    `TyS` much clearer, and makes the uniqueness more explicit.
    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    925ec0d View commit details
    Browse the repository at this point in the history
  5. Overhaul RegionKind and Region.

    Specifically, change `Region` from this:
    ```
    pub type Region<'tcx> = &'tcx RegionKind;
    ```
    to this:
    ```
    pub struct Region<'tcx>(&'tcx Interned<RegionKind>);
    ```
    
    This now matches `Ty` and `Predicate` more closely.
    
    Things to note
    - Regions have always been interned, but we haven't been using pointer-based
      `Eq` and `Hash`. This is now happening.
    - I chose to impl `Deref` for `Region` because it makes pattern matching a lot
      nicer, and `Region` can be viewed as just a smart wrapper for `RegionKind`.
    - Various methods are moved from `RegionKind` to `Region`.
    - There is a lot of tedious sigil changes.
    - A couple of types like `HighlightBuilder`, `RegionHighlightMode` now have a
      `'tcx` lifetime because they hold a `Ty<'tcx>`, so they can call `mk_region`.
    - A couple of test outputs change slightly, I'm not sure why, but the new
      outputs are a little better.
    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    7024dc5 View commit details
    Browse the repository at this point in the history
  6. Remove unnecessary RegionKind:: quals.

    The variant names are exported, so we can use them directly (possibly
    with a `ty::` qualifier). Lots of places already do this, this commit
    just increases consistency.
    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    7eb1550 View commit details
    Browse the repository at this point in the history
  7. Overhaul Const.

    Specifically, rename the `Const` struct as `ConstS` and re-introduce `Const` as
    this:
    ```
    pub struct Const<'tcx>(&'tcx Interned<ConstS>);
    ```
    This now matches `Ty` and `Predicate` more closely, including using
    pointer-based `eq` and `hash`.
    
    Notable changes:
    - `mk_const` now takes a `ConstS`.
    - `Const` was copy, despite being 48 bytes. Now `ConstS` is not, so need a
      we need separate arena for it, because we can't use the `Dropless` one any
      more.
    - Many `&'tcx Const<'tcx>`/`&Const<'tcx>` to `Const<'tcx>` changes
    - Many `ct.ty` to `ct.ty()` and `ct.val` to `ct.val()` changes.
    - Lots of tedious sigil fiddling.
    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    a95fb8b View commit details
    Browse the repository at this point in the history
  8. Address review comments.

    nnethercote committed Feb 15, 2022
    Configuration menu
    Copy the full SHA
    80632de View commit details
    Browse the repository at this point in the history