]> git.lizzy.rs Git - rust.git/blob - src/etc/generate-deriving-span-tests.py
Rollup merge of #53093 - 0e4ef622:issue-52169-fix, r=petrochenkov
[rust.git] / src / etc / generate-deriving-span-tests.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2013 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
6 #
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
12
13 """
14 This script creates a pile of compile-fail tests check that all the
15 derives have spans that point to the fields, rather than the
16 #[derive(...)] line.
17
18 sample usage: src/etc/generate-deriving-span-tests.py
19 """
20
21 import sys, os, datetime, stat, re
22
23 TEST_DIR = os.path.abspath(
24     os.path.join(os.path.dirname(__file__), '../test/compile-fail'))
25
26 YEAR = datetime.datetime.now().year
27
28 TEMPLATE = """// Copyright {year} The Rust Project Developers. See the COPYRIGHT
29 // file at the top-level directory of this distribution and at
30 // http://rust-lang.org/COPYRIGHT.
31 //
32 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
33 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
34 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
35 // option. This file may not be copied, modified, or distributed
36 // except according to those terms.
37
38 // This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
39
40 {error_deriving}
41 struct Error;
42 {code}
43 fn main() {{}}
44 """
45
46 ENUM_STRING = """
47 #[derive({traits})]
48 enum Enum {{
49    A(
50      Error {errors}
51      )
52 }}
53 """
54 ENUM_STRUCT_VARIANT_STRING = """
55 #[derive({traits})]
56 enum Enum {{
57    A {{
58      x: Error {errors}
59    }}
60 }}
61 """
62 STRUCT_STRING = """
63 #[derive({traits})]
64 struct Struct {{
65     x: Error {errors}
66 }}
67 """
68 STRUCT_TUPLE_STRING = """
69 #[derive({traits})]
70 struct Struct(
71     Error {errors}
72 );
73 """
74
75 ENUM_TUPLE, ENUM_STRUCT, STRUCT_FIELDS, STRUCT_TUPLE = range(4)
76
77 def create_test_case(type, trait, super_traits, error_count):
78     string = [ENUM_STRING, ENUM_STRUCT_VARIANT_STRING, STRUCT_STRING, STRUCT_TUPLE_STRING][type]
79     all_traits = ','.join([trait] + super_traits)
80     super_traits = ','.join(super_traits)
81     error_deriving = '#[derive(%s)]' % super_traits if super_traits else ''
82
83     errors = '\n'.join('//~%s ERROR' % ('^' * n) for n in range(error_count))
84     code = string.format(traits = all_traits, errors = errors)
85     return TEMPLATE.format(year = YEAR, error_deriving=error_deriving, code = code)
86
87 def write_file(name, string):
88     test_file = os.path.join(TEST_DIR, 'derives-span-%s.rs' % name)
89
90     with open(test_file) as f:
91         old_str = f.read()
92         old_str_ignoring_date = re.sub(r'^// Copyright \d+',
93                                         '// Copyright {year}'.format(year = YEAR), old_str)
94         if old_str_ignoring_date == string:
95             # if all we're doing is updating the copyright year, ignore it
96             return 0
97
98     # set write permission if file exists, so it can be changed
99     if os.path.exists(test_file):
100         os.chmod(test_file, stat.S_IWUSR)
101
102     with open(test_file, 'w') as f:
103         f.write(string)
104
105     # mark file read-only
106     os.chmod(test_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
107
108     return 1
109
110
111 ENUM = 1
112 STRUCT = 2
113 ALL = STRUCT | ENUM
114
115 traits = {
116     'Default': (STRUCT, [], 1),
117     'FromPrimitive': (0, [], 0), # only works for C-like enums
118
119     'Decodable': (0, [], 0), # FIXME: quoting gives horrible spans
120     'Encodable': (0, [], 0), # FIXME: quoting gives horrible spans
121 }
122
123 for (trait, supers, errs) in [('Clone', [], 1),
124                               ('PartialEq', [], 2),
125                               ('PartialOrd', ['PartialEq'], 1),
126                               ('Eq', ['PartialEq'], 1),
127                               ('Ord', ['Eq', 'PartialOrd', 'PartialEq'], 1),
128                               ('Debug', [], 1),
129                               ('Hash', [], 1)]:
130     traits[trait] = (ALL, supers, errs)
131
132 files = 0
133
134 for (trait, (types, super_traits, error_count)) in traits.items():
135     mk = lambda ty: create_test_case(ty, trait, super_traits, error_count)
136     if types & ENUM:
137         files += write_file(trait + '-enum', mk(ENUM_TUPLE))
138         files += write_file(trait + '-enum-struct-variant', mk(ENUM_STRUCT))
139     if types & STRUCT:
140         files += write_file(trait + '-struct', mk(STRUCT_FIELDS))
141         files += write_file(trait + '-tuple-struct', mk(STRUCT_TUPLE))
142
143 print('Generated {files} deriving span test{}.'.format('s' if files != 1 else '', files = files))