Skip to content

change handling of subtyping to be less fragile after analysis #112651

Open

Description

@lcnr

edit: no longer up-to date. the current plan is to add the explicitly add the source type to MIR casts instead

We should add a MIR pass after analysis (and before any optimizations) which makes subtyping explicit in the MIR. This is a robust way to fix #107205, preventing similar issues in the future. I don't quite know how to do that, probably by adding an explicit ProjectionElem::Cast (probably reusing OpaqueCast for that).

Activity

  1. added
    C-cleanupCategory: PRs that clean code up or issues documenting cleanup.
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
    A-mir-optArea: MIR optimizations
    A-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)
    on Jun 15, 2023
  2. lcnr commented on Jun 19, 2023

    @lcnr
    ContributorAuthor

    When implementing this, also handle all the FIXME(#112651) added in #112307

  3. ouz-a commented on Aug 15, 2023

    @ouz-a
    Contributor

    @rustbot claim

  4. added 2 commits that reference this issue on Oct 3, 2023
  5. goffrie commented on Nov 26, 2023

    @goffrie
    Contributor

    Should this be closed now that #115025 has been merged?

    edit: I guess there are still some FIXMEs referencing this issue

  6. lcnr commented on Nov 30, 2023

    @lcnr
    ContributorAuthor

    we encountered an issue with function pointers:

    fn main() {
        let x: fn(fn(&'static ())) = some_function;
        let y: fn(for<'a> fn(&'a ())) = x;
        y(|&()| ());
    }

    here we can make subtyping inside of main explicit, resulting in something like the following

    fn main() {
        let x: fn(fn(&'static ())) = some_function;
        let y: fn(for<'a> fn(&'a ())) = x as fn(fn(&'static ()));
        y(|&()| ());
    }

    however, when calling y, it is still a function pointer to some function expecting fn(&'static ()). This means that the calling y results in implicit subtyping. To handle this the let y = x assignment would also have to wrap the value of x to explicitly subtype. Modifying values in this way feels very difficult implement. Especially given that the function pointer could be nested in x, e.g. in some union or whatever.

    As this means that subtyping has to still be implicitly handled somewhere, I believe that it's safer to always implicitly handle this. I believe that it's otherwise easier to accidentally rely on subtyping being explicit, resulting in bugs which are even harder to diagnose.

  7. 18 remaining items

  8. added 3 commits that reference this issue on Nov 8, 2025
  9. removed
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    on Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-mir-optArea: MIR optimizationsA-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)C-cleanupCategory: PRs that clean code up or issues documenting cleanup.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions