-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Convert match to let-else
assist
#13516
Add Convert match to let-else
assist
#13516
Conversation
crates/hir-ty/src/infer.rs
Outdated
@@ -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>>, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@bors r+ |
☀️ Test successful - checks-actions |
…-return-assist, r=jonas-schievink Use let-else statements in `Convert to guarded return` assist Follow up for #13516, addresses remaining part of #13254 (comment)
Does this also fix #11908? |
@unexge - I was hoping you could explain to me what you mean by the terms "diverging arm" and "extracting arm" in match statements? I'm not seeing that term used elsewhere so i'm not sure what exactly they're supposed to describe. The reason i'm interested is that i'm trying to expand the functionality of this convert-match-to-let-else assist that you created! My current guess is that a diverging arm is one which simply Would really appreciate any more context you may be able to give about how you approached creating this assist, thanks! |
diverging arm = a match arm that has type extracting I imagine is the arm that "extracts" the value of interest from the value being match on |
@liamhession I believe @Veykril answered your question but to give a bit more context: You can see that pattern a lot in Rust Analyzer especially in // This only applies for tuple patterns, types, and initializers.
let tuple_pat = match pat { // <- extracts `ast::Pat::TuplePat` from `ast::Pat`
ast::Pat::TuplePat(pat) => pat, // <- extracting arm
_ => return None, // <- diverging arm
};
// ...
let tuple_init = match init { // <- extracts `ast::Expr::TupleExpr` from `ast::Expr`
ast::Expr::TupleExpr(expr) => expr, // <- extracting arm
_ => return None, // <- diverging arm
}; from rust-analyzer/crates/ide-assists/src/handlers/unwrap_tuple.rs Lines 33 to 45 in 2656297
Happy to help if you have any questions and I can also try to give you some pointers if you have some specific improvements in your mind! |
Very nice context to have, thanks @unexge and @Veykril My confusion came about in part because i couldn't find really any other use of those terms "extracting arm" and "diverging arm" through Googling. But their names make sense in the context you laid out. The improvements i'm aiming for are to expand the variety of "scrutinee expressions" (just learned that vocab from https://doc.rust-lang.org/reference/expressions/match-expr.html) this assist can be used for. The relevant issue within rust-analyzer is #13540 I've first been working on tweaking it so that it will recognize a statement with tuple after the I think first i'll need to update the |
@liamhession yes I believe, you need to update following functions to be more generic: |
Closes #13254