1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module contains `HashStable` implementations for various data types
12 //! from libsyntax in no particular order.
14 use ich::StableHashingContext;
16 use std::hash as std_hash;
20 use syntax::parse::token;
21 use syntax::tokenstream;
24 use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
26 use rustc_data_structures::accumulate_vec::AccumulateVec;
28 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ::syntax::symbol::InternedString {
30 fn hash_stable<W: StableHasherResult>(&self,
31 hcx: &mut StableHashingContext<'a, 'tcx>,
32 hasher: &mut StableHasher<W>) {
33 let s: &str = &**self;
34 s.hash_stable(hcx, hasher);
38 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ast::Name {
40 fn hash_stable<W: StableHasherResult>(&self,
41 hcx: &mut StableHashingContext<'a, 'tcx>,
42 hasher: &mut StableHasher<W>) {
43 self.as_str().hash_stable(hcx, hasher);
47 impl_stable_hash_for!(enum ::syntax::ast::AsmDialect {
52 impl_stable_hash_for!(enum ::syntax::ext::base::MacroKind {
59 impl_stable_hash_for!(enum ::syntax::abi::Abi {
79 impl_stable_hash_for!(struct ::syntax::attr::Deprecation { since, note });
80 impl_stable_hash_for!(struct ::syntax::attr::Stability { level, feature, rustc_depr });
82 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ::syntax::attr::StabilityLevel {
83 fn hash_stable<W: StableHasherResult>(&self,
84 hcx: &mut StableHashingContext<'a, 'tcx>,
85 hasher: &mut StableHasher<W>) {
86 mem::discriminant(self).hash_stable(hcx, hasher);
88 ::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue } => {
89 reason.hash_stable(hcx, hasher);
90 issue.hash_stable(hcx, hasher);
92 ::syntax::attr::StabilityLevel::Stable { ref since } => {
93 since.hash_stable(hcx, hasher);
99 impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason });
102 impl_stable_hash_for!(enum ::syntax::attr::IntType {
107 impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
113 impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
114 impl_stable_hash_for!(enum ::syntax::ast::LitKind {
119 Int(value, lit_int_type),
120 Float(value, float_ty),
121 FloatUnsuffixed(value),
125 impl_stable_hash_for!(enum ::syntax::ast::IntTy { Is, I8, I16, I32, I64, I128 });
126 impl_stable_hash_for!(enum ::syntax::ast::UintTy { Us, U8, U16, U32, U64, U128 });
127 impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
128 impl_stable_hash_for!(enum ::syntax::ast::Unsafety { Unsafe, Normal });
129 impl_stable_hash_for!(enum ::syntax::ast::Constness { Const, NotConst });
130 impl_stable_hash_for!(enum ::syntax::ast::Defaultness { Default, Final });
131 impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, span, name });
132 impl_stable_hash_for!(enum ::syntax::ast::StrStyle { Cooked, Raw(pounds) });
133 impl_stable_hash_for!(enum ::syntax::ast::AttrStyle { Outer, Inner });
135 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for [ast::Attribute] {
136 fn hash_stable<W: StableHasherResult>(&self,
137 hcx: &mut StableHashingContext<'a, 'tcx>,
138 hasher: &mut StableHasher<W>) {
139 // Some attributes are always ignored during hashing.
140 let filtered: AccumulateVec<[&ast::Attribute; 8]> = self
143 !attr.is_sugared_doc &&
144 attr.name().map(|name| !hcx.is_ignored_attr(name)).unwrap_or(true)
148 filtered.len().hash_stable(hcx, hasher);
149 for attr in filtered {
150 attr.hash_stable(hcx, hasher);
155 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ast::Attribute {
156 fn hash_stable<W: StableHasherResult>(&self,
157 hcx: &mut StableHashingContext<'a, 'tcx>,
158 hasher: &mut StableHasher<W>) {
159 // Make sure that these have been filtered out.
160 debug_assert!(self.name().map(|name| !hcx.is_ignored_attr(name)).unwrap_or(true));
161 debug_assert!(!self.is_sugared_doc);
172 style.hash_stable(hcx, hasher);
173 path.segments.len().hash_stable(hcx, hasher);
174 for segment in &path.segments {
175 segment.identifier.name.hash_stable(hcx, hasher);
177 for tt in tokens.trees() {
178 tt.hash_stable(hcx, hasher);
180 span.hash_stable(hcx, hasher);
184 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for tokenstream::TokenTree {
185 fn hash_stable<W: StableHasherResult>(&self,
186 hcx: &mut StableHashingContext<'a, 'tcx>,
187 hasher: &mut StableHasher<W>) {
188 mem::discriminant(self).hash_stable(hcx, hasher);
190 tokenstream::TokenTree::Token(span, ref token) => {
191 span.hash_stable(hcx, hasher);
192 hash_token(token, hcx, hasher, span);
194 tokenstream::TokenTree::Delimited(span, ref delimited) => {
195 span.hash_stable(hcx, hasher);
196 std_hash::Hash::hash(&delimited.delim, hasher);
197 for sub_tt in delimited.stream().trees() {
198 sub_tt.hash_stable(hcx, hasher);
205 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for tokenstream::TokenStream {
206 fn hash_stable<W: StableHasherResult>(&self,
207 hcx: &mut StableHashingContext<'a, 'tcx>,
208 hasher: &mut StableHasher<W>) {
209 for sub_tt in self.trees() {
210 sub_tt.hash_stable(hcx, hasher);
215 fn hash_token<'a, 'tcx, W: StableHasherResult>(token: &token::Token,
216 hcx: &mut StableHashingContext<'a, 'tcx>,
217 hasher: &mut StableHasher<W>,
218 error_reporting_span: Span) {
219 mem::discriminant(token).hash_stable(hcx, hasher);
228 token::Token::AndAnd |
231 token::Token::Tilde |
234 token::Token::DotDot |
235 token::Token::DotDotDot |
236 token::Token::Comma |
238 token::Token::Colon |
239 token::Token::ModSep |
240 token::Token::RArrow |
241 token::Token::LArrow |
242 token::Token::FatArrow |
243 token::Token::Pound |
244 token::Token::Dollar |
245 token::Token::Question |
246 token::Token::Underscore |
247 token::Token::Whitespace |
248 token::Token::Comment |
249 token::Token::Eof => {}
251 token::Token::BinOp(bin_op_token) |
252 token::Token::BinOpEq(bin_op_token) => {
253 std_hash::Hash::hash(&bin_op_token, hasher);
256 token::Token::OpenDelim(delim_token) |
257 token::Token::CloseDelim(delim_token) => {
258 std_hash::Hash::hash(&delim_token, hasher);
260 token::Token::Literal(ref lit, ref opt_name) => {
261 mem::discriminant(lit).hash_stable(hcx, hasher);
263 token::Lit::Byte(val) |
264 token::Lit::Char(val) |
265 token::Lit::Integer(val) |
266 token::Lit::Float(val) |
267 token::Lit::Str_(val) |
268 token::Lit::ByteStr(val) => val.hash_stable(hcx, hasher),
269 token::Lit::StrRaw(val, n) |
270 token::Lit::ByteStrRaw(val, n) => {
271 val.hash_stable(hcx, hasher);
272 n.hash_stable(hcx, hasher);
275 opt_name.hash_stable(hcx, hasher);
278 token::Token::Ident(ident) |
279 token::Token::Lifetime(ident) |
280 token::Token::SubstNt(ident) => ident.name.hash_stable(hcx, hasher),
282 token::Token::Interpolated(ref non_terminal) => {
283 // FIXME(mw): This could be implemented properly. It's just a
284 // lot of work, since we would need to hash the AST
285 // in a stable way, in addition to the HIR.
286 // Since this is hardly used anywhere, just emit a
288 if hcx.tcx().sess.opts.debugging_opts.incremental.is_some() {
289 let msg = format!("Quasi-quoting might make incremental \
290 compilation very inefficient: {:?}",
292 hcx.tcx().sess.span_warn(error_reporting_span, &msg[..]);
295 std_hash::Hash::hash(non_terminal, hasher);
298 token::Token::DocComment(val) |
299 token::Token::Shebang(val) => val.hash_stable(hcx, hasher),