Skip to content
Merged
Changes from 3 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
189 changes: 187 additions & 2 deletions crates/hir-expand/src/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
preorder.skip_subtree();
continue;
}

// In some other situations, we can fix things by just appending some tokens.
let end_range = TextRange::empty(node.text_range().end());
match_ast! {
Expand Down Expand Up @@ -194,7 +193,105 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
}
},
// FIXME: foo::
// FIXME: for, match etc.
ast::MatchExpr(it) => {
if it.expr().is_none() {
let match_token = match it.match_token() {
Some(t) => t,
None => continue
};
append.insert(match_token.into(), vec![
SyntheticToken {
kind: SyntaxKind::IDENT,
text: "__ra_fixup".into(),
range: end_range,
id: EMPTY_ID
},
]);
}
if it.match_arm_list().is_none() {
// No match arms
append.insert(node.clone().into(), vec![
SyntheticToken {
kind: SyntaxKind::L_CURLY,
text: "{".into(),
range: end_range,
id: EMPTY_ID,
},
SyntheticToken {
kind: SyntaxKind::UNDERSCORE,
text: "_".into(),
range: end_range,
id: EMPTY_ID
},
SyntheticToken {
kind: SyntaxKind::EQ,
text: "=".into(),
range: end_range,
id: EMPTY_ID
},
SyntheticToken {
kind: SyntaxKind::R_ANGLE,
text: ">".into(),
range: end_range,
id: EMPTY_ID
},
SyntheticToken {
kind: SyntaxKind::L_CURLY,
text: "{".into(),
range: end_range,
id: EMPTY_ID,
},
SyntheticToken {
kind: SyntaxKind::R_CURLY,
text: "}".into(),
range: end_range,
id: EMPTY_ID,
},
SyntheticToken {
kind: SyntaxKind::R_CURLY,
text: "}".into(),
range: end_range,
id: EMPTY_ID,
},
]);
}
},
ast::ForExpr(it) => {
let for_token = match it.for_token() {
Some(token) => token,
None => continue
};

let [pat, in_token, iter] = [
(SyntaxKind::UNDERSCORE, "_"),
(SyntaxKind::IN_KW, "in"),
(SyntaxKind::IDENT, "__ra_fixup")
].map(|(kind, text)| SyntheticToken { kind, text: text.into(), range: end_range, id: EMPTY_ID});

if it.pat().is_none() && it.in_token().is_none() && it.iterable().is_none() {
append.insert(for_token.into(), vec![pat, in_token, iter]);
}

// Tricky: add logic to add in just a pattern or iterable if not all
// the pieces are missing

if it.loop_body().is_none() {
append.insert(node.clone().into(), vec![
SyntheticToken {
kind: SyntaxKind::L_CURLY,
text: "{".into(),
range: end_range,
id: EMPTY_ID,
},
SyntheticToken {
kind: SyntaxKind::R_CURLY,
text: "}".into(),
range: end_range,
id: EMPTY_ID,
},
]);
}
},
_ => (),
}
}
Expand Down Expand Up @@ -287,6 +384,94 @@ mod tests {
assert_eq!(tt.to_string(), original_as_tt.to_string());
}

#[test]
fn for_no_iter_no_body() {
check(
r#"
fn foo() {
for
}
"#,
expect![[r#"
fn foo () {for _ in __ra_fixup {}}
"#]],
)
}

#[test]
fn for_no_iter() {
check(
r#"
fn foo() {
for {}
}
"#,
expect![[r#"
fn foo () {for _ in __ra_fixup {}}
"#]],
)
}

#[test]
fn for_no_body() {
check(
r#"
fn foo() {
for bar in qux
}
"#,
expect![[r#"
fn foo () {for bar in qux {}}
"#]],
)
}

#[test]
fn match_no_expr_no_arms() {
check(
r#"
fn foo() {
match
}
"#,
expect![[r#"
fn foo () {match __ra_fixup {_ => {}}}
"#]],
)
}

#[test]
fn match_expr_no_arms() {
check(
r#"
fn foo() {
match x {

}
}
"#,
expect![[r#"
fn foo () {match x {}}
"#]],
)
}

#[test]
fn match_no_expr() {
check(
r#"
fn foo() {
match {
_ => {}
}
}
"#,
expect![[r#"
fn foo () {match __ra_fixup {_ => {}}}
"#]],
)
}

#[test]
fn incomplete_field_expr_1() {
check(
Expand Down