about summary refs log tree commit diff
path: root/compiler/rustc_hir/src/hir/tests.rs
blob: 1fd793bc1616f43bcdf031f46d7771d27ce88bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use rustc_span::def_id::DefIndex;

use super::*;

macro_rules! define_tests {
    ($($name:ident $kind:ident $variant:ident {$($init:tt)*})*) => {$(
        #[test]
        fn $name() {
            let unambig = $kind::$variant::<'_, ()> { $($init)* };
            let unambig_to_ambig = unsafe { std::mem::transmute::<_, $kind<'_, AmbigArg>>(unambig) };

            assert!(matches!(&unambig_to_ambig, &$kind::$variant { $($init)* }));

            let ambig_to_unambig = unsafe { std::mem::transmute::<_, $kind<'_, ()>>(unambig_to_ambig) };

            assert!(matches!(&ambig_to_unambig, &$kind::$variant { $($init)* }));
        }
    )*};
}

define_tests! {
    cast_never TyKind Never {}
    cast_tup TyKind Tup { 0: &[Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }] }
    cast_ptr TyKind Ptr { 0: MutTy { ty: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never }, mutbl: Mutability::Not }}
    cast_array TyKind Array {
        0: &Ty { span: DUMMY_SP, hir_id: HirId::INVALID, kind: TyKind::Never },
        1: &ConstArg { hir_id: HirId::INVALID, kind: ConstArgKind::Anon(&AnonConst {
            hir_id: HirId::INVALID,
            def_id: LocalDefId { local_def_index: DefIndex::ZERO },
            body: BodyId { hir_id: HirId::INVALID },
            span: DUMMY_SP,
        })}
    }

    cast_anon ConstArgKind Anon {
        0: &AnonConst {
            hir_id: HirId::INVALID,
            def_id: LocalDefId { local_def_index: DefIndex::ZERO },
            body: BodyId { hir_id: HirId::INVALID },
            span: DUMMY_SP,
        }
    }
}

#[test]
fn trait_object_roundtrips() {
    trait_object_roundtrips_impl(TraitObjectSyntax::Dyn);
    trait_object_roundtrips_impl(TraitObjectSyntax::DynStar);
    trait_object_roundtrips_impl(TraitObjectSyntax::None);
}

fn trait_object_roundtrips_impl(syntax: TraitObjectSyntax) {
    let lt = Lifetime {
        hir_id: HirId::INVALID,
        ident: Ident::new(sym::name, DUMMY_SP),
        kind: LifetimeKind::Static,
        source: LifetimeSource::Other,
        syntax: LifetimeSyntax::Implicit,
    };
    let unambig = TyKind::TraitObject::<'_, ()>(&[], TaggedRef::new(&lt, syntax));
    let unambig_to_ambig = unsafe { std::mem::transmute::<_, TyKind<'_, AmbigArg>>(unambig) };

    match unambig_to_ambig {
        TyKind::TraitObject(_, tagged_ref) => {
            assert!(tagged_ref.tag() == syntax)
        }
        _ => panic!("`TyKind::TraitObject` did not roundtrip"),
    };

    let ambig_to_unambig = unsafe { std::mem::transmute::<_, TyKind<'_, ()>>(unambig_to_ambig) };

    match ambig_to_unambig {
        TyKind::TraitObject(_, tagged_ref) => {
            assert!(tagged_ref.tag() == syntax)
        }
        _ => panic!("`TyKind::TraitObject` did not roundtrip"),
    };
}