Skip to content

Commit cde20de

Browse files
committed
update derive_macro
1 parent 220daf0 commit cde20de

1 file changed

Lines changed: 40 additions & 43 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;

0 commit comments

Comments
 (0)