X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fhir_def%2Fsrc%2Fbody%2Fscope.rs;h=2658eece8e85e60fa93a8d95dbd4cd9bcb7a3bb4;hb=9c6542f2097df1cfcc9491036ec607c6a2842070;hp=6764de3a75e4b4105938b182bec80000def974d2;hpb=da80dfc0226af546244dacf3fbfdbb2b7136539a;p=rust.git diff --git a/crates/hir_def/src/body/scope.rs b/crates/hir_def/src/body/scope.rs index 6764de3a75e..2658eece8e8 100644 --- a/crates/hir_def/src/body/scope.rs +++ b/crates/hir_def/src/body/scope.rs @@ -8,7 +8,7 @@ use crate::{ body::Body, db::DefDatabase, - expr::{Expr, ExprId, LabelId, Pat, PatId, Statement}, + expr::{Expr, ExprId, LabelId, MatchGuard, Pat, PatId, Statement}, BlockId, DefWithBodyId, }; @@ -149,16 +149,17 @@ fn compute_block_scopes( ) { for stmt in statements { match stmt { - Statement::Let { pat, initializer, .. } => { + Statement::Let { pat, initializer, else_branch, .. } => { if let Some(expr) = initializer { - scopes.set_scope(*expr, scope); + compute_expr_scopes(*expr, body, scopes, scope); + } + if let Some(expr) = else_branch { compute_expr_scopes(*expr, body, scopes, scope); } scope = scopes.new_scope(scope); scopes.add_bindings(body, scope, *pat); } Statement::Expr { expr, .. } => { - scopes.set_scope(*expr, scope); compute_expr_scopes(*expr, body, scopes, scope); } } @@ -170,7 +171,7 @@ fn compute_block_scopes( fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) { let make_label = - |label: &Option<_>| label.map(|label| (label, body.labels[label].name.clone())); + |label: &Option| label.map(|label| (label, body.labels[label].name.clone())); scopes.set_scope(expr, scope); match &body[expr] { @@ -198,18 +199,27 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope } Expr::Lambda { args, body: body_expr, .. } => { let scope = scopes.new_scope(scope); - scopes.add_params_bindings(body, scope, &args); + scopes.add_params_bindings(body, scope, args); compute_expr_scopes(*body_expr, body, scopes, scope); } Expr::Match { expr, arms } => { compute_expr_scopes(*expr, body, scopes, scope); - for arm in arms { - let scope = scopes.new_scope(scope); + for arm in arms.iter() { + let mut scope = scopes.new_scope(scope); scopes.add_bindings(body, scope, arm.pat); - if let Some(guard) = arm.guard { - scopes.set_scope(guard, scope); - compute_expr_scopes(guard, body, scopes, scope); - } + match arm.guard { + Some(MatchGuard::If { expr: guard }) => { + scopes.set_scope(guard, scope); + compute_expr_scopes(guard, body, scopes, scope); + } + Some(MatchGuard::IfLet { pat, expr: guard }) => { + scopes.set_scope(guard, scope); + compute_expr_scopes(guard, body, scopes, scope); + scope = scopes.new_scope(scope); + scopes.add_bindings(body, scope, pat); + } + _ => {} + }; scopes.set_scope(arm.expr, scope); compute_expr_scopes(arm.expr, body, scopes, scope); } @@ -269,7 +279,7 @@ fn do_check(ra_fixture: &str, expected: &[&str]) { let actual = scopes .scope_chain(scope) .flat_map(|scope| scopes.entries(scope)) - .map(|it| it.name().to_string()) + .map(|it| it.name().to_smol_str()) .collect::>() .join("\n"); let expected = expected.join("\n");