summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_builtin_macros/src/deriving
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
commitd1b2d29528b7794b41e66fc2136e395a02f8529b (patch)
treea4a17504b260206dec3cf55b2dca82929a348ac2 /compiler/rustc_builtin_macros/src/deriving
parentReleasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz
rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_builtin_macros/src/deriving')
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/clone.rs8
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs4
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs4
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/decodable.rs2
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/encodable.rs2
-rw-r--r--compiler/rustc_builtin_macros/src/deriving/generic/mod.rs27
6 files changed, 28 insertions, 19 deletions
diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs
index 9ba98d0a5..b468abe32 100644
--- a/compiler/rustc_builtin_macros/src/deriving/clone.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs
@@ -144,7 +144,7 @@ fn cs_clone_simple(
}
_ => cx.span_bug(
trait_span,
- format!("unexpected substructure in simple `derive({})`", name),
+ format!("unexpected substructure in simple `derive({name})`"),
),
}
}
@@ -178,10 +178,10 @@ fn cs_clone(
vdata = &variant.data;
}
EnumTag(..) | AllFieldlessEnum(..) => {
- cx.span_bug(trait_span, format!("enum tags in `derive({})`", name,))
+ cx.span_bug(trait_span, format!("enum tags in `derive({name})`",))
}
StaticEnum(..) | StaticStruct(..) => {
- cx.span_bug(trait_span, format!("associated function in `derive({})`", name))
+ cx.span_bug(trait_span, format!("associated function in `derive({name})`"))
}
}
@@ -193,7 +193,7 @@ fn cs_clone(
let Some(ident) = field.name else {
cx.span_bug(
trait_span,
- format!("unnamed field in normal struct in `derive({})`", name,),
+ format!("unnamed field in normal struct in `derive({name})`",),
);
};
let call = subcall(cx, field);
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
index 4401cf8a9..ea81cee78 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
@@ -61,8 +61,8 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
|cx, fold| match fold {
CsFold::Single(field) => {
let [other_expr] = &field.other_selflike_exprs[..] else {
- cx.span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
- };
+ cx.span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
+ };
let args = thin_vec![field.self_expr.clone(), other_expr.clone()];
cx.expr_call_global(field.span, cmp_path.clone(), args)
}
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
index 54b6cb7d7..a5b3a504e 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
@@ -94,8 +94,8 @@ fn cs_partial_cmp(
|cx, fold| match fold {
CsFold::Single(field) => {
let [other_expr] = &field.other_selflike_exprs[..] else {
- cx.span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
- };
+ cx.span_bug(field.span, "not exactly 2 arguments in `derive(Ord)`");
+ };
let args = thin_vec![field.self_expr.clone(), other_expr.clone()];
cx.expr_call_global(field.span, partial_cmp_path.clone(), args)
}
diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
index 3921533c8..bcf11cb4c 100644
--- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
@@ -204,7 +204,7 @@ where
let fields = fields
.iter()
.enumerate()
- .map(|(i, &span)| getarg(cx, span, Symbol::intern(&format!("_field{}", i)), i))
+ .map(|(i, &span)| getarg(cx, span, Symbol::intern(&format!("_field{i}")), i))
.collect();
cx.expr_call(trait_span, path_expr, fields)
diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
index a3b11309d..2dc20c324 100644
--- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
@@ -173,7 +173,7 @@ fn encodable_substructure(
for (i, &FieldInfo { name, ref self_expr, span, .. }) in fields.iter().enumerate() {
let name = match name {
Some(id) => id.name,
- None => Symbol::intern(&format!("_field{}", i)),
+ None => Symbol::intern(&format!("_field{i}")),
};
let self_ref = cx.expr_addr_of(span, self_expr.clone());
let enc =
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index 4ba09335c..6597ee3cf 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -1134,9 +1134,14 @@ impl<'a> MethodDef<'a> {
trait_: &TraitDef<'b>,
enum_def: &'b EnumDef,
type_ident: Ident,
- selflike_args: ThinVec<P<Expr>>,
+ mut selflike_args: ThinVec<P<Expr>>,
nonselflike_args: &[P<Expr>],
) -> BlockOrExpr {
+ assert!(
+ !selflike_args.is_empty(),
+ "static methods must use `expand_static_enum_method_body`",
+ );
+
let span = trait_.span;
let variants = &enum_def.variants;
@@ -1144,10 +1149,15 @@ impl<'a> MethodDef<'a> {
let unify_fieldless_variants =
self.fieldless_variants_strategy == FieldlessVariantsStrategy::Unify;
- // There is no sensible code to be generated for *any* deriving on a
- // zero-variant enum. So we just generate a failing expression.
+ // For zero-variant enum, this function body is unreachable. Generate
+ // `match *self {}`. This produces machine code identical to `unsafe {
+ // core::intrinsics::unreachable() }` while being safe and stable.
if variants.is_empty() {
- return BlockOrExpr(ThinVec::new(), Some(deriving::call_unreachable(cx, span)));
+ selflike_args.truncate(1);
+ let match_arg = cx.expr_deref(span, selflike_args.pop().unwrap());
+ let match_arms = ThinVec::new();
+ let expr = cx.expr_match(span, match_arg, match_arms);
+ return BlockOrExpr(ThinVec::new(), Some(expr));
}
let prefixes = iter::once("__self".to_string())
@@ -1156,7 +1166,7 @@ impl<'a> MethodDef<'a> {
.iter()
.enumerate()
.skip(1)
- .map(|(arg_count, _selflike_arg)| format!("__arg{}", arg_count)),
+ .map(|(arg_count, _selflike_arg)| format!("__arg{arg_count}")),
)
.collect::<Vec<String>>();
@@ -1171,7 +1181,7 @@ impl<'a> MethodDef<'a> {
let get_tag_pieces = |cx: &ExtCtxt<'_>| {
let tag_idents: Vec<_> = prefixes
.iter()
- .map(|name| Ident::from_str_and_span(&format!("{}_tag", name), span))
+ .map(|name| Ident::from_str_and_span(&format!("{name}_tag"), span))
.collect();
let mut tag_exprs: Vec<_> = tag_idents
@@ -1511,7 +1521,7 @@ impl<'a> TraitDef<'a> {
}
fn mk_pattern_ident(&self, prefix: &str, i: usize) -> Ident {
- Ident::from_str_and_span(&format!("{}_{}", prefix, i), self.span)
+ Ident::from_str_and_span(&format!("{prefix}_{i}"), self.span)
}
fn create_struct_pattern_fields(
@@ -1592,8 +1602,7 @@ impl<'a> TraitDef<'a> {
sp,
ast::CRATE_NODE_ID,
format!(
- "{} slice in a packed struct that derives a built-in trait",
- ty
+ "{ty} slice in a packed struct that derives a built-in trait"
),
rustc_lint_defs::BuiltinLintDiagnostics::ByteSliceInPackedStructWithDerive
);