Skip to content

Commit bf893d5

Browse files
committed
internal: Assume condition/iterable is missing if there is only a BlockExpr
1 parent b4d652a commit bf893d5

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

crates/syntax/src/ast/generated/nodes.rs

-4
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,6 @@ impl ForExpr {
880880
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
881881
pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
882882
pub fn in_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![in]) }
883-
pub fn iterable(&self) -> Option<Expr> { support::child(&self.syntax) }
884883
}
885884

886885
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -890,7 +889,6 @@ pub struct IfExpr {
890889
impl ast::HasAttrs for IfExpr {}
891890
impl IfExpr {
892891
pub fn if_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![if]) }
893-
pub fn condition(&self) -> Option<Expr> { support::child(&self.syntax) }
894892
pub fn else_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![else]) }
895893
}
896894

@@ -1051,7 +1049,6 @@ pub struct WhileExpr {
10511049
impl ast::HasAttrs for WhileExpr {}
10521050
impl WhileExpr {
10531051
pub fn while_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![while]) }
1054-
pub fn condition(&self) -> Option<Expr> { support::child(&self.syntax) }
10551052
}
10561053

10571054
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -1170,7 +1167,6 @@ pub struct MatchGuard {
11701167
}
11711168
impl MatchGuard {
11721169
pub fn if_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![if]) }
1173-
pub fn condition(&self) -> Option<Expr> { support::child(&self.syntax) }
11741170
}
11751171

11761172
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

crates/syntax/src/ast/node_ext.rs

+38
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,19 @@ impl ast::GenericParamList {
806806
}
807807
}
808808

809+
impl ast::ForExpr {
810+
pub fn iterable(&self) -> Option<ast::Expr> {
811+
// If the iterable is a BlockExpr, check if the body is missing.
812+
// If it is assume the iterable is the expression that is missing instead.
813+
let mut exprs = support::children(self.syntax());
814+
let first = exprs.next();
815+
match first {
816+
Some(ast::Expr::BlockExpr(_)) => exprs.next().and(first),
817+
first => first,
818+
}
819+
}
820+
}
821+
809822
impl ast::HasLoopBody for ast::ForExpr {
810823
fn loop_body(&self) -> Option<ast::BlockExpr> {
811824
let mut exprs = support::children(self.syntax());
@@ -815,6 +828,19 @@ impl ast::HasLoopBody for ast::ForExpr {
815828
}
816829
}
817830

831+
impl ast::WhileExpr {
832+
pub fn condition(&self) -> Option<ast::Expr> {
833+
// If the condition is a BlockExpr, check if the body is missing.
834+
// If it is assume the condition is the expression that is missing instead.
835+
let mut exprs = support::children(self.syntax());
836+
let first = exprs.next();
837+
match first {
838+
Some(ast::Expr::BlockExpr(_)) => exprs.next().and(first),
839+
first => first,
840+
}
841+
}
842+
}
843+
818844
impl ast::HasLoopBody for ast::WhileExpr {
819845
fn loop_body(&self) -> Option<ast::BlockExpr> {
820846
let mut exprs = support::children(self.syntax());
@@ -835,3 +861,15 @@ impl From<ast::Adt> for ast::Item {
835861
}
836862
}
837863
}
864+
865+
impl ast::IfExpr {
866+
pub fn condition(&self) -> Option<ast::Expr> {
867+
support::child(&self.syntax)
868+
}
869+
}
870+
871+
impl ast::MatchGuard {
872+
pub fn condition(&self) -> Option<ast::Expr> {
873+
support::child(&self.syntax)
874+
}
875+
}

crates/syntax/src/tests/sourcegen_ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, label: Option<&String>, r
682682
| "value"
683683
| "trait"
684684
| "self_ty"
685+
| "iterable"
686+
| "condition"
685687
);
686688
if manually_implemented {
687689
return;

0 commit comments

Comments
 (0)