fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #69854 - pietroalbini:stable-next, r=Centrilb8cedc00407

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