Skip to content

Refactor away the TypeckTables::cast_kinds field #52482

Closed
@oli-obk

Description

@oli-obk

Motivation

This field has a lot of code going into creating it, while its use sites barely care at all about all the nice data it contains. They could just as well compute the data they need themselves. Computing the data is cheap, so there's no point in computing this ahead of time.

Instructions

1. Eliminate uses

The field is used in two places:

  1. HIR -> MIR conversion
    if let Some(&TyCastKind::CoercionCast) = cx.tables()
  2. rvalue promotion
    match v.tables.cast_kinds().get(from.hir_id) {

The 1. use can be fixed by just checking whether the type of source is the same type as expr_ty. This means all foo as Bar casts get ignored if foo is already of type Bar. Which is exactly what that code is trying to do.

The 2. use actually wants to know the cast kind, but there's no need to get that from the cast_kinds field. Instead one would again obtain the type of the expression being cast and the type being cast to, and then do exactly what MIR based borrowck does when going through the same code (Use CastTy to figure out the details):

Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {
let operand_ty = operand.ty(self.mir, self.tcx);
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
match (cast_in, cast_out) {
(CastTy::Ptr(_), CastTy::Int(_)) |
(CastTy::FnPtr, CastTy::Int(_)) => {

2. Eliminate the field and all the code feeding into it

This part is easy now. Remove the field, and the compiler will guide you to all the places it's used. All you have to do is remove code until the compiler is happy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions