Skip to content

Commit 7765c1a

Browse files
bors[bot]Veykril
andauthored
Merge #10668
10668: fix: Fix for-loop expressions breaking with BlockExpr iterable r=Veykril a=Veykril Fixes #10665 bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 3ad83cc + c93983e commit 7765c1a

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

crates/hir_ty/src/tests/regression.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,3 +1221,27 @@ fn mamba(a: U32!(), p: u32) -> u32 {
12211221
"#,
12221222
)
12231223
}
1224+
1225+
#[test]
1226+
fn for_loop_block_expr_iterable() {
1227+
check_infer(
1228+
r#"
1229+
fn test() {
1230+
for _ in { let x = 0; } {
1231+
let y = 0;
1232+
}
1233+
}
1234+
"#,
1235+
expect![[r#"
1236+
10..68 '{ ... } }': ()
1237+
16..66 'for _ ... }': ()
1238+
20..21 '_': {unknown}
1239+
25..39 '{ let x = 0; }': ()
1240+
31..32 'x': i32
1241+
35..36 '0': i32
1242+
40..66 '{ ... }': ()
1243+
54..55 'y': i32
1244+
58..59 '0': i32
1245+
"#]],
1246+
);
1247+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,6 @@ pub struct ForExpr {
850850
pub(crate) syntax: SyntaxNode,
851851
}
852852
impl ast::HasAttrs for ForExpr {}
853-
impl ast::HasLoopBody for ForExpr {}
854853
impl ForExpr {
855854
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
856855
pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }

crates/syntax/src/ast/node_ext.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,15 @@ impl ast::GenericParamList {
754754
}
755755
}
756756

757+
impl ast::HasLoopBody for ast::ForExpr {
758+
fn loop_body(&self) -> Option<ast::BlockExpr> {
759+
let mut exprs = support::children(self.syntax());
760+
let first = exprs.next();
761+
let second = exprs.next();
762+
second.or(first)
763+
}
764+
}
765+
757766
impl ast::HasDocComments for ast::SourceFile {}
758767
impl ast::HasDocComments for ast::Fn {}
759768
impl ast::HasDocComments for ast::Struct {}

crates/syntax/src/tests/sourcegen_ast.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,17 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
8181
.map(|node| {
8282
let name = format_ident!("{}", node.name);
8383
let kind = format_ident!("{}", to_upper_snake_case(&node.name));
84-
let traits = node.traits.iter().map(|trait_name| {
85-
let trait_name = format_ident!("{}", trait_name);
86-
quote!(impl ast::#trait_name for #name {})
87-
});
84+
let traits = node
85+
.traits
86+
.iter()
87+
.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"
90+
})
91+
.map(|trait_name| {
92+
let trait_name = format_ident!("{}", trait_name);
93+
quote!(impl ast::#trait_name for #name {})
94+
});
8895

8996
let methods = node.fields.iter().map(|field| {
9097
let method_name = field.method_name();

0 commit comments

Comments
 (0)