Skip to content

fix: Fix body selection in while loops #11565

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

Merged
merged 1 commit into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/hir_ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,3 +1245,25 @@ fn test() {
"#]],
);
}

#[test]
fn while_loop_block_expr_iterable() {
check_infer(
r#"
fn test() {
while { true } {
let y = 0;
}
}
"#,
expect![[r#"
10..59 '{ ... } }': ()
16..57 'while ... }': ()
22..30 '{ true }': bool
24..28 'true': bool
31..57 '{ ... }': ()
45..46 'y': i32
49..50 '0': i32
"#]],
);
}
1 change: 0 additions & 1 deletion crates/syntax/src/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,6 @@ pub struct WhileExpr {
pub(crate) syntax: SyntaxNode,
}
impl ast::HasAttrs for WhileExpr {}
impl ast::HasLoopBody for WhileExpr {}
impl WhileExpr {
pub fn while_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![while]) }
pub fn condition(&self) -> Option<Expr> { support::child(&self.syntax) }
Expand Down
9 changes: 9 additions & 0 deletions crates/syntax/src/ast/node_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,15 @@ impl ast::HasLoopBody for ast::ForExpr {
}
}

impl ast::HasLoopBody for ast::WhileExpr {
fn loop_body(&self) -> Option<ast::BlockExpr> {
let mut exprs = support::children(self.syntax());
let first = exprs.next();
let second = exprs.next();
second.or(first)
}
}

impl ast::HasAttrs for ast::AnyHasDocComments {}

impl From<ast::Adt> for ast::Item {
Expand Down
5 changes: 3 additions & 2 deletions crates/syntax/src/tests/sourcegen_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
.traits
.iter()
.filter(|trait_name| {
// For loops have two expressions so this might collide, therefor manual impl it
node.name != "ForExpr" || trait_name.as_str() != "HasLoopBody"
// Loops have two expressions so this might collide, therefor manual impl it
node.name != "ForExpr" && node.name != "WhileExpr"
|| trait_name.as_str() != "HasLoopBody"
})
.map(|trait_name| {
let trait_name = format_ident!("{}", trait_name);
Expand Down