Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) fn expand_deriving_clone(
}
}
ItemKind::Union(..) => {
bounds = smallvec![Path(path_std!(marker::Copy))];
bounds = smallvec![path_std!(cx, span, marker::Copy)];
is_simple = true;
substructure = combine_substructure(|c, s, sub| cs_clone_simple(c, s, sub, true));
}
Expand All @@ -62,7 +62,7 @@ pub(crate) fn expand_deriving_clone(
if is_simple {
let trivial_def = TraitDef {
span,
path: path_std!(clone::TrivialClone),
path: path_std!(cx, span, clone::TrivialClone),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: true,
additional_bounds: bounds.clone(),
Expand All @@ -81,7 +81,7 @@ pub(crate) fn expand_deriving_clone(

let trait_def = TraitDef {
span,
path: path_std!(clone::Clone),
path: path_std!(cx, span, clone::Clone),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: true,
additional_bounds: bounds,
Expand Down Expand Up @@ -149,7 +149,7 @@ fn cs_clone_simple(
&[sym::clone, sym::AssertParamIsCopy],
);
} else {
match substr.fields {
match substr {
StaticStruct(vdata, ..) => {
process_variant(vdata);
}
Expand All @@ -171,17 +171,18 @@ fn cs_clone(cx: &ExtCtxt<'_>, trait_span: Span, substr: Substructure<'_>) -> Blo
cx.expr_call_global(field.span, fn_path.clone(), args)
};

let self_ident = Ident::new(kw::SelfUpper, trait_span);
let ctor_path;
let all_fields;
let vdata;
match substr.fields {
match substr {
Struct(vdata_, af) => {
ctor_path = cx.path(trait_span, vec![substr.type_ident]);
ctor_path = cx.path(trait_span, vec![self_ident]);
all_fields = af;
vdata = vdata_;
}
EnumMatching(.., variant, af) => {
ctor_path = cx.path(trait_span, vec![substr.type_ident, variant.ident]);
ctor_path = cx.path(trait_span, vec![self_ident, variant.ident]);
all_fields = af;
vdata = &variant.data;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/const_param_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub(crate) fn expand_deriving_const_param_ty(
) {
let trait_def = TraitDef {
span,
path: path_std!(marker::ConstParamTy_),
path: path_std!(cx, span, marker::ConstParamTy_),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: false,
additional_bounds: smallvec![ty::Ty::Path(path_std!(cmp::Eq))],
additional_bounds: smallvec![path_std!(cx, span, cmp::Eq)],
supports_unions: false,
methods: SmallVec::new(),
associated_types: SmallVec::new(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn expand_deriving_copy(
) {
let trait_def = TraitDef {
span,
path: path_std!(marker::Copy),
path: path_std!(cx, span, marker::Copy),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: false,
additional_bounds: SmallVec::new(),
Expand Down
54 changes: 32 additions & 22 deletions compiler/rustc_builtin_macros/src/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub(crate) fn expand_deriving_debug(
is_const: bool,
) {
// &mut ::std::fmt::Formatter
let fmtr = Ref(Box::new(Path(path_std!(fmt::Formatter))), ast::Mutability::Mut);
let fmtr = Ref(Box::new(Path(path_std!(cx, span, fmt::Formatter))), ast::Mutability::Mut);

let trait_def = TraitDef {
span,
path: path_std!(fmt::Debug),
path: path_std!(cx, span, fmt::Debug),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: true,
additional_bounds: SmallVec::new(),
Expand All @@ -30,11 +30,16 @@ pub(crate) fn expand_deriving_debug(
generics: cx.empty_generics(span),
explicit_self: true,
nonself_args: smallvec![(fmtr, sym::character('f'))],
ret_ty: Path(path_std!(fmt::Result)),
ret_ty: Path(path_std!(cx, span, fmt::Result)),
attributes: thin_vec![cx.attr_word(sym::inline, span)],
fieldless_variants_strategy:
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
combine_substructure: combine_substructure(show_substructure),
combine_substructure: combine_substructure(|cx, span, substr| show_substructure(
cx,
span,
substr,
item.kind.ident().unwrap()
)),
}],
associated_types: SmallVec::new(),
is_const,
Expand All @@ -44,24 +49,30 @@ pub(crate) fn expand_deriving_debug(
trait_def.expand(cx, item, push)
}

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);
fn formatter_ident(cx: &ExtCtxt<'_>, span: Span) -> Box<ast::Expr> {
cx.expr_ident(span, Ident::new(sym::character('f'), span))
}

fn show_substructure(
cx: &ExtCtxt<'_>,
span: Span,
substr: Substructure<'_>,
type_ident: Ident,
) -> BlockOrExpr {
let fmt_detail = cx.sess.opts.unstable_opts.fmt_debug;
if fmt_detail == FmtDebug::None {
return BlockOrExpr::new_expr(cx.expr_ok(span, cx.expr_tuple(span, ThinVec::new())));
}

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

let name = cx.expr_str(span, ident.name);
let fmt = substr.nonselflike_args[0].clone();
let fmt = formatter_ident(cx, span);

// Fieldless enums have been special-cased earlier
if fmt_detail == FmtDebug::Shallow {
Expand All @@ -85,13 +96,14 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) ->
// The number of fields that can be handled without an array.
const CUTOFF: usize = 5;

let expr_for_field = |field: &FieldInfo, index: usize| -> Box<ast::Expr> {
if index < fields.len() - 1 {
field.self_expr.clone()
let len = fields.len();
let expr_for_field = |field: FieldInfo, index: usize| -> Box<ast::Expr> {
if index < len - 1 {
field.self_expr
} 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())
cx.expr_addr_of(field.span, field.self_expr)
}
};

Expand All @@ -111,8 +123,7 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) ->

let mut args = ThinVec::with_capacity(2 + fields.len() * args_per_field);
args.extend([fmt, name]);
for i in 0..fields.len() {
let field = &fields[i];
for (i, field) in fields.into_iter().enumerate() {
if is_struct {
let name = cx.expr_str(field.span, field.name.unwrap().name);
args.push(name);
Expand All @@ -128,8 +139,7 @@ fn show_substructure(cx: &ExtCtxt<'_>, span: Span, substr: Substructure<'_>) ->
let mut name_exprs = ThinVec::with_capacity(fields.len());
let mut value_exprs = ThinVec::with_capacity(fields.len());

for i in 0..fields.len() {
let field = &fields[i];
for (i, field) in fields.into_iter().enumerate() {
if is_struct {
name_exprs.push(cx.expr_str(field.span, field.name.unwrap().name));
}
Expand Down Expand Up @@ -216,14 +226,14 @@ fn show_fieldless_enum(
cx: &ExtCtxt<'_>,
span: Span,
def: &EnumDef,
substr: Substructure<'_>,
type_ident: Ident,
) -> BlockOrExpr {
let fmt = substr.nonselflike_args[0].clone();
let fmt = formatter_ident(cx, span);
let arms = def
.variants
.iter()
.map(|v| {
let variant_path = cx.path(span, vec![substr.type_ident, v.ident]);
let variant_path = cx.path(span, vec![type_ident, v.ident]);
let pat = match &v.data {
ast::VariantData::Tuple(fields, _) => {
debug_assert!(fields.is_empty());
Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn expand_deriving_default(

let trait_def = TraitDef {
span,
path: Path::new(vec![kw::Default, sym::Default]),
path: new_path(cx, span, &[kw::Default, sym::Default], &[]),
skip_path_as_bound: has_a_default_variant(item),
needs_copy_as_bound_if_packed: false,
additional_bounds: SmallVec::new(),
Expand All @@ -36,9 +36,9 @@ pub(crate) fn expand_deriving_default(
attributes: thin_vec![cx.attr_word(sym::inline, span)],
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
combine_substructure: combine_substructure(|cx, trait_span, substr| {
match substr.fields {
match substr {
StaticStruct(variant_data) => {
default_struct_substructure(cx, trait_span, substr, variant_data)
default_struct_substructure(cx, trait_span, variant_data)
}
StaticEnum(enum_def) => {
default_enum_substructure(cx, trait_span, enum_def, item.span)
Expand Down Expand Up @@ -66,26 +66,23 @@ fn default_call(cx: &ExtCtxt<'_>, span: Span) -> Box<ast::Expr> {
fn default_struct_substructure(
cx: &ExtCtxt<'_>,
trait_span: Span,
substr: Substructure<'_>,
variant_data: &VariantData,
) -> BlockOrExpr {
let expr = match variant_data {
VariantData::Unit(_) => cx.expr_ident(trait_span, substr.type_ident),
VariantData::Unit(_) => cx.expr_ident(trait_span, Ident::new(kw::SelfUpper, trait_span)),
VariantData::Tuple(fields, _) => {
let exprs = fields
.iter()
.map(|field| default_call(cx, field.span.with_ctxt(trait_span.ctxt())))
.collect();
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
cx.expr_call_ident(trait_span, Ident::new(kw::SelfUpper, trait_span), exprs)
}
VariantData::Struct { fields, .. } => {
let default_fields = fields
.iter()
.map(|field| {
let span = field.span.with_ctxt(trait_span.ctxt());
let value = if let Some(extras) = &field.extras
&& let Some(default_val) = &extras.default
{
let value = if let Some(default_val) = field.default_value() {
// We use the field default const expression.
cx.expr(
default_val.value.span,
Expand All @@ -98,7 +95,7 @@ fn default_struct_substructure(
cx.field_imm(span, field.ident.unwrap(), value)
})
.collect();
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
cx.expr_struct_ident(trait_span, Ident::new(kw::SelfUpper, trait_span), default_fields)
}
};
BlockOrExpr::new_expr(expr)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn expand_deriving_eq(

let trait_def = TraitDef {
span,
path: path_std!(cmp::Eq),
path: path_std!(cx, span, cmp::Eq),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: true,
additional_bounds: SmallVec::new(),
Expand Down Expand Up @@ -73,7 +73,7 @@ fn cs_total_eq_assert(cx: &ExtCtxt<'_>, trait_span: Span, substr: Substructure<'
}
};

match substr.fields {
match substr {
StaticStruct(vdata, ..) => {
process_variant(vdata);
}
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_builtin_macros/src/deriving/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_expand::base::{DummyResult, ExtCtxt};
use rustc_span::{Ident, Span, kw, sym};
use thin_vec::thin_vec;

use crate::deriving::generic::ty::{Path, PathKind, Ty};
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;
use crate::deriving::pathvec;
use crate::diagnostics;
Expand Down Expand Up @@ -52,8 +52,7 @@ pub(crate) fn expand_deriving_from(
Err(guar) => cx.ty(span, ast::TyKind::Err(guar)),
});

let path =
Path::new_(pathvec!(convert::From), vec![Box::new(from_type.clone())], PathKind::Std);
let path = new_path(cx, span, pathvec!(convert::From), &[from_type.clone()]);

// Generate code like this:
//
Expand Down Expand Up @@ -89,8 +88,8 @@ pub(crate) fn expand_deriving_from(
};

let self_kw = Ident::new(kw::SelfUpper, span);
let expr: Box<ast::Expr> = match substructure.fields {
SubstructureFields::StaticStruct(variant) => match variant {
let expr: Box<ast::Expr> = match substructure {
StaticStruct(variant) => match variant {
// Self { field: value }
VariantData::Struct { .. } => cx.expr_struct_ident(
span,
Expand Down
Loading
Loading