fork/rust
git clone https://git.julia.blue/fork/rust
f3e1a954d2e
1.41.1
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// ignore-x86 FIXME: missing sysroot spans (#53081) 18// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py' 19 20{error_deriving} 21struct Error; 22{code} 23fn main() {{}} 24""" 25 26ENUM_STRING = """ 27#[derive({traits})] 28enum Enum {{ 29 A( 30 Error {errors} 31 ) 32}} 33""" 34ENUM_STRUCT_VARIANT_STRING = """ 35#[derive({traits})] 36enum Enum {{ 37 A {{ 38 x: Error {errors} 39 }} 40}} 41""" 42STRUCT_STRING = """ 43#[derive({traits})] 44struct Struct {{ 45 x: Error {errors} 46}} 47""" 48STRUCT_TUPLE_STRING = """ 49#[derive({traits})] 50struct Struct( 51 Error {errors} 52); 53""" 54 55ENUM_TUPLE, ENUM_STRUCT, STRUCT_FIELDS, STRUCT_TUPLE = range(4) 56 57def create_test_case(type, trait, super_traits, error_count): 58 string = [ENUM_STRING, ENUM_STRUCT_VARIANT_STRING, STRUCT_STRING, STRUCT_TUPLE_STRING][type] 59 all_traits = ','.join([trait] + super_traits) 60 super_traits = ','.join(super_traits) 61 error_deriving = '#[derive(%s)]' % super_traits if super_traits else '' 62 63 errors = '\n'.join('//~%s ERROR' % ('^' * n) for n in range(error_count)) 64 code = string.format(traits = all_traits, errors = errors) 65 return TEMPLATE.format(error_deriving=error_deriving, code = code) 66 67def write_file(name, string): 68 test_file = os.path.join(TEST_DIR, 'derives-span-%s.rs' % name) 69 70 # set write permission if file exists, so it can be changed 71 if os.path.exists(test_file): 72 os.chmod(test_file, stat.S_IWUSR) 73 74 with open(test_file, 'w') as f: 75 f.write(string) 76 77 # mark file read-only 78 os.chmod(test_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH) 79 80 81ENUM = 1 82STRUCT = 2 83ALL = STRUCT | ENUM 84 85traits = { 86 'Default': (STRUCT, [], 1), 87 'FromPrimitive': (0, [], 0), # only works for C-like enums 88 89 'Decodable': (0, [], 0), # FIXME: quoting gives horrible spans 90 'Encodable': (0, [], 0), # FIXME: quoting gives horrible spans 91} 92 93for (trait, supers, errs) in [('Clone', [], 1), 94 ('PartialEq', [], 2), 95 ('PartialOrd', ['PartialEq'], 1), 96 ('Eq', ['PartialEq'], 1), 97 ('Ord', ['Eq', 'PartialOrd', 'PartialEq'], 1), 98 ('Debug', [], 1), 99 ('Hash', [], 1)]: 100 traits[trait] = (ALL, supers, errs) 101 102for (trait, (types, super_traits, error_count)) in traits.items(): 103 mk = lambda ty: create_test_case(ty, trait, super_traits, error_count) 104 if types & ENUM: 105 write_file(trait + '-enum', mk(ENUM_TUPLE)) 106 write_file(trait + '-enum-struct-variant', mk(ENUM_STRUCT)) 107 if types & STRUCT: 108 write_file(trait + '-struct', mk(STRUCT_FIELDS)) 109 write_file(trait + '-tuple-struct', mk(STRUCT_TUPLE))