<commit_before>use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;

use rustc_ast::ptr::P;
use rustc_ast::EnumDef;
use rustc_ast::VariantData;
use rustc_ast::{Expr, MetaItem};
use rustc_errors::Applicability;
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
use rustc_span::symbol::Ident;
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use smallvec::SmallVec;

pub fn expand_deriving_default(
    cx: &mut ExtCtxt<'_>,
    span: Span,
    mitem: &MetaItem,
    item: &Annotatable,
    push: &mut dyn FnMut(Annotatable),
) {
    let inline = cx.meta_word(span, sym::inline);
    let attrs = vec![cx.attribute(inline)];
    let trait_def = TraitDef {
        span,
        attributes: Vec::new(),
        path: Path::new(vec![kw::Default, sym::Default]),
        additional_bounds: Vec::new(),
        generics: Bounds::empty(),
        is_unsafe: false,
        supports_unions: false,
        methods: vec![MethodDef {
            name: kw::Default,
            generics: Bounds::empty(),
            explicit_self: None,
            args: Vec::new(),
            ret_ty: Self_,
            attributes: attrs,
            is_unsafe: false,
            unify_fieldless_variants: false,
            combine_substructure: combine_substructure(Box::new(|cx, trait_span, substr| {
                match substr.fields {
                    StaticStruct(_, fields) => {
                        default_struct_substructure(cx, trait_span, substr, fields)
                    }
                    StaticEnum(enum_def, _) => {
                        if !cx.sess.features_untracked().derive_default_enum {
                            rustc_session::parse::feature_err(
                                cx.parse_sess(),
                                sym::derive_default_enum,
                                span,
                                "deriving `Default` on enums is experimental",
                            )
                            .emit();
                        }
                        default_enum_substructure(cx, trait_span, enum_def)
                    }
                    _ => cx.span_bug(trait_span, "method in `derive(Default)`"),
                }
            })),
        }],
        associated_types: Vec::new(),
    };
    trait_def.expand(cx, mitem, item, push)
}

fn default_struct_substructure(
    cx: &mut ExtCtxt<'_>,
    trait_span: Span,
    substr: &Substructure<'_>,
    summary: &StaticFields,
) -> P<Expr> {
    // Note that `kw::Default` is "default" and `sym::Default` is "Default"!
    let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
    let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());

    match summary {
        Unnamed(ref fields, is_tuple) => {
            if !is_tuple {
                cx.expr_ident(trait_span, substr.type_ident)
            } else {
                let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
                cx.expr_call_ident(trait_span, substr.type_ident, exprs)
            }
        }
        Named(ref fields) => {
            let default_fields = fields
                .iter()
                .map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))
                .collect();
            cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
        }
    }
}

fn default_enum_substructure(
    cx: &mut ExtCtxt<'_>,
    trait_span: Span,
    enum_def: &EnumDef,
) -> P<Expr> {
    let default_variant = match extract_default_variant(cx, enum_def, trait_span) {
        Ok(value) => value,
        Err(()) => return DummyResult::raw_expr(trait_span, true),
    };

    // At this point, we know that there is exactly one variant with a `#[default]` attribute. The
    // attribute hasn't yet been validated.

    if let Err(()) = validate_default_attribute(cx, default_variant) {
        return DummyResult::raw_expr(trait_span, true);
    }

    // We now know there is exactly one unit variant with exactly one `#[default]` attribute.

    cx.expr_path(cx.path(
        default_variant.span,
        vec![Ident::new(kw::SelfUpper, default_variant.span), default_variant.ident],
    ))
}

fn extract_default_variant<'a>(
    cx: &mut ExtCtxt<'_>,
    enum_def: &'a EnumDef,
    trait_span: Span,
) -> Result<&'a rustc_ast::Variant, ()> {
    let default_variants: SmallVec<[_; 1]> = enum_def
        .variants
        .iter()
        .filter(|variant| cx.sess.contains_name(&variant.attrs, kw::Default))
        .collect();

    let variant = match default_variants.as_slice() {
        [variant] => variant,
        [] => {
            cx.struct_span_err(trait_span, "no default declared")
                .help("make a unit variant default by placing `#[default]` above it")
                .emit();

            return Err(());
        }
        [first, rest @ ..] => {
            cx.struct_span_err(trait_span, "multiple declared defaults")
                .span_label(first.span, "first default")
                .span_labels(rest.iter().map(|variant| variant.span), "additional default")
                .note("only one variant can be default")
                .emit();

            return Err(());
        }
    };

    if !matches!(variant.data, VariantData::Unit(..)) {
        cx.struct_span_err(variant.ident.span, "`#[default]` may only be used on unit variants")
            .help("consider a manual implementation of `Default`")
            .emit();

        return Err(());
    }
<commit_msg>Add machine-applicable suggestions<commit_after>use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;

use rustc_ast::ptr::P;
use rustc_ast::EnumDef;
use rustc_ast::VariantData;
use rustc_ast::{Expr, MetaItem};
use rustc_errors::Applicability;
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
use rustc_span::symbol::Ident;
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use smallvec::SmallVec;

