Skip to content

Commit c115470

Browse files
committed
Get some stuff compiling
1 parent 30b3f35 commit c115470

File tree

12 files changed

+246
-36
lines changed

12 files changed

+246
-36
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ pub struct Crate {
521521
///
522522
/// E.g., the '..' in `#[name(..)]`.
523523
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
524+
#[stable_hasher(no_hash_stable_eq)]
524525
pub enum NestedMetaItem {
525526
/// A full MetaItem, for recursive meta items.
526527
MetaItem(MetaItem),
@@ -534,6 +535,7 @@ pub enum NestedMetaItem {
534535
///
535536
/// E.g., `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`.
536537
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
538+
#[stable_hasher(no_hash_stable_eq)]
537539
pub struct MetaItem {
538540
pub path: Path,
539541
pub kind: MetaItemKind,
@@ -544,6 +546,7 @@ pub struct MetaItem {
544546
///
545547
/// E.g., `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`.
546548
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
549+
#[stable_hasher(no_hash_stable_eq)]
547550
pub enum MetaItemKind {
548551
/// Word meta item.
549552
///
@@ -1527,6 +1530,7 @@ impl MacCall {
15271530

15281531
/// Arguments passed to an attribute or a function-like macro.
15291532
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
1533+
#[stable_hasher(no_hash_stable_eq)]
15301534
pub enum MacArgs {
15311535
/// No arguments - `#[attr]`.
15321536
Empty,
@@ -1602,6 +1606,7 @@ impl MacDelimiter {
16021606

16031607
/// Represents a macro definition.
16041608
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
1609+
#[stable_hasher(no_hash_stable_eq)]
16051610
pub struct MacroDef {
16061611
pub body: P<MacArgs>,
16071612
/// `true` if macro was defined with `macro_rules`.
@@ -1621,6 +1626,7 @@ pub enum StrStyle {
16211626

16221627
/// An AST literal.
16231628
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
1629+
#[stable_hasher(no_hash_stable_eq)]
16241630
pub struct Lit {
16251631
/// The original literal token as written in source code.
16261632
pub token: token::Lit,
@@ -2021,6 +2027,7 @@ bitflags::bitflags! {
20212027
}
20222028

20232029
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Hash, HashStable_Generic)]
2030+
#[stable_hasher(no_hash_stable_eq)]
20242031
pub enum InlineAsmTemplatePiece {
20252032
String(String),
20262033
Placeholder { operand_idx: usize, modifier: Option<char>, span: Span },
@@ -2425,6 +2432,7 @@ impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId {
24252432
}
24262433

24272434
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2435+
#[stable_hasher(no_hash_stable_eq)]
24282436
pub struct AttrItem {
24292437
pub path: Path,
24302438
pub args: MacArgs,

compiler/rustc_ast/src/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: bool) -> bool {
178178
}
179179

180180
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
181+
#[stable_hasher(no_hash_stable_eq)]
181182
pub enum TokenKind {
182183
/* Expression-operator symbols. */
183184
Eq,
@@ -246,6 +247,7 @@ pub enum TokenKind {
246247
rustc_data_structures::static_assert_size!(TokenKind, 16);
247248

248249
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
250+
#[stable_hasher(no_hash_stable_eq)]
249251
pub struct Token {
250252
pub kind: TokenKind,
251253
pub span: Span,

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use std::{fmt, iter, mem};
3838
/// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
3939
/// Nothing special happens to misnamed or misplaced `SubstNt`s.
4040
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
41+
#[stable_hasher(no_hash_stable_eq)]
4142
pub enum TokenTree {
4243
/// A single token.
4344
Token(Token),

compiler/rustc_data_structures/src/stable_hasher.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ pub trait HashStable<CTX> {
199199
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher);
200200
}
201201

202+
pub trait HashStableEq {
203+
fn hash_stable_eq(&self, other: &Self) -> bool;
204+
}
205+
202206
/// Implement this for types that can be turned into stable keys like, for
203207
/// example, for DefId that can be converted to a DefPathHash. This is used for
204208
/// bringing maps into a predictable order before hashing them.
@@ -218,6 +222,13 @@ macro_rules! impl_stable_hash_via_hash {
218222
::std::hash::Hash::hash(self, hasher);
219223
}
220224
}
225+
226+
impl $crate::stable_hasher::HashStableEq for $t {
227+
#[inline]
228+
fn hash_stable_eq(&self, other: &Self) -> bool {
229+
self == other
230+
}
231+
}
221232
};
222233
}
223234

@@ -322,6 +333,24 @@ where
322333
}
323334
}
324335

336+
impl<T: HashStableEq> HashStableEq for [T] {
337+
fn hash_stable_eq(&self, other: &Self) -> bool {
338+
for (first, second) in self.iter().zip(other.iter()) {
339+
if !first.hash_stable_eq(second) {
340+
return false;
341+
}
342+
}
343+
true
344+
}
345+
}
346+
347+
impl<T: HashStableEq, const N: usize> HashStableEq for [T; N] {
348+
fn hash_stable_eq(&self, other: &Self) -> bool {
349+
(self[..]).hash_stable_eq(other)
350+
}
351+
}
352+
353+
325354
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
326355
default fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
327356
self.len().hash_stable(ctx, hasher);
@@ -331,6 +360,15 @@ impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
331360
}
332361
}
333362

363+
impl<T: HashStable<CTX>, CTX, const N: usize> HashStable<CTX> for [T; N] {
364+
default fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
365+
self.len().hash_stable(ctx, hasher);
366+
for item in self {
367+
item.hash_stable(ctx, hasher);
368+
}
369+
}
370+
}
371+
334372
impl<CTX> HashStable<CTX> for [u8] {
335373
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
336374
self.len().hash_stable(ctx, hasher);
@@ -405,6 +443,18 @@ impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> {
405443
}
406444
}
407445

446+
impl<T: ?Sized + HashStableEq> HashStableEq for ::std::rc::Rc<T> {
447+
fn hash_stable_eq(&self, other: &Self) -> bool {
448+
(**self).hash_stable_eq(other)
449+
}
450+
}
451+
452+
impl<T: ?Sized + HashStableEq> HashStableEq for ::std::sync::Arc<T> {
453+
fn hash_stable_eq(&self, other: &Self) -> bool {
454+
(**self).hash_stable_eq(other)
455+
}
456+
}
457+
408458
impl<CTX> HashStable<CTX> for str {
409459
#[inline]
410460
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
@@ -434,6 +484,22 @@ impl<CTX> HashStable<CTX> for bool {
434484
}
435485
}
436486

487+
impl<T: HashStableEq> HashStableEq for Option<T> {
488+
fn hash_stable_eq(&self, other: &Self) -> bool {
489+
match (self, other) {
490+
(Some(first), Some(second)) => first.hash_stable_eq(second),
491+
(None, None) => true,
492+
_ => false
493+
}
494+
}
495+
}
496+
497+
impl HashStableEq for bool {
498+
fn hash_stable_eq(&self, other: &Self) -> bool {
499+
self == other
500+
}
501+
}
502+
437503
impl<T, CTX> HashStable<CTX> for Option<T>
438504
where
439505
T: HashStable<CTX>,

compiler/rustc_macros/src/hash_stable.rs

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,27 @@ fn parse_attributes(field: &syn::Field) -> Attributes {
4141
attrs
4242
}
4343

44+
fn no_hash_stable_eq(s: &synstructure::Structure<'_>) -> bool {
45+
for attr in &s.ast().attrs {
46+
if let Ok(meta) = attr.parse_meta() {
47+
if !meta.path().is_ident("stable_hasher") {
48+
continue;
49+
}
50+
let arg: syn::Ident = attr.parse_args().unwrap();
51+
if arg.to_string() == "no_hash_stable_eq" {
52+
return true;
53+
} else {
54+
panic!("Unexpected argument {:?}", arg);
55+
}
56+
}
57+
}
58+
return false;
59+
}
60+
4461
pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
62+
let orig = s.clone();
63+
let no_eq = no_hash_stable_eq(&orig);
64+
4565
let generic: syn::GenericParam = parse_quote!(__CTX);
4666
s.add_bounds(synstructure::AddBounds::Generics);
4767
s.add_impl_generic(generic);
@@ -61,6 +81,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
6181
}
6282
});
6383

84+
6485
let discriminant = match s.ast().data {
6586
syn::Data::Enum(_) => quote! {
6687
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
@@ -69,7 +90,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
6990
syn::Data::Union(_) => panic!("cannot derive on union"),
7091
};
7192

72-
s.bound_impl(
93+
let impl_body = s.bound_impl(
7394
quote!(::rustc_data_structures::stable_hasher::HashStable<__CTX>),
7495
quote! {
7596
#[inline]
@@ -81,10 +102,72 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
81102
match *self { #body }
82103
}
83104
},
84-
)
105+
);
106+
107+
if no_eq {
108+
impl_body
109+
} else {
110+
let eq_impl = hash_stable_eq_derive(orig);
111+
//println!("Eq impl:\n{}", eq_impl);
112+
quote! {
113+
#impl_body
114+
#eq_impl
115+
}
116+
}
117+
}
118+
119+
pub fn hash_stable_eq_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
120+
let mut other = s.clone();
121+
other.binding_name(|_bi, i| Ident::new(&format!("__binding_other_{}", i), proc_macro2::Span::call_site()));
122+
123+
let eq_body: proc_macro2::TokenStream = s.variants().iter().zip(other.variants()).map(|(variant1, variant2)| {
124+
125+
let first_pat = variant1.pat();
126+
let second_pat = variant2.pat();
127+
128+
let compare = std::iter::once(quote! { true }).chain(variant1.bindings().iter().zip(variant2.bindings()).map(|(binding1, binding2)| {
129+
let attrs = parse_attributes(binding1.ast());
130+
if attrs.ignore {
131+
quote! { true }
132+
} else if let Some(project) = attrs.project {
133+
quote! {
134+
::rustc_data_structures::stable_hasher::HashStableEq::hash_stable_eq(
135+
#binding1.#project, #binding2.#project
136+
)
137+
}
138+
} else {
139+
quote! {
140+
::rustc_data_structures::stable_hasher::HashStableEq::hash_stable_eq(
141+
#binding1, #binding2
142+
)
143+
}
144+
}
145+
}));
146+
quote! {
147+
(#first_pat, #second_pat) => {
148+
#(#compare)&&*
149+
}
150+
}
151+
}).collect();
152+
153+
s.add_bounds(synstructure::AddBounds::Generics);
154+
s.bound_impl(
155+
quote!(::rustc_data_structures::stable_hasher::HashStableEq),
156+
quote! {
157+
fn hash_stable_eq(&self, other: &Self) -> bool {
158+
match (self, other) {
159+
#eq_body
160+
_ => false
161+
}
162+
}
163+
}
164+
)
85165
}
86166

87167
pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
168+
let orig = s.clone();
169+
let no_eq = no_hash_stable_eq(&orig);
170+
88171
let generic: syn::GenericParam = parse_quote!('__ctx);
89172
s.add_bounds(synstructure::AddBounds::Generics);
90173
s.add_impl_generic(generic);
@@ -111,7 +194,7 @@ pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
111194
syn::Data::Union(_) => panic!("cannot derive on union"),
112195
};
113196

114-
s.bound_impl(
197+
let impl_body = s.bound_impl(
115198
quote!(
116199
::rustc_data_structures::stable_hasher::HashStable<
117200
::rustc_query_system::ich::StableHashingContext<'__ctx>,
@@ -127,5 +210,15 @@ pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
127210
match *self { #body }
128211
}
129212
},
130-
)
213+
);
214+
215+
if no_eq {
216+
impl_body
217+
} else {
218+
let eq_impl = hash_stable_eq_derive(orig);
219+
quote! {
220+
#impl_body
221+
#eq_impl
222+
}
223+
}
131224
}

compiler/rustc_macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
2525
}
2626

2727
decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
28+
decl_derive!([HashStableEq, attributes(stable_hasher)] => hash_stable::hash_stable_eq_derive);
2829
decl_derive!(
2930
[HashStable_Generic, attributes(stable_hasher)] =>
3031
hash_stable::hash_stable_generic_derive

0 commit comments

Comments
 (0)