Skip to content
49 changes: 17 additions & 32 deletions compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::{self as ast, Generics, ItemKind, MetaItem, Safety, VariantData};
use rustc_ast::{self as ast, Generics, ItemKind, Safety, VariantData};
use rustc_data_structures::fx::FxHashSet;
use rustc_expand::base::ExtCtxt;
use rustc_span::{DUMMY_SP, Ident, Span, kw, sym};
Expand All @@ -11,7 +11,6 @@ use crate::deriving::path_std;
pub(crate) fn expand_deriving_clone(
cx: &ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &ast::Item,
push: &mut dyn FnMut(Box<ast::Item>),
is_const: bool,
Expand Down Expand Up @@ -55,7 +54,7 @@ pub(crate) fn expand_deriving_clone(
is_simple = true;
substructure = combine_substructure(|c, s, sub| cs_clone_simple(c, s, sub, true));
}
_ => cx.dcx().span_bug(span, "`#[derive(Clone)]` on wrong item kind"),
_ => cx.dcx().span_bug(span, "`derive(Clone)` on wrong item kind"),
}

// If the clone method is just copying the value, also mark the type as
Expand All @@ -77,7 +76,7 @@ pub(crate) fn expand_deriving_clone(
document: false,
};

trivial_def.expand(cx, mitem, item, push);
trivial_def.expand(cx, item, push);
}

