OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 import 'package:kernel/ast.dart'; |
| 5 import 'package:kernel/text/ast_to_text.dart'; |
| 6 import 'package:kernel/verifier.dart'; |
| 7 import 'package:test/test.dart'; |
| 8 |
| 9 /// Checks that the sanity checks correctly find errors in invalid programs. |
| 10 /// |
| 11 /// The frontend should never generate invalid programs, so we have to test |
| 12 /// these by manually constructing invalid ASTs. |
| 13 /// |
| 14 /// We only test negative cases here, as we get plenty of positive cases by |
| 15 /// compiling the Dart test suite with sanity checks enabled. |
| 16 main() { |
| 17 negativeTest('VariableGet out of scope', () { |
| 18 return new VariableGet(makeVariable()); |
| 19 }); |
| 20 negativeTest('VariableSet out of scope', () { |
| 21 return new VariableSet(makeVariable(), new NullLiteral()); |
| 22 }); |
| 23 negativeTest('Variable block scope', () { |
| 24 VariableDeclaration variable = makeVariable(); |
| 25 return new Block([ |
| 26 new Block([variable]), |
| 27 new ReturnStatement(new VariableGet(variable)) |
| 28 ]); |
| 29 }); |
| 30 negativeTest('Variable let scope', () { |
| 31 VariableDeclaration variable = makeVariable(); |
| 32 return new LogicalExpression(new Let(variable, new VariableGet(variable)), |
| 33 '&&', new VariableGet(variable)); |
| 34 }); |
| 35 negativeTest('Variable redeclared', () { |
| 36 VariableDeclaration variable = makeVariable(); |
| 37 return new Block([variable, variable]); |
| 38 }); |
| 39 negativeTest('Member redeclared', () { |
| 40 Field field = new Field(new Name('field'), initializer: new NullLiteral()); |
| 41 return new Class( |
| 42 name: 'Test', |
| 43 supertype: objectClass.asRawSupertype, |
| 44 fields: [field, field]); |
| 45 }); |
| 46 negativeTest('Type parameter out of scope', () { |
| 47 var parameter = makeTypeParameter(); |
| 48 return new ListLiteral([], typeArgument: new TypeParameterType(parameter)); |
| 49 }); |
| 50 negativeTest('Class type parameter from another class', () { |
| 51 return new TypeLiteral(new TypeParameterType(otherClass.typeParameters[0])); |
| 52 }); |
| 53 negativeTest('Class type parameter in static method', () { |
| 54 return new Procedure( |
| 55 new Name('test'), |
| 56 ProcedureKind.Method, |
| 57 new FunctionNode(new ReturnStatement( |
| 58 new TypeLiteral(new TypeParameterType(classTypeParameter)))), |
| 59 isStatic: true); |
| 60 }); |
| 61 negativeTest('Class type parameter in static field', () { |
| 62 return new Field(new Name('field'), |
| 63 initializer: new TypeLiteral(new TypeParameterType(classTypeParameter)), |
| 64 isStatic: true); |
| 65 }); |
| 66 negativeTest('Method type parameter out of scope', () { |
| 67 var parameter = makeTypeParameter(); |
| 68 return new Class( |
| 69 name: 'Test', |
| 70 supertype: objectClass.asRawSupertype, |
| 71 procedures: [ |
| 72 new Procedure( |
| 73 new Name('generic'), |
| 74 ProcedureKind.Method, |
| 75 new FunctionNode(new EmptyStatement(), |
| 76 typeParameters: [parameter])), |
| 77 new Procedure( |
| 78 new Name('use'), |
| 79 ProcedureKind.Method, |
| 80 new FunctionNode(new ReturnStatement( |
| 81 new TypeLiteral(new TypeParameterType(parameter))))) |
| 82 ]); |
| 83 }); |
| 84 negativeTest('Interface type arity too low', () { |
| 85 return new TypeLiteral(new InterfaceType(otherClass, [])); |
| 86 }); |
| 87 negativeTest('Interface type arity too high', () { |
| 88 return new TypeLiteral( |
| 89 new InterfaceType(otherClass, [new DynamicType(), new DynamicType()])); |
| 90 }); |
| 91 negativeTest('Dangling interface type', () { |
| 92 return new TypeLiteral(new InterfaceType(new Class())); |
| 93 }); |
| 94 negativeTest('Dangling field get', () { |
| 95 return new DirectPropertyGet(new NullLiteral(), new Field(new Name('foo'))); |
| 96 }); |
| 97 negativeTest('Missing block parent pointer', () { |
| 98 var block = new Block([]); |
| 99 block.statements.add(new ReturnStatement()); |
| 100 return block; |
| 101 }); |
| 102 negativeTest('Missing function parent pointer', () { |
| 103 var procedure = new Procedure(new Name('test'), ProcedureKind.Method, null); |
| 104 procedure.function = new FunctionNode(new EmptyStatement()); |
| 105 return procedure; |
| 106 }); |
| 107 } |
| 108 |
| 109 checkHasError(Program program) { |
| 110 bool passed = false; |
| 111 try { |
| 112 verifyProgram(program); |
| 113 passed = true; |
| 114 } catch (e) {} |
| 115 if (passed) { |
| 116 fail('Failed to reject invalid program:\n${programToString(program)}'); |
| 117 } |
| 118 } |
| 119 |
| 120 Class objectClass = new Class(name: 'Object'); |
| 121 |
| 122 Library stubLibrary = new Library(Uri.parse('dart:core')) |
| 123 ..addClass(objectClass); |
| 124 |
| 125 TypeParameter classTypeParameter = makeTypeParameter('T'); |
| 126 |
| 127 Class otherClass = new Class( |
| 128 name: 'OtherClass', |
| 129 typeParameters: [makeTypeParameter('OtherT')], |
| 130 supertype: objectClass.asRawSupertype); |
| 131 |
| 132 negativeTest(String name, TreeNode makeBody()) { |
| 133 test(name, () { |
| 134 var node = makeBody(); |
| 135 if (node is Expression) { |
| 136 node = new ReturnStatement(node); |
| 137 } |
| 138 if (node is Statement) { |
| 139 node = new FunctionNode(node); |
| 140 } |
| 141 if (node is FunctionNode) { |
| 142 node = new Procedure(new Name('test'), ProcedureKind.Method, node); |
| 143 } |
| 144 if (node is Member) { |
| 145 node = new Class( |
| 146 name: 'Test', |
| 147 typeParameters: [classTypeParameter], |
| 148 supertype: objectClass.asRawSupertype)..addMember(node); |
| 149 } |
| 150 if (node is Class) { |
| 151 node = new Library(Uri.parse('test.dart'), |
| 152 classes: <Class>[node, otherClass]); |
| 153 } |
| 154 if (node is Library) { |
| 155 node = new Program(<Library>[node, stubLibrary]); |
| 156 } |
| 157 assert(node is Program); |
| 158 checkHasError(node); |
| 159 }); |
| 160 } |
| 161 |
| 162 VariableDeclaration makeVariable() => new VariableDeclaration(null); |
| 163 |
| 164 TypeParameter makeTypeParameter([String name]) { |
| 165 return new TypeParameter(name, new InterfaceType(objectClass)); |
| 166 } |
OLD | NEW |