PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/Src/Compilers/CSharp/Test/Syntax/Parsing/ParserErrorMessageTests.cs

https://github.com/EkardNT/Roslyn
C# | 4833 lines | 3733 code | 345 blank | 755 comment | 0 complexity | b9bf06fc231d64395ae251b9abc9c464 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. // Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System.Linq;
  3. using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
  4. using Microsoft.CodeAnalysis.Test.Utilities;
  5. using Roslyn.Test.Utilities;
  6. using Xunit;
  7. namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  8. {
  9. public class ParserErrorMessageTests : CSharpTestBase
  10. {
  11. #region "Targeted Error Tests - please arrange tests in the order of error code"
  12. [WorkItem(536666, "DevDiv")]
  13. [Fact]
  14. public void CS0071ERR_ExplicitEventFieldImpl()
  15. {
  16. // Diff errors
  17. var test = @"
  18. public delegate void D();
  19. interface Itest
  20. {
  21. event D E;
  22. }
  23. class Test : Itest
  24. {
  25. event D ITest.E() // CS0071
  26. {
  27. }
  28. public static int Main()
  29. {
  30. return 1;
  31. }
  32. }
  33. ";
  34. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ExplicitEventFieldImpl, "."), Diagnostic(ErrorCode.ERR_MemberNeedsType, "E"));
  35. }
  36. // Infinite loop
  37. [Fact]
  38. public void CS0073ERR_AddRemoveMustHaveBody()
  39. {
  40. var test = @"
  41. using System;
  42. class C
  43. {
  44. event Action E { add; remove; }
  45. }
  46. abstract class A
  47. {
  48. public abstract event Action E { add; remove; }
  49. }
  50. ";
  51. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  52. // (5,25): error CS0073: An add or remove accessor must have a body
  53. // event Action E { add; remove; }
  54. Diagnostic(ErrorCode.ERR_AddRemoveMustHaveBody, ";"),
  55. // (5,33): error CS0073: An add or remove accessor must have a body
  56. // event Action E { add; remove; }
  57. Diagnostic(ErrorCode.ERR_AddRemoveMustHaveBody, ";"),
  58. // (9,41): error CS0073: An add or remove accessor must have a body
  59. // public abstract event Action E { add; remove; }
  60. Diagnostic(ErrorCode.ERR_AddRemoveMustHaveBody, ";"),
  61. // (9,49): error CS0073: An add or remove accessor must have a body
  62. // public abstract event Action E { add; remove; }
  63. Diagnostic(ErrorCode.ERR_AddRemoveMustHaveBody, ";"));
  64. }
  65. [Fact]
  66. public void CS0080ERR_ConstraintOnlyAllowedOnGenericDecl()
  67. {
  68. var test = @"
  69. interface I {}
  70. class C where C : I // CS0080 - C is not generic class
  71. {
  72. }
  73. public class Test
  74. {
  75. public static int Main ()
  76. {
  77. return 1;
  78. }
  79. }
  80. ";
  81. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  82. // (3,9): error CS0080: Constraints are not allowed on non-generic declarations
  83. Diagnostic(ErrorCode.ERR_ConstraintOnlyAllowedOnGenericDecl, "where").WithLocation(3, 9));
  84. }
  85. [WorkItem(527827, "DevDiv")]
  86. [Fact]
  87. public void CS0080ERR_ConstraintOnlyAllowedOnGenericDecl_2()
  88. {
  89. var test = @"
  90. class C
  91. where C : I
  92. {
  93. }
  94. ";
  95. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  96. // (3,5): error CS0080: Constraints are not allowed on non-generic declarations
  97. // where C : I
  98. Diagnostic(ErrorCode.ERR_ConstraintOnlyAllowedOnGenericDecl, "where"));
  99. }
  100. [Fact]
  101. public void CS0107ERR_BadMemberProtection()
  102. {
  103. var test = @"
  104. public class C
  105. {
  106. private internal void f() {}
  107. public static int Main()
  108. {
  109. return 1;
  110. }
  111. }
  112. ";
  113. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadMemberProtection, "internal"));
  114. }
  115. [Fact, WorkItem(543622, "DevDiv")]
  116. public void CS0116ERR__NamespaceUnexpected()
  117. {
  118. var test = @"{
  119. get
  120. {
  121. ParseDefaultDir();
  122. }
  123. }";
  124. // Extra errors
  125. ParseAndValidate(test,
  126. // (1,1): error CS1022: Type or namespace definition, or end-of-file expected
  127. // {
  128. Diagnostic(ErrorCode.ERR_EOFExpected, "{"),
  129. // (3,5): error CS1022: Type or namespace definition, or end-of-file expected
  130. // {
  131. Diagnostic(ErrorCode.ERR_EOFExpected, "{"),
  132. // (3,6): error CS1520: Method must have a return type
  133. // {
  134. Diagnostic(ErrorCode.ERR_MemberNeedsType, ""),
  135. // (2,5): error CS0116: A namespace does not directly contain members such as fields or methods
  136. // get
  137. Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "get"),
  138. // (5,5): error CS1022: Type or namespace definition, or end-of-file expected
  139. // }
  140. Diagnostic(ErrorCode.ERR_EOFExpected, "}"),
  141. // (6,1): error CS1022: Type or namespace definition, or end-of-file expected
  142. // }
  143. Diagnostic(ErrorCode.ERR_EOFExpected, "}"));
  144. }
  145. [Fact]
  146. public void CS0145ERR_ConstValueRequired()
  147. {
  148. var test = @"
  149. namespace x
  150. {
  151. public class a
  152. {
  153. public static int Main()
  154. {
  155. return 1;
  156. }
  157. }
  158. public class b : a
  159. {
  160. public const int i;
  161. }
  162. }
  163. ";
  164. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ConstValueRequired, "i"));
  165. }
  166. [WorkItem(536667, "DevDiv")]
  167. [Fact]
  168. public void CS0150ERR_ConstantExpected()
  169. {
  170. var test = @"
  171. using namespace System;
  172. public class mine {
  173. public enum e1 {one=1, two=2, three= };
  174. public static int Main()
  175. {
  176. return 1;
  177. }
  178. };
  179. }
  180. ";
  181. ParseAndValidate(test,
  182. // (2,7): error CS1041: Identifier expected; 'namespace' is a keyword
  183. // using namespace System;
  184. Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "namespace").WithArguments("", "namespace"),
  185. // (2,23): error CS1514: { expected
  186. // using namespace System;
  187. Diagnostic(ErrorCode.ERR_LbraceExpected, ";"),
  188. // (4,42): error CS0150: A constant value is expected
  189. // public enum e1 {one=1, two=2, three= };
  190. Diagnostic(ErrorCode.ERR_ConstantExpected, ""));
  191. }
  192. [WorkItem(862028, "DevDiv/Personal")]
  193. [Fact]
  194. public void CS0178ERR_InvalidArray()
  195. {
  196. // Diff errors
  197. var test = @"
  198. class A
  199. {
  200. public static int Main()
  201. {
  202. int[] arr = new int[5][5;
  203. return 1;
  204. }
  205. }
  206. ";
  207. ParseAndValidate(test,
  208. // (6,32): error CS0178: Invalid rank specifier: expected ',' or ']'
  209. // int[] arr = new int[5][5;
  210. Diagnostic(ErrorCode.ERR_InvalidArray, "5"),
  211. // (6,33): error CS1003: Syntax error, ',' expected
  212. // int[] arr = new int[5][5;
  213. Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments(",", ";"),
  214. // (6,33): error CS0443: Syntax error; value expected
  215. // int[] arr = new int[5][5;
  216. Diagnostic(ErrorCode.ERR_ValueExpected, ""),
  217. // (6,33): error CS1003: Syntax error, ']' expected
  218. // int[] arr = new int[5][5;
  219. Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";"));
  220. }
  221. [WorkItem(862031, "DevDiv/Personal")]
  222. [Fact]
  223. public void CS0201ERR_IllegalStatement()
  224. {
  225. var test = @"
  226. class A
  227. {
  228. public static int Main()
  229. {
  230. (a) => a;
  231. (a, b) =>
  232. {
  233. };
  234. int x = 0; int y = 0;
  235. x + y; x == 1;
  236. }
  237. }
  238. ";
  239. ParseAndValidate(test);
  240. }
  241. [Fact]
  242. public void CS0230ERR_BadForeachDecl()
  243. {
  244. var test = @"
  245. class MyClass
  246. {
  247. public static int Main()
  248. {
  249. int[] myarray = new int[3] {10,2,3};
  250. foreach (int in myarray) // CS0230
  251. {
  252. }
  253. return 1;
  254. }
  255. }
  256. ";
  257. ParseAndValidate(test,
  258. // (7,22): error CS1001: Identifier expected
  259. // foreach (int in myarray) // CS0230
  260. Diagnostic(ErrorCode.ERR_IdentifierExpected, "in"),
  261. // (7,22): error CS0230: Type and identifier are both required in a foreach statement
  262. // foreach (int in myarray) // CS0230
  263. Diagnostic(ErrorCode.ERR_BadForeachDecl, "in") );
  264. }
  265. [Fact]
  266. public void CS0230ERR_BadForeachDecl02()
  267. {
  268. // TODO: Extra error
  269. var test = @"
  270. public class Test
  271. {
  272. static void Main(string[] args)
  273. {
  274. int[] myarray = new int[3] { 1, 2, 3 };
  275. foreach (x in myarray) { }// Invalid
  276. }
  277. }
  278. ";
  279. ParseAndValidate(test,
  280. // (7,20): error CS1001: Identifier expected
  281. // foreach (x in myarray) { }// Invalid
  282. Diagnostic(ErrorCode.ERR_IdentifierExpected, "in"),
  283. // (7,20): error CS0230: Type and identifier are both required in a foreach statement
  284. // foreach (x in myarray) { }// Invalid
  285. Diagnostic(ErrorCode.ERR_BadForeachDecl, "in"));
  286. }
  287. [Fact]
  288. public void CS0230ERR_BadForeachDecl03()
  289. {
  290. // TODO: Extra error
  291. var test = @"
  292. public class Test
  293. {
  294. static void Main(string[] args)
  295. {
  296. st[][] myarray = new st[1000][];
  297. foreach (st[] in myarray) { }
  298. }
  299. }
  300. public struct st { }
  301. ";
  302. ParseAndValidate(test,
  303. // (7,23): error CS1001: Identifier expected
  304. // foreach (st[] in myarray) { }
  305. Diagnostic(ErrorCode.ERR_IdentifierExpected, "in"),
  306. // (7,23): error CS0230: Type and identifier are both required in a foreach statement
  307. // foreach (st[] in myarray) { }
  308. Diagnostic(ErrorCode.ERR_BadForeachDecl, "in"));
  309. }
  310. [Fact]
  311. public void CS0231ERR_ParamsLast()
  312. {
  313. var test = @"
  314. using System;
  315. public class MyClass {
  316. public void MyMeth(params int[] values, int i) {}
  317. public static int Main() {
  318. return 1;
  319. }
  320. }
  321. ";
  322. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ParamsLast, "params int[] values"));
  323. }
  324. [Fact]
  325. public void CS0257ERR_VarargsLast()
  326. {
  327. var test = @"
  328. class Foo
  329. {
  330. public void Bar(__arglist, int b)
  331. {
  332. }
  333. }
  334. ";
  335. ParseAndValidate(test,
  336. // (4,19): error CS0257: An __arglist parameter must be the last parameter in a formal parameter list
  337. // public void Bar(__arglist, int b)
  338. Diagnostic(ErrorCode.ERR_VarargsLast, "__arglist"));
  339. }
  340. [WorkItem(536668, "DevDiv")]
  341. [Fact]
  342. public void CS0267ERR_PartialMisplaced()
  343. {
  344. // Diff error
  345. var test = @"
  346. partial public class C // CS0267
  347. {
  348. }
  349. public class Test
  350. {
  351. public static int Main ()
  352. {
  353. return 1;
  354. }
  355. }
  356. ";
  357. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_PartialMisplaced, "partial") );
  358. }
  359. [Fact]
  360. public void CS0267ERR_PartialMisplaced_Enum()
  361. {
  362. var test = @"
  363. partial enum E { }
  364. ";
  365. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_PartialMisplaced, "partial"));
  366. }
  367. [Fact]
  368. public void CS0267ERR_PartialMisplaced_Delegate()
  369. {
  370. var test = @"
  371. partial delegate E { }
  372. ";
  373. // Extra errors
  374. ParseAndValidate(test,
  375. // (2,1): error CS0267: The 'partial' modifier can only appear immediately before 'class', 'struct', 'interface', or 'void'
  376. // partial delegate E { }
  377. Diagnostic(ErrorCode.ERR_PartialMisplaced, "partial"),
  378. // (2,20): error CS1001: Identifier expected
  379. // partial delegate E { }
  380. Diagnostic(ErrorCode.ERR_IdentifierExpected, "{"),
  381. // (2,20): error CS1003: Syntax error, '(' expected
  382. // partial delegate E { }
  383. Diagnostic(ErrorCode.ERR_SyntaxError, "{").WithArguments("(", "{"),
  384. // (2,20): error CS1026: ) expected
  385. // partial delegate E { }
  386. Diagnostic(ErrorCode.ERR_CloseParenExpected, "{"),
  387. // (2,20): error CS1002: ; expected
  388. // partial delegate E { }
  389. Diagnostic(ErrorCode.ERR_SemicolonExpected, "{"),
  390. // (2,20): error CS1022: Type or namespace definition, or end-of-file expected
  391. // partial delegate E { }
  392. Diagnostic(ErrorCode.ERR_EOFExpected, "{"),
  393. // (2,22): error CS1022: Type or namespace definition, or end-of-file expected
  394. // partial delegate E { }
  395. Diagnostic(ErrorCode.ERR_EOFExpected, "}"));
  396. }
  397. // TODO: Extra errors
  398. [Fact]
  399. public void CS0270ERR_ArraySizeInDeclaration()
  400. {
  401. var test = @"
  402. public class MyClass
  403. {
  404. enum E { }
  405. public static void Main()
  406. {
  407. int myarray[2];
  408. MyClass m[0];
  409. byte b[13,5];
  410. double d[14,5,6];
  411. E e[,50];
  412. }
  413. }
  414. ";
  415. ParseAndValidate(test,
  416. // (7,20): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  417. // int myarray[2];
  418. Diagnostic(ErrorCode.ERR_CStyleArray, "[2]"),
  419. // (7,21): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  420. // int myarray[2];
  421. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "2"),
  422. // (8,18): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  423. // MyClass m[0];
  424. Diagnostic(ErrorCode.ERR_CStyleArray, "[0]"),
  425. // (8,19): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  426. // MyClass m[0];
  427. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "0"),
  428. // (9,15): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  429. // byte b[13,5];
  430. Diagnostic(ErrorCode.ERR_CStyleArray, "[13,5]"),
  431. // (9,16): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  432. // byte b[13,5];
  433. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "13"),
  434. // (9,19): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  435. // byte b[13,5];
  436. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "5"),
  437. // (10,17): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  438. // double d[14,5,6];
  439. Diagnostic(ErrorCode.ERR_CStyleArray, "[14,5,6]"),
  440. // (10,18): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  441. // double d[14,5,6];
  442. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "14"),
  443. // (10,21): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  444. // double d[14,5,6];
  445. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "5"),
  446. // (10,23): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  447. // double d[14,5,6];
  448. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "6"),
  449. // (11,12): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  450. // E e[,50];
  451. Diagnostic(ErrorCode.ERR_CStyleArray, "[,50]"),
  452. // (11,13): error CS0443: Syntax error; value expected
  453. // E e[,50];
  454. Diagnostic(ErrorCode.ERR_ValueExpected, ""),
  455. // (11,14): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  456. // E e[,50];
  457. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "50"));
  458. }
  459. [Fact]
  460. public void CS0401ERR_NewBoundMustBeLast()
  461. {
  462. var test = @"
  463. interface IA
  464. {
  465. }
  466. class C<T> where T : new(), IA // CS0401 - should be T : IA, new()
  467. {
  468. }
  469. public class Test
  470. {
  471. public static int Main ()
  472. {
  473. return 1;
  474. }
  475. }
  476. ";
  477. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  478. // (5,22): error CS0401: The new() constraint must be the last constraint specified
  479. Diagnostic(ErrorCode.ERR_NewBoundMustBeLast, "new").WithLocation(5, 22));
  480. }
  481. [Fact]
  482. public void CS0439ERR_ExternAfterElements()
  483. {
  484. var test = @"
  485. using System;
  486. extern alias MyType; // CS0439
  487. // To resolve the error, make the extern alias the first line in the file.
  488. public class Test
  489. {
  490. public static void Main()
  491. {
  492. }
  493. }
  494. ";
  495. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ExternAfterElements, "extern"));
  496. }
  497. [WorkItem(862086, "DevDiv/Personal")]
  498. [Fact]
  499. public void CS0443ERR_ValueExpected()
  500. {
  501. var test = @"
  502. using System;
  503. class MyClass
  504. {
  505. public static void Main()
  506. {
  507. int[,] x = new int[1,5];
  508. if (x[] == 5) {} // CS0443
  509. // if (x[0, 0] == 5) {}
  510. }
  511. }
  512. ";
  513. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ValueExpected, "]"));
  514. }
  515. [Fact]
  516. public void CS0443ERR_ValueExpected_MultiDimensional()
  517. {
  518. var test = @"
  519. using System;
  520. class MyClass
  521. {
  522. public static void Main()
  523. {
  524. int[,] x = new int[1,5];
  525. if (x[,] == 5) {} // CS0443
  526. // if (x[0, 0] == 5) {}
  527. }
  528. }
  529. ";
  530. ParseAndValidate(test,
  531. // (8,15): error CS0443: Syntax error; value expected
  532. // if (x[,] == 5) {} // CS0443
  533. Diagnostic(ErrorCode.ERR_ValueExpected, ","),
  534. // (8,16): error CS0443: Syntax error; value expected
  535. // if (x[,] == 5) {} // CS0443
  536. Diagnostic(ErrorCode.ERR_ValueExpected, "]"));
  537. }
  538. [Fact]
  539. public void CS0449ERR_RefValBoundMustBeFirst()
  540. {
  541. var test = @"
  542. interface I {}
  543. class C4
  544. {
  545. public void F1<T>() where T : class, struct, I {} // CS0449
  546. public void F2<T>() where T : I, struct {} // CS0449
  547. public void F3<T>() where T : I, class {} // CS0449
  548. // OK
  549. public void F4<T>() where T : class {}
  550. public void F5<T>() where T : struct {}
  551. public void F6<T>() where T : I {}
  552. }
  553. ";
  554. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  555. // (5,41): error CS0449: The 'class' or 'struct' constraint must come before any other constraints
  556. Diagnostic(ErrorCode.ERR_RefValBoundMustBeFirst, "struct").WithLocation(5, 41),
  557. // (6,37): error CS0449: The 'class' or 'struct' constraint must come before any other constraints
  558. Diagnostic(ErrorCode.ERR_RefValBoundMustBeFirst, "struct").WithLocation(6, 37),
  559. // (7,37): error CS0449: The 'class' or 'struct' constraint must come before any other constraints
  560. Diagnostic(ErrorCode.ERR_RefValBoundMustBeFirst, "class").WithLocation(7, 37));
  561. }
  562. [Fact]
  563. public void CS0451ERR_NewBoundWithVal()
  564. {
  565. var test = @"
  566. public class C4
  567. {
  568. public void F4<T>() where T : struct, new() {} // CS0451
  569. }
  570. // OK
  571. public class C5
  572. {
  573. public void F5<T>() where T : struct {}
  574. }
  575. public class C6
  576. {
  577. public void F6<T>() where T : new() {}
  578. }
  579. ";
  580. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  581. // (4,42): error CS0451: The 'new()' constraint cannot be used with the 'struct' constraint
  582. Diagnostic(ErrorCode.ERR_NewBoundWithVal, "new").WithLocation(4, 42));
  583. }
  584. [WorkItem(862089, "DevDiv/Personal")]
  585. [Fact]
  586. public void CS0460ERR_OverrideWithConstraints()
  587. {
  588. var source =
  589. @"interface I
  590. {
  591. void M1<T>() where T : I;
  592. void M2<T, U>();
  593. }
  594. abstract class A
  595. {
  596. internal virtual void M1<T>() where T : class { }
  597. internal abstract void M2<T>() where T : struct;
  598. internal abstract void M3<T>();
  599. }
  600. abstract class B : A, I
  601. {
  602. void I.M1<T>() where T : I { }
  603. void I.M2<T,U>() where U : T { }
  604. internal override void M1<T>() where T : class { }
  605. internal override void M2<T>() where T : new() { }
  606. internal override abstract void M3<U>() where U : A;
  607. internal override abstract void M4<T>() where T : struct;
  608. }";
  609. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  610. // (14,20): error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
  611. Diagnostic(ErrorCode.ERR_OverrideWithConstraints, "where").WithLocation(14, 20),
  612. // (15,22): error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
  613. Diagnostic(ErrorCode.ERR_OverrideWithConstraints, "where").WithLocation(15, 22),
  614. // (16,37): error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
  615. Diagnostic(ErrorCode.ERR_OverrideWithConstraints, "where").WithLocation(16, 37),
  616. // (17,36): error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
  617. Diagnostic(ErrorCode.ERR_OverrideWithConstraints, "where").WithLocation(17, 36),
  618. // (18,45): error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
  619. Diagnostic(ErrorCode.ERR_OverrideWithConstraints, "where").WithLocation(18, 45),
  620. // (19,37): error CS0115: 'B.M4<T>()': no suitable method found to override
  621. Diagnostic(ErrorCode.ERR_OverrideNotExpected, "M4").WithArguments("B.M4<T>()").WithLocation(19, 37),
  622. // (19,45): error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
  623. Diagnostic(ErrorCode.ERR_OverrideWithConstraints, "where").WithLocation(19, 45));
  624. }
  625. [WorkItem(862094, "DevDiv/Personal")]
  626. [Fact]
  627. public void CS0514ERR_StaticConstructorWithExplicitConstructorCall()
  628. {
  629. var test = @"
  630. namespace x
  631. {
  632. public class clx
  633. {
  634. public clx(int i){}
  635. }
  636. public class cly : clx
  637. {
  638. // static does not have an object, therefore base cannot be called.
  639. // objects must be known at compiler time
  640. static cly() : base(0){} // sc0514
  641. }
  642. }
  643. ";
  644. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_StaticConstructorWithExplicitConstructorCall, "base").WithArguments("cly"));
  645. }
  646. [Fact]
  647. public void CS0514ERR_StaticConstructorWithExplicitConstructorCall2()
  648. {
  649. var test = @"
  650. class C
  651. {
  652. C() { }
  653. static C() : this() { } //CS0514
  654. }
  655. ";
  656. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_StaticConstructorWithExplicitConstructorCall, "this").WithArguments("C"));
  657. }
  658. [Fact]
  659. public void CS0574ERR_BadDestructorName()
  660. {
  661. var test = @"
  662. namespace x
  663. {
  664. public class iii
  665. {
  666. ~iiii(){}
  667. public static void Main()
  668. {
  669. }
  670. }
  671. }
  672. ";
  673. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadDestructorName, "iiii"));
  674. }
  675. // Extra same errors
  676. [Fact]
  677. public void CS0650ERR_CStyleArray()
  678. {
  679. var test = @"
  680. public class MyClass
  681. {
  682. public static void Main()
  683. {
  684. int myarray[2];
  685. MyClass m[0];
  686. byte b[13,5];
  687. double d[14,5,6];
  688. }
  689. }
  690. ";
  691. ParseAndValidate(test,
  692. // (6,20): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  693. // int myarray[2];
  694. Diagnostic(ErrorCode.ERR_CStyleArray, "[2]"),
  695. // (6,21): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  696. // int myarray[2];
  697. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "2"),
  698. // (7,18): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  699. // MyClass m[0];
  700. Diagnostic(ErrorCode.ERR_CStyleArray, "[0]"),
  701. // (7,19): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  702. // MyClass m[0];
  703. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "0"),
  704. // (8,15): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  705. // byte b[13,5];
  706. Diagnostic(ErrorCode.ERR_CStyleArray, "[13,5]"),
  707. // (8,16): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  708. // byte b[13,5];
  709. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "13"),
  710. // (8,19): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  711. // byte b[13,5];
  712. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "5"),
  713. // (9,17): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
  714. // double d[14,5,6];
  715. Diagnostic(ErrorCode.ERR_CStyleArray, "[14,5,6]"),
  716. // (9,18): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  717. // double d[14,5,6];
  718. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "14"),
  719. // (9,21): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  720. // double d[14,5,6];
  721. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "5"),
  722. // (9,23): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
  723. // double d[14,5,6];
  724. Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "6") );
  725. }
  726. [Fact, WorkItem(535883, "DevDiv")]
  727. public void CS0687ERR_AliasQualAsExpression()
  728. {
  729. var test = @"
  730. class Test
  731. {
  732. public static int Main()
  733. {
  734. int i = global::MyType(); // CS0687
  735. return 1;
  736. }
  737. }
  738. ";
  739. // Semantic error
  740. // (6,25): error CS0400: The type or namespace name 'MyType' could not be found in the global namespace (are you missing an assembly reference?)
  741. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  742. Diagnostic(ErrorCode.ERR_GlobalSingleTypeNameNotFound, "MyType").WithArguments("MyType", "<global namespace>")
  743. );
  744. }
  745. [WorkItem(542478, "DevDiv")]
  746. [Fact]
  747. public void CS0706ERR_BadConstraintType()
  748. {
  749. var source =
  750. @"interface IA<T, U, V>
  751. where U : T*
  752. where V : T[]
  753. {
  754. }
  755. interface IB<T>
  756. {
  757. void M<U, V>()
  758. where U : T*
  759. where V : T[];
  760. }";
  761. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  762. // (2,15): error CS0706: Invalid constraint type. A type used as a constraint must be an interface, a non-sealed class or a type parameter.
  763. Diagnostic(ErrorCode.ERR_BadConstraintType, "T*").WithLocation(2, 15),
  764. // (3,15): error CS0706: Invalid constraint type. A type used as a constraint must be an interface, a non-sealed class or a type parameter.
  765. Diagnostic(ErrorCode.ERR_BadConstraintType, "T[]").WithLocation(3, 15),
  766. // (9,19): error CS0706: Invalid constraint type. A type used as a constraint must be an interface, a non-sealed class or a type parameter.
  767. Diagnostic(ErrorCode.ERR_BadConstraintType, "T*").WithLocation(9, 19),
  768. // (10,19): error CS0706: Invalid constraint type. A type used as a constraint must be an interface, a non-sealed class or a type parameter.
  769. Diagnostic(ErrorCode.ERR_BadConstraintType, "T[]").WithLocation(10, 19),
  770. // CONSIDER: Dev10 doesn't report these cascading errors.
  771. // (2,15): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
  772. Diagnostic(ErrorCode.ERR_UnsafeNeeded, "T*"),
  773. // (2,15): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')
  774. Diagnostic(ErrorCode.ERR_ManagedAddr, "T*").WithArguments("T"),
  775. // (9,19): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
  776. Diagnostic(ErrorCode.ERR_UnsafeNeeded, "T*"),
  777. // (9,19): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')
  778. Diagnostic(ErrorCode.ERR_ManagedAddr, "T*").WithArguments("T"));
  779. }
  780. [Fact]
  781. public void CS0742ERR_ExpectedSelectOrGroup()
  782. {
  783. var test = @"
  784. using System;
  785. using System.Linq;
  786. public class C
  787. {
  788. public static int Main()
  789. {
  790. int[] array = { 1, 2, 3 };
  791. var c = from num in array;
  792. return 1;
  793. }
  794. }
  795. ";
  796. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ExpectedSelectOrGroup, ";"));
  797. }
  798. [Fact]
  799. public void CS0743ERR_ExpectedContextualKeywordOn()
  800. {
  801. var test = @"
  802. using System;
  803. using System.Linq;
  804. public class C
  805. {
  806. public static int Main()
  807. {
  808. int[] array1 = { 1, 2, 3 ,4, 5, 6,};
  809. int[] array2 = { 5, 6, 7, 8, 9 };
  810. var c = from x in array1
  811. join y in array2 x equals y
  812. select x;
  813. return 1;
  814. }
  815. }
  816. ";
  817. ParseAndValidate(test, TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental),
  818. // (11,36): error CS0743: Expected contextual keyword 'on'
  819. // join y in array2 x equals y
  820. Diagnostic(ErrorCode.ERR_ExpectedContextualKeywordOn, "equals").WithLocation(11, 36),
  821. // (11,36): error CS1525: Invalid expression term 'equals'
  822. // join y in array2 x equals y
  823. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "equals").WithArguments("equals").WithLocation(11, 36));
  824. }
  825. [Fact]
  826. public void CS0743ERR_ExpectedContextualKeywordOn_NoDeclExpr()
  827. {
  828. var test = @"
  829. using System;
  830. using System.Linq;
  831. public class C
  832. {
  833. public static int Main()
  834. {
  835. int[] array1 = { 1, 2, 3 ,4, 5, 6,};
  836. int[] array2 = { 5, 6, 7, 8, 9 };
  837. var c = from x in array1
  838. join y in array2 x equals y
  839. select x;
  840. return 1;
  841. }
  842. }
  843. ";
  844. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ExpectedContextualKeywordOn, "x"));
  845. }
  846. [Fact]
  847. public void CS0744ERR_ExpectedContextualKeywordEquals()
  848. {
  849. var test = @"
  850. using System;
  851. using System.Linq;
  852. public class C
  853. {
  854. public static int Main()
  855. {
  856. int[] array1 = { 1, 2, 3 ,4, 5, 6,};
  857. int[] array2 = { 5, 6, 7, 8, 9 };
  858. var c = from x in array1
  859. join y in array2 on x y
  860. select x;
  861. return 1;
  862. }
  863. }
  864. ";
  865. ParseAndValidate(test, TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental),
  866. // (11,40): error CS0744: Expected contextual keyword 'equals'
  867. // join y in array2 on x y
  868. Diagnostic(ErrorCode.ERR_ExpectedContextualKeywordEquals, "").WithLocation(11, 40),
  869. // (11,40): error CS1525: Invalid expression term 'select'
  870. // join y in array2 on x y
  871. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "").WithArguments("select").WithLocation(11, 40));
  872. }
  873. [Fact]
  874. public void CS0744ERR_ExpectedContextualKeywordEquals_NoDeclExpr()
  875. {
  876. var test = @"
  877. using System;
  878. using System.Linq;
  879. public class C
  880. {
  881. public static int Main()
  882. {
  883. int[] array1 = { 1, 2, 3 ,4, 5, 6,};
  884. int[] array2 = { 5, 6, 7, 8, 9 };
  885. var c = from x in array1
  886. join y in array2 on x y
  887. select x;
  888. return 1;
  889. }
  890. }
  891. ";
  892. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ExpectedContextualKeywordEquals, "y"));
  893. }
  894. [WorkItem(862121, "DevDiv/Personal")]
  895. [Fact]
  896. public void CS0745ERR_ExpectedContextualKeywordBy()
  897. {
  898. var test = @"
  899. using System;
  900. using System.Linq;
  901. public class C
  902. {
  903. public static int Main()
  904. {
  905. int[] array1 = { 1, 2, 3 ,4, 5, 6,};
  906. int[] array2 = { 5, 6, 7, 8, 9 };
  907. var c = from x in array1
  908. join y in array2 on x equals y
  909. group x y;
  910. return 1;
  911. }
  912. }
  913. ";
  914. ParseAndValidate(test, TestOptions.Regular.WithLanguageVersion(LanguageVersion.Experimental),
  915. // (12,26): error CS0745: Expected contextual keyword 'by'
  916. // group x y;
  917. Diagnostic(ErrorCode.ERR_ExpectedContextualKeywordBy, ";").WithLocation(12, 26),
  918. // (12,26): error CS1525: Invalid expression term ';'
  919. // group x y;
  920. Diagnostic(ErrorCode.ERR_InvalidExprTerm, ";").WithArguments(";").WithLocation(12, 26));
  921. }
  922. [WorkItem(862121, "DevDiv/Personal")]
  923. [Fact]
  924. public void CS0745ERR_ExpectedContextualKeywordBy_NoDeclExpr()
  925. {
  926. var test = @"
  927. using System;
  928. using System.Linq;
  929. public class C
  930. {
  931. public static int Main()
  932. {
  933. int[] array1 = { 1, 2, 3 ,4, 5, 6,};
  934. int[] array2 = { 5, 6, 7, 8, 9 };
  935. var c = from x in array1
  936. join y in array2 on x equals y
  937. group x y;
  938. return 1;
  939. }
  940. }
  941. ";
  942. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ExpectedContextualKeywordBy, "y"));
  943. }
  944. [Fact]
  945. public void CS0746ERR_InvalidAnonymousTypeMemberDeclarator()
  946. {
  947. var test = @"
  948. using System;
  949. public class C
  950. {
  951. public static int Main()
  952. {
  953. int i = 1;
  954. var t = new { a.b = 1 };
  955. return 1;
  956. }
  957. }
  958. ";
  959. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InvalidAnonymousTypeMemberDeclarator, "a.b = 1"));
  960. }
  961. [Fact]
  962. public void CS0746ERR_InvalidAnonymousTypeMemberDeclarator_2()
  963. {
  964. var test = @"
  965. using System;
  966. public class C
  967. {
  968. public static void Main()
  969. {
  970. string s = """";
  971. var t = new { s.Length = 1 };
  972. }
  973. }
  974. ";
  975. ParseAndValidate(test,
  976. // (8,23): error CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
  977. // var t = new { s.Length = 1 };
  978. Diagnostic(ErrorCode.ERR_InvalidAnonymousTypeMemberDeclarator, "s.Length = 1"));
  979. }
  980. [Fact]
  981. public void CS0746ERR_InvalidAnonymousTypeMemberDeclarator_3()
  982. {
  983. var test = @"
  984. using System;
  985. public class C
  986. {
  987. public static void Main()
  988. {
  989. string s = """";
  990. var t = new { s.ToString() = 1 };
  991. }
  992. }
  993. ";
  994. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InvalidAnonymousTypeMemberDeclarator, "s.ToString() = 1"));
  995. }
  996. [Fact]
  997. public void CS0748ERR_InconsistentLambdaParameterUsage()
  998. {
  999. var test = @"
  1000. class C
  1001. {
  1002. delegate T Func<T>();
  1003. delegate T Func<A0, T>(A0 a0);
  1004. delegate T Func<A0, A1, T>(A0 a0, A1 a1);
  1005. delegate T Func<A0, A1, A2, A3, T>(A0 a0, A1 a1, A2 a2, A3 a3);
  1006. static void X()
  1007. {
  1008. Func<int,int> f1 = (int x, y) => 1; // err: mixed parameters
  1009. Func<int,int> f2 = (x, int y) => 1; // err: mixed parameters
  1010. Func<int,int> f3 = (int x, int y, z) => 1; // err: mixed parameters
  1011. Func<int,int> f4 = (int x, y, int z) => 1; // err: mixed parameters
  1012. Func<int,int> f5 = (x, int y, int z) => 1; // err: mixed parameters
  1013. Func<int,int> f6 = (x, y, int z) => 1; // err: mixed parameters
  1014. }
  1015. }
  1016. ";
  1017. ParseAndValidate(test,
  1018. // (10,41): error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
  1019. // Func<int,int> f1 = (int x, y) => 1; // err: mixed parameters
  1020. Diagnostic(ErrorCode.ERR_InconsistentLambdaParameterUsage, "y"),
  1021. // (11,37): error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
  1022. // Func<int,int> f2 = (x, int y) => 1; // err: mixed parameters
  1023. Diagnostic(ErrorCode.ERR_InconsistentLambdaParameterUsage, "int"),
  1024. // (12,48): error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
  1025. // Func<int,int> f3 = (int x, int y, z) => 1; // err: mixed parameters
  1026. Diagnostic(ErrorCode.ERR_InconsistentLambdaParameterUsage, "z"),
  1027. // (13,41): error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
  1028. // Func<int,int> f4 = (int x, y, int z) => 1; // err: mixed parameters
  1029. Diagnostic(ErrorCode.ERR_InconsistentLambdaParameterUsage, "y"),
  1030. // (14,37): error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
  1031. // Func<int,int> f5 = (x, int y, int z) => 1; // err: mixed parameters
  1032. Diagnostic(ErrorCode.ERR_InconsistentLambdaParameterUsage, "int"),
  1033. // (14,44): error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
  1034. // Func<int,int> f5 = (x, int y, int z) => 1; // err: mixed parameters
  1035. Diagnostic(ErrorCode.ERR_InconsistentLambdaParameterUsage, "int"),
  1036. // (15,40): error CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
  1037. // Func<int,int> f6 = (x, y, int z) => 1; // err: mixed parameters
  1038. Diagnostic(ErrorCode.ERR_InconsistentLambdaParameterUsage, "int"));
  1039. }
  1040. [WorkItem(535915, "DevDiv")]
  1041. [Fact]
  1042. public void CS0839ERR_MissingArgument()
  1043. {
  1044. // Diff error
  1045. var test = @"
  1046. using System;
  1047. namespace TestNamespace
  1048. {
  1049. class Test
  1050. {
  1051. static int Add(int i, int j)
  1052. {
  1053. return i + j;
  1054. }
  1055. static int Main()
  1056. {
  1057. int i = Test.Add(
  1058. ,
  1059. 5);
  1060. return 1;
  1061. }
  1062. }
  1063. }
  1064. ";
  1065. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_MissingArgument, ""));
  1066. }
  1067. [WorkItem(863064, "DevDiv/Personal")]
  1068. [Fact]
  1069. public void CS1001ERR_IdentifierExpected()
  1070. {
  1071. var test = @"
  1072. public class clx
  1073. {
  1074. enum splitch
  1075. {
  1076. 'a'
  1077. }
  1078. }
  1079. ";
  1080. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IdentifierExpected, ""));
  1081. }
  1082. [Fact, WorkItem(542408, "DevDiv")]
  1083. public void CS1001ERR_IdentifierExpected_2()
  1084. {
  1085. var test = @"
  1086. enum
  1087. ";
  1088. ParseAndValidate(test,
  1089. Diagnostic(ErrorCode.ERR_IdentifierExpected, ""),
  1090. Diagnostic(ErrorCode.ERR_LbraceExpected, ""),
  1091. Diagnostic(ErrorCode.ERR_RbraceExpected, ""));
  1092. }
  1093. [Fact, WorkItem(542408, "DevDiv")]
  1094. public void CS1001ERR_IdentifierExpected_5()
  1095. {
  1096. var test = @"
  1097. using System;
  1098. struct
  1099. ";
  1100. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IdentifierExpected, ""),
  1101. Diagnostic(ErrorCode.ERR_LbraceExpected, ""),
  1102. Diagnostic(ErrorCode.ERR_RbraceExpected, ""));
  1103. }
  1104. [Fact, WorkItem(542416, "DevDiv")]
  1105. public void CS1001ERR_IdentifierExpected_3()
  1106. {
  1107. var test = @"
  1108. using System;
  1109. class NamedExample
  1110. {
  1111. static void Main(string[] args)
  1112. {
  1113. ExampleMethod(3, optionalint:4);
  1114. }
  1115. static void ExampleMethod(int required, string 1 = ""default string"",int optionalint = 10)
  1116. { }
  1117. }
  1118. ";
  1119. ParseAndValidate(test,
  1120. // (9,52): error CS1001: Identifier expected
  1121. // static void ExampleMethod(int required, string 1 = "default string",int optionalint = 10)
  1122. Diagnostic(ErrorCode.ERR_IdentifierExpected, "1"),
  1123. // (9,52): error CS1003: Syntax error, ',' expected
  1124. // static void ExampleMethod(int required, string 1 = "default string",int optionalint = 10)
  1125. Diagnostic(ErrorCode.ERR_SyntaxError, "1").WithArguments(",", ""));
  1126. }
  1127. [Fact, WorkItem(542416, "DevDiv")]
  1128. public void CS1001ERR_IdentifierExpected_4()
  1129. {
  1130. var test = @"
  1131. using System;
  1132. class NamedExample
  1133. {
  1134. static void Main(string[] args)
  1135. {
  1136. ExampleMethod(3, optionalint:4);
  1137. }
  1138. static void ExampleMethod(int required, ,int optionalint = 10)
  1139. { }
  1140. }
  1141. ";
  1142. // Extra errors
  1143. ParseAndValidate(test,
  1144. // (9,45): error CS1031: Type expected
  1145. // static void ExampleMethod(int required, ,int optionalint = 10)
  1146. Diagnostic(ErrorCode.ERR_TypeExpected, ","),
  1147. // (9,45): error CS1001: Identifier expected
  1148. // static void ExampleMethod(int required, ,int optionalint = 10)
  1149. Diagnostic(ErrorCode.ERR_IdentifierExpected, ","));
  1150. }
  1151. [Fact, WorkItem(542416, "DevDiv")]
  1152. public void CS1001ERR_IdentifierExpected_6()
  1153. {
  1154. var test = @"
  1155. class Program
  1156. {
  1157. const int max = 10;
  1158. static void M(int p2 = max is int?1,)
  1159. {
  1160. }
  1161. static void Main()
  1162. {
  1163. M(1);
  1164. }
  1165. }
  1166. ";
  1167. // Extra errors
  1168. ParseAndValidate(test,
  1169. // (5,40): error CS1003: Syntax error, ':' expected
  1170. // static void M(int p2 = max is int?1,)
  1171. Diagnostic(ErrorCode.ERR_SyntaxError, ",").WithArguments(":", ","),
  1172. // (5,40): error CS1525: Invalid expression term ','
  1173. // static void M(int p2 = max is int?1,)
  1174. Diagnostic(ErrorCode.ERR_InvalidExprTerm, ",").WithArguments(","),
  1175. // (5,41): error CS1031: Type expected
  1176. // static void M(int p2 = max is int?1,)
  1177. Diagnostic(ErrorCode.ERR_TypeExpected, ")"),
  1178. // (5,41): error CS1001: Identifier expected
  1179. // static void M(int p2 = max is int?1,)
  1180. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"));
  1181. }
  1182. [Fact]
  1183. public void CS1002ERR_SemicolonExpected()
  1184. {
  1185. var test = @"
  1186. namespace x {
  1187. abstract public class clx
  1188. {
  1189. int i // CS1002, missing semicolon
  1190. public static int Main()
  1191. {
  1192. return 0;
  1193. }
  1194. }
  1195. }
  1196. ";
  1197. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_SemicolonExpected, ""));
  1198. }
  1199. [WorkItem(528008, "DevDiv")]
  1200. [Fact]
  1201. public void CS1002ERR_SemicolonExpected_2()
  1202. {
  1203. var test = @"
  1204. class Program
  1205. {
  1206. static void Main(string[] args)
  1207. {
  1208. goto Lab2,Lab1;
  1209. Lab1:
  1210. System.Console.WriteLine(""1"");
  1211. Lab2:
  1212. System.Console.WriteLine(""2"");
  1213. }
  1214. }
  1215. ";
  1216. ParseAndValidate(test,
  1217. // (6,18): error CS1002: ; expected
  1218. // goto Lab2,Lab1;
  1219. Diagnostic(ErrorCode.ERR_SemicolonExpected, ","),
  1220. // (6,18): error CS1513: } expected
  1221. // goto Lab2,Lab1;
  1222. Diagnostic(ErrorCode.ERR_RbraceExpected, ","));
  1223. }
  1224. [WorkItem(527944, "DevDiv")]
  1225. [Fact]
  1226. public void CS1002ERR_SemicolonExpected_3()
  1227. {
  1228. var test = @"
  1229. class Program
  1230. {
  1231. static void Main(string[] args)
  1232. {
  1233. goto L1;
  1234. return;
  1235. L1: //invalid
  1236. }
  1237. }
  1238. ";
  1239. ParseAndValidate(test,
  1240. // (8,8): error CS1525: Invalid expression term '}'
  1241. // L1: //invalid
  1242. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "").WithArguments("}"),
  1243. // (8,8): error CS1002: ; expected
  1244. // L1: //invalid
  1245. Diagnostic(ErrorCode.ERR_SemicolonExpected, ""));
  1246. }
  1247. [Fact()]
  1248. public void CS1002ERR_SemicolonExpected_4()
  1249. {
  1250. var test = @"
  1251. class Program
  1252. {
  1253. static void Main(string[] args)
  1254. {
  1255. string target = ""t1"";
  1256. switch (target)
  1257. {
  1258. label1:
  1259. case ""t1"":
  1260. goto label1;
  1261. }
  1262. }
  1263. }
  1264. ";
  1265. // Extra errors
  1266. ParseAndValidate(test,
  1267. // (8,10): error CS1513: } expected
  1268. // {
  1269. Diagnostic(ErrorCode.ERR_RbraceExpected, ""),
  1270. // (9,16): error CS1525: Invalid expression term 'case'
  1271. // label1:
  1272. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "").WithArguments("case"),
  1273. // (9,16): error CS1002: ; expected
  1274. // label1:
  1275. Diagnostic(ErrorCode.ERR_SemicolonExpected, ""),
  1276. // (9,16): error CS1513: } expected
  1277. // label1:
  1278. Diagnostic(ErrorCode.ERR_RbraceExpected, ""),
  1279. // (10,18): error CS1002: ; expected
  1280. // case "t1":
  1281. Diagnostic(ErrorCode.ERR_SemicolonExpected, ":"),
  1282. // (10,18): error CS1513: } expected
  1283. // case "t1":
  1284. Diagnostic(ErrorCode.ERR_RbraceExpected, ":"),
  1285. // (14,1): error CS1022: Type or namespace definition, or end-of-file expected
  1286. // }
  1287. Diagnostic(ErrorCode.ERR_EOFExpected, "}"));
  1288. }
  1289. // TODO: diff error CS1525 vs. CS1513
  1290. [Fact]
  1291. public void CS1003ERR_SyntaxError()
  1292. {
  1293. var test = @"
  1294. namespace x
  1295. {
  1296. public class b
  1297. {
  1298. public static void Main() {
  1299. int[] a;
  1300. a[);
  1301. }
  1302. }
  1303. }
  1304. ";
  1305. ParseAndValidate(test,
  1306. // (8,15): error CS1003: Syntax error, ']' expected
  1307. // a[);
  1308. Diagnostic(ErrorCode.ERR_SyntaxError, ")").WithArguments("]", ")"),
  1309. // (8,15): error CS1002: ; expected
  1310. // a[);
  1311. Diagnostic(ErrorCode.ERR_SemicolonExpected, ")"),
  1312. // (8,15): error CS1513: } expected
  1313. // a[);
  1314. Diagnostic(ErrorCode.ERR_RbraceExpected, ")"));
  1315. }
  1316. [Fact]
  1317. public void CS1003ERR_SyntaxError_ForeachExpected1()
  1318. {
  1319. var test = @"
  1320. public class b
  1321. {
  1322. public void Main()
  1323. {
  1324. for (var v in
  1325. }
  1326. }
  1327. ";
  1328. //the first error should be
  1329. // (6,9): error CS1003: Syntax error, 'foreach' expected
  1330. // don't care about any others.
  1331. var parsedTree = ParseWithRoundTripCheck(test);
  1332. var firstDiag = parsedTree.GetDiagnostics().Take(1);
  1333. firstDiag.Verify(Diagnostic(ErrorCode.ERR_SyntaxError, "for").WithArguments("foreach", "for"));
  1334. }
  1335. [Fact]
  1336. public void CS1004ERR_DuplicateModifier()
  1337. {
  1338. var test = @"
  1339. namespace x {
  1340. abstract public class clx
  1341. {
  1342. int i;
  1343. public public static int Main() // CS1004, two public keywords
  1344. {
  1345. return 0;
  1346. }
  1347. }
  1348. }
  1349. ";
  1350. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_DuplicateModifier, "public").WithArguments("public"));
  1351. }
  1352. [Fact]
  1353. public void CS1007ERR_DuplicateAccessor()
  1354. {
  1355. var test = @"using System;
  1356. public class Container
  1357. {
  1358. public int Prop1{ protected get{return 1;} set {} protected get { return 1;} }
  1359. public static int Prop2{ get{return 1;} internal set {} internal set{} }
  1360. public int this[int i]{ protected get{return 1;} internal set {} protected get { return 1;} internal set {} }
  1361. }
  1362. ";
  1363. ParseAndValidate(test,
  1364. // (5,65): error CS1007: Property accessor already defined
  1365. // public int Prop1{ protected get{return 1;} set {} protected get { return 1;} }
  1366. Diagnostic(ErrorCode.ERR_DuplicateAccessor, "get"),
  1367. // (6,70): error CS1007: Property ac…

Large files files are truncated, but you can click here to view the full file