Skip to content

Commit c271a56

Browse files
Merge pull request #22041 from Shourya742/2026-04-14-remove-generic-params-owner-edit
Remove generic params owner edit
2 parents f539111 + cde20de commit c271a56

2 files changed

Lines changed: 41 additions & 281 deletions

File tree

crates/hir-expand/src/builtin/derive_macro.rs

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use crate::{
2222
use syntax::{
2323
ast::{
2424
self, AstNode, FieldList, HasAttrs, HasGenericArgs, HasGenericParams, HasModuleItem,
25-
HasName, HasTypeBounds, edit_in_place::GenericParamsOwnerEdit, make,
25+
HasName, HasTypeBounds, make, syntax_factory::SyntaxFactory,
2626
},
27+
syntax_editor::{GetOrCreateWhereClause, SyntaxEditor},
2728
ted,
2829
};
2930

@@ -1150,11 +1151,9 @@ fn coerce_pointee_expand(
11501151

11511152
const ADDED_PARAM: &str = "__S";
11521153

1153-
let where_clause = strukt.get_or_create_where_clause();
1154+
let mut new_predicates: Vec<ast::WherePred> = Vec::new();
11541155

11551156
{
1156-
let mut new_predicates = Vec::new();
1157-
11581157
// # Rewrite generic parameter bounds
11591158
// For each bound `U: ..` in `struct<U: ..>`, make a new bound with `__S` in place of `#[pointee]`
11601159
// Example:
@@ -1196,16 +1195,13 @@ fn coerce_pointee_expand(
11961195
} else {
11971196
make::name_ref(&param_name.text())
11981197
};
1199-
new_predicates.push(
1200-
make::where_pred(
1201-
Either::Right(make::ty_path(make::path_from_segments(
1202-
[make::path_segment(new_bounds_target)],
1203-
false,
1204-
))),
1205-
new_bounds,
1206-
)
1207-
.clone_for_update(),
1208-
);
1198+
new_predicates.push(make::where_pred(
1199+
Either::Right(make::ty_path(make::path_from_segments(
1200+
[make::path_segment(new_bounds_target)],
1201+
false,
1202+
))),
1203+
new_bounds,
1204+
));
12091205
}
12101206
}
12111207

@@ -1235,7 +1231,7 @@ fn coerce_pointee_expand(
12351231
//
12361232
// We should also write a few new `where` bounds from `#[pointee] T` to `__S`
12371233
// as well as any bound that indirectly involves the `#[pointee] T` type.
1238-
for predicate in where_clause.predicates() {
1234+
for predicate in strukt.where_clause().into_iter().flat_map(|wc| wc.predicates()) {
12391235
let predicate = predicate.clone_subtree().clone_for_update();
12401236
let Some(pred_target) = predicate.ty() else { continue };
12411237

@@ -1269,42 +1265,43 @@ fn coerce_pointee_expand(
12691265
);
12701266
}
12711267
}
1272-
1273-
for new_predicate in new_predicates {
1274-
where_clause.add_predicate(new_predicate);
1275-
}
12761268
}
12771269

12781270
{
12791271
// # Add `Unsize<__S>` bound to `#[pointee]` at the generic parameter location
12801272
//
12811273
// Find the `#[pointee]` parameter and add an `Unsize<__S>` bound to it.
1282-
where_clause.add_predicate(
1283-
make::where_pred(
1284-
Either::Right(make::ty_path(make::path_from_segments(
1285-
[make::path_segment(make::name_ref(&pointee_param_name.text()))],
1286-
false,
1287-
))),
1288-
[make::type_bound(make::ty_path(make::path_from_segments(
1289-
[
1290-
make::path_segment(make::name_ref("core")),
1291-
make::path_segment(make::name_ref("marker")),
1292-
make::generic_ty_path_segment(
1293-
make::name_ref("Unsize"),
1294-
[make::type_arg(make::ty_path(make::path_from_segments(
1295-
[make::path_segment(make::name_ref(ADDED_PARAM))],
1296-
false,
1297-
)))
1298-
.into()],
1299-
),
1300-
],
1301-
true,
1302-
)))],
1303-
)
1304-
.clone_for_update(),
1305-
);
1274+
new_predicates.push(make::where_pred(
1275+
Either::Right(make::ty_path(make::path_from_segments(
1276+
[make::path_segment(make::name_ref(&pointee_param_name.text()))],
1277+
false,
1278+
))),
1279+
[make::type_bound(make::ty_path(make::path_from_segments(
1280+
[
1281+
make::path_segment(make::name_ref("core")),
1282+
make::path_segment(make::name_ref("marker")),
1283+
make::generic_ty_path_segment(
1284+
make::name_ref("Unsize"),
1285+
[make::type_arg(make::ty_path(make::path_from_segments(
1286+
[make::path_segment(make::name_ref(ADDED_PARAM))],
1287+
false,
1288+
)))
1289+
.into()],
1290+
),
1291+
],
1292+
true,
1293+
)))],
1294+
));
13061295
}
13071296

1297+
let (mut editor, strukt) = SyntaxEditor::with_ast_node(strukt);
1298+
let make = SyntaxFactory::with_mappings();
1299+
strukt.get_or_create_where_clause(&mut editor, &make, new_predicates.into_iter());
1300+
editor.add_mappings(make.finish_with_mappings());
1301+
let edit = editor.finish();
1302+
let strukt = ast::Struct::cast(edit.new_root().clone()).unwrap();
1303+
let adt = ast::Adt::Struct(strukt.clone());
1304+
13081305
let self_for_traits = {
13091306
// Replace the `#[pointee]` with `__S`.
13101307
let mut type_param_idx = 0;

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 1 addition & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -9,221 +9,13 @@ use crate::{
99
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
1010
SyntaxNode, SyntaxToken,
1111
algo::{self, neighbor},
12-
ast::{self, HasGenericParams, edit::IndentLevel, make, syntax_factory::SyntaxFactory},
12+
ast::{self, edit::IndentLevel, make, syntax_factory::SyntaxFactory},
1313
syntax_editor::{Position, SyntaxEditor},
1414
ted,
1515
};
1616

1717
use super::{GenericParam, HasName};
1818

19-
pub trait GenericParamsOwnerEdit: ast::HasGenericParams {
20-
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList;
21-
fn get_or_create_where_clause(&self) -> ast::WhereClause;
22-
}
23-
24-
impl GenericParamsOwnerEdit for ast::Fn {
25-
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
26-
match self.generic_param_list() {
27-
Some(it) => it,
28-
None => {
29-
let position = if let Some(name) = self.name() {
30-
ted::Position::after(name.syntax)
31-
} else if let Some(fn_token) = self.fn_token() {
32-
ted::Position::after(fn_token)
33-
} else if let Some(param_list) = self.param_list() {
34-
ted::Position::before(param_list.syntax)
35-
} else {
36-
ted::Position::last_child_of(self.syntax())
37-
};
38-
create_generic_param_list(position)
39-
}
40-
}
41-
}
42-
43-
fn get_or_create_where_clause(&self) -> ast::WhereClause {
44-
if self.where_clause().is_none() {
45-
let position = if let Some(ty) = self.ret_type() {
46-
ted::Position::after(ty.syntax())
47-
} else if let Some(param_list) = self.param_list() {
48-
ted::Position::after(param_list.syntax())
49-
} else {
50-
ted::Position::last_child_of(self.syntax())
51-
};
52-
create_where_clause(position);
53-
}
54-
self.where_clause().unwrap()
55-
}
56-
}
57-
58-
impl GenericParamsOwnerEdit for ast::Impl {
59-
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
60-
match self.generic_param_list() {
61-
Some(it) => it,
62-
None => {
63-
let position = match self.impl_token() {
64-
Some(imp_token) => ted::Position::after(imp_token),
65-
None => ted::Position::last_child_of(self.syntax()),
66-
};
67-
create_generic_param_list(position)
68-
}
69-
}
70-
}
71-
72-
fn get_or_create_where_clause(&self) -> ast::WhereClause {
73-
if self.where_clause().is_none() {
74-
let position = match self.assoc_item_list() {
75-
Some(items) => ted::Position::before(items.syntax()),
76-
None => ted::Position::last_child_of(self.syntax()),
77-
};
78-
create_where_clause(position);
79-
}
80-
self.where_clause().unwrap()
81-
}
82-
}
83-
84-
impl GenericParamsOwnerEdit for ast::Trait {
85-
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
86-
match self.generic_param_list() {
87-
Some(it) => it,
88-
None => {
89-
let position = if let Some(name) = self.name() {
90-
ted::Position::after(name.syntax)
91-
} else if let Some(trait_token) = self.trait_token() {
92-
ted::Position::after(trait_token)
93-
} else {
94-
ted::Position::last_child_of(self.syntax())
95-
};
96-
create_generic_param_list(position)
97-
}
98-
}
99-
}
100-
101-
fn get_or_create_where_clause(&self) -> ast::WhereClause {
102-
if self.where_clause().is_none() {
103-
let position = match (self.assoc_item_list(), self.semicolon_token()) {
104-
(Some(items), _) => ted::Position::before(items.syntax()),
105-
(_, Some(tok)) => ted::Position::before(tok),
106-
(None, None) => ted::Position::last_child_of(self.syntax()),
107-
};
108-
create_where_clause(position);
109-
}
110-
self.where_clause().unwrap()
111-
}
112-
}
113-
114-
impl GenericParamsOwnerEdit for ast::TypeAlias {
115-
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
116-
match self.generic_param_list() {
117-
Some(it) => it,
118-
None => {
119-
let position = if let Some(name) = self.name() {
120-
ted::Position::after(name.syntax)
121-
} else if let Some(trait_token) = self.type_token() {
122-
ted::Position::after(trait_token)
123-
} else {
124-
ted::Position::last_child_of(self.syntax())
125-
};
126-
create_generic_param_list(position)
127-
}
128-
}
129-
}
130-
131-
fn get_or_create_where_clause(&self) -> ast::WhereClause {
132-
if self.where_clause().is_none() {
133-
let position = match self.eq_token() {
134-
Some(tok) => ted::Position::before(tok),
135-
None => match self.semicolon_token() {
136-
Some(tok) => ted::Position::before(tok),
137-
None => ted::Position::last_child_of(self.syntax()),
138-
},
139-
};
140-
create_where_clause(position);
141-
}
142-
self.where_clause().unwrap()
143-
}
144-
}
145-
146-
impl GenericParamsOwnerEdit for ast::Struct {
147-
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
148-
match self.generic_param_list() {
149-
Some(it) => it,
150-
None => {
151-
let position = if let Some(name) = self.name() {
152-
ted::Position::after(name.syntax)
153-
} else if let Some(struct_token) = self.struct_token() {
154-
ted::Position::after(struct_token)
155-
} else {
156-
ted::Position::last_child_of(self.syntax())
157-
};
158-
create_generic_param_list(position)
159-
}
160-
}
161-
}
162-
163-
fn get_or_create_where_clause(&self) -> ast::WhereClause {
164-
if self.where_clause().is_none() {
165-
let tfl = self.field_list().and_then(|fl| match fl {
166-
ast::FieldList::RecordFieldList(_) => None,
167-
ast::FieldList::TupleFieldList(it) => Some(it),
168-
});
169-
let position = if let Some(tfl) = tfl {
170-
ted::Position::after(tfl.syntax())
171-
} else if let Some(gpl) = self.generic_param_list() {
172-
ted::Position::after(gpl.syntax())
173-
} else if let Some(name) = self.name() {
174-
ted::Position::after(name.syntax())
175-
} else {
176-
ted::Position::last_child_of(self.syntax())
177-
};
178-
create_where_clause(position);
179-
}
180-
self.where_clause().unwrap()
181-
}
182-
}
183-
184-
impl GenericParamsOwnerEdit for ast::Enum {
185-
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
186-
match self.generic_param_list() {
187-
Some(it) => it,
188-
None => {
189-
let position = if let Some(name) = self.name() {
190-
ted::Position::after(name.syntax)
191-
} else if let Some(enum_token) = self.enum_token() {
192-
ted::Position::after(enum_token)
193-
} else {
194-
ted::Position::last_child_of(self.syntax())
195-
};
196-
create_generic_param_list(position)
197-
}
198-
}
199-
}
200-
201-
fn get_or_create_where_clause(&self) -> ast::WhereClause {
202-
if self.where_clause().is_none() {
203-
let position = if let Some(gpl) = self.generic_param_list() {
204-
ted::Position::after(gpl.syntax())
205-
} else if let Some(name) = self.name() {
206-
ted::Position::after(name.syntax())
207-
} else {
208-
ted::Position::last_child_of(self.syntax())
209-
};
210-
create_where_clause(position);
211-
}
212-
self.where_clause().unwrap()
213-
}
214-
}
215-
216-
fn create_where_clause(position: ted::Position) {
217-
let where_clause = make::where_clause(empty()).clone_for_update();
218-
ted::insert(position, where_clause.syntax());
219-
}
220-
221-
fn create_generic_param_list(position: ted::Position) -> ast::GenericParamList {
222-
let gpl = make::generic_param_list(empty()).clone_for_update();
223-
ted::insert_raw(position, gpl.syntax());
224-
gpl
225-
}
226-
22719
pub trait AttrsOwnerEdit: ast::HasAttrs {
22820
fn remove_attrs_and_docs(&self) {
22921
remove_attrs_and_docs(self.syntax());
@@ -879,8 +671,6 @@ impl<N: AstNode + Clone> Indent for N {}
879671

880672
#[cfg(test)]
881673
mod tests {
882-
use std::fmt;
883-
884674
use parser::Edition;
885675

886676
use crate::SourceFile;
@@ -892,33 +682,6 @@ mod tests {
892682
parse.tree().syntax().descendants().find_map(N::cast).unwrap().clone_for_update()
893683
}
894684

895-
#[test]
896-
fn test_create_generic_param_list() {
897-
fn check_create_gpl<N: GenericParamsOwnerEdit + fmt::Display>(before: &str, after: &str) {
898-
let gpl_owner = ast_mut_from_text::<N>(before);
899-
gpl_owner.get_or_create_generic_param_list();
900-
assert_eq!(gpl_owner.to_string(), after);
901-
}
902-
903-
check_create_gpl::<ast::Fn>("fn foo", "fn foo<>");
904-
check_create_gpl::<ast::Fn>("fn foo() {}", "fn foo<>() {}");
905-
906-
check_create_gpl::<ast::Impl>("impl", "impl<>");
907-
check_create_gpl::<ast::Impl>("impl Struct {}", "impl<> Struct {}");
908-
check_create_gpl::<ast::Impl>("impl Trait for Struct {}", "impl<> Trait for Struct {}");
909-
910-
check_create_gpl::<ast::Trait>("trait Trait<>", "trait Trait<>");
911-
check_create_gpl::<ast::Trait>("trait Trait<> {}", "trait Trait<> {}");
912-
913-
check_create_gpl::<ast::Struct>("struct A", "struct A<>");
914-
check_create_gpl::<ast::Struct>("struct A;", "struct A<>;");
915-
check_create_gpl::<ast::Struct>("struct A();", "struct A<>();");
916-
check_create_gpl::<ast::Struct>("struct A {}", "struct A<> {}");
917-
918-
check_create_gpl::<ast::Enum>("enum E", "enum E<>");
919-
check_create_gpl::<ast::Enum>("enum E {", "enum E<> {");
920-
}
921-
922685
#[test]
923686
fn test_increase_indent() {
924687
let arm_list = ast_mut_from_text::<ast::Fn>(

0 commit comments

Comments
 (0)