Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive PartialOrd by calling tuple's version #108515

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2695,6 +2695,8 @@ pub enum VariantData {
/// Struct variant.
///
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
///
/// The `bool` is `true` if recovery was used while parsing it.
Struct(ThinVec<FieldDef>, bool),
/// Tuple variant.
///
Expand Down
36 changes: 35 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
use thin_vec::thin_vec;
use std::ops::Range;
use thin_vec::{thin_vec, ThinVec};

pub fn expand_deriving_partial_ord(
cx: &mut ExtCtxt<'_>,
Expand Down Expand Up @@ -79,6 +80,13 @@ fn cs_partial_cmp(
let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);

if let SubstructureFields::Struct(_vdata, field_info) = substr.fields {
const FIELD_COUNTS_TO_USE_TUPLES: Range<usize> = 3..13;
if FIELD_COUNTS_TO_USE_TUPLES.contains(&field_info.len()) {
return cs_partial_cmp_via_tuple(cx, span, field_info);
}
}

// Builds:
//
// match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
Expand Down Expand Up @@ -151,3 +159,29 @@ fn cs_partial_cmp(
);
BlockOrExpr::new_expr(expr)
}

fn cs_partial_cmp_via_tuple(cx: &mut ExtCtxt<'_>, span: Span, fields: &[FieldInfo]) -> BlockOrExpr {
debug_assert!(fields.len() <= 12, "This won't work for more fields than tuples support");

let mut lhs_exprs = ThinVec::with_capacity(fields.len());
let mut rhs_exprs = ThinVec::with_capacity(fields.len());

for field in fields {
lhs_exprs.push(field.self_expr.clone());
// if i + 1 == fields.len() {
// // The last field might need an extra indirection because unsized
// expr = cx.expr_addr_of(field.span, expr);
// };
let [other_expr] = field.other_selflike_exprs.as_slice() else {
cx.span_bug(field.span, "not exactly 2 arguments in `derive(PartialOrd)`");
};
rhs_exprs.push(other_expr.clone());
}

let lhs = cx.expr_addr_of(span, cx.expr_tuple(span, lhs_exprs));
let rhs = cx.expr_addr_of(span, cx.expr_tuple(span, rhs_exprs));

let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
let call = cx.expr_call_global(span, partial_cmp_path, thin_vec![lhs, rhs]);
BlockOrExpr::new_expr(call)
}
3 changes: 3 additions & 0 deletions tests/ui/deriving/deriving-all-codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,6 @@ pub union Union {
pub u: u32,
pub i: i32,
}

#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct TooLongForTuple(u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8);
Loading