Skip to content

Commit b7855fa

Browse files
committed
Factor out the repeated assert_ty_bounds function.
1 parent e739668 commit b7855fa

File tree

4 files changed

+42
-44
lines changed

4 files changed

+42
-44
lines changed

compiler/rustc_builtin_macros/src/deriving/clone.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::deriving::generic::*;
33
use crate::deriving::path_std;
44

55
use rustc_ast::ptr::P;
6-
use rustc_ast::{self as ast, Expr, GenericArg, Generics, ItemKind, MetaItem, VariantData};
6+
use rustc_ast::{self as ast, Expr, Generics, ItemKind, MetaItem, VariantData};
77
use rustc_expand::base::{Annotatable, ExtCtxt};
8-
use rustc_span::symbol::{kw, sym, Ident, Symbol};
8+
use rustc_span::symbol::{kw, sym, Ident};
99
use rustc_span::Span;
1010

1111
pub fn expand_deriving_clone(
@@ -107,36 +107,30 @@ fn cs_clone_shallow(
107107
substr: &Substructure<'_>,
108108
is_union: bool,
109109
) -> P<Expr> {
110-
fn assert_ty_bounds(
111-
cx: &mut ExtCtxt<'_>,
112-
stmts: &mut Vec<ast::Stmt>,
113-
ty: P<ast::Ty>,
114-
span: Span,
115-
helper_name: &str,
116-
) {
117-
// Generate statement `let _: helper_name<ty>;`,
118-
// set the expn ID so we can use the unstable struct.
119-
let span = cx.with_def_site_ctxt(span);
120-
let assert_path = cx.path_all(
121-
span,
122-
true,
123-
cx.std_path(&[sym::clone, Symbol::intern(helper_name)]),
124-
vec![GenericArg::Type(ty)],
125-
);
126-
stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
127-
}
128110
fn process_variant(cx: &mut ExtCtxt<'_>, stmts: &mut Vec<ast::Stmt>, variant: &VariantData) {
129111
for field in variant.fields() {
130112
// let _: AssertParamIsClone<FieldTy>;
131-
assert_ty_bounds(cx, stmts, field.ty.clone(), field.span, "AssertParamIsClone");
113+
super::assert_ty_bounds(
114+
cx,
115+
stmts,
116+
field.ty.clone(),
117+
field.span,
118+
&[sym::clone, sym::AssertParamIsClone],
119+
);
132120
}
133121
}
134122

135123
let mut stmts = Vec::new();
136124
if is_union {
137125
// let _: AssertParamIsCopy<Self>;
138126
let self_ty = cx.ty_path(cx.path_ident(trait_span, Ident::with_dummy_span(kw::SelfUpper)));
139-
assert_ty_bounds(cx, &mut stmts, self_ty, trait_span, "AssertParamIsCopy");
127+
super::assert_ty_bounds(
128+
cx,
129+
&mut stmts,
130+
self_ty,
131+
trait_span,
132+
&[sym::clone, sym::AssertParamIsCopy],
133+
);
140134
} else {
141135
match *substr.fields {
142136
StaticStruct(vdata, ..) => {

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::deriving::generic::*;
33
use crate::deriving::path_std;
44

55
use rustc_ast::ptr::P;
6-
use rustc_ast::{self as ast, Expr, GenericArg, MetaItem};
6+
use rustc_ast::{self as ast, Expr, MetaItem};
77
use rustc_expand::base::{Annotatable, ExtCtxt};
8-
use rustc_span::symbol::{sym, Ident, Symbol};
8+
use rustc_span::symbol::{sym, Ident};
99
use rustc_span::Span;
1010

1111
pub fn expand_deriving_eq(
@@ -55,32 +55,20 @@ fn cs_total_eq_assert(
5555
trait_span: Span,
5656
substr: &Substructure<'_>,
5757
) -> P<Expr> {
58-
fn assert_ty_bounds(
59-
cx: &mut ExtCtxt<'_>,
60-
stmts: &mut Vec<ast::Stmt>,
61-
ty: P<ast::Ty>,
62-
span: Span,
63-
helper_name: &str,
64-
) {
65-
// Generate statement `let _: helper_name<ty>;`,
66-
// set the expn ID so we can use the unstable struct.
67-
let span = cx.with_def_site_ctxt(span);
68-
let assert_path = cx.path_all(
69-
span,
70-
true,
71-
cx.std_path(&[sym::cmp, Symbol::intern(helper_name)]),
72-
vec![GenericArg::Type(ty)],
73-
);
74-
stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
75-
}
7658
fn process_variant(
7759
cx: &mut ExtCtxt<'_>,
7860
stmts: &mut Vec<ast::Stmt>,
7961
variant: &ast::VariantData,
8062
) {
8163
for field in variant.fields() {
8264
// let _: AssertParamIsEq<FieldTy>;
83-
assert_ty_bounds(cx, stmts, field.ty.clone(), field.span, "AssertParamIsEq");
65+
super::assert_ty_bounds(
66+
cx,
67+
stmts,
68+
field.ty.clone(),
69+
field.span,
70+
&[sym::cmp, sym::AssertParamIsEq],
71+
);
8472
}
8573
}
8674

compiler/rustc_builtin_macros/src/deriving/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc_ast as ast;
44
use rustc_ast::ptr::P;
5-
use rustc_ast::{Impl, ItemKind, MetaItem};
5+
use rustc_ast::{GenericArg, Impl, ItemKind, MetaItem};
66
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier};
77
use rustc_span::symbol::{sym, Ident, Symbol};
88
use rustc_span::Span;
@@ -193,3 +193,16 @@ fn inject_impl_of_structural_trait(
193193

194194
push(Annotatable::Item(newitem));
195195
}
196+
197+
fn assert_ty_bounds(
198+
cx: &mut ExtCtxt<'_>,
199+
stmts: &mut Vec<ast::Stmt>,
200+
ty: P<ast::Ty>,
201+
span: Span,
202+
assert_path: &[Symbol],
203+
) {
204+
// Generate statement `let _: assert_path<ty>;`.
205+
let span = cx.with_def_site_ctxt(span);
206+
let assert_path = cx.path_all(span, true, cx.std_path(assert_path), vec![GenericArg::Type(ty)]);
207+
stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path)));
208+
}

compiler/rustc_span/src/symbol.rs

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ symbols! {
135135
Arguments,
136136
AsMut,
137137
AsRef,
138+
AssertParamIsClone,
139+
AssertParamIsCopy,
140+
AssertParamIsEq,
138141
AtomicBool,
139142
AtomicI128,
140143
AtomicI16,

0 commit comments

Comments
 (0)