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

Cast and Coercion enums duplicate a lot of structure #59588

Closed
oli-obk opened this issue Mar 31, 2019 · 4 comments · Fixed by #59987
Closed

Cast and Coercion enums duplicate a lot of structure #59588

oli-obk opened this issue Mar 31, 2019 · 4 comments · Fixed by #59987
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Mar 31, 2019

The Adjust enum in src/librustc/ty/adjustment.rs and the CastKind enum in src/librustc/mir/mod.rs have all fields but one of the CastKind enum in common. We should pull them out into a single enum that both use. Additionally this will allow merging some variants of ExprKind in src/librustc_mir/hair/mod.rs by having a single Coerce variant that has a field of the newly created enum.

@oli-obk oli-obk added the C-cleanup Category: PRs that clean code up or issues documenting cleanup. label Mar 31, 2019
@jonas-schievink jonas-schievink added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-coercions Area: implicit and explicit `expr as Type` coercions labels Mar 31, 2019
@saleemjaffer
Copy link
Contributor

@oli-obk Can i pick this up?

@oli-obk
Copy link
Contributor Author

oli-obk commented Apr 9, 2019

It's all yours!

@saleemjaffer
Copy link
Contributor

pub enum Adjust<'tcx> {
    /// Go from ! to any type.
    NeverToAny,

    /// Go from a fn-item type to a fn-pointer type.
    ReifyFnPointer,

    /// Go from a safe fn pointer to an unsafe fn pointer.
    UnsafeFnPointer,

    /// Go from a non-capturing closure to an fn pointer or an unsafe fn pointer.
    /// It cannot convert a closure that requires unsafe.
    ClosureFnPointer(hir::Unsafety),

    /// Go from a mut raw pointer to a const raw pointer.
    MutToConstPointer,

    /// Dereference once, producing a place.
    Deref(Option<OverloadedDeref<'tcx>>),

    /// Take the address and produce either a `&` or `*` pointer.
    Borrow(AutoBorrow<'tcx>),

    /// Unsize a pointer/reference value, e.g., `&[T; n]` to
    /// `&[T]`. Note that the source could be a thin or fat pointer.
    /// This will do things like convert thin pointers to fat
    /// pointers, or convert structs containing thin pointers to
    /// structs containing fat pointers, or convert between fat
    /// pointers. We don't store the details of how the transform is
    /// done (in fact, we don't know that, because it might depend on
    /// the precise type parameters). We just store the target
    /// type. Codegen backends and miri figure out what has to be done
    /// based on the precise source/target type at hand.
    Unsize,
}
pub enum CastKind {
    Misc,

    /// Converts unique, zero-sized type for a fn to fn()
    ReifyFnPointer,

    /// Converts non capturing closure to fn() or unsafe fn().
    /// It cannot convert a closure that requires unsafe.
    ClosureFnPointer(hir::Unsafety),

    /// Converts safe fn() to unsafe fn()
    UnsafeFnPointer,

    /// Coerces *mut T to *const T, preserving T.
    MutToConstPointer,

    /// "Unsize" -- convert a thin-or-fat pointer to a fat pointer.
    /// codegen must figure out the details once full monomorphization
    /// is known. For example, this could be used to cast from a
    /// `&[i32;N]` to a `&[i32]`, or a `Box<T>` to a `Box<dyn Trait>`
    /// (presuming `T: Trait`).
    Unsize,
}

Adjust has 2 extra fieldsDeref and Borrowand CastKind has a Misc extra.

@oli-obk
Copy link
Contributor Author

oli-obk commented Apr 9, 2019

Yes, that's expected. The shared variants can be moved to a new enum so e.g. CastKind could be

enum CastKind {
    Misc,
    Pointer(PointerCast),
}

bors added a commit that referenced this issue Apr 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants