Skip to content

Add Convert match to let-else assist #13516

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

Merged
merged 6 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ pub struct InferenceResult {
assoc_resolutions: FxHashMap<ExprOrPatId, AssocItemId>,
pub diagnostics: Vec<InferenceDiagnostic>,
pub type_of_expr: ArenaMap<ExprId, Ty>,
/// For each match expr, record diverging arm's expr.
pub diverging_arms: FxHashMap<ExprId, Vec<ExprId>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to get diverging arms by looking types of arm expressions via hir::semantics::Semantics::type_of_expr but we coerce all match arm expressions and the whole match expression to the Never type when we encounter any diverging arm, so I couldn't find a way to distinguish diverging and non-diverging arms without recording this information.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it help if you look at the unadjusted type? In an expression like

    match Some(0) {
        Some(_) => {}
        None => return
    }

return has type ! but is adjusted to (), so the unadjusted type should provide the information you need

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I found the issue – your tests use Option but don't declare it. I'll push a fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh 😅 great! Thanks for the fix.

/// For each pattern record the type it resolves to.
///
/// **Note**: When a pattern type is resolved it may still contain
Expand Down
5 changes: 5 additions & 0 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ impl<'a> InferenceContext<'a> {

let matchee_diverges = self.diverges;
let mut all_arms_diverge = Diverges::Always;
let mut diverging_arms = Vec::new();

for arm in arms.iter() {
self.diverges = Diverges::Maybe;
Expand All @@ -387,11 +388,15 @@ impl<'a> InferenceContext<'a> {
}

let arm_ty = self.infer_expr_inner(arm.expr, &expected);
if self.diverges.is_always() {
diverging_arms.push(arm.expr);
}
all_arms_diverge &= self.diverges;
coerce.coerce(self, Some(arm.expr), &arm_ty);
}

self.diverges = matchee_diverges | all_arms_diverge;
self.result.diverging_arms.insert(tgt_expr, diverging_arms);

coerce.complete()
}
Expand Down
8 changes: 8 additions & 0 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool {
self.imp.is_unsafe_ident_pat(ident_pat)
}

pub fn is_diverging_match_arm(&self, match_arm: &ast::MatchArm) -> Option<bool> {
self.imp.is_diverging_match_arm(match_arm)
}
}

impl<'db> SemanticsImpl<'db> {
Expand Down Expand Up @@ -1421,6 +1425,10 @@ impl<'db> SemanticsImpl<'db> {
.map(|ty| ty.original.is_packed(self.db))
.unwrap_or(false)
}

fn is_diverging_match_arm(&self, match_arm: &ast::MatchArm) -> Option<bool> {
self.analyze(match_arm.syntax())?.is_diverging_match_arm(self.db, match_arm)
}
}

fn macro_call_to_macro_id(
Expand Down
15 changes: 15 additions & 0 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,21 @@ impl SourceAnalyzer {
false
}

pub(crate) fn is_diverging_match_arm(
&self,
db: &dyn HirDatabase,
match_arm: &ast::MatchArm,
) -> Option<bool> {
let infer = self.infer.as_ref()?;
let match_expr = match_arm.syntax().ancestors().find_map(ast::MatchExpr::cast)?;
let match_id = self.expr_id(db, &match_expr.into())?;
let diverging_arms = infer.diverging_arms.get(&match_id)?;
let match_arm_expr = match_arm.expr()?;
let match_arm_expr_id = self.expr_id(db, &match_arm_expr)?;

Some(diverging_arms.contains(&match_arm_expr_id))
}

fn resolve_impl_method_or_trait_def(
&self,
db: &dyn HirDatabase,
Expand Down
Loading