The current crate structure of StableMIR is making it very hard to properly encapsulate our stable structures. All the internal details of stable items have to somehow be exposed in order to allow rustc_smir to build them.
For example, the definition of Ty exposes its internal representation:
pub struct Ty(pub usize);
There is no mechanism for us today to ensure that this usize corresponds to an index inside StableMIR. The Context trait is also exposed, even though we expect users to never invoke them directly.
The main problems I see with the lack of encapsulation are:
- Usability. It's confusing for users to know exactly what they can rely on, and what they shouldn't.
- Defining the boundaries of what is semantically versioned.
- APIs are very fragile. Users can easily make a mistake that will break a type invariant, which will result in one of the following:
a. Their code will trigger an ICE
b. The API may return inconsistent result
c. We will have to constantly check for these invariants ourselves as much as possible and return Result for everything we do.
We should investigate what is the best mechanism to fix this.
### Tasks
- [ ] Enable proper encapsulation (merge crates?)
- [ ] Audit the StableMIR code and change the visibility of items that shouldn't be public
The current crate structure of StableMIR is making it very hard to properly encapsulate our stable structures. All the internal details of stable items have to somehow be exposed in order to allow
rustc_smirto build them.For example, the definition of
Tyexposes its internal representation:There is no mechanism for us today to ensure that this
usizecorresponds to an index inside StableMIR. TheContexttrait is also exposed, even though we expect users to never invoke them directly.The main problems I see with the lack of encapsulation are:
a. Their code will trigger an ICE
b. The API may return inconsistent result
c. We will have to constantly check for these invariants ourselves as much as possible and return
Resultfor everything we do.We should investigate what is the best mechanism to fix this.