Skip to content

Clarify our story with as and type placeholders. #12894

Closed
@Kimundi

Description

@Kimundi

Right now the compiler accepts partial type hints for casts with as like this:

let x = &5 as *_;

However, it fails with a fully unrestricted type on the right hand side with a unhelpful error message:

.../type_hole_1.rs:35:18: 35:22 error: the type of this value must be known in this context
.../type_hole_1.rs:35     let a: int = 1u32 as _;
                                       ^~~~

To quote @nikomatsakis:
"The reason that the type check doesn't work is because we check immediately that the as conversion is legal. We could just say that the type of <expr> as T is T and then go back and check later that the type conversion is legal once types are known. I kind of like that."

There are three possible ways to resolve this:

  • Explicitly allow type placeholders in as, which means fixing the as _ case.
  • Explicitly forbid type placeholders in as, which means preventing partial type hints in as in general.
  • Only forbid as _, while allowing partial type hints otherwise. This means catching the error case and emitting a useful error message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions