Skip to content

Cleanup hack to expect scrutinee: bool in if-to-match desugaring in match-expr typeck #60707

Closed as not planned
@Centril

Description

@Centril

In #59288 a hack is introduced in rustc_typeck::check::_match::check_match that checks, via MatchSource::IfDesugar, whether an if-expression was desugared to match and if so, it requires that the scrutinee be typed at bool.

We might want to remove this hack in favor of a more principled solution, with options:

  1. Fix type ascription such that it admits coercions and then use ExprKind::Type(...) to set the expectation that the type be coercible to bool. This will require adjustments to diagnostics logic to reduce clutter.

  2. Use a cast $scrutinee as bool and use ExprKind::Cast(...) to set the expectation that the type be coercible to bool. Same applies wrt. diagnostics.

  3. Add a flag in the type ascription HIR that enables the "coercion" behavior we want. This is still a hack but potentially less of a hack.

  4. Remove the hack entirely and permit if &true { ... } to type check.

    This is a semantic change to the language but perhaps a desirable one. I (@Centril) believe it would facilitate usability since there are cases where you call a function and end up with &bool, e.g. indexing a slice of bools. Right now you would have to deref the returned &bool to make it work. I also think it is a semantic simplification of the language since if becomes more of a pure in-language desugaring than before. cc @rust-lang/lang about this idea of applying default-match-bindings to if conditions.

For more context, see #59288 (comment) and #59288 (review).

Metadata

Metadata

Assignees

Labels

C-cleanupCategory: PRs that clean code up or issues documenting cleanup.F-let_chains`#![feature(let_chains)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions