]> git.lizzy.rs Git - rust.git/blob - src/etc/generate-deriving-span-tests.py
Rollup merge of #67642 - Mark-Simulacrum:relax-bounds, r=Amanieu
[rust.git] / src / etc / generate-deriving-span-tests.py
1 #!/usr/bin/env python
2
3 """
4 This script creates a pile of compile-fail tests check that all the
5 derives have spans that point to the fields, rather than the
6 #[derive(...)] line.
7
8 sample usage: src/etc/generate-deriving-span-tests.py
9 """
10
11 import os
12 import stat
13
14 TEST_DIR = os.path.abspath(
15     os.path.join(os.path.dirname(__file__), '../test/ui/derives/'))
16
17 TEMPLATE = """\
18 // FIXME: missing sysroot spans (#53081)
19 // ignore-i586-unknown-linux-gnu
20 // ignore-i586-unknown-linux-musl
21 // This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
22
23 {error_deriving}
24 struct Error;
25 {code}
26 fn main() {{}}
27 """
28
29 ENUM_STRING = """
30 #[derive({traits})]
31 enum Enum {{
32    A(
33      Error {errors}
34      )
35 }}
36 """
37 ENUM_STRUCT_VARIANT_STRING = """
38 #[derive({traits})]
39 enum Enum {{
40    A {{
41      x: Error {errors}
42    }}
43 }}
44 """
45 STRUCT_STRING = """
46 #[derive({traits})]
47 struct Struct {{
48     x: Error {errors}
49 }}
50 """
51 STRUCT_TUPLE_STRING = """
52 #[derive({traits})]
53 struct Struct(
54     Error {errors}
55 );
56 """
57
58 ENUM_TUPLE, ENUM_STRUCT, STRUCT_FIELDS, STRUCT_TUPLE = range(4)
59
60
61 def create_test_case(type, trait, super_traits, error_count):
62     string = [ENUM_STRING, ENUM_STRUCT_VARIANT_STRING, STRUCT_STRING, STRUCT_TUPLE_STRING][type]
63     all_traits = ','.join([trait] + super_traits)
64     super_traits = ','.join(super_traits)
65     error_deriving = '#[derive(%s)]' % super_traits if super_traits else ''
66
67     errors = '\n'.join('//~%s ERROR' % ('^' * n) for n in range(error_count))
68     code = string.format(traits=all_traits, errors=errors)
69     return TEMPLATE.format(error_deriving=error_deriving, code=code)
70
71
72 def write_file(name, string):
73     test_file = os.path.join(TEST_DIR, 'derives-span-%s.rs' % name)
74
75     # set write permission if file exists, so it can be changed
76     if os.path.exists(test_file):
77         os.chmod(test_file, stat.S_IWUSR)
78
79     with open(test_file, 'w') as f:
80         f.write(string)
81
82     # mark file read-only
83     os.chmod(test_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
84
85
86 ENUM = 1
87 STRUCT = 2
88 ALL = STRUCT | ENUM
89
90 traits = {
91     'Default': (STRUCT, [], 1),
92     'FromPrimitive': (0, [], 0),  # only works for C-like enums
93
94     'Decodable': (0, [], 0),  # FIXME: quoting gives horrible spans
95     'Encodable': (0, [], 0),  # FIXME: quoting gives horrible spans
96 }
97
98 for (trait, supers, errs) in [('Clone', [], 1),
99                               ('PartialEq', [], 2),
100                               ('PartialOrd', ['PartialEq'], 1),
101                               ('Eq', ['PartialEq'], 1),
102                               ('Ord', ['Eq', 'PartialOrd', 'PartialEq'], 1),
103                               ('Debug', [], 1),
104                               ('Hash', [], 1)]:
105     traits[trait] = (ALL, supers, errs)
106
107 for (trait, (types, super_traits, error_count)) in traits.items():
108     mk = lambda ty: create_test_case(ty, trait, super_traits, error_count)
109     if types & ENUM:
110         write_file(trait + '-enum', mk(ENUM_TUPLE))
111         write_file(trait + '-enum-struct-variant', mk(ENUM_STRUCT))
112     if types & STRUCT:
113         write_file(trait + '-struct', mk(STRUCT_FIELDS))
114         write_file(trait + '-tuple-struct', mk(STRUCT_TUPLE))