impls_mir.rs 21.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! This module contains `HashStable` implementations for various MIR data
//! types in no particular order.

use ich::StableHashingContext;
use mir;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
                                           StableHasherResult};
use std::mem;

J
John Kåre Alsaker 已提交
20
impl_stable_hash_for!(struct mir::GeneratorLayout<'tcx> { fields });
21 22 23 24
impl_stable_hash_for!(struct mir::SourceInfo { span, scope });
impl_stable_hash_for!(enum mir::Mutability { Mut, Not });
impl_stable_hash_for!(enum mir::BorrowKind { Shared, Unique, Mut });
impl_stable_hash_for!(enum mir::LocalKind { Var, Temp, Arg, ReturnPointer });
25 26 27 28 29
impl_stable_hash_for!(struct mir::LocalDecl<'tcx> {
    mutability,
    ty,
    name,
    source_info,
30
    internal,
31
    lexical_scope,
32 33
    is_user_variable
});
34
impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref, mutability });
35
impl_stable_hash_for!(struct mir::BasicBlockData<'tcx> { statements, terminator, is_cleanup });
36
impl_stable_hash_for!(struct mir::UnsafetyViolation { source_info, description, kind });
A
Ariel Ben-Yehuda 已提交
37
impl_stable_hash_for!(struct mir::UnsafetyCheckResult { violations, unsafe_blocks });
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
impl<'gcx> HashStable<StableHashingContext<'gcx>>
for mir::UnsafetyViolationKind {
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
                                          hcx: &mut StableHashingContext<'gcx>,
                                          hasher: &mut StableHasher<W>) {

        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::UnsafetyViolationKind::General => {}
            mir::UnsafetyViolationKind::ExternStatic(lint_node_id) |
            mir::UnsafetyViolationKind::BorrowPacked(lint_node_id) => {
                lint_node_id.hash_stable(hcx, hasher);
            }

        }
    }
}
58
impl<'gcx> HashStable<StableHashingContext<'gcx>>
59
for mir::Terminator<'gcx> {
60 61
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
62
                                          hcx: &mut StableHashingContext<'gcx>,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
                                          hasher: &mut StableHasher<W>) {
        let mir::Terminator {
            ref kind,
            ref source_info,
        } = *self;

        let hash_spans_unconditionally = match *kind {
            mir::TerminatorKind::Assert { .. } => {
                // Assert terminators generate a panic message that contains the
                // source location, so we always have to feed its span into the
                // ICH.
                true
            }
            mir::TerminatorKind::Goto { .. } |
            mir::TerminatorKind::SwitchInt { .. } |
            mir::TerminatorKind::Resume |
            mir::TerminatorKind::Return |
J
John Kåre Alsaker 已提交
80
            mir::TerminatorKind::GeneratorDrop |
81 82 83
            mir::TerminatorKind::Unreachable |
            mir::TerminatorKind::Drop { .. } |
            mir::TerminatorKind::DropAndReplace { .. } |
J
John Kåre Alsaker 已提交
84
            mir::TerminatorKind::Yield { .. } |
85 86
            mir::TerminatorKind::Call { .. } |
            mir::TerminatorKind::FalseEdges { .. } => false,
87 88 89 90 91 92 93 94 95 96 97 98 99 100
        };

        if hash_spans_unconditionally {
            hcx.while_hashing_spans(true, |hcx| {
                source_info.hash_stable(hcx, hasher);
            })
        } else {
            source_info.hash_stable(hcx, hasher);
        }

        kind.hash_stable(hcx, hasher);
    }
}

101
impl<'gcx, T> HashStable<StableHashingContext<'gcx>> for mir::ClearCrossCrate<T>
102 103 104 105 106 107 108 109
    where T: HashStable<StableHashingContext<'gcx>>
{
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
                                          hcx: &mut StableHashingContext<'gcx>,
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
110 111
            mir::ClearCrossCrate::Clear => {}
            mir::ClearCrossCrate::Set(ref value) => {
112 113 114 115 116
                value.hash_stable(hcx, hasher);
            }
        }
    }
}
117

