Skip to content

Create Ty type alias in rustc_type_ir#154270

Draft
Jamesbarford wants to merge 1 commit intorust-lang:mainfrom
Jamesbarford:chore/move-ty-pt1
Draft

Create Ty type alias in rustc_type_ir#154270
Jamesbarford wants to merge 1 commit intorust-lang:mainfrom
Jamesbarford:chore/move-ty-pt1

Conversation

@Jamesbarford
Copy link
Contributor

@Jamesbarford Jamesbarford commented Mar 23, 2026

r? lcnr

Anywhere that required the use of the trait Ty I used ty::Ty<I> otherwise it should be Ty<I>

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Mar 23, 2026
@rust-bors

This comment has been minimized.

/// We can simply cache based on the ty itself, because we use
/// `ty::BoundVarIndexKind::Canonical`.
cache: HashMap<I::Ty, I::Ty>,
cache: HashMap<ty::Ty<I>, ty::Ty<I>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add the lint that we should use Ty without any prefix to the type alias? it should be some attribute on struct Ty, idk if it already works with type aliases.

anyways, please import ty::Ty. There isn't really a good reason for that outside of consistency

Copy link
Contributor Author

@Jamesbarford Jamesbarford Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to do this however I'd need to add another #[rustc_diagnostic_item = "Ty"] which then clashes with the one in rustc_middle. I thought perhaps doing something like #[rustc_diagnostic_item = "IrTy"] may work however it then simply ignored the diagnostic. (I've expanded on this in my comment below)

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice

please also do so for all uses of <whatever as Interner>::Ty

View changes since this review

@Jamesbarford
Copy link
Contributor Author

Couple of things I found;

  1. For methods like Ty::new_error I've had to prefix it with I::Ty::new_error to prevent the error below, the other option would be to do crate::inherent::Ty::new_error(...).
error[E0283]: type annotations needed
   --> compiler/rustc_type_ir/src/ty_kind/closure.rs:486:40
    |
486 |             ty::ClosureKind::FnOnce => Ty::new_tup_from_iter(
    |                                        ^^ cannot infer type for type parameter `I` declared on the type alias `Ty`
    |
    = note: cannot satisfy `_: interner::Interner`
    = note: associated types cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
help: use the fully qualified path to an implementation
  1. To be able to access the traits methods like ty.is_fresh(...) I've done;
#[cfg_attr(feature = "nightly", allow(rustc::non_glob_import_of_type_ir_inherent))]
use crate::inherent::Ty as _;
  1. To prevent ty::Ty the correct thing to do is;
// in compiler/rustc_type_ir/src/sty/mod.rs
#[rustc_diagnostic_item = "Ty"]
pub type Ty ...

// in compiler/rustc_type_ir/src/lib.rs
#![deny(rustc::usage_of_qualified_ty)]

However I then get the below error, so I've not added it. But we should add it when we delete the struct from rustc_middle.

error: duplicate diagnostic item in crate `rustc_middle`: `Ty`
   --> compiler/rustc_middle/src/ty/mod.rs:431:1
    |
431 | pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>);
    | ^^^^^^^^^^^^^^^^^^^
    |
    = note: the diagnostic item is first defined in crate `rustc_type_ir`

error: could not compile `rustc_middle` (lib) due to 1 previous error
  1. In compiler/rustc_type_ir/src/inherent.rs I import Ty as IrTy to prevent clashing with the trait definition. Seeing as we hope to delete the trait I didn't think it was too bad.

@lcnr
Copy link
Contributor

lcnr commented Mar 24, 2026

That is really annoying, didn't consider that... I guess let's keep ty::Ty for now 🤔 this does mean any future PR will have to remove the ty:: at that point, but that should be a single (probably even separate commit which just does a find-and-replace)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants