Skip to content

Commit 5cade9b

Browse files
bors[bot]lnicola
andauthored
Merge #11565
11565: fix: Fix body selection in while loops r=Veykril a=lnicola CC #11561 Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents a153b18 + 87c4a41 commit 5cade9b

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

crates/hir_ty/src/tests/regression.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1245,3 +1245,25 @@ fn test() {
12451245
"#]],
12461246
);
12471247
}
1248+
1249+
#[test]
1250+
fn while_loop_block_expr_iterable() {
1251+
check_infer(
1252+
r#"
1253+
fn test() {
1254+
while { true } {
1255+
let y = 0;
1256+
}
1257+
}
1258+
"#,
1259+
expect![[r#"
1260+
10..59 '{ ... } }': ()
1261+
16..57 'while ... }': ()
1262+
22..30 '{ true }': bool
1263+
24..28 'true': bool
1264+
31..57 '{ ... }': ()
1265+
45..46 'y': i32
1266+
49..50 '0': i32
1267+
"#]],
1268+
);
1269+
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,6 @@ pub struct WhileExpr {
10351035
pub(crate) syntax: SyntaxNode,
10361036
}
10371037
impl ast::HasAttrs for WhileExpr {}
1038-
impl ast::HasLoopBody for WhileExpr {}
10391038
impl WhileExpr {
10401039
pub fn while_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![while]) }
10411040
pub fn condition(&self) -> Option<Expr> { support::child(&self.syntax) }

crates/syntax/src/ast/node_ext.rs

+9
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,15 @@ impl ast::HasLoopBody for ast::ForExpr {
769769
}
770770
}
771771

772+
impl ast::HasLoopBody for ast::WhileExpr {
773+
fn loop_body(&self) -> Option<ast::BlockExpr> {
774+
let mut exprs = support::children(self.syntax());
775+
let first = exprs.next();
776+
let second = exprs.next();
777+
second.or(first)
778+
}
779+
}
780+
772781
impl ast::HasAttrs for ast::AnyHasDocComments {}
773782

774783
impl From<ast::Adt> for ast::Item {

crates/syntax/src/tests/sourcegen_ast.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
8585
.traits
8686
.iter()
8787
.filter(|trait_name| {
88-
// For loops have two expressions so this might collide, therefor manual impl it
89-
node.name != "ForExpr" || trait_name.as_str() != "HasLoopBody"
88+
// Loops have two expressions so this might collide, therefor manual impl it
89+
node.name != "ForExpr" && node.name != "WhileExpr"
90+
|| trait_name.as_str() != "HasLoopBody"
9091
})
9192
.map(|trait_name| {
9293
let trait_name = format_ident!("{}", trait_name);

0 commit comments

Comments
 (0)