118
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Local {
119 120
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
121
                                          hcx: &mut StableHashingContext<'gcx>,
122 123 124 125 126 127
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

128
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::BasicBlock {
129 130
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
131
                                          hcx: &mut StableHashingContext<'gcx>,
132 133 134 135 136 137
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

138
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Field {
139 140
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
141
                                          hcx: &mut StableHashingContext<'gcx>,
142 143 144 145 146 147
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

148
impl<'gcx> HashStable<StableHashingContext<'gcx>>
149
for mir::VisibilityScope {
150 151
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
152
                                          hcx: &mut StableHashingContext<'gcx>,
153 154 155 156 157 158
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

159
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Promoted {
160 161
    #[inline]
    fn hash_stable<W: StableHasherResult>(&self,
162
                                          hcx: &mut StableHashingContext<'gcx>,
163 164 165 166 167 168
                                          hasher: &mut StableHasher<W>) {
        use rustc_data_structures::indexed_vec::Idx;
        self.index().hash_stable(hcx, hasher);
    }
}

169
impl<'gcx> HashStable<StableHashingContext<'gcx>>
170
for mir::TerminatorKind<'gcx> {
171
    fn hash_stable<W: StableHasherResult>(&self,
172
                                          hcx: &mut StableHashingContext<'gcx>,
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::TerminatorKind::Goto { ref target } => {
                target.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::SwitchInt { ref discr,
                                             switch_ty,
                                             ref values,
                                             ref targets } => {
                discr.hash_stable(hcx, hasher);
                switch_ty.hash_stable(hcx, hasher);
                values.hash_stable(hcx, hasher);
                targets.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::Resume |
            mir::TerminatorKind::Return |
J
John Kåre Alsaker 已提交
191
            mir::TerminatorKind::GeneratorDrop |
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
            mir::TerminatorKind::Unreachable => {}
            mir::TerminatorKind::Drop { ref location, target, unwind } => {
                location.hash_stable(hcx, hasher);
                target.hash_stable(hcx, hasher);
                unwind.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::DropAndReplace { ref location,
                                                  ref value,
                                                  target,
                                                  unwind, } => {
                location.hash_stable(hcx, hasher);
                value.hash_stable(hcx, hasher);
                target.hash_stable(hcx, hasher);
                unwind.hash_stable(hcx, hasher);
            }
J
John Kåre Alsaker 已提交
207
            mir::TerminatorKind::Yield { ref value,
J
John Kåre Alsaker 已提交
208 209 210 211 212 213
                                        resume,
                                        drop } => {
                value.hash_stable(hcx, hasher);
                resume.hash_stable(hcx, hasher);
                drop.hash_stable(hcx, hasher);
            }
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
            mir::TerminatorKind::Call { ref func,
                                        ref args,
                                        ref destination,
                                        cleanup } => {
                func.hash_stable(hcx, hasher);
                args.hash_stable(hcx, hasher);
                destination.hash_stable(hcx, hasher);
                cleanup.hash_stable(hcx, hasher);
            }
            mir::TerminatorKind::Assert { ref cond,
                                          expected,
                                          ref msg,
                                          target,
                                          cleanup } => {
                cond.hash_stable(hcx, hasher);
                expected.hash_stable(hcx, hasher);
                msg.hash_stable(hcx, hasher);
                target.hash_stable(hcx, hasher);
                cleanup.hash_stable(hcx, hasher);
            }
234 235 236 237 238 239
            mir::TerminatorKind::FalseEdges { ref real_target, ref imaginary_targets } => {
                real_target.hash_stable(hcx, hasher);
                for target in imaginary_targets {
                    target.hash_stable(hcx, hasher);
                }
            }
240 241 242 243
        }
    }
}

244
impl<'gcx> HashStable<StableHashingContext<'gcx>>
245
for mir::AssertMessage<'gcx> {
246
    fn hash_stable<W: StableHasherResult>(&self,
247
                                          hcx: &mut StableHashingContext<'gcx>,
248 249 250 251 252 253 254 255 256 257 258
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::AssertMessage::BoundsCheck { ref len, ref index } => {
                len.hash_stable(hcx, hasher);
                index.hash_stable(hcx, hasher);
            }
            mir::AssertMessage::Math(ref const_math_err) => {
                const_math_err.hash_stable(hcx, hasher);
            }
J
John Kåre Alsaker 已提交
259 260
            mir::AssertMessage::GeneratorResumedAfterReturn => (),
            mir::AssertMessage::GeneratorResumedAfterPanic => (),
261 262 263 264 265 266
        }
    }
}

impl_stable_hash_for!(struct mir::Statement<'tcx> { source_info, kind });

267
impl<'gcx> HashStable<StableHashingContext<'gcx>>
268
for mir::StatementKind<'gcx> {
269
    fn hash_stable<W: StableHasherResult>(&self,
270
                                          hcx: &mut StableHashingContext<'gcx>,
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::StatementKind::Assign(ref lvalue, ref rvalue) => {
                lvalue.hash_stable(hcx, hasher);
                rvalue.hash_stable(hcx, hasher);
            }
            mir::StatementKind::SetDiscriminant { ref lvalue, variant_index } => {
                lvalue.hash_stable(hcx, hasher);
                variant_index.hash_stable(hcx, hasher);
            }
            mir::StatementKind::StorageLive(ref lvalue) |
            mir::StatementKind::StorageDead(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
287 288
            mir::StatementKind::EndRegion(ref region_scope) => {
                region_scope.hash_stable(hcx, hasher);
289 290 291 292
            }
            mir::StatementKind::Validate(ref op, ref lvalues) => {
                op.hash_stable(hcx, hasher);
                lvalues.hash_stable(hcx, hasher);
293
            }
294 295 296 297 298 299 300 301 302 303
            mir::StatementKind::Nop => {}
            mir::StatementKind::InlineAsm { ref asm, ref outputs, ref inputs } => {
                asm.hash_stable(hcx, hasher);
                outputs.hash_stable(hcx, hasher);
                inputs.hash_stable(hcx, hasher);
            }
        }
    }
}

304
impl<'gcx, T> HashStable<StableHashingContext<'gcx>>
305
    for mir::ValidationOperand<'gcx, T>
306
    where T: HashStable<StableHashingContext<'gcx>>
307 308
{
    fn hash_stable<W: StableHasherResult>(&self,
309
                                          hcx: &mut StableHashingContext<'gcx>,
310 311 312 313 314 315 316 317
                                          hasher: &mut StableHasher<W>)
    {
        self.lval.hash_stable(hcx, hasher);
        self.ty.hash_stable(hcx, hasher);
        self.re.hash_stable(hcx, hasher);
        self.mutbl.hash_stable(hcx, hasher);
    }
}
318

319
impl_stable_hash_for!(enum mir::ValidationOp { Acquire, Release, Suspend(region_scope) });
320

321
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Place<'gcx> {
322
    fn hash_stable<W: StableHasherResult>(&self,
323
                                          hcx: &mut StableHashingContext<'gcx>,
324 325 326
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
327
            mir::Place::Local(ref local) => {
328 329
                local.hash_stable(hcx, hasher);
            }
330
            mir::Place::Static(ref statik) => {
331 332
                statik.hash_stable(hcx, hasher);
            }
333
            mir::Place::Projection(ref lvalue_projection) => {
334 335 336 337 338 339
                lvalue_projection.hash_stable(hcx, hasher);
            }
        }
    }
}

340
impl<'gcx, B, V, T> HashStable<StableHashingContext<'gcx>>
341
for mir::Projection<'gcx, B, V, T>
342 343 344
    where B: HashStable<StableHashingContext<'gcx>>,
          V: HashStable<StableHashingContext<'gcx>>,
          T: HashStable<StableHashingContext<'gcx>>
345 346
{
    fn hash_stable<W: StableHasherResult>(&self,
347
                                          hcx: &mut StableHashingContext<'gcx>,
348 349 350 351 352 353 354 355 356 357 358
                                          hasher: &mut StableHasher<W>) {
        let mir::Projection {
            ref base,
            ref elem,
        } = *self;

        base.hash_stable(hcx, hasher);
        elem.hash_stable(hcx, hasher);
    }
}

359
impl<'gcx, V, T> HashStable<StableHashingContext<'gcx>>
360
for mir::ProjectionElem<'gcx, V, T>
361 362
    where V: HashStable<StableHashingContext<'gcx>>,
          T: HashStable<StableHashingContext<'gcx>>
363 364
{
    fn hash_stable<W: StableHasherResult>(&self,
365
                                          hcx: &mut StableHashingContext<'gcx>,
366 367 368 369
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
            mir::ProjectionElem::Deref => {}
370
            mir::ProjectionElem::Field(field, ref ty) => {
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                field.hash_stable(hcx, hasher);
                ty.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::Index(ref value) => {
                value.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
                offset.hash_stable(hcx, hasher);
                min_length.hash_stable(hcx, hasher);
                from_end.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::Subslice { from, to } => {
                from.hash_stable(hcx, hasher);
                to.hash_stable(hcx, hasher);
            }
            mir::ProjectionElem::Downcast(adt_def, variant) => {
                adt_def.hash_stable(hcx, hasher);
                variant.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(struct mir::VisibilityScopeData { span, parent_scope });
A
Ariel Ben-Yehuda 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
impl_stable_hash_for!(struct mir::VisibilityScopeInfo {
    lint_root, safety
});

impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Safety {
    fn hash_stable<W: StableHasherResult>(&self,
                                          hcx: &mut StableHashingContext<'gcx>,
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::Safety::Safe |
            mir::Safety::BuiltinUnsafe |
            mir::Safety::FnUnsafe => {}
            mir::Safety::ExplicitUnsafe(node_id) => {
                node_id.hash_stable(hcx, hasher);
            }
        }
    }
}
415

416
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Operand<'gcx> {
417
    fn hash_stable<W: StableHasherResult>(&self,
418
                                          hcx: &mut StableHashingContext<'gcx>,
419 420 421 422
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
423 424 425 426
            mir::Operand::Copy(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
            mir::Operand::Move(ref lvalue) => {
427 428 429 430 431 432 433 434 435
                lvalue.hash_stable(hcx, hasher);
            }
            mir::Operand::Constant(ref constant) => {
                constant.hash_stable(hcx, hasher);
            }
        }
    }
}

436
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Rvalue<'gcx> {
437
    fn hash_stable<W: StableHasherResult>(&self,
438
                                          hcx: &mut StableHashingContext<'gcx>,
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);

        match *self {
            mir::Rvalue::Use(ref operand) => {
                operand.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Repeat(ref operand, ref val) => {
                operand.hash_stable(hcx, hasher);
                val.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Ref(region, borrow_kind, ref lvalue) => {
                region.hash_stable(hcx, hasher);
                borrow_kind.hash_stable(hcx, hasher);
                lvalue.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Len(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Cast(cast_kind, ref operand, ty) => {
                cast_kind.hash_stable(hcx, hasher);
                operand.hash_stable(hcx, hasher);
                ty.hash_stable(hcx, hasher);
            }
            mir::Rvalue::BinaryOp(op, ref operand1, ref operand2) |
            mir::Rvalue::CheckedBinaryOp(op, ref operand1, ref operand2) => {
                op.hash_stable(hcx, hasher);
                operand1.hash_stable(hcx, hasher);
                operand2.hash_stable(hcx, hasher);
            }
            mir::Rvalue::UnaryOp(op, ref operand) => {
                op.hash_stable(hcx, hasher);
                operand.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Discriminant(ref lvalue) => {
                lvalue.hash_stable(hcx, hasher);
            }
476 477
            mir::Rvalue::NullaryOp(op, ty) => {
                op.hash_stable(hcx, hasher);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
                ty.hash_stable(hcx, hasher);
            }
            mir::Rvalue::Aggregate(ref kind, ref operands) => {
                kind.hash_stable(hcx, hasher);
                operands.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(enum mir::CastKind {
    Misc,
    ReifyFnPointer,
    ClosureFnPointer,
    UnsafeFnPointer,
    Unsize
});

496
impl<'gcx> HashStable<StableHashingContext<'gcx>>
497
for mir::AggregateKind<'gcx> {
498
    fn hash_stable<W: StableHasherResult>(&self,
499
                                          hcx: &mut StableHashingContext<'gcx>,
500 501 502 503 504 505 506 507 508 509 510 511 512
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
            mir::AggregateKind::Tuple => {}
            mir::AggregateKind::Array(t) => {
                t.hash_stable(hcx, hasher);
            }
            mir::AggregateKind::Adt(adt_def, idx, substs, active_field) => {
                adt_def.hash_stable(hcx, hasher);
                idx.hash_stable(hcx, hasher);
                substs.hash_stable(hcx, hasher);
                active_field.hash_stable(hcx, hasher);
            }
513
            mir::AggregateKind::Closure(def_id, ref substs) => {
514 515 516
                def_id.hash_stable(hcx, hasher);
                substs.hash_stable(hcx, hasher);
            }
517 518 519 520 521
            mir::AggregateKind::Generator(def_id, ref substs, ref interior) => {
                def_id.hash_stable(hcx, hasher);
                substs.hash_stable(hcx, hasher);
                interior.hash_stable(hcx, hasher);
            }
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
        }
    }
}

impl_stable_hash_for!(enum mir::BinOp {
    Add,
    Sub,
    Mul,
    Div,
    Rem,
    BitXor,
    BitAnd,
    BitOr,
    Shl,
    Shr,
    Eq,
    Lt,
    Le,
    Ne,
    Ge,
542 543
    Gt,
    Offset
544 545 546 547 548 549 550
});

impl_stable_hash_for!(enum mir::UnOp {
    Not,
    Neg
});

551 552 553 554
impl_stable_hash_for!(enum mir::NullOp {
    Box,
    SizeOf
});
555 556 557

impl_stable_hash_for!(struct mir::Constant<'tcx> { span, ty, literal });

558
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Literal<'gcx> {
559
    fn hash_stable<W: StableHasherResult>(&self,
560
                                          hcx: &mut StableHashingContext<'gcx>,
561 562 563 564 565 566 567 568 569 570 571 572 573 574
                                          hasher: &mut StableHasher<W>) {
        mem::discriminant(self).hash_stable(hcx, hasher);
        match *self {
            mir::Literal::Value { ref value } => {
                value.hash_stable(hcx, hasher);
            }
            mir::Literal::Promoted { index } => {
                index.hash_stable(hcx, hasher);
            }
        }
    }
}

impl_stable_hash_for!(struct mir::Location { block, statement_index });