let trait_def = TraitDef {
Expand All @@ -89,7 +88,7 @@ pub(crate) fn expand_deriving_clone(
supports_unions: true,
methods: smallvec![MethodDef {
name: sym::clone,
generics: Bounds::empty(),
generics: cx.empty_generics(span),
explicit_self: true,
nonself_args: SmallVec::new(),
ret_ty: Self_,
Expand All @@ -103,13 +102,13 @@ pub(crate) fn expand_deriving_clone(
document: true,
};

trait_def.expand_ext(cx, mitem, item, push, is_simple)
trait_def.expand_ext(cx, item, push, is_simple)
}

fn cs_clone_simple(
cx: &ExtCtxt<'_>,
trait_span: Span,
substr: &Substructure<'_>,
substr: Substructure<'_>,
is_union: bool,
) -> BlockOrExpr {
let mut stmts = ThinVec::new();
Expand Down Expand Up @@ -150,7 +149,7 @@ fn cs_clone_simple(
&[sym::clone, sym::AssertParamIsCopy],
);
} else {
match *substr.fields {
match substr.fields {
StaticStruct(vdata, ..) => {
process_variant(vdata);
}
Expand All @@ -165,55 +164,41 @@ fn cs_clone_simple(
BlockOrExpr::new_mixed(stmts, Some(cx.expr_deref(trait_span, cx.expr_self(trait_span))))
}

fn cs_clone(cx: &ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
let ctor_path;
let all_fields;
fn cs_clone(cx: &ExtCtxt<'_>, trait_span: Span, substr: Substructure<'_>) -> BlockOrExpr {
let fn_path = cx.std_path(&[sym::clone, sym::Clone, sym::clone]);
let subcall = |cx: &ExtCtxt<'_>, field: &FieldInfo| {
let args = thin_vec![field.self_expr.clone()];
let subcall = |field: FieldInfo| {
let args = thin_vec![field.self_expr];
cx.expr_call_global(field.span, fn_path.clone(), args)
};

let ctor_path;
let all_fields;
let vdata;
match substr.fields {
Struct(vdata_, af) => {
ctor_path = cx.path(trait_span, vec![substr.type_ident]);
all_fields = af;
vdata = *vdata_;
vdata = vdata_;
}
EnumMatching(.., variant, af) => {
ctor_path = cx.path(trait_span, vec![substr.type_ident, variant.ident]);
all_fields = af;
vdata = &variant.data;
}
EnumDiscr(..) | AllFieldlessEnum(..) => {
cx.dcx().span_bug(trait_span, "enum discriminants in `derive(Clone)`")
}
StaticEnum(..) | StaticStruct(..) => {
cx.dcx().span_bug(trait_span, "associated function in `derive(Clone)`")
}
_ => cx.dcx().span_bug(trait_span, "unexpected substructure in `derive(Clone)`"),
}

let expr = match *vdata {
VariantData::Struct { .. } => {
let fields = all_fields
.iter()
.map(|field| {
let Some(ident) = field.name else {
cx.dcx().span_bug(
trait_span,
"unnamed field in normal struct in `derive(Clone)`",
);
};
let call = subcall(cx, field);
cx.field_imm(field.span, ident, call)
})
.into_iter()
.map(|field| cx.field_imm(field.span, field.name.unwrap(), subcall(field)))
.collect::<ThinVec<_>>();

cx.expr_struct(trait_span, ctor_path, fields)
}
VariantData::Tuple(..) => {
let subcalls = all_fields.iter().map(|f| subcall(cx, f)).collect();
let subcalls = all_fields.into_iter().map(subcall).collect();
let path = cx.expr_path(ctor_path);
cx.expr_call(trait_span, path, subcalls)
}
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ast::HasAttrs;
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::visit::{BoundKind, Visitor};
use rustc_ast::{
self as ast, GenericArg, GenericBound, GenericParamKind, Generics, ItemKind, MetaItem,
self as ast, GenericArg, GenericBound, GenericParamKind, Generics, ItemKind,
TraitBoundModifiers, VariantData, WherePredicate,
};
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
Expand All @@ -21,7 +21,6 @@ macro_rules! path {
pub(crate) fn expand_deriving_coerce_pointee(
cx: &ExtCtxt<'_>,
span: Span,
_mitem: &MetaItem,
item: &ast::Item,
push: &mut dyn FnMut(Box<ast::Item>),
_is_const: bool,
Expand Down Expand Up @@ -320,18 +319,16 @@ pub(crate) fn expand_deriving_coerce_pointee(
// Add the impl blocks for `DispatchFromDyn` and `CoerceUnsized`.
let gen_args = vec![GenericArg::Type(alt_self_type)];
add_impl_block(impl_generics.clone(), sym::DispatchFromDyn, gen_args.clone());
add_impl_block(impl_generics.clone(), sym::CoerceUnsized, gen_args);
add_impl_block(impl_generics, sym::CoerceUnsized, gen_args);
}

fn contains_maybe_sized_bound_on_pointee(predicates: &[WherePredicate], pointee: Symbol) -> bool {
for bound in predicates {
if let ast::WherePredicateKind::BoundPredicate(bound) = &bound.kind
&& bound.bounded_ty.kind.is_simple_path().is_some_and(|name| name == pointee)
{
for bound in &bound.bounds {
if is_maybe_sized_bound(bound) {
return true;
}
if contains_maybe_sized_bound(&bound.bounds) {
return true;
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_builtin_macros/src/deriving/const_param_ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::{MetaItem, Safety};
use rustc_ast::Safety;
use rustc_expand::base::ExtCtxt;
use rustc_span::Span;

Expand All @@ -8,7 +8,6 @@ use crate::deriving::path_std;
pub(crate) fn expand_deriving_const_param_ty(
cx: &ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &ast::Item,
push: &mut dyn FnMut(Box<ast::Item>),
is_const: bool,
Expand All @@ -27,5 +26,5 @@ pub(crate) fn expand_deriving_const_param_ty(
document: true,
};

trait_def.expand(cx, mitem, item, push);
trait_def.expand(cx, item, push);
}
5 changes: 2 additions & 3 deletions compiler/rustc_builtin_macros/src/deriving/copy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::{MetaItem, Safety};
use rustc_ast::Safety;
use rustc_expand::base::ExtCtxt;
use rustc_span::Span;

Expand All @@ -8,7 +8,6 @@ use crate::deriving::path_std;
pub(crate) fn expand_deriving_copy(
cx: &ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &ast::Item,
push: &mut dyn FnMut(Box<ast::Item>),
is_const: bool,
Expand All @@ -27,5 +26,5 @@ pub(crate) fn expand_deriving_copy(
document: true,
};

trait_def.expand(cx, mitem, item, push);
trait_def.expand(cx, item, push);
}
32 changes: 12 additions & 20 deletions compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::{self as ast, EnumDef, MetaItem, Safety};
use rustc_ast::{self as ast, EnumDef, Safety};
use rustc_expand::base::ExtCtxt;
use rustc_session::config::FmtDebug;
use rustc_span::{Ident, Span, Symbol, sym};
Expand All @@ -11,7 +11,6 @@ use crate::deriving::path_std;
pub(crate) fn expand_deriving_debug(
cx: &ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &ast::Item,
push: &mut dyn FnMut(Box<ast::Item>),
is_const: bool,
Expand All @@ -28,7 +27,7 @@ pub(crate) fn expand_deriving_debug(
supports_unions: false,
methods: smallvec![MethodDef {
name: sym::fmt,
generics: Bounds::empty(),
generics: cx.empty_generics(span),
explicit_self: true,
nonself_args: smallvec![(fmtr, sym::character('f'))],
ret_ty: Path(path_std!(fmt::Result)),
Expand All @@ -42,10 +41,10 @@ pub(crate) fn expand_deriving_debug(
safety: Safety::Default,
document: true,
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, item, push)
}

fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) -> BlockOrExpr {
// We want to make sure we have the ctxt set so that we can use unstable methods
let span = cx.with_def_site_ctxt(span);

Expand All @@ -55,12 +54,10 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) ->
}

let (ident, vdata, fields) = match substr.fields {
Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
Struct(vdata, fields) => (substr.type_ident, vdata, fields),
EnumMatching(v, fields) => (v.ident, &v.data, fields),
AllFieldlessEnum(enum_def) => return show_fieldless_enum(cx, span, enum_def, substr),
EnumDiscr(..) | StaticStruct(..) | StaticEnum(..) => {
cx.dcx().span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
}
_ => cx.dcx().span_bug(span, "unexpected substructure in `derive(Debug)`"),
};

let name = cx.expr_str(span, ident.name);
Expand Down Expand Up @@ -88,20 +85,15 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) ->
// The number of fields that can be handled without an array.
const CUTOFF: usize = 5;

fn expr_for_field(
cx: &ExtCtxt<'_>,
field: &FieldInfo,
index: usize,
len: usize,
) -> Box<ast::Expr> {
if index < len - 1 {
let expr_for_field = |field: &FieldInfo, index: usize| -> Box<ast::Expr> {
if index < fields.len() - 1 {
field.self_expr.clone()
} else {
// Unsized types need an extra indirection, but only the last field
// may be unsized.
cx.expr_addr_of(field.span, field.self_expr.clone())
}
}
};

if fields.is_empty() {
// Special case for no fields.
Expand All @@ -126,7 +118,7 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) ->
args.push(name);
}

let field = expr_for_field(cx, field, i, fields.len());
let field = expr_for_field(field, i);
args.push(field);
}
let expr = cx.expr_call_global(span, fn_path_debug, args);
Expand All @@ -142,7 +134,7 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) ->
name_exprs.push(cx.expr_str(field.span, field.name.unwrap().name));
}

let field = expr_for_field(cx, field, i, fields.len());
let field = expr_for_field(field, i);
value_exprs.push(field);
}

Expand Down Expand Up @@ -224,7 +216,7 @@ fn show_fieldless_enum(
cx: &ExtCtxt<'_>,
span: Span,
def: &EnumDef,
substr: &Substructure<'_>,
substr: Substructure<'_>,
) -> BlockOrExpr {
let fmt = substr.nonselflike_args[0].clone();
let arms = def
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::diagnostics;
pub(crate) fn expand_deriving_default(
cx: &ExtCtxt<'_>,
span: Span,
mitem: &ast::MetaItem,
item: &ast::Item,
push: &mut dyn FnMut(Box<ast::Item>),
is_const: bool,
Expand All @@ -30,7 +29,7 @@ pub(crate) fn expand_deriving_default(
supports_unions: false,
methods: smallvec![MethodDef {
name: kw::Default,
generics: Bounds::empty(),
generics: cx.empty_generics(span),
explicit_self: false,
nonself_args: SmallVec::new(),
ret_ty: Self_,
Expand All @@ -44,7 +43,9 @@ pub(crate) fn expand_deriving_default(
StaticEnum(enum_def) => {
default_enum_substructure(cx, trait_span, enum_def, item.span)
}
_ => cx.dcx().span_bug(trait_span, "method in `derive(Default)`"),
_ => cx
.dcx()
.span_bug(trait_span, "unexpected substructure in `derive(Default)`"),
}
}),
}],
Expand All @@ -53,7 +54,7 @@ pub(crate) fn expand_deriving_default(
safety: Safety::Default,
document: true,
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, item, push)
}

fn default_call(cx: &ExtCtxt<'_>, span: Span) -> Box<ast::Expr> {
Expand All @@ -65,7 +66,7 @@ fn default_call(cx: &ExtCtxt<'_>, span: Span) -> Box<ast::Expr> {
fn default_struct_substructure(
cx: &ExtCtxt<'_>,
trait_span: Span,
substr: &Substructure<'_>,
substr: Substructure<'_>,
variant_data: &VariantData,
) -> BlockOrExpr {
let expr = match variant_data {
Expand Down
Loading
Loading