11887: fix: Add missing fields diagnostic fix for patterns r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2022-04-03 13:42:15 +00:00 committed by GitHub
commit 65eaabc200
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 184 additions and 62 deletions

View file

@ -563,6 +563,49 @@ impl ast::RecordExprField {
}
}
impl ast::RecordPatFieldList {
pub fn add_field(&self, field: ast::RecordPatField) {
let is_multiline = self.syntax().text().contains_char('\n');
let whitespace = if is_multiline {
let indent = IndentLevel::from_node(self.syntax()) + 1;
make::tokens::whitespace(&format!("\n{}", indent))
} else {
make::tokens::single_space()
};
if is_multiline {
normalize_ws_between_braces(self.syntax());
}
let position = match self.fields().last() {
Some(last_field) => {
let comma = match last_field
.syntax()
.siblings_with_tokens(Direction::Next)
.filter_map(|it| it.into_token())
.find(|it| it.kind() == T![,])
{
Some(it) => it,
None => {
let comma = ast::make::token(T![,]);
ted::insert(Position::after(last_field.syntax()), &comma);
comma
}
};
Position::after(comma)
}
None => match self.l_curly_token() {
Some(it) => Position::after(it),
None => Position::last_child_of(self.syntax()),
},
};
ted::insert_all(position, vec![whitespace.into(), field.syntax().clone().into()]);
if is_multiline {
ted::insert(Position::after(field.syntax()), ast::make::token(T![,]));
}
}
}
impl ast::StmtList {
pub fn push_front(&self, statement: ast::Stmt) {
ted::insert(Position::after(self.l_curly_token().unwrap()), statement.syntax());

View file

@ -555,6 +555,10 @@ pub fn record_pat_field(name_ref: ast::NameRef, pat: ast::Pat) -> ast::RecordPat
ast_from_text(&format!("fn f(S {{ {}: {} }}: ()))", name_ref, pat))
}
pub fn record_pat_field_shorthand(name_ref: ast::NameRef) -> ast::RecordPatField {
ast_from_text(&format!("fn f(S {{ {} }}: ()))", name_ref))
}
/// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise.
pub fn path_pat(path: ast::Path) -> ast::Pat {
return from_text(&path.to_string());