]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_db/src/helpers.rs
Merge #10563
[rust.git] / crates / ide_db / src / helpers.rs
index c92893716fb11940b871de876e29f6b3d0ac713c..173e55b33f6ac7b6579b96d4a0065639be65740e 100644 (file)
@@ -13,7 +13,7 @@
 use either::Either;
 use hir::{ItemInNs, MacroDef, ModuleDef, Name, Semantics};
 use syntax::{
-    ast::{self, make, LoopBodyOwner},
+    ast::{self, make, HasLoopBody},
     AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxToken, TokenAtOffset, WalkEvent, T,
 };
 
@@ -53,12 +53,9 @@ pub fn try_resolve_derive_input_at(
         .take_while(|tok| tok.kind() != T!['('] && tok.kind() != T![,])
         .collect();
     let path = ast::Path::parse(&tokens.into_iter().rev().join("")).ok()?;
-    match sema.scope(tt.syntax()).speculative_resolve(&path) {
-        Some(hir::PathResolution::Macro(makro)) if makro.kind() == hir::MacroKind::Derive => {
-            Some(makro)
-        }
-        _ => None,
-    }
+    sema.scope(tt.syntax())
+        .speculative_resolve_as_mac(&path)
+        .filter(|mac| mac.kind() == hir::MacroKind::Derive)
 }
 
 /// Picks the token with the highest rank returned by the passed in function.
@@ -139,26 +136,27 @@ pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
 pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
     match expr {
         ast::Expr::BlockExpr(b) => {
-            if let Some(e) = b.tail_expr() {
-                for_each_tail_expr(&e, cb);
-            }
-        }
-        ast::Expr::EffectExpr(e) => match e.effect() {
-            ast::Effect::Label(label) => {
-                for_each_break_expr(Some(label), e.block_expr(), &mut |b| {
-                    cb(&ast::Expr::BreakExpr(b))
-                });
-                if let Some(b) = e.block_expr() {
-                    for_each_tail_expr(&ast::Expr::BlockExpr(b), cb);
+            match b.modifier() {
+                Some(
+                    ast::BlockModifier::Async(_)
+                    | ast::BlockModifier::Try(_)
+                    | ast::BlockModifier::Const(_),
+                ) => return cb(expr),
+
+                Some(ast::BlockModifier::Label(label)) => {
+                    for_each_break_expr(Some(label), b.stmt_list(), &mut |b| {
+                        cb(&ast::Expr::BreakExpr(b))
+                    });
                 }
+                Some(ast::BlockModifier::Unsafe(_)) => (),
+                None => (),
             }
-            ast::Effect::Unsafe(_) => {
-                if let Some(e) = e.block_expr().and_then(|b| b.tail_expr()) {
+            if let Some(stmt_list) = b.stmt_list() {
+                if let Some(e) = stmt_list.tail_expr() {
                     for_each_tail_expr(&e, cb);
                 }
             }
-            ast::Effect::Async(_) | ast::Effect::Try(_) | ast::Effect::Const(_) => cb(expr),
-        },
+        }
         ast::Expr::IfExpr(if_) => {
             let mut if_ = if_.clone();
             loop {
@@ -176,7 +174,9 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
             }
         }
         ast::Expr::LoopExpr(l) => {
-            for_each_break_expr(l.label(), l.loop_body(), &mut |b| cb(&ast::Expr::BreakExpr(b)))
+            for_each_break_expr(l.label(), l.loop_body().and_then(|it| it.stmt_list()), &mut |b| {
+                cb(&ast::Expr::BreakExpr(b))
+            })
         }
         ast::Expr::MatchExpr(m) => {
             if let Some(arms) = m.match_arm_list() {
@@ -216,7 +216,7 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
 /// Calls `cb` on each break expr inside of `body` that is applicable for the given label.
 pub fn for_each_break_expr(
     label: Option<ast::Label>,
-    body: Option<ast::BlockExpr>,
+    body: Option<ast::StmtList>,
     cb: &mut dyn FnMut(ast::BreakExpr),
 ) {
     let label = label.and_then(|lbl| lbl.lifetime());
@@ -236,7 +236,7 @@ pub fn for_each_break_expr(
                     ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_) => {
                         depth += 1
                     }
-                    ast::Expr::EffectExpr(e) if e.label().is_some() => depth += 1,
+                    ast::Expr::BlockExpr(e) if e.label().is_some() => depth += 1,
                     ast::Expr::BreakExpr(b)
                         if (depth == 0 && b.lifetime().is_none()) || eq_label(b.lifetime()) =>
                     {
@@ -248,7 +248,7 @@ pub fn for_each_break_expr(
                     ast::Expr::LoopExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::ForExpr(_) => {
                         depth -= 1
                     }
-                    ast::Expr::EffectExpr(e) if e.label().is_some() => depth -= 1,
+                    ast::Expr::BlockExpr(e) if e.label().is_some() => depth -= 1,
                     _ => (),
                 },
             }