Skip to content

Commit 0a8a1ce

Browse files
committed
Make Ord, PartialOrd opt-out in newtype_index
1 parent 8604ef0 commit 0a8a1ce

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

compiler/rustc_macros/src/newtype.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ mod kw {
1010
syn::custom_keyword!(MAX);
1111
syn::custom_keyword!(ENCODABLE);
1212
syn::custom_keyword!(custom);
13+
syn::custom_keyword!(ORD_IMPL);
14+
syn::custom_keyword!(off);
1315
}
1416

1517
#[derive(Debug)]
@@ -42,6 +44,7 @@ impl Parse for Newtype {
4244
let mut max = None;
4345
let mut consts = Vec::new();
4446
let mut encodable = true;
47+
let mut ord = true;
4548

4649
// Parse an optional trailing comma
4750
let try_comma = || -> Result<()> {
@@ -100,6 +103,15 @@ impl Parse for Newtype {
100103
continue;
101104
}
102105

106+
if body.lookahead1().peek(kw::ORD_IMPL) {
107+
body.parse::<kw::ORD_IMPL>()?;
108+
body.parse::<Token![=]>()?;
109+
body.parse::<kw::off>()?;
110+
try_comma()?;
111+
ord = false;
112+
continue;
113+
}
114+
103115
// We've parsed everything that the user provided, so we're done
104116
if body.is_empty() {
105117
break;
@@ -137,6 +149,16 @@ impl Parse for Newtype {
137149
quote! {}
138150
};
139151

152+
let default_derives = if ord {
153+
quote! {
154+
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
155+
}
156+
} else {
157+
quote! {
158+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
159+
}
160+
};
161+
140162
let debug_impl = match debug_format {
141163
DebugFormat::Custom => quote! {},
142164
DebugFormat::Format(format) => {
@@ -152,7 +174,8 @@ impl Parse for Newtype {
152174

153175
Ok(Self(quote! {
154176
#(#attrs)*
155-
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, #(#derive_paths),*)]
177+
#default_derives
178+
#[derive(#(#derive_paths),*)]
156179
#[rustc_layout_scalar_valid_range_end(#max)]
157180
#vis struct #name {
158181
private: u32,

0 commit comments

Comments
 (0)