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