pub fn expand_deriving_default(
    cx: &mut ExtCtxt<'_>,
    span: Span,
    mitem: &MetaItem,
    item: &Annotatable,
    push: &mut dyn FnMut(Annotatable),
) {
    let inline = cx.meta_word(span, sym::inline);
    let attrs = vec![cx.attribute(inline)];
    let trait_def = TraitDef {
        span,
        attributes: Vec::new(),
        path: Path::new(vec![kw::Default, sym::Default]),
        additional_bounds: Vec::new(),
        generics: Bounds::empty(),
        is_unsafe: false,
        supports_unions: false,
        methods: vec![MethodDef {
            name: kw::Default,
            generics: Bounds::empty(),
            explicit_self: None,
            args: Vec::new(),
            ret_ty: Self_,
            attributes: attrs,
            is_unsafe: false,
            unify_fieldless_variants: false,
            combine_substructure: combine_substructure(Box::new(|cx, trait_span, substr| {
                match substr.fields {
                    StaticStruct(_, fields) => {
                        default_struct_substructure(cx, trait_span, substr, fields)
                    }
                    StaticEnum(enum_def, _) => {
                        if !cx.sess.features_untracked().derive_default_enum {
                            rustc_session::parse::feature_err(
                                cx.parse_sess(),
                                sym::derive_default_enum,
                                span,
                                "deriving `Default` on enums is experimental",
                            )
                            .emit();
                        }
                        default_enum_substructure(cx, trait_span, enum_def)
                    }
                    _ => cx.span_bug(trait_span, "method in `derive(Default)`"),
                }
            })),
        }],
        associated_types: Vec::new(),
    };
    trait_def.expand(cx, mitem, item, push)
}

fn default_struct_substructure(
    cx: &mut ExtCtxt<'_>,
    trait_span: Span,
    substr: &Substructure<'_>,
    summary: &StaticFields,
) -> P<Expr> {
    // Note that `kw::Default` is "default" and `sym::Default` is "Default"!
    let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
    let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());

    match summary {
        Unnamed(ref fields, is_tuple) => {
            if !is_tuple {
                cx.expr_ident(trait_span, substr.type_ident)
            } else {
                let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
                cx.expr_call_ident(trait_span, substr.type_ident, exprs)
            }
        }
        Named(ref fields) => {
            let default_fields = fields
                .iter()
                .map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))
                .collect();
            cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
        }
    }
}

fn default_enum_substructure(
    cx: &mut ExtCtxt<'_>,
    trait_span: Span,
    enum_def: &EnumDef,
) -> P<Expr> {
    let default_variant = match extract_default_variant(cx, enum_def, trait_span) {
        Ok(value) => value,
        Err(()) => return DummyResult::raw_expr(trait_span, true),
    };

    // At this point, we know that there is exactly one variant with a `#[default]` attribute. The
    // attribute hasn't yet been validated.

    if let Err(()) = validate_default_attribute(cx, default_variant) {
        return DummyResult::raw_expr(trait_span, true);
    }

    // We now know there is exactly one unit variant with exactly one `#[default]` attribute.

    cx.expr_path(cx.path(
        default_variant.span,
        vec![Ident::new(kw::SelfUpper, default_variant.span), default_variant.ident],
    ))
}

fn extract_default_variant<'a>(
    cx: &mut ExtCtxt<'_>,
    enum_def: &'a EnumDef,
    trait_span: Span,
) -> Result<&'a rustc_ast::Variant, ()> {
    let default_variants: SmallVec<[_; 1]> = enum_def
        .variants
        .iter()
        .filter(|variant| cx.sess.contains_name(&variant.attrs, kw::Default))
        .collect();

    let variant = match default_variants.as_slice() {
        [variant] => variant,
        [] => {
            let possible_defaults = enum_def
                .variants
                .iter()
                .filter(|variant| matches!(variant.data, VariantData::Unit(..)))
                .filter(|variant| !cx.sess.contains_name(&variant.attrs, sym::non_exhaustive));

            let mut diag = cx.struct_span_err(trait_span, "no default declared");
            diag.help("make a unit variant default by placing `#[default]` above it");
            for variant in possible_defaults {
                // Suggest making each unit variant default.
                diag.tool_only_span_suggestion(
                    variant.span,
                    &format!("make `{}` default", variant.ident),
                    format!("#[default] {}", variant.ident),
                    Applicability::MaybeIncorrect,
                );
            }
            diag.emit();

            return Err(());
        }
        [first, rest @ ..] => {
            let mut diag = cx.struct_span_err(trait_span, "multiple declared defaults");
            diag.span_label(first.span, "first default");
            diag.span_labels(rest.iter().map(|variant| variant.span), "additional default");
            diag.note("only one variant can be default");
            for variant in &default_variants {
                // Suggest making each variant already tagged default.
                let suggestion = default_variants
                    .iter()
                    .filter_map(|v| {
                        if v.ident == variant.ident {
                            None
                        } else {
                            Some((cx.sess.find_by_name(&v.attrs, kw::Default)?.span, String::new()))
                        }
                    })
                    .collect();

                diag.tool_only_multipart_suggestion(
                    &format!("make `{}` default", variant.ident),
                    suggestion,
                    Applicability::MaybeIncorrect,
                );
            }
            diag.emit();

            return Err(());
        }
    };

    if !matches!(variant.data, VariantData::Unit(..)) {
        cx.struct_span_err(variant.ident.span, "`#[default]` may only be used on unit variants")
            .help("consider a manual implementation of `Default`")
            .emit();

        return Err(());
    }

{'fraction_non_alphanumeric': 0.12771026923993328, 'fraction_numerical': 0.00015884361845762846, 'mean_word_length': 1.9372521576860275, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6738781', 'n_tokens_mistral': 3677, 'n_tokens_neox': 3617, 'n_words': 764}