PageRenderTime 82ms CodeModel.GetById 29ms 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
  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 accessor already defined
  1368. // public static int Prop2{ get{return 1;} internal set {} internal set{} }
  1369. Diagnostic(ErrorCode.ERR_DuplicateAccessor, "set"),
  1370. // (7,80): error CS1007: Property accessor already defined
  1371. // public int this[int i]{ protected get{return 1;} internal set {} protected get { return 1;} internal set {} }
  1372. Diagnostic(ErrorCode.ERR_DuplicateAccessor, "get"),
  1373. // (7,106): error CS1007: Property accessor already defined
  1374. // public int this[int i]{ protected get{return 1;} internal set {} protected get { return 1;} internal set {} }
  1375. Diagnostic(ErrorCode.ERR_DuplicateAccessor, "set"));
  1376. }
  1377. [Fact]
  1378. public void CS1008ERR_IntegralTypeExpected01()
  1379. {
  1380. CreateCompilationWithMscorlib(
  1381. @"namespace x
  1382. {
  1383. abstract public class clx
  1384. {
  1385. enum E : sbyte { x, y, z } // no error
  1386. enum F : char { x, y, z } // CS1008, char not valid type for enums
  1387. enum G : short { A, B, C } // no error
  1388. enum H : System.Int16 { A, B, C } // CS1008, short not System.Int16
  1389. }
  1390. }
  1391. ")
  1392. .VerifyDiagnostics(
  1393. // (6,18): error CS1008: Type byte, sbyte, short, ushort, int, uint, long, or ulong expected
  1394. // enum F : char { x, y, z } // CS1008, char not valid type for enums
  1395. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "char").WithLocation(6, 18)
  1396. );
  1397. }
  1398. [Fact]
  1399. public void CS1008ERR_IntegralTypeExpected02()
  1400. {
  1401. CreateCompilationWithMscorlib(
  1402. @"interface I { }
  1403. class C { }
  1404. enum E { }
  1405. enum F : I { A }
  1406. enum G : C { A }
  1407. enum H : E { A }
  1408. enum K : System.Enum { A }
  1409. enum L : string { A }
  1410. enum M : float { A }
  1411. enum N : decimal { A }
  1412. ")
  1413. .VerifyDiagnostics(
  1414. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "I").WithLocation(4, 10),
  1415. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "C").WithLocation(5, 10),
  1416. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "E").WithLocation(6, 10),
  1417. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "System.Enum").WithLocation(7, 10),
  1418. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "string").WithLocation(8, 10),
  1419. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "float").WithLocation(9, 10),
  1420. Diagnostic(ErrorCode.ERR_IntegralTypeExpected, "decimal").WithLocation(10, 10));
  1421. }
  1422. [Fact, WorkItem(667303)]
  1423. public void CS1008ERR_IntegralTypeExpected03()
  1424. {
  1425. ParseAndValidate(@"enum E : byt { A, B }"); // no *parser* errors. This is a semantic error now.
  1426. }
  1427. [Fact, WorkItem(540117, "DevDiv")]
  1428. public void CS1009ERR_IllegalEscape_Strings()
  1429. {
  1430. var text = @"
  1431. class Program
  1432. {
  1433. static void Main()
  1434. {
  1435. string s;
  1436. s = ""\u"";
  1437. s = ""\u0"";
  1438. s = ""\u00"";
  1439. s = ""\u000"";
  1440. s = ""a\uz"";
  1441. s = ""a\u0z"";
  1442. s = ""a\u00z"";
  1443. s = ""a\u000z"";
  1444. }
  1445. }
  1446. ";
  1447. ParseAndValidate(text,
  1448. // (7,14): error CS1009: Unrecognized escape sequence
  1449. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u"),
  1450. // (8,14): error CS1009: Unrecognized escape sequence
  1451. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u0"),
  1452. // (9,14): error CS1009: Unrecognized escape sequence
  1453. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u00"),
  1454. // (10,14): error CS1009: Unrecognized escape sequence
  1455. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u000"),
  1456. // (12,15): error CS1009: Unrecognized escape sequence
  1457. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u"),
  1458. // (13,15): error CS1009: Unrecognized escape sequence
  1459. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u0"),
  1460. // (14,15): error CS1009: Unrecognized escape sequence
  1461. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u00"),
  1462. // (15,15): error CS1009: Unrecognized escape sequence
  1463. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u000")
  1464. );
  1465. }
  1466. [Fact, WorkItem(528100, "DevDiv")]
  1467. public void CS1009ERR_IllegalEscape_Identifiers()
  1468. {
  1469. var text = @"using System;
  1470. class Program
  1471. {
  1472. static void Main()
  1473. {
  1474. int \u;
  1475. int \u0;
  1476. int \u00;
  1477. int \u000;
  1478. int a\uz;
  1479. int a\u0z;
  1480. int a\u00z;
  1481. int a\u000z;
  1482. }
  1483. }
  1484. ";
  1485. ParseAndValidate(text,
  1486. // (6,13): error CS1009: Unrecognized escape sequence
  1487. // int \u;
  1488. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u"),
  1489. // (7,13): error CS1009: Unrecognized escape sequence
  1490. // int \u0;
  1491. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u0"),
  1492. // (7,13): error CS1056: Unexpected character '\u0'
  1493. // int \u0;
  1494. Diagnostic(ErrorCode.ERR_UnexpectedCharacter, "").WithArguments(@"\u0"),
  1495. // (8,13): error CS1009: Unrecognized escape sequence
  1496. // int \u00;
  1497. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u00"),
  1498. // (8,13): error CS1056: Unexpected character '\u00'
  1499. // int \u00;
  1500. Diagnostic(ErrorCode.ERR_UnexpectedCharacter, "").WithArguments(@"\u00"),
  1501. // (9,13): error CS1009: Unrecognized escape sequence
  1502. // int \u000;
  1503. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u000"),
  1504. // (9,13): error CS1056: Unexpected character '\u000'
  1505. // int \u000;
  1506. Diagnostic(ErrorCode.ERR_UnexpectedCharacter, "").WithArguments(@"\u000"),
  1507. // (11,14): error CS1009: Unrecognized escape sequence
  1508. // int a\uz;
  1509. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u"),
  1510. // (12,14): error CS1009: Unrecognized escape sequence
  1511. // int a\u0z;
  1512. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u0"),
  1513. // (12,14): error CS1056: Unexpected character '\u0'
  1514. // int a\u0z;
  1515. Diagnostic(ErrorCode.ERR_UnexpectedCharacter, "").WithArguments(@"\u0"),
  1516. // (13,14): error CS1009: Unrecognized escape sequence
  1517. // int a\u00z;
  1518. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u00"),
  1519. // (13,14): error CS1056: Unexpected character '\u00'
  1520. // int a\u00z;
  1521. Diagnostic(ErrorCode.ERR_UnexpectedCharacter, "").WithArguments(@"\u00"),
  1522. // (14,14): error CS1009: Unrecognized escape sequence
  1523. // int a\u000z;
  1524. Diagnostic(ErrorCode.ERR_IllegalEscape, @"\u000"),
  1525. // (14,14): error CS1056: Unexpected character '\u000'
  1526. // int a\u000z;
  1527. Diagnostic(ErrorCode.ERR_UnexpectedCharacter, "").WithArguments(@"\u000"),
  1528. // NOTE: Dev11 doesn't report these cascading diagnostics.
  1529. // (7,13): error CS1001: Identifier expected
  1530. // int \u0;
  1531. Diagnostic(ErrorCode.ERR_IdentifierExpected, @"\u0"),
  1532. // (8,13): error CS1001: Identifier expected
  1533. // int \u00;
  1534. Diagnostic(ErrorCode.ERR_IdentifierExpected, @"\u00"),
  1535. // (9,13): error CS1001: Identifier expected
  1536. // int \u000;
  1537. Diagnostic(ErrorCode.ERR_IdentifierExpected, @"\u000"),
  1538. // (12,17): error CS1002: ; expected
  1539. // int a\u0z;
  1540. Diagnostic(ErrorCode.ERR_SemicolonExpected, "z"),
  1541. // (13,18): error CS1002: ; expected
  1542. // int a\u00z;
  1543. Diagnostic(ErrorCode.ERR_SemicolonExpected, "z"),
  1544. // (14,19): error CS1002: ; expected
  1545. // int a\u000z;
  1546. Diagnostic(ErrorCode.ERR_SemicolonExpected, "z")
  1547. );
  1548. }
  1549. [WorkItem(535921, "DevDiv")]
  1550. [Fact]
  1551. public void CS1013ERR_InvalidNumber()
  1552. {
  1553. // Diff error
  1554. var test = @"
  1555. namespace x
  1556. {
  1557. public class a
  1558. {
  1559. public static int Main()
  1560. {
  1561. return 1;
  1562. }
  1563. }
  1564. public class b
  1565. {
  1566. public int d = 0x;
  1567. }
  1568. }
  1569. ";
  1570. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InvalidNumber, ""));
  1571. }
  1572. [WorkItem(862116, "DevDiv/Personal")]
  1573. [Fact]
  1574. public void CS1014ERR_GetOrSetExpected()
  1575. {
  1576. var test = @"using System;
  1577. public sealed class Container
  1578. {
  1579. public string Prop1 { protected }
  1580. public string Prop2 { get { return null; } protected }
  1581. public string Prop3 { get { return null; } protected set { } protected }
  1582. }
  1583. ";
  1584. ParseAndValidate(test,
  1585. Diagnostic(ErrorCode.ERR_GetOrSetExpected, "}"),
  1586. Diagnostic(ErrorCode.ERR_GetOrSetExpected, "}"),
  1587. Diagnostic(ErrorCode.ERR_GetOrSetExpected, "}"));
  1588. }
  1589. [Fact]
  1590. public void CS1015ERR_ClassTypeExpected()
  1591. {
  1592. var test = @"
  1593. using System;
  1594. public class Test
  1595. {
  1596. public static void Main()
  1597. {
  1598. try
  1599. {
  1600. }
  1601. catch(int)
  1602. {
  1603. }
  1604. catch(byte)
  1605. {
  1606. }
  1607. }
  1608. }
  1609. ";
  1610. ParseAndValidate(test,
  1611. Diagnostic(ErrorCode.ERR_ClassTypeExpected, "int"),
  1612. Diagnostic(ErrorCode.ERR_ClassTypeExpected, "byte"));
  1613. }
  1614. [WorkItem(863382, "DevDiv/Personal")]
  1615. [Fact]
  1616. public void CS1016ERR_NamedArgumentExpected()
  1617. {
  1618. var test = @"
  1619. namespace x
  1620. {
  1621. [foo(a=5, b)]
  1622. class foo
  1623. {
  1624. }
  1625. public class a
  1626. {
  1627. public static int Main()
  1628. {
  1629. return 1;
  1630. }
  1631. }
  1632. }
  1633. ";
  1634. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_NamedArgumentExpected, "b"));
  1635. }
  1636. [Fact]
  1637. public void CS1017ERR_TooManyCatches()
  1638. {
  1639. var test = @"
  1640. using System;
  1641. namespace nms {
  1642. public class S : Exception {
  1643. };
  1644. public class S1 : Exception {
  1645. };
  1646. public class mine {
  1647. private static int retval = 2;
  1648. public static int Main()
  1649. {
  1650. try {
  1651. throw new S();
  1652. }
  1653. catch {}
  1654. catch (S1) {}
  1655. catch (S) {}
  1656. catch if (false) {}
  1657. if (retval == 0) Console.WriteLine(""PASS"");
  1658. else Console.WriteLine(""FAIL"");
  1659. return retval;
  1660. }
  1661. };
  1662. }
  1663. ";
  1664. ParseAndValidate(test,
  1665. Diagnostic(ErrorCode.ERR_TooManyCatches, "catch"),
  1666. Diagnostic(ErrorCode.ERR_TooManyCatches, "catch"),
  1667. Diagnostic(ErrorCode.ERR_TooManyCatches, "catch"));
  1668. }
  1669. [Fact]
  1670. public void CS1017ERR_TooManyCatches_NoError()
  1671. {
  1672. var test = @"
  1673. using System;
  1674. namespace nms {
  1675. public class S : Exception {
  1676. };
  1677. public class S1 : Exception {
  1678. };
  1679. public class mine {
  1680. private static int retval = 2;
  1681. public static int Main()
  1682. {
  1683. try {
  1684. throw new S();
  1685. }
  1686. catch if (true) {}
  1687. catch (S1) {}
  1688. catch (S) {}
  1689. if (retval == 0) Console.WriteLine(""PASS"");
  1690. else Console.WriteLine(""FAIL"");
  1691. return retval;
  1692. }
  1693. };
  1694. }
  1695. ";
  1696. ParseAndValidate(test);
  1697. }
  1698. [Fact]
  1699. public void CS1018ERR_ThisOrBaseExpected()
  1700. {
  1701. var test = @"
  1702. namespace x
  1703. {
  1704. public class C
  1705. {
  1706. }
  1707. public class a : C
  1708. {
  1709. public a () : {}
  1710. public static int Main()
  1711. {
  1712. return 1;
  1713. }
  1714. }
  1715. }
  1716. ";
  1717. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ThisOrBaseExpected, "{"));
  1718. }
  1719. [WorkItem(535924, "DevDiv")]
  1720. [Fact]
  1721. public void CS1019ERR_OvlUnaryOperatorExpected()
  1722. {
  1723. // Diff errors
  1724. var test = @"
  1725. namespace x
  1726. {
  1727. public class ii
  1728. {
  1729. int i
  1730. {
  1731. get
  1732. {
  1733. return 0;
  1734. }
  1735. }
  1736. }
  1737. public class a
  1738. {
  1739. public static ii operator ii(a aa) // replace ii with explicit or implicit
  1740. {
  1741. return new ii();
  1742. }
  1743. public static void Main()
  1744. {
  1745. }
  1746. }
  1747. }
  1748. ";
  1749. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_OvlUnaryOperatorExpected, "ii"));
  1750. }
  1751. [WorkItem(906502, "DevDiv/Personal")]
  1752. [Fact]
  1753. public void CS1020ERR_OvlBinaryOperatorExpected()
  1754. {
  1755. // Diff error
  1756. var test = @"
  1757. namespace x
  1758. {
  1759. public class iii
  1760. {
  1761. public static implicit operator int(iii x)
  1762. {
  1763. return 0;
  1764. }
  1765. public static implicit operator iii(int x)
  1766. {
  1767. return null;
  1768. }
  1769. public static int operator ++(iii aa, int bb) // change ++ to +
  1770. {
  1771. return 0;
  1772. }
  1773. public static void Main()
  1774. {
  1775. }
  1776. }
  1777. }
  1778. ";
  1779. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_OvlBinaryOperatorExpected, "++") );
  1780. }
  1781. [Fact]
  1782. public void CS1022ERR_EOFExpected()
  1783. {
  1784. var test = @"
  1785. }}}}}
  1786. ";
  1787. ParseAndValidate(test,
  1788. Diagnostic(ErrorCode.ERR_EOFExpected, "}"),
  1789. Diagnostic(ErrorCode.ERR_EOFExpected, "}"),
  1790. Diagnostic(ErrorCode.ERR_EOFExpected, "}"),
  1791. Diagnostic(ErrorCode.ERR_EOFExpected, "}"),
  1792. Diagnostic(ErrorCode.ERR_EOFExpected, "}"));
  1793. }
  1794. [Fact]
  1795. public void CS1022ERR_EOFExpected02()
  1796. {
  1797. var test = @" > Roslyn.Utilities.dll! Basic";
  1798. CreateCompilationWithMscorlib(test).VerifyDiagnostics(
  1799. // (1,2): error CS1022: Type or namespace definition, or end-of-file expected
  1800. Diagnostic(ErrorCode.ERR_EOFExpected, ">").WithLocation(1, 2),
  1801. // (1,21): error CS0116: A namespace does not directly contain members such as fields or methods
  1802. Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "dll").WithLocation(1, 21),
  1803. // (1,24): error CS1022: Type or namespace definition, or end-of-file expected
  1804. Diagnostic(ErrorCode.ERR_EOFExpected, "!").WithLocation(1, 24),
  1805. // (1,27): error CS0116: A namespace does not directly contain members such as fields or methods
  1806. Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "Basic").WithLocation(1, 27));
  1807. }
  1808. [Fact]
  1809. public void CS1023ERR_BadEmbeddedStmt()
  1810. {
  1811. var test = @"
  1812. struct S {
  1813. }
  1814. public class a {
  1815. public static int Main() {
  1816. for (int i=0; i < 3; i++) MyLabel: {}
  1817. return 1;
  1818. }
  1819. }
  1820. ";
  1821. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "MyLabel: {}"));
  1822. }
  1823. // Preprocessor:
  1824. [Fact]
  1825. public void CS1024ERR_PPDirectiveExpectedpp()
  1826. {
  1827. var test = @"#import System;";
  1828. ParseAndValidate(test, // (1,2): error CS1024: Preprocessor directive expected
  1829. // #import System;
  1830. Diagnostic(ErrorCode.ERR_PPDirectiveExpected, "import"));
  1831. }
  1832. // Preprocessor:
  1833. [Fact]
  1834. public void CS1025ERR_EndOfPPLineExpectedpp()
  1835. {
  1836. var test = @"
  1837. public class Test
  1838. {
  1839. # line hidden 123
  1840. public static void MyHiddenMethod()
  1841. {
  1842. }
  1843. #undef x y
  1844. public static void Main()
  1845. {
  1846. }
  1847. }
  1848. ";
  1849. // Extra Errors
  1850. ParseAndValidate(test,
  1851. // (4,15): error CS1025: Single-line comment or end-of-line expected
  1852. // # line hidden 123
  1853. Diagnostic(ErrorCode.ERR_EndOfPPLineExpected, "123"),
  1854. // (9,6): error CS1032: Cannot define/undefine preprocessor symbols after first token in file
  1855. // #undef x y
  1856. Diagnostic(ErrorCode.ERR_PPDefFollowsToken, "undef"),
  1857. // (9,14): error CS1025: Single-line comment or end-of-line expected
  1858. // #undef x y
  1859. Diagnostic(ErrorCode.ERR_EndOfPPLineExpected, "y"));
  1860. }
  1861. [WorkItem(863388, "DevDiv/Personal")]
  1862. [Fact]
  1863. public void CS1026ERR_CloseParenExpected()
  1864. {
  1865. var test = @"
  1866. #if (fred == barney
  1867. #endif
  1868. namespace x
  1869. {
  1870. public class a
  1871. {
  1872. public static int Main()
  1873. {
  1874. return 1;
  1875. }
  1876. }
  1877. }
  1878. ";
  1879. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_CloseParenExpected, ""));
  1880. }
  1881. // Preprocessor:
  1882. [Fact]
  1883. public void CS1027ERR_EndifDirectiveExpectedpp()
  1884. {
  1885. var test = @"
  1886. public class Test
  1887. {
  1888. # if true
  1889. }
  1890. ";
  1891. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_EndifDirectiveExpected, ""));
  1892. }
  1893. // Preprocessor:
  1894. [Fact]
  1895. public void CS1028ERR_UnexpectedDirectivepp()
  1896. {
  1897. var test = @"
  1898. class Test
  1899. {
  1900. #endregion
  1901. public static int Main()
  1902. {
  1903. return 0;
  1904. }
  1905. # endif
  1906. }
  1907. ";
  1908. ParseAndValidate(test,
  1909. // (4,3): error CS1028: Unexpected preprocessor directive
  1910. // #endregion
  1911. Diagnostic(ErrorCode.ERR_UnexpectedDirective, "#endregion"),
  1912. // (9,1): error CS1028: Unexpected preprocessor directive
  1913. // # endif
  1914. Diagnostic(ErrorCode.ERR_UnexpectedDirective, "# endif"));
  1915. }
  1916. // Preprocessor:
  1917. [Fact]
  1918. public void CS1029ERR_ErrorDirectivepp()
  1919. {
  1920. var test = @"
  1921. public class Test
  1922. {
  1923. # error (12345)
  1924. }
  1925. ";
  1926. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ErrorDirective, "(12345)").WithArguments("(12345)"));
  1927. }
  1928. [WorkItem(541954, "DevDiv")]
  1929. [Fact]
  1930. public void CS1029ERR_ErrorDirectiveppNonLatin()
  1931. {
  1932. var test = "public class Test\r\n{\r\n# error \u0444\u0430\u0439\u043B\r\n}";
  1933. var parsedTree = ParseWithRoundTripCheck(test);
  1934. var error = parsedTree.GetDiagnostics().Single();
  1935. Assert.Equal((int)ErrorCode.ERR_ErrorDirective, error.Code);
  1936. Assert.Equal("error CS1029: #error: '\u0444\u0430\u0439\u043B'", CSharpDiagnosticFormatter.Instance.Format(error.WithLocation(Location.None)));
  1937. }
  1938. [Fact(), WorkItem(526991, "DevDiv")]
  1939. public void CS1031ERR_TypeExpected01()
  1940. {
  1941. // Diff error - CS1003
  1942. var test = @"
  1943. namespace x
  1944. {
  1945. interface ii
  1946. {
  1947. int i
  1948. {
  1949. get;
  1950. }
  1951. }
  1952. public class a
  1953. {
  1954. public operator ii(a aa)
  1955. {
  1956. return new ii();
  1957. }
  1958. }
  1959. }
  1960. ";
  1961. ParseAndValidate(test,
  1962. // (13,16): error CS1003: Syntax error, 'explicit' expected
  1963. // public operator ii(a aa)
  1964. Diagnostic(ErrorCode.ERR_SyntaxError, "operator").WithArguments("explicit", "operator")
  1965. );
  1966. }
  1967. [Fact]
  1968. public void CS1031ERR_TypeExpected02()
  1969. {
  1970. var text = @"namespace x
  1971. {
  1972. public class a
  1973. {
  1974. public static void Main()
  1975. {
  1976. e = new base; // CS1031, not a type
  1977. e = new this; // CS1031, not a type
  1978. e = new (); // CS1031, not a type
  1979. }
  1980. }
  1981. }
  1982. ";
  1983. // TODO: this appears to be a severe regression from Dev10, which neatly reported 3 errors.
  1984. ParseAndValidate(text,
  1985. // (7,21): error CS1031: Type expected
  1986. // e = new base; // CS1031, not a type
  1987. Diagnostic(ErrorCode.ERR_TypeExpected, "base"),
  1988. // (7,21): error CS1526: A new expression requires (), [], or {} after type
  1989. // e = new base; // CS1031, not a type
  1990. Diagnostic(ErrorCode.ERR_BadNewExpr, "base"),
  1991. // (7,21): error CS1002: ; expected
  1992. // e = new base; // CS1031, not a type
  1993. Diagnostic(ErrorCode.ERR_SemicolonExpected, "base"),
  1994. // (8,21): error CS1031: Type expected
  1995. // e = new this; // CS1031, not a type
  1996. Diagnostic(ErrorCode.ERR_TypeExpected, "this"),
  1997. // (8,21): error CS1526: A new expression requires (), [], or {} after type
  1998. // e = new this; // CS1031, not a type
  1999. Diagnostic(ErrorCode.ERR_BadNewExpr, "this"),
  2000. // (8,21): error CS1002: ; expected
  2001. // e = new this; // CS1031, not a type
  2002. Diagnostic(ErrorCode.ERR_SemicolonExpected, "this"),
  2003. // (9,21): error CS1031: Type expected
  2004. // e = new (); // CS1031, not a type
  2005. Diagnostic(ErrorCode.ERR_TypeExpected, "(")
  2006. );
  2007. }
  2008. [WorkItem(541347, "DevDiv")]
  2009. [Fact]
  2010. public void CS1031ERR_TypeExpected03()
  2011. {
  2012. var test = @"
  2013. using System;
  2014. public class Extensions
  2015. {
  2016. //Extension method must be static
  2017. public Extensions(this int i) {}
  2018. public static void Main(){}
  2019. }
  2020. ";
  2021. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "this").WithArguments("", "this"));
  2022. }
  2023. [Fact]
  2024. public void CS1031ERR_TypeExpected04_RoslynCS1001()
  2025. {
  2026. var test = @"public struct S<>
  2027. {
  2028. public void M<>() {}
  2029. }
  2030. ";
  2031. ParseAndValidate(test,
  2032. // (1,17): error CS1001: Identifier expected
  2033. // public struct S<>
  2034. Diagnostic(ErrorCode.ERR_IdentifierExpected, ">"),
  2035. // (3,19): error CS1001: Identifier expected
  2036. // public void M<>() {}
  2037. Diagnostic(ErrorCode.ERR_IdentifierExpected, ">"));
  2038. }
  2039. [Fact]
  2040. public void CS1037ERR_OvlOperatorExpected()
  2041. {
  2042. var test = @"
  2043. class A
  2044. {
  2045. public static int explicit operator ()
  2046. {
  2047. return 0;
  2048. }
  2049. public static A operator ()
  2050. {
  2051. return null;
  2052. }
  2053. }";
  2054. ParseAndValidate(test,
  2055. // (4,19): error CS1553: Declaration is not valid; use '+ operator <dest-type> (...' instead
  2056. // public static int explicit operator ()
  2057. Diagnostic(ErrorCode.ERR_BadOperatorSyntax, "int").WithArguments("+"),
  2058. // (4,23): error CS1003: Syntax error, 'operator' expected
  2059. // public static int explicit operator ()
  2060. Diagnostic(ErrorCode.ERR_SyntaxError, "explicit").WithArguments("operator", "explicit"),
  2061. // (4,23): error CS1037: Overloadable operator expected
  2062. // public static int explicit operator ()
  2063. Diagnostic(ErrorCode.ERR_OvlOperatorExpected, "explicit"),
  2064. // (4,32): error CS1003: Syntax error, '(' expected
  2065. // public static int explicit operator ()
  2066. Diagnostic(ErrorCode.ERR_SyntaxError, "operator").WithArguments("(", "operator"),
  2067. // (4,32): error CS1041: Identifier expected; 'operator' is a keyword
  2068. // public static int explicit operator ()
  2069. Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "operator").WithArguments("", "operator"),
  2070. // (8,30): error CS1037: Overloadable operator expected
  2071. // public static A operator ()
  2072. Diagnostic(ErrorCode.ERR_OvlOperatorExpected, "("),
  2073. // (8,31): error CS1003: Syntax error, '(' expected
  2074. // public static A operator ()
  2075. Diagnostic(ErrorCode.ERR_SyntaxError, ")").WithArguments("(", ")"));
  2076. }
  2077. // Preprocessor:
  2078. [Fact]
  2079. public void CS1038ERR_EndRegionDirectiveExpectedpp()
  2080. {
  2081. var test = @"
  2082. class Test
  2083. {
  2084. # region
  2085. }
  2086. ";
  2087. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_EndRegionDirectiveExpected, ""));
  2088. }
  2089. [Fact, WorkItem(535926, "DevDiv")]
  2090. public void CS1041ERR_IdentifierExpectedKW()
  2091. {
  2092. // Diff errors
  2093. var test = @"
  2094. class MyClass {
  2095. public void f(int long) { // CS1041
  2096. }
  2097. public static int Main() {
  2098. return 1;
  2099. }
  2100. }
  2101. ";
  2102. ParseAndValidate(test,
  2103. // (3,23): error CS1001: Identifier expected
  2104. // public void f(int long) { // CS1041
  2105. Diagnostic(ErrorCode.ERR_IdentifierExpected, "long"),
  2106. // (3,23): error CS1003: Syntax error, ',' expected
  2107. // public void f(int long) { // CS1041
  2108. Diagnostic(ErrorCode.ERR_SyntaxError, "long").WithArguments(",", "long"),
  2109. // (3,27): error CS1001: Identifier expected
  2110. // public void f(int long) { // CS1041
  2111. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"));
  2112. }
  2113. [WorkItem(919476, "DevDiv/Personal")]
  2114. [Fact]
  2115. public void CS1041RegressKeywordInEnumField()
  2116. {
  2117. var test = @"enum ColorA
  2118. {
  2119. const Red,
  2120. Green = 10,
  2121. readonly Blue,
  2122. }";
  2123. ParseAndValidate(test,
  2124. Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "").WithArguments("", "const"),
  2125. Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "").WithArguments("", "readonly"));
  2126. }
  2127. [Fact, WorkItem(541347, "DevDiv")]
  2128. public void CS1041ERR_IdentifierExpectedKW02()
  2129. {
  2130. var test =
  2131. @"class C
  2132. {
  2133. C(this object o) { }
  2134. }";
  2135. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "this").WithArguments("", "this"));
  2136. }
  2137. [Fact, WorkItem(541347, "DevDiv")]
  2138. public void CS1041ERR_IdentifierExpectedKW03()
  2139. {
  2140. var test =
  2141. @"class C
  2142. {
  2143. object this[this object o]
  2144. {
  2145. get { return null; }
  2146. }
  2147. }";
  2148. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "this").WithArguments("", "this") );
  2149. }
  2150. [Fact, WorkItem(541347, "DevDiv")]
  2151. public void CS1041ERR_IdentifierExpectedKW04()
  2152. {
  2153. var test = @"delegate void D(this object o);";
  2154. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "this").WithArguments("", "this"));
  2155. }
  2156. [Fact, WorkItem(541347, "DevDiv")]
  2157. public void CS1041ERR_IdentifierExpectedKW05()
  2158. {
  2159. var test =
  2160. @"delegate void D(object o);
  2161. class C
  2162. {
  2163. static void M()
  2164. {
  2165. D d = delegate (this object o) { };
  2166. }
  2167. }";
  2168. ParseAndValidate(test,
  2169. Diagnostic(ErrorCode.ERR_CloseParenExpected, "this"),
  2170. Diagnostic(ErrorCode.ERR_LbraceExpected, "this"),
  2171. Diagnostic(ErrorCode.ERR_SemicolonExpected, "object"),
  2172. Diagnostic(ErrorCode.ERR_SemicolonExpected, ")"),
  2173. Diagnostic(ErrorCode.ERR_RbraceExpected, ")"),
  2174. Diagnostic(ErrorCode.ERR_SemicolonExpected, ")"),
  2175. Diagnostic(ErrorCode.ERR_RbraceExpected, ")"));
  2176. }
  2177. [Fact, WorkItem(541347, "DevDiv")]
  2178. public void CS1041ERR_IdentifierExpectedKW06()
  2179. {
  2180. var test =
  2181. @"delegate object D(object o);
  2182. class C
  2183. {
  2184. static void M()
  2185. {
  2186. D d = (this object o) => null;
  2187. }
  2188. }";
  2189. ParseAndValidate(test,
  2190. Diagnostic(ErrorCode.ERR_CloseParenExpected, "object"),
  2191. Diagnostic(ErrorCode.ERR_SemicolonExpected, "object"),
  2192. Diagnostic(ErrorCode.ERR_SemicolonExpected, ")"),
  2193. Diagnostic(ErrorCode.ERR_RbraceExpected, ")"));
  2194. }
  2195. // TODO: extra error CS1014
  2196. [Fact]
  2197. public void CS1043ERR_SemiOrLBraceExpected()
  2198. {
  2199. var test = @"
  2200. using System;
  2201. public class Test
  2202. {
  2203. public int Prop
  2204. {
  2205. get return 1;
  2206. }
  2207. public static int Main()
  2208. {
  2209. return 1;
  2210. }
  2211. }
  2212. ";
  2213. ParseAndValidate(test,
  2214. // (7,13): error CS1043: { or ; expected
  2215. // get return 1;
  2216. Diagnostic(ErrorCode.ERR_SemiOrLBraceExpected, "return"),
  2217. // (9,15): error CS1014: A get or set accessor expected
  2218. // public static int Main()
  2219. Diagnostic(ErrorCode.ERR_GetOrSetExpected, "int"),
  2220. // (9,15): error CS1513: } expected
  2221. // public static int Main()
  2222. Diagnostic(ErrorCode.ERR_RbraceExpected, "int"));
  2223. }
  2224. [Fact]
  2225. public void CS1044ERR_MultiTypeInDeclaration()
  2226. {
  2227. var test = @"
  2228. using System;
  2229. // two normal classes...
  2230. public class Res1 : IDisposable
  2231. {
  2232. public void Dispose()
  2233. {
  2234. }
  2235. public void Func()
  2236. {
  2237. }
  2238. public void Throw()
  2239. {
  2240. }
  2241. }
  2242. public class Res2 : IDisposable
  2243. {
  2244. public void Dispose()
  2245. {
  2246. }
  2247. public void Func()
  2248. {
  2249. }
  2250. public void Throw()
  2251. {
  2252. }
  2253. }
  2254. public class Test
  2255. {
  2256. public static int Main()
  2257. {
  2258. using ( Res1 res1 = new Res1(),
  2259. Res2 res2 = new Res2())
  2260. {
  2261. res1.Func();
  2262. res2.Func();
  2263. }
  2264. return 1;
  2265. }
  2266. }
  2267. ";
  2268. // Extra Errors
  2269. ParseAndValidate(test,
  2270. // (36,9): error CS1044: Cannot use more than one type in a for, using, fixed, or declaration statement
  2271. // Res2 res2 = new Res2())
  2272. Diagnostic(ErrorCode.ERR_MultiTypeInDeclaration, "Res2"),
  2273. // (36,14): error CS1026: ) expected
  2274. // Res2 res2 = new Res2())
  2275. Diagnostic(ErrorCode.ERR_CloseParenExpected, "res2"),
  2276. // (36,31): error CS1002: ; expected
  2277. // Res2 res2 = new Res2())
  2278. Diagnostic(ErrorCode.ERR_SemicolonExpected, ")"),
  2279. // (36,31): error CS1513: } expected
  2280. // Res2 res2 = new Res2())
  2281. Diagnostic(ErrorCode.ERR_RbraceExpected, ")"));
  2282. }
  2283. [WorkItem(863395, "DevDiv/Personal")]
  2284. [Fact]
  2285. public void CS1055ERR_AddOrRemoveExpected()
  2286. {
  2287. // TODO: extra errors
  2288. var test = @"
  2289. delegate void del();
  2290. class Test
  2291. {
  2292. public event del MyEvent
  2293. {
  2294. return value;
  2295. }
  2296. public static int Main()
  2297. {
  2298. return 1;
  2299. }
  2300. }
  2301. ";
  2302. ParseAndValidate(test,
  2303. // (7,9): error CS1055: An add or remove accessor expected
  2304. // return value;
  2305. Diagnostic(ErrorCode.ERR_AddOrRemoveExpected, "return"),
  2306. // (7,16): error CS1055: An add or remove accessor expected
  2307. // return value;
  2308. Diagnostic(ErrorCode.ERR_AddOrRemoveExpected, "value"),
  2309. // (7,21): error CS0073: An add or remove accessor must have a body
  2310. // return value;
  2311. Diagnostic(ErrorCode.ERR_AddRemoveMustHaveBody, ";"));
  2312. }
  2313. [WorkItem(536956, "DevDiv")]
  2314. [Fact]
  2315. public void CS1065ERR_DefaultValueNotAllowed()
  2316. {
  2317. var test = @"
  2318. class A
  2319. {
  2320. delegate void D(int x);
  2321. D d1 = delegate(int x = 42) { };
  2322. }
  2323. ";
  2324. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_DefaultValueNotAllowed, "="));
  2325. }
  2326. [Fact]
  2327. public void CS1065ERR_DefaultValueNotAllowed_2()
  2328. {
  2329. var test = @"
  2330. class A
  2331. {
  2332. delegate void D(int x, int y);
  2333. D d1 = delegate(int x, int y = 42) { };
  2334. }
  2335. ";
  2336. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_DefaultValueNotAllowed, "="));
  2337. }
  2338. [WorkItem(540251, "DevDiv")]
  2339. [Fact]
  2340. public void CS7014ERR_AttributesNotAllowed()
  2341. {
  2342. var test = @"
  2343. using System;
  2344. class Program
  2345. {
  2346. static void Main()
  2347. {
  2348. const string message = ""the parameter is obsolete"";
  2349. Action<int> a = delegate (
  2350. [ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
  2351. [ObsoleteAttribute(message)] int y
  2352. ) { };
  2353. }
  2354. }
  2355. ";
  2356. ParseAndValidate(test,
  2357. // (10,13): error CS7014: Attributes are not valid in this context.
  2358. // [ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
  2359. Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]"),
  2360. // (10,42): error CS7014: Attributes are not valid in this context.
  2361. // [ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
  2362. Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]"),
  2363. // (11,13): error CS7014: Attributes are not valid in this context.
  2364. // [ObsoleteAttribute(message)] int y
  2365. Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]"));
  2366. }
  2367. [WorkItem(863401, "DevDiv/Personal")]
  2368. [Fact]
  2369. public void CS1101ERR_BadRefWithThis()
  2370. {
  2371. // No error
  2372. var test = @"
  2373. using System;
  2374. public static class Extensions
  2375. {
  2376. //No type parameters
  2377. public static void Foo(ref this int i) {}
  2378. //Single type parameter
  2379. public static void Foo<T>(ref this T t) {}
  2380. //Multiple type parameters
  2381. public static void Foo<T,U,V>(ref this U u) {}
  2382. }
  2383. public static class GenExtensions<X>
  2384. {
  2385. //No type parameters
  2386. public static void Foo(ref this int i) {}
  2387. public static void Foo(ref this X x) {}
  2388. //Single type parameter
  2389. public static void Foo<T>(ref this T t) {}
  2390. public static void Foo<T>(ref this X x) {}
  2391. //Multiple type parameters
  2392. public static void Foo<T,U,V>(ref this U u) {}
  2393. public static void Foo<T,U,V>(ref this X x) {}
  2394. }
  2395. ";
  2396. ParseAndValidate(test,
  2397. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2398. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2399. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2400. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2401. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2402. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2403. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2404. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"),
  2405. Diagnostic(ErrorCode.ERR_BadRefWithThis, "this"));
  2406. }
  2407. [WorkItem(906072, "DevDiv/Personal")]
  2408. [Fact]
  2409. public void CS1102ERR_BadOutWithThis()
  2410. {
  2411. // No error
  2412. var test = @"
  2413. using System;
  2414. public static class Extensions
  2415. {
  2416. //No type parameters
  2417. public static void Foo(this out int i) {}
  2418. //Single type parameter
  2419. public static void Foo<T>(this out T t) {}
  2420. //Multiple type parameters
  2421. public static void Foo<T,U,V>(this out U u) {}
  2422. }
  2423. public static class GenExtensions<X>
  2424. {
  2425. //No type parameters
  2426. public static void Foo(this out int i) {}
  2427. public static void Foo(this out X x) {}
  2428. //Single type parameter
  2429. public static void Foo<T>(this out T t) {}
  2430. public static void Foo<T>(this out X x) {}
  2431. //Multiple type parameters
  2432. public static void Foo<T,U,V>(this out U u) {}
  2433. public static void Foo<T,U,V>(this out X x) {}
  2434. }
  2435. ";
  2436. ParseAndValidate(test,
  2437. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2438. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2439. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2440. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2441. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2442. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2443. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2444. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"),
  2445. Diagnostic(ErrorCode.ERR_BadOutWithThis, "out"));
  2446. }
  2447. [WorkItem(863402, "DevDiv/Personal")]
  2448. [Fact]
  2449. public void CS1104ERR_BadParamModThis()
  2450. {
  2451. // NO error
  2452. var test = @"
  2453. using System;
  2454. public static class Extensions
  2455. {
  2456. //No type parameters
  2457. public static void Foo(this params int[] iArr) {}
  2458. //Single type parameter
  2459. public static void Foo<T>(this params T[] tArr) {}
  2460. //Multiple type parameters
  2461. public static void Foo<T,U,V>(this params U[] uArr) {}
  2462. }
  2463. public static class GenExtensions<X>
  2464. {
  2465. //No type parameters
  2466. public static void Foo(this params int[] iArr) {}
  2467. public static void Foo(this params X[] xArr) {}
  2468. //Single type parameter
  2469. public static void Foo<T>(this params T[] tArr) {}
  2470. public static void Foo<T>(this params X[] xArr) {}
  2471. //Multiple type parameters
  2472. public static void Foo<T,U,V>(this params U[] uArr) {}
  2473. public static void Foo<T,U,V>(this params X[] xArr) {}
  2474. }
  2475. ";
  2476. ParseAndValidate(test,
  2477. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2478. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2479. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2480. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2481. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2482. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2483. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2484. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"),
  2485. Diagnostic(ErrorCode.ERR_BadParamModThis, "params"));
  2486. }
  2487. [Fact, WorkItem(535930, "DevDiv")]
  2488. public void CS1107ERR_DupParamMod()
  2489. {
  2490. // Diff errors
  2491. var test = @"
  2492. using System;
  2493. public static class Extensions
  2494. {
  2495. //Extension methods
  2496. public static void Foo(this this t) {}
  2497. public static void Foo(this int this) {}
  2498. //Non-extension methods
  2499. public static void Foo(this t) {}
  2500. public static void Foo(int this) {}
  2501. }
  2502. ";
  2503. // Extra errors
  2504. ParseAndValidate(test,
  2505. // (6,33): error CS1107: A parameter can only have one 'this' modifier
  2506. // public static void Foo(this this t) {}
  2507. Diagnostic(ErrorCode.ERR_DupParamMod, "this").WithArguments("this"),
  2508. // (6,39): error CS1001: Identifier expected
  2509. // public static void Foo(this this t) {}
  2510. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"),
  2511. // (7,37): error CS1001: Identifier expected
  2512. // public static void Foo(this int this) {}
  2513. Diagnostic(ErrorCode.ERR_IdentifierExpected, "this"),
  2514. // (7,37): error CS1003: Syntax error, ',' expected
  2515. // public static void Foo(this int this) {}
  2516. Diagnostic(ErrorCode.ERR_SyntaxError, "this").WithArguments(",", "this"),
  2517. // (7,41): error CS1031: Type expected
  2518. // public static void Foo(this int this) {}
  2519. Diagnostic(ErrorCode.ERR_TypeExpected, ")"),
  2520. // (7,41): error CS1001: Identifier expected
  2521. // public static void Foo(this int this) {}
  2522. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"),
  2523. // (9,34): error CS1001: Identifier expected
  2524. // public static void Foo(this t) {}
  2525. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"),
  2526. // (10,32): error CS1001: Identifier expected
  2527. // public static void Foo(int this) {}
  2528. Diagnostic(ErrorCode.ERR_IdentifierExpected, "this"),
  2529. // (10,32): error CS1003: Syntax error, ',' expected
  2530. // public static void Foo(int this) {}
  2531. Diagnostic(ErrorCode.ERR_SyntaxError, "this").WithArguments(",", "this"),
  2532. // (10,36): error CS1031: Type expected
  2533. // public static void Foo(int this) {}
  2534. Diagnostic(ErrorCode.ERR_TypeExpected, ")"),
  2535. // (10,36): error CS1001: Identifier expected
  2536. // public static void Foo(int this) {}
  2537. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"));
  2538. }
  2539. [WorkItem(863405, "DevDiv/Personal")]
  2540. [Fact]
  2541. public void CS1108ERR_MultiParamMod()
  2542. {
  2543. // No error
  2544. var test = @"
  2545. using System;
  2546. public static class Extensions
  2547. {
  2548. //No type parameters
  2549. public static void Foo(ref out int i) {}
  2550. //Single type parameter
  2551. public static void Foo<T>(ref out T t) {}
  2552. //Multiple type parameters
  2553. public static void Foo<T,U,V>(ref out U u) {}
  2554. }
  2555. public static class GenExtensions<X>
  2556. {
  2557. //No type parameters
  2558. public static void Foo(ref out int i) {}
  2559. public static void Foo(ref out X x) {}
  2560. //Single type parameter
  2561. public static void Foo<T>(ref out T t) {}
  2562. public static void Foo<T>(ref out X x) {}
  2563. //Multiple type parameters
  2564. public static void Foo<T,U,V>(ref out U u) {}
  2565. public static void Foo<T,U,V>(ref out X x) {}
  2566. }
  2567. ";
  2568. ParseAndValidate(test,
  2569. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2570. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2571. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2572. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2573. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2574. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2575. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2576. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"),
  2577. Diagnostic(ErrorCode.ERR_MultiParamMod, "out"));
  2578. }
  2579. [Fact]
  2580. public void CS1513ERR_RbraceExpected()
  2581. {
  2582. var test = @"
  2583. struct S {
  2584. }
  2585. public class a {
  2586. public static int Main() {
  2587. return 1;
  2588. }
  2589. ";
  2590. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_RbraceExpected, "") );
  2591. }
  2592. // Infinite loop
  2593. [Fact]
  2594. public void CS1514ERR_LbraceExpected()
  2595. {
  2596. var test = @"
  2597. namespace x
  2598. ";
  2599. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_LbraceExpected, ""), Diagnostic(ErrorCode.ERR_RbraceExpected, ""));
  2600. }
  2601. [Fact]
  2602. public void CS1514ERR_LbraceExpected02()
  2603. {
  2604. var test = @"public class S.D
  2605. {
  2606. public string P.P { get; set; }
  2607. }
  2608. ";
  2609. ParseAndValidate(test,
  2610. // (1,15): error CS1514: { expected
  2611. // public class S.D
  2612. Diagnostic(ErrorCode.ERR_LbraceExpected, "."),
  2613. // (1,15): error CS1513: } expected
  2614. // public class S.D
  2615. Diagnostic(ErrorCode.ERR_RbraceExpected, "."),
  2616. // (1,15): error CS1022: Type or namespace definition, or end-of-file expected
  2617. // public class S.D
  2618. Diagnostic(ErrorCode.ERR_EOFExpected, "."),
  2619. // (1,16): error CS0116: A namespace does not directly contain members such as fields or methods
  2620. // public class S.D
  2621. Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "D"),
  2622. // (2,1): error CS1022: Type or namespace definition, or end-of-file expected
  2623. // {
  2624. Diagnostic(ErrorCode.ERR_EOFExpected, "{"),
  2625. // (4,1): error CS1022: Type or namespace definition, or end-of-file expected
  2626. // }
  2627. Diagnostic(ErrorCode.ERR_EOFExpected, "}"));
  2628. }
  2629. [WorkItem(535932, "DevDiv")]
  2630. [Fact]
  2631. public void CS1515ERR_InExpected()
  2632. {
  2633. // Diff error - CS1003
  2634. var test = @"
  2635. using System;
  2636. class Test
  2637. {
  2638. public static int Main()
  2639. {
  2640. int[] arr = new int[] {1, 2, 3};
  2641. foreach (int x arr) // CS1515
  2642. {
  2643. Console.WriteLine(x);
  2644. }
  2645. return 1;
  2646. }
  2647. }
  2648. ";
  2649. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InExpected, "arr"));
  2650. }
  2651. [Fact]
  2652. public void CS1515ERR_InExpected02()
  2653. {
  2654. var test = @"
  2655. class C
  2656. {
  2657. static void Main()
  2658. {
  2659. foreach (1)
  2660. System.Console.WriteLine(1);
  2661. }
  2662. }
  2663. ";
  2664. ParseAndValidate(test,
  2665. // (6,18): error CS1031: Type expected
  2666. // foreach (1)
  2667. Diagnostic(ErrorCode.ERR_TypeExpected, "1"),
  2668. // (6,18): error CS1001: Identifier expected
  2669. // foreach (1)
  2670. Diagnostic(ErrorCode.ERR_IdentifierExpected, "1"),
  2671. // (6,18): error CS1515: 'in' expected
  2672. // foreach (1)
  2673. Diagnostic(ErrorCode.ERR_InExpected, "1"));
  2674. }
  2675. [WorkItem(906503, "DevDiv/Personal")]
  2676. [Fact]
  2677. public void CS1517ERR_InvalidPreprocExprpp()
  2678. {
  2679. var test = @"
  2680. class Test
  2681. {
  2682. #if 1=2
  2683. #endif
  2684. public static int Main()
  2685. {
  2686. #if 0
  2687. return 0;
  2688. #endif
  2689. }
  2690. }
  2691. ";
  2692. // TODO: Extra errors
  2693. ParseAndValidate(test,
  2694. // (4,5): error CS1517: Invalid preprocessor expression
  2695. // #if 1=2
  2696. Diagnostic(ErrorCode.ERR_InvalidPreprocExpr, "1"),
  2697. // (4,5): error CS1025: Single-line comment or end-of-line expected
  2698. // #if 1=2
  2699. Diagnostic(ErrorCode.ERR_EndOfPPLineExpected, "1"),
  2700. // (8,5): error CS1517: Invalid preprocessor expression
  2701. // #if 0
  2702. Diagnostic(ErrorCode.ERR_InvalidPreprocExpr, "0"),
  2703. // (8,5): error CS1025: Single-line comment or end-of-line expected
  2704. // #if 0
  2705. Diagnostic(ErrorCode.ERR_EndOfPPLineExpected, "0"));
  2706. }
  2707. // TODO: Extra errors
  2708. [Fact]
  2709. public void CS1519ERR_InvalidMemberDecl_1()
  2710. {
  2711. var test = @"
  2712. namespace x
  2713. {
  2714. public void f() {}
  2715. public class C
  2716. {
  2717. return 1;
  2718. }
  2719. }
  2720. ";
  2721. // member declarations in namespace trigger semantic error, not parse error:
  2722. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "return").WithArguments("return"));
  2723. }
  2724. [Fact]
  2725. public void CS1519ERR_InvalidMemberDecl_2()
  2726. {
  2727. var test = @"
  2728. public class C
  2729. {
  2730. int[] i = new int[5];;
  2731. }
  2732. public class D
  2733. {
  2734. public static int Main ()
  2735. {
  2736. return 1;
  2737. }
  2738. }
  2739. ";
  2740. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";"));
  2741. }
  2742. [Fact]
  2743. public void CS1519ERR_InvalidMemberDecl_3()
  2744. {
  2745. var test = @"
  2746. struct s1
  2747. {
  2748. goto Labl; // Invalid
  2749. const int x = 1;
  2750. Lab1:
  2751. const int y = 2;
  2752. }
  2753. ";
  2754. // Extra errors
  2755. ParseAndValidate(test,
  2756. // (4,5): error CS1519: Invalid token 'goto' in class, struct, or interface member declaration
  2757. // goto Labl; // Invalid
  2758. Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "goto").WithArguments("goto"),
  2759. // (4,14): error CS1519: Invalid token ';' in class, struct, or interface member declaration
  2760. // goto Labl; // Invalid
  2761. Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";"),
  2762. // (4,14): error CS1519: Invalid token ';' in class, struct, or interface member declaration
  2763. // goto Labl; // Invalid
  2764. Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";"),
  2765. // (6,9): error CS1519: Invalid token ':' in class, struct, or interface member declaration
  2766. // Lab1:
  2767. Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ":").WithArguments(":"),
  2768. // (6,9): error CS1519: Invalid token ':' in class, struct, or interface member declaration
  2769. // Lab1:
  2770. Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ":").WithArguments(":"));
  2771. }
  2772. [Fact]
  2773. public void CS1520ERR_MemberNeedsType()
  2774. {
  2775. var test = @"
  2776. namespace x {
  2777. public class clx {
  2778. public int i;
  2779. public static int Main(){return 0;}
  2780. }
  2781. public class clz
  2782. {
  2783. public x(){}
  2784. }
  2785. }
  2786. ";
  2787. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_MemberNeedsType, "x"));
  2788. }
  2789. [Fact]
  2790. public void CS1521ERR_BadBaseType()
  2791. {
  2792. var test = @"
  2793. class Test1{}
  2794. class Test2 : Test1[] // CS1521
  2795. {
  2796. }
  2797. class Test3 : Test1* // CS1521
  2798. {
  2799. }
  2800. class Program
  2801. {
  2802. static int Main()
  2803. {
  2804. return -1;
  2805. }
  2806. }
  2807. ";
  2808. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadBaseType, "Test1[]"), Diagnostic(ErrorCode.ERR_BadBaseType, "Test1*"));
  2809. }
  2810. [WorkItem(906299, "DevDiv/Personal")]
  2811. [Fact]
  2812. public void CS1524ERR_ExpectedEndTry()
  2813. {
  2814. var test = @"using System;
  2815. namespace nms
  2816. {
  2817. public class mine
  2818. {
  2819. private static int retval = 5;
  2820. public static int Main()
  2821. {
  2822. try {
  2823. Console.WriteLine(""In try block, ready to throw."");
  2824. sizeof (throw new RecoverableException(""An exception has occurred""));
  2825. }
  2826. return retval;
  2827. }
  2828. };
  2829. }
  2830. ";
  2831. // Extra Errors
  2832. ParseAndValidate(test,
  2833. // (12,13): error CS1524: Expected catch or finally
  2834. // }
  2835. Diagnostic(ErrorCode.ERR_ExpectedEndTry, "}"),
  2836. // (11,21): error CS1031: Type expected
  2837. // sizeof (throw new RecoverableException("An exception has occurred"));
  2838. Diagnostic(ErrorCode.ERR_TypeExpected, "throw"),
  2839. // (11,21): error CS1026: ) expected
  2840. // sizeof (throw new RecoverableException("An exception has occurred"));
  2841. Diagnostic(ErrorCode.ERR_CloseParenExpected, "throw"),
  2842. // (11,21): error CS1002: ; expected
  2843. // sizeof (throw new RecoverableException("An exception has occurred"));
  2844. Diagnostic(ErrorCode.ERR_SemicolonExpected, "throw"),
  2845. // (11,80): error CS1002: ; expected
  2846. // sizeof (throw new RecoverableException("An exception has occurred"));
  2847. Diagnostic(ErrorCode.ERR_SemicolonExpected, ")"),
  2848. // (11,80): error CS1513: } expected
  2849. // sizeof (throw new RecoverableException("An exception has occurred"));
  2850. Diagnostic(ErrorCode.ERR_RbraceExpected, ")"));
  2851. }
  2852. [WorkItem(906299, "DevDiv/Personal")]
  2853. [Fact]
  2854. public void CS1525ERR_InvalidExprTerm()
  2855. {
  2856. var test = @"public class mine
  2857. {
  2858. public static int Main()
  2859. {
  2860. throw
  2861. }
  2862. };
  2863. ";
  2864. ParseAndValidate(test,
  2865. // (5,18): error CS1525: Invalid expression term '}'
  2866. // throw
  2867. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "").WithArguments("}"),
  2868. // (5,18): error CS1002: ; expected
  2869. // throw
  2870. Diagnostic(ErrorCode.ERR_SemicolonExpected, ""));
  2871. }
  2872. [WorkItem(919539, "DevDiv/Personal")]
  2873. [Fact]
  2874. public void CS1525RegressBadStatement()
  2875. {
  2876. // Dev10 CS1525 vs. new parser CS1513
  2877. var test = @"class C
  2878. {
  2879. static void X()
  2880. {
  2881. => // error
  2882. }
  2883. }";
  2884. ParseAndValidate(test,
  2885. // (4,6): error CS1513: } expected
  2886. // {
  2887. Diagnostic(ErrorCode.ERR_RbraceExpected, ""));
  2888. }
  2889. [WorkItem(540245, "DevDiv")]
  2890. [Fact]
  2891. public void CS1525RegressVoidInfiniteLoop()
  2892. {
  2893. var test = @"class C
  2894. {
  2895. void M()
  2896. {
  2897. void.Foo();
  2898. }
  2899. }";
  2900. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InvalidExprTerm, "void").WithArguments("void"));
  2901. }
  2902. [Fact]
  2903. public void CS1525ERR_InvalidExprTerm_TernaryOperator()
  2904. {
  2905. var test = @"class Program
  2906. {
  2907. static void Main(string[] args)
  2908. {
  2909. int x = 1;
  2910. int y = 1;
  2911. int s = true ? : y++; // Invalid
  2912. s = true ? x++ : ; // Invalid
  2913. s = ? x++ : y++; // Invalid
  2914. }
  2915. }
  2916. ";
  2917. ParseAndValidate(test,
  2918. Diagnostic(ErrorCode.ERR_InvalidExprTerm, ":").WithArguments(":"),
  2919. Diagnostic(ErrorCode.ERR_InvalidExprTerm, ";").WithArguments(";"),
  2920. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "?").WithArguments("?"));
  2921. }
  2922. [Fact]
  2923. public void CS1525ERR_InvalidExprTerm_MultiExpression()
  2924. {
  2925. var test = @"class Program
  2926. {
  2927. static void Main(string[] args)
  2928. {
  2929. int x = 1;
  2930. int y = 1;
  2931. int s = true ? x++, y++ : y++; // Invalid
  2932. s = true ? x++ : x++, y++; // Invalid
  2933. }
  2934. }
  2935. ";
  2936. ParseAndValidate(test,
  2937. // (7,27): error CS1003: Syntax error, ':' expected
  2938. // int s = true ? x++, y++ : y++; // Invalid
  2939. Diagnostic(ErrorCode.ERR_SyntaxError, ",").WithArguments(":", ","),
  2940. // (7,27): error CS1525: Invalid expression term ','
  2941. // int s = true ? x++, y++ : y++; // Invalid
  2942. Diagnostic(ErrorCode.ERR_InvalidExprTerm, ",").WithArguments(","),
  2943. // (7,30): error CS1002: ; expected
  2944. // int s = true ? x++, y++ : y++; // Invalid
  2945. Diagnostic(ErrorCode.ERR_SemicolonExpected, "++"),
  2946. // (7,33): error CS1525: Invalid expression term ':'
  2947. // int s = true ? x++, y++ : y++; // Invalid
  2948. Diagnostic(ErrorCode.ERR_InvalidExprTerm, ":").WithArguments(":"),
  2949. // (7,33): error CS1002: ; expected
  2950. // int s = true ? x++, y++ : y++; // Invalid
  2951. Diagnostic(ErrorCode.ERR_SemicolonExpected, ":"),
  2952. // (7,33): error CS1513: } expected
  2953. // int s = true ? x++, y++ : y++; // Invalid
  2954. Diagnostic(ErrorCode.ERR_RbraceExpected, ":"),
  2955. // (8,29): error CS1002: ; expected
  2956. // s = true ? x++ : x++, y++; // Invalid
  2957. Diagnostic(ErrorCode.ERR_SemicolonExpected, ","),
  2958. // (8,29): error CS1513: } expected
  2959. // s = true ? x++ : x++, y++; // Invalid
  2960. Diagnostic(ErrorCode.ERR_RbraceExpected, ","));
  2961. }
  2962. [WorkItem(542229, "DevDiv")]
  2963. [Fact]
  2964. public void CS1525ERR_InvalidExprTerm_FromInExprInQuery()
  2965. {
  2966. var test = @"
  2967. class Program
  2968. {
  2969. static void Main(string[] args)
  2970. {
  2971. var f1 = from num1 in new int[from] select num1;
  2972. }
  2973. }
  2974. ";
  2975. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_InvalidExprTerm, "from").WithArguments("]"));
  2976. }
  2977. [Fact]
  2978. public void CS1525ERR_InvalidExprTerm_ReturnInCondition()
  2979. {
  2980. var test = @"class Program
  2981. {
  2982. static void Main(string[] args)
  2983. {
  2984. int s = 1>2 ? return 0: return 1; // Invalid
  2985. }
  2986. }
  2987. ";
  2988. ParseAndValidate(test,
  2989. // (5,23): error CS1525: Invalid expression term 'return'
  2990. // int s = 1>2 ? return 0: return 1; // Invalid
  2991. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "return").WithArguments("return"),
  2992. // (5,23): error CS1003: Syntax error, ':' expected
  2993. // int s = 1>2 ? return 0: return 1; // Invalid
  2994. Diagnostic(ErrorCode.ERR_SyntaxError, "return").WithArguments(":", "return"),
  2995. // (5,23): error CS1525: Invalid expression term 'return'
  2996. // int s = 1>2 ? return 0: return 1; // Invalid
  2997. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "return").WithArguments("return"),
  2998. // (5,23): error CS1002: ; expected
  2999. // int s = 1>2 ? return 0: return 1; // Invalid
  3000. Diagnostic(ErrorCode.ERR_SemicolonExpected, "return"),
  3001. // (5,31): error CS1002: ; expected
  3002. // int s = 1>2 ? return 0: return 1; // Invalid
  3003. Diagnostic(ErrorCode.ERR_SemicolonExpected, ":"),
  3004. // (5,31): error CS1513: } expected
  3005. // int s = 1>2 ? return 0: return 1; // Invalid
  3006. Diagnostic(ErrorCode.ERR_RbraceExpected, ":"));
  3007. }
  3008. [Fact]
  3009. public void CS1525ERR_InvalidExprTerm_GotoInCondition()
  3010. {
  3011. var test = @"class Program
  3012. {
  3013. static int Main(string[] args)
  3014. {
  3015. int s = true ? goto lab1: goto lab2; // Invalid
  3016. lab1:
  3017. return 0;
  3018. lab2:
  3019. return 1;
  3020. }
  3021. }
  3022. ";
  3023. ParseAndValidate(test,
  3024. // (5,24): error CS1525: Invalid expression term 'goto'
  3025. // int s = true ? goto lab1: goto lab2; // Invalid
  3026. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "goto").WithArguments("goto"),
  3027. // (5,24): error CS1003: Syntax error, ':' expected
  3028. // int s = true ? goto lab1: goto lab2; // Invalid
  3029. Diagnostic(ErrorCode.ERR_SyntaxError, "goto").WithArguments(":", "goto"),
  3030. // (5,24): error CS1525: Invalid expression term 'goto'
  3031. // int s = true ? goto lab1: goto lab2; // Invalid
  3032. Diagnostic(ErrorCode.ERR_InvalidExprTerm, "goto").WithArguments("goto"),
  3033. // (5,24): error CS1002: ; expected
  3034. // int s = true ? goto lab1: goto lab2; // Invalid
  3035. Diagnostic(ErrorCode.ERR_SemicolonExpected, "goto"),
  3036. // (5,33): error CS1002: ; expected
  3037. // int s = true ? goto lab1: goto lab2; // Invalid
  3038. Diagnostic(ErrorCode.ERR_SemicolonExpected, ":"),
  3039. // (5,33): error CS1513: } expected
  3040. // int s = true ? goto lab1: goto lab2; // Invalid
  3041. Diagnostic(ErrorCode.ERR_RbraceExpected, ":"));
  3042. }
  3043. [Fact]
  3044. public void CS1526ERR_BadNewExpr()
  3045. {
  3046. var test = @"
  3047. public class MainClass
  3048. {
  3049. public static int Main ()
  3050. {
  3051. int []pi = new int;
  3052. return 1;
  3053. }
  3054. }
  3055. ";
  3056. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadNewExpr, ";"));
  3057. }
  3058. [Fact]
  3059. public void CS1528ERR_BadVarDecl()
  3060. {
  3061. var test = @"
  3062. using System;
  3063. namespace nms {
  3064. public class B {
  3065. public B(int i) {}
  3066. public void toss () { throw new Exception(""Exception thrown in function toss()."");}
  3067. };
  3068. public class mine {
  3069. private static int retval = 5;
  3070. public static int Main()
  3071. {
  3072. try {B b(3);
  3073. b.toss();
  3074. }
  3075. catch ( Exception e ) {retval -= 5; Console.WriteLine (e.GetMessage()); }
  3076. return retval;
  3077. }
  3078. };
  3079. }
  3080. ";
  3081. // Extra errors
  3082. ParseAndValidate(test,
  3083. // (12,17): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration)
  3084. // try {B b(3);
  3085. Diagnostic(ErrorCode.ERR_BadVarDecl, "(3)"),
  3086. // (12,17): error CS1003: Syntax error, '[' expected
  3087. // try {B b(3);
  3088. Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "("),
  3089. // (12,20): error CS1003: Syntax error, ']' expected
  3090. // try {B b(3);
  3091. Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";"));
  3092. }
  3093. [Fact]
  3094. public void CS1528RegressEventVersion()
  3095. {
  3096. var test = @"
  3097. class C
  3098. {
  3099. event System.Action E();
  3100. }
  3101. ";
  3102. // Extra errors
  3103. ParseAndValidate(test,
  3104. // (4,26): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration)
  3105. // event System.Action E();
  3106. Diagnostic(ErrorCode.ERR_BadVarDecl, "()"),
  3107. // (4,26): error CS1003: Syntax error, '[' expected
  3108. // event System.Action E();
  3109. Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "("),
  3110. // (4,27): error CS1525: Invalid expression term ')'
  3111. // event System.Action E();
  3112. Diagnostic(ErrorCode.ERR_InvalidExprTerm, ")").WithArguments(")"),
  3113. // (4,28): error CS1003: Syntax error, ']' expected
  3114. // event System.Action E();
  3115. Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";"));
  3116. }
  3117. [Fact]
  3118. public void CS1529ERR_UsingAfterElements()
  3119. {
  3120. var test = @"
  3121. namespace NS
  3122. {
  3123. class SomeClass
  3124. {}
  3125. using System;
  3126. }
  3127. using Microsoft;
  3128. ";
  3129. ParseAndValidate(test,
  3130. Diagnostic(ErrorCode.ERR_UsingAfterElements, "using System;"),
  3131. Diagnostic(ErrorCode.ERR_UsingAfterElements, "using Microsoft;"));
  3132. }
  3133. [Fact]
  3134. public void CS1534ERR_BadBinOpArgs()
  3135. {
  3136. var test = @"
  3137. class MyClass {
  3138. public int intI = 2;
  3139. public static MyClass operator + (MyClass MC1, MyClass MC2, MyClass MC3) {
  3140. return new MyClass();
  3141. }
  3142. public static int Main() {
  3143. return 1;
  3144. }
  3145. }
  3146. ";
  3147. ParseAndValidate(test,
  3148. // (4,36): error CS1534: Overloaded binary operator '+' takes two parameters
  3149. // public static MyClass operator + (MyClass MC1, MyClass MC2, MyClass MC3) {
  3150. Diagnostic(ErrorCode.ERR_BadBinOpArgs, "+").WithArguments("+"));
  3151. }
  3152. [WorkItem(863409, "DevDiv/Personal")]
  3153. [WorkItem(906305, "DevDiv/Personal")]
  3154. [Fact]
  3155. public void CS1535ERR_BadUnOpArgs()
  3156. {
  3157. var test = @"
  3158. class MyClass {
  3159. public int intI = 2;
  3160. public static MyClass operator ++ () {
  3161. return new MyClass();
  3162. }
  3163. public static int Main() {
  3164. return 1;
  3165. }
  3166. }
  3167. ";
  3168. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadUnOpArgs, "++").WithArguments("++"));
  3169. }
  3170. // TODO: extra error CS1001
  3171. [Fact]
  3172. public void CS1536ERR_NoVoidParameter()
  3173. {
  3174. var test = @"
  3175. class Test
  3176. {
  3177. public void foo(void){}
  3178. }
  3179. ";
  3180. ParseAndValidate(test,
  3181. // (4,21): error CS1536: Invalid parameter type 'void'
  3182. // public void foo(void){}
  3183. Diagnostic(ErrorCode.ERR_NoVoidParameter, "void"),
  3184. // (4,25): error CS1001: Identifier expected
  3185. // public void foo(void){}
  3186. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"));
  3187. }
  3188. [Fact]
  3189. public void CS1547ERR_NoVoidHere()
  3190. {
  3191. var test = @"
  3192. using System;
  3193. public class MainClass
  3194. {
  3195. public static int Main ()
  3196. {
  3197. void v;
  3198. Console.WriteLine (5);
  3199. return 1;
  3200. }
  3201. }
  3202. ";
  3203. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_NoVoidHere, "void"));
  3204. }
  3205. [WorkItem(919490, "DevDiv/Personal")]
  3206. [Fact]
  3207. public void CS1547ERR_NoVoidHereInDefaultAndSizeof()
  3208. {
  3209. var test = @"class C {
  3210. void M()
  3211. {
  3212. var x = sizeof(void);
  3213. return default(void);
  3214. } }
  3215. ";
  3216. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_NoVoidHere, "void"), Diagnostic(ErrorCode.ERR_NoVoidHere, "void"));
  3217. }
  3218. [Fact]
  3219. public void CS1551ERR_IndexerNeedsParam()
  3220. {
  3221. var test = @"
  3222. public class MyClass {
  3223. int intI;
  3224. int this[] {
  3225. get {
  3226. return intI;
  3227. }
  3228. set {
  3229. intI = value;
  3230. }
  3231. }
  3232. public static int Main() {
  3233. return 1;
  3234. }
  3235. }
  3236. ";
  3237. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IndexerNeedsParam, "]"));
  3238. }
  3239. [Fact]
  3240. public void CS1552ERR_BadArraySyntax()
  3241. {
  3242. var test = @"
  3243. public class C {
  3244. public static void Main(string args[]) {
  3245. }
  3246. }
  3247. ";
  3248. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadArraySyntax, "["));
  3249. }
  3250. [Fact, WorkItem(535933, "DevDiv")] // ?
  3251. public void CS1553ERR_BadOperatorSyntax()
  3252. {
  3253. // Extra errors
  3254. var test = @"
  3255. class foo {
  3256. public static int implicit operator (foo f) { return 6; } // Error
  3257. }
  3258. public class MainClass
  3259. {
  3260. public static int Main ()
  3261. {
  3262. return 1;
  3263. }
  3264. }
  3265. ";
  3266. ParseAndValidate(test,
  3267. // (3,19): error CS1553: Declaration is not valid; use '+ operator <dest-type> (...' instead
  3268. // public static int implicit operator (foo f) { return 6; } // Error
  3269. Diagnostic(ErrorCode.ERR_BadOperatorSyntax, "int").WithArguments("+"),
  3270. // (3,23): error CS1003: Syntax error, 'operator' expected
  3271. // public static int implicit operator (foo f) { return 6; } // Error
  3272. Diagnostic(ErrorCode.ERR_SyntaxError, "implicit").WithArguments("operator", "implicit"),
  3273. // (3,23): error CS1019: Overloadable unary operator expected
  3274. // public static int implicit operator (foo f) { return 6; } // Error
  3275. Diagnostic(ErrorCode.ERR_OvlUnaryOperatorExpected, "implicit"),
  3276. // (3,32): error CS1003: Syntax error, '(' expected
  3277. // public static int implicit operator (foo f) { return 6; } // Error
  3278. Diagnostic(ErrorCode.ERR_SyntaxError, "operator").WithArguments("(", "operator"),
  3279. // (3,32): error CS1041: Identifier expected; 'operator' is a keyword
  3280. // public static int implicit operator (foo f) { return 6; } // Error
  3281. Diagnostic(ErrorCode.ERR_IdentifierExpectedKW, "operator").WithArguments("", "operator"));
  3282. }
  3283. [Fact(), WorkItem(526995, "DevDiv")]
  3284. public void CS1554ERR_BadOperatorSyntax2()
  3285. {
  3286. // Diff errors: CS1003, 1031 etc. (8 errors)
  3287. var test = @"
  3288. class foo {
  3289. public static operator ++ foo (foo f) { return new foo(); } // Error
  3290. }
  3291. public class MainClass
  3292. {
  3293. public static int Main ()
  3294. {
  3295. return 1;
  3296. }
  3297. }
  3298. ";
  3299. ParseAndValidateFirst(test, Diagnostic(ErrorCode.ERR_TypeExpected, "operator"));
  3300. }
  3301. [Fact, WorkItem(536673, "DevDiv")]
  3302. public void CS1575ERR_BadStackAllocExpr()
  3303. {
  3304. // Diff errors
  3305. var test = @"
  3306. public class Test
  3307. {
  3308. unsafe public static int Main()
  3309. {
  3310. int *p = stackalloc int (30);
  3311. int *pp = stackalloc int 30;
  3312. return 1;
  3313. }
  3314. }
  3315. ";
  3316. // Extra errors
  3317. ParseAndValidate(test,
  3318. // (6,29): error CS1575: A stackalloc expression requires [] after type
  3319. // int *p = stackalloc int (30);
  3320. Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int"),
  3321. // (6,33): error CS1002: ; expected
  3322. // int *p = stackalloc int (30);
  3323. Diagnostic(ErrorCode.ERR_SemicolonExpected, "("),
  3324. // (7,30): error CS1575: A stackalloc expression requires [] after type
  3325. // int *pp = stackalloc int 30;
  3326. Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "int"),
  3327. // (7,34): error CS1002: ; expected
  3328. // int *pp = stackalloc int 30;
  3329. Diagnostic(ErrorCode.ERR_SemicolonExpected, "30"));
  3330. }
  3331. [WorkItem(906993, "DevDiv/Personal")]
  3332. [Fact]
  3333. public void CS1576ERR_InvalidLineNumberpp()
  3334. {
  3335. var test = @"
  3336. public class Test
  3337. {
  3338. # line abc hidden
  3339. public static void MyHiddenMethod()
  3340. {
  3341. #line 0
  3342. }
  3343. }
  3344. ";
  3345. ParseAndValidate(test,
  3346. // (4,12): error CS1576: The line number specified for #line directive is missing or invalid
  3347. // # line abc hidden
  3348. Diagnostic(ErrorCode.ERR_InvalidLineNumber, "abc"),
  3349. // (7,7): error CS1576: The line number specified for #line directive is missing or invalid
  3350. // #line 0
  3351. Diagnostic(ErrorCode.ERR_InvalidLineNumber, "0"));
  3352. }
  3353. [WorkItem(541952, "DevDiv")]
  3354. [Fact]
  3355. public void CS1576ERR_InvalidLineNumber02()
  3356. {
  3357. var test = @"
  3358. #line 0
  3359. #error
  3360. ";
  3361. ParseAndValidate(test,
  3362. // (2,7): error CS1576: The line number specified for #line directive is missing or invalid
  3363. // #line 0
  3364. Diagnostic(ErrorCode.ERR_InvalidLineNumber, "0"),
  3365. // (3,7): error CS1029: #error: ''
  3366. // #error
  3367. Diagnostic(ErrorCode.ERR_ErrorDirective, "").WithArguments(""));
  3368. }
  3369. [WorkItem(536689, "DevDiv")]
  3370. [Fact]
  3371. public void CS1578ERR_MissingPPFile()
  3372. {
  3373. var test = @"
  3374. public class Test
  3375. {
  3376. #line 5 hidden
  3377. public static void MyHiddenMethod()
  3378. {
  3379. }
  3380. }
  3381. ";
  3382. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_MissingPPFile, "hidden"));
  3383. }
  3384. [WorkItem(863414, "DevDiv/Personal")]
  3385. [Fact]
  3386. public void CS1585ERR_BadModifierLocation()
  3387. {
  3388. // Diff error: CS1519 v.s. CS1585
  3389. var test = @"
  3390. namespace oo {
  3391. public class clx {
  3392. public void f(){}
  3393. } // class clx
  3394. public class clxx : clx {
  3395. public static void virtual f() {}
  3396. }
  3397. public class cly {
  3398. public static int Main(){return 0;}
  3399. } // class cly
  3400. } // namespace
  3401. ";
  3402. ParseAndValidate(test,
  3403. // (7,24): error CS1585: Member modifier 'virtual' must precede the member type and name
  3404. // public static void virtual f() {}
  3405. Diagnostic(ErrorCode.ERR_BadModifierLocation, "virtual").WithArguments("virtual"),
  3406. // (7,32): error CS1520: Method must have a return type
  3407. // public static void virtual f() {}
  3408. Diagnostic(ErrorCode.ERR_MemberNeedsType, "f"));
  3409. }
  3410. [Fact]
  3411. public void CS1586ERR_MissingArraySize()
  3412. {
  3413. var test = @"
  3414. class Test
  3415. {
  3416. public static int Main()
  3417. {
  3418. int[] a = new int[];
  3419. byte[] b = new byte[];
  3420. string[] s = new string[];
  3421. return 1;
  3422. }
  3423. }
  3424. ";
  3425. ParseAndValidate(test,
  3426. Diagnostic(ErrorCode.ERR_MissingArraySize, "[]"),
  3427. Diagnostic(ErrorCode.ERR_MissingArraySize, "[]"),
  3428. Diagnostic(ErrorCode.ERR_MissingArraySize, "[]"));
  3429. }
  3430. [Fact, WorkItem(535935, "DevDiv")]
  3431. public void CS1597ERR_UnexpectedSemicolon()
  3432. {
  3433. // Diff error: CS1519
  3434. var test = @"
  3435. public class Test
  3436. {
  3437. public static int Main()
  3438. {
  3439. return 1;
  3440. };
  3441. }
  3442. ";
  3443. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_UnexpectedSemicolon, ";") );
  3444. }
  3445. [Fact]
  3446. public void CS1609ERR_NoModifiersOnAccessor()
  3447. {
  3448. var test = @"
  3449. public delegate void Del();
  3450. public class Test
  3451. {
  3452. public int Prop
  3453. {
  3454. get
  3455. {
  3456. return 0;
  3457. }
  3458. private set
  3459. {
  3460. }
  3461. }
  3462. public event Del E
  3463. {
  3464. private add{}
  3465. public remove{}
  3466. }
  3467. public static int Main()
  3468. {
  3469. return 1;
  3470. }
  3471. }
  3472. ";
  3473. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_NoModifiersOnAccessor, "private"), Diagnostic(ErrorCode.ERR_NoModifiersOnAccessor, "public"));
  3474. }
  3475. [Fact]
  3476. public void CS1609ERR_NoModifiersOnAccessor_Event()
  3477. {
  3478. var test = @"
  3479. public delegate void Del();
  3480. public class Test
  3481. {
  3482. event Del E
  3483. {
  3484. public add { }
  3485. private remove { }
  3486. }
  3487. }
  3488. ";
  3489. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_NoModifiersOnAccessor, "public"), Diagnostic(ErrorCode.ERR_NoModifiersOnAccessor, "private"));
  3490. }
  3491. [WorkItem(863423, "DevDiv/Personal")]
  3492. [Fact]
  3493. public void CS1611ERR_ParamsCantBeRefOut()
  3494. {
  3495. // No error
  3496. var test = @"
  3497. public class Test
  3498. {
  3499. public static void foo(params ref int[] a)
  3500. {
  3501. }
  3502. public static void boo(params out int[] a)
  3503. {
  3504. }
  3505. public static int Main()
  3506. {
  3507. int i = 10;
  3508. foo(ref i);
  3509. boo(out i);
  3510. return 1;
  3511. }
  3512. }
  3513. ";
  3514. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ParamsCantBeRefOut, "ref"), Diagnostic(ErrorCode.ERR_ParamsCantBeRefOut, "out"));
  3515. }
  3516. [Fact]
  3517. public void CS1627ERR_EmptyYield()
  3518. {
  3519. var test = @"
  3520. using System.Collections;
  3521. class C : IEnumerable
  3522. {
  3523. public IEnumerator GetEnumerator()
  3524. {
  3525. yield return; // CS1627
  3526. }
  3527. }
  3528. class Test
  3529. {
  3530. public static int Main()
  3531. {
  3532. return 1;
  3533. }
  3534. }
  3535. ";
  3536. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_EmptyYield, "return"));
  3537. }
  3538. [Fact]
  3539. public void CS1641ERR_FixedDimsRequired()
  3540. {
  3541. var test = @"
  3542. unsafe struct S
  3543. {
  3544. fixed int [] ia; // CS1641
  3545. fixed int [] ib[]; // CS0443
  3546. };
  3547. class Test
  3548. {
  3549. public static int Main()
  3550. {
  3551. return 1;
  3552. }
  3553. }
  3554. ";
  3555. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_FixedDimsRequired, "ia"), Diagnostic(ErrorCode.ERR_ValueExpected, "]"));
  3556. }
  3557. [WorkItem(863435, "DevDiv/Personal")]
  3558. [Fact]
  3559. public void CS1671ERR_BadModifiersOnNamespace01()
  3560. {
  3561. var test = @"
  3562. public namespace NS // CS1671
  3563. {
  3564. class Test
  3565. {
  3566. public static int Main()
  3567. {
  3568. return 1;
  3569. }
  3570. }
  3571. }
  3572. ";
  3573. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadModifiersOnNamespace, "public"));
  3574. }
  3575. [Fact]
  3576. public void CS1671ERR_BadModifiersOnNamespace02()
  3577. {
  3578. var test = @"[System.Obsolete]
  3579. namespace N { }
  3580. ";
  3581. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_BadModifiersOnNamespace, "[System.Obsolete]"));
  3582. }
  3583. [WorkItem(863437, "DevDiv/Personal")]
  3584. [Fact]
  3585. public void CS1675ERR_InvalidGenericEnumNowCS7002()
  3586. {
  3587. var test = @"
  3588. enum E<T> // CS1675
  3589. {
  3590. }
  3591. class Test
  3592. {
  3593. public static int Main()
  3594. {
  3595. return 1;
  3596. }
  3597. }
  3598. ";
  3599. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_UnexpectedGenericName, "E"));
  3600. }
  3601. [WorkItem(863438, "DevDiv/Personal")]
  3602. [Fact]
  3603. public void CS1730ERR_GlobalAttributesNotFirst()
  3604. {
  3605. var test = @"
  3606. class Test
  3607. {
  3608. }
  3609. [assembly:System.Attribute]
  3610. ";
  3611. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_GlobalAttributesNotFirst, "assembly"));
  3612. }
  3613. [Fact(), WorkItem(527039, "DevDiv")]
  3614. public void CS1732ERR_ParameterExpected()
  3615. {
  3616. var test = @"
  3617. using System;
  3618. static class Test
  3619. {
  3620. static int Main()
  3621. {
  3622. Func<int,int> f1 = (x,) => 1;
  3623. Func<int,int, int> f2 = (y,) => 2;
  3624. return 1;
  3625. }
  3626. }
  3627. ";
  3628. ParseAndValidate(test,
  3629. // (7,31): error CS1001: Identifier expected
  3630. // Func<int,int> f1 = (x,) => 1;
  3631. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"),
  3632. // (8,36): error CS1001: Identifier expected
  3633. // Func<int,int, int> f2 = (y,) => 2;
  3634. Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"));
  3635. }
  3636. [WorkItem(536674, "DevDiv")]
  3637. [Fact]
  3638. public void CS1733ERR_ExpressionExpected()
  3639. {
  3640. // diff error msg - CS1525
  3641. var test = @"
  3642. using System.Collections.Generic;
  3643. using System.Collections;
  3644. static class Test
  3645. {
  3646. static void Main()
  3647. {
  3648. A a = new A { 5, {9, 5, }, 3 };
  3649. }
  3650. }
  3651. ";
  3652. ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ExpressionExpected, "}"));
  3653. }
  3654. [WorkItem(536674, "DevDiv")]
  3655. [Fact]
  3656. public void CS1733ERR_ExpressionExpected_02()
  3657. {
  3658. var test = @"using System;
  3659. using System.Collections.Generic;
  3660. using System.Linq;
  3661. class Program
  3662. {
  3663. static void Main(string[] args)
  3664. {
  3665. Console.WriteLine(""Hello"")?";
  3666. ParseAndValidate(test,
  3667. // (9,36): error CS1733: Expected expression
  3668. // Console.WriteLine("Hello")?
  3669. Diagnostic(ErrorCode.ERR_ExpressionExpected, "").WithLocation(9, 36),
  3670. // (9,36): error CS1003: Syntax error, ':' expected
  3671. // Console.WriteLine("Hello")?
  3672. Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments(":", "").WithLocation(9, 36),
  3673. // (9,36): error CS1733: Expected expression
  3674. // Console.WriteLine("Hello")?
  3675. Diagnostic(ErrorCode.ERR_ExpressionExpected, "").WithLocation(9, 36),
  3676. // (9,36): error CS1002: ; expected
  3677. // Console.WriteLine("Hello")?
  3678. Diagnostic(ErrorCode.ERR_SemicolonExpected, "").WithLocation(9, 36),
  3679. // (9,36): error CS1513: } expected
  3680. // Console.WriteLine("Hello")?
  3681. Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(9, 36),
  3682. // (9,36): error CS1513: } expected
  3683. // Console.WriteLine("Hello")?
  3684. Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(9, 36));
  3685. }
  3686. [Fact]
  3687. public void CS1960ERR_IllegalVarianceSyntax()
  3688. {
  3689. var test =
  3690. @"interface I<in T>
  3691. {
  3692. void M<in U>();
  3693. object this<out U>[int i] { get; set; }
  3694. }
  3695. struct S<out T>
  3696. {
  3697. void M<out U>();
  3698. }
  3699. delegate void D<in T>();
  3700. class A<out T>
  3701. {
  3702. void M<out U>();
  3703. interface I<in U> { }
  3704. struct S<out U> { }
  3705. delegate void D<in U>();
  3706. class B<out U> { }
  3707. }";
  3708. ParseAndValidate(test,
  3709. // (3,12): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3710. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "in").WithLocation(3, 12),
  3711. // (4,12): error CS7002: Unexpected use of a generic name
  3712. Diagnostic(ErrorCode.ERR_UnexpectedGenericName, "this").WithLocation(4, 12),
  3713. // (4,17): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3714. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "out").WithLocation(4, 17),
  3715. // (6,10): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3716. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "out").WithLocation(6, 10),
  3717. // (8,12): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3718. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "out").WithLocation(8, 12),
  3719. // (11,9): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3720. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "out").WithLocation(11, 9),
  3721. // (13, 12): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3722. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "out").WithLocation(13, 12),
  3723. // (15, 14): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3724. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "out").WithLocation(15, 14),
  3725. // (17,13): error CS1960: Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
  3726. Diagnostic(ErrorCode.ERR_IllegalVarianceSyntax, "out").WithLocation(17, 13));
  3727. }
  3728. [Fact]
  3729. public void CS7000ERR_UnexpectedAliasedName()
  3730. {
  3731. var test = @"using System;
  3732. using N1Alias = N1;
  3733. namespace N1
  3734. {
  3735. namespace N1Alias::N2 {}
  3736. class Test
  3737. {
  3738. static int Main()
  3739. {
  3740. N1.global::Test.M1();
  3741. return 1;
  3742. }
  3743. }
  3744. }
  3745. ";
  3746. // Native compiler : CS1003
  3747. ParseAndValidate(test,
  3748. // (6,15): error CS7000: Unexpected use of an aliased name
  3749. // namespace N1Alias::N2 {}
  3750. Diagnostic(ErrorCode.ERR_UnexpectedAliasedName, "N1Alias::N2"),
  3751. // (12,22): error CS7000: Unexpected use of an aliased name
  3752. // N1.global::Test.M1();
  3753. Diagnostic(ErrorCode.ERR_UnexpectedAliasedName, "::"));
  3754. }
  3755. [Fact]
  3756. public void CS7002ERR_UnexpectedGenericName()
  3757. {
  3758. var test = @"enum E<T> { One, Two, Three }
  3759. public class Test
  3760. {
  3761. public int this<V>[V v] { get { return 0; } }
  3762. }
  3763. ";
  3764. // Native Compiler : CS1675 etc.
  3765. ParseAndValidate(test,
  3766. // (1,6): error CS7002: Unexpected use of a generic name
  3767. // enum E<T> { One, Two, Three }
  3768. Diagnostic(ErrorCode.ERR_UnexpectedGenericName, "E"),
  3769. // (5,16): error CS7002: Unexpected use of a generic name
  3770. // public int this<V>[V v] { get { return 0; } }
  3771. Diagnostic(ErrorCode.ERR_UnexpectedGenericName, "this") );
  3772. }
  3773. [Fact, WorkItem(546212, "DevDiv")]
  3774. public void InvalidQueryExpression()
  3775. {
  3776. var text = @"
  3777. using System;
  3778. using System.Linq;
  3779. public class QueryExpressionTest
  3780. {
  3781. public static void Main()
  3782. {
  3783. var expr1 = new[] { 1, 2, 3 };
  3784. var expr2 = new[] { 1, 2, 3 };
  3785. var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3786. }
  3787. }
  3788. ";
  3789. // error CS1002: ; expected
  3790. // error CS1031: Type expected
  3791. // error CS1525: Invalid expression term 'in' ... ...
  3792. ParseAndValidate(text,
  3793. // (12,29): error CS1002: ; expected
  3794. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3795. Diagnostic(ErrorCode.ERR_SemicolonExpected, "const"),
  3796. // (12,35): error CS1031: Type expected
  3797. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3798. Diagnostic(ErrorCode.ERR_TypeExpected, "in"),
  3799. // (12,35): error CS1001: Identifier expected
  3800. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3801. Diagnostic(ErrorCode.ERR_IdentifierExpected, "in"),
  3802. // (12,35): error CS0145: A const field requires a value to be provided
  3803. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3804. Diagnostic(ErrorCode.ERR_ConstValueRequired, "in"),
  3805. // (12,35): error CS1003: Syntax error, ',' expected
  3806. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3807. Diagnostic(ErrorCode.ERR_SyntaxError, "in").WithArguments(",", "in"),
  3808. // (12,38): error CS1002: ; expected
  3809. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3810. Diagnostic(ErrorCode.ERR_SemicolonExpected, "expr1"),
  3811. // (12,50): error CS1002: ; expected
  3812. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3813. Diagnostic(ErrorCode.ERR_SemicolonExpected, "i"),
  3814. // (12,52): error CS1002: ; expected
  3815. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3816. Diagnostic(ErrorCode.ERR_SemicolonExpected, "in"),
  3817. // (12,52): error CS1513: } expected
  3818. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3819. Diagnostic(ErrorCode.ERR_RbraceExpected, "in"),
  3820. // (12,64): error CS1002: ; expected
  3821. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3822. Diagnostic(ErrorCode.ERR_SemicolonExpected, "const"),
  3823. // (12,77): error CS0145: A const field requires a value to be provided
  3824. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3825. Diagnostic(ErrorCode.ERR_ConstValueRequired, "i"),
  3826. // (12,79): error CS1002: ; expected
  3827. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3828. Diagnostic(ErrorCode.ERR_SemicolonExpected, "select"),
  3829. // (12,86): error CS1002: ; expected
  3830. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3831. Diagnostic(ErrorCode.ERR_SemicolonExpected, "new"),
  3832. // (12,92): error CS1513: } expected
  3833. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3834. Diagnostic(ErrorCode.ERR_RbraceExpected, "const"),
  3835. // (12,92): error CS1002: ; expected
  3836. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3837. Diagnostic(ErrorCode.ERR_SemicolonExpected, "const"),
  3838. // (12,97): error CS1031: Type expected
  3839. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3840. Diagnostic(ErrorCode.ERR_TypeExpected, ","),
  3841. // (12,97): error CS1001: Identifier expected
  3842. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3843. Diagnostic(ErrorCode.ERR_IdentifierExpected, ","),
  3844. // (12,97): error CS0145: A const field requires a value to be provided
  3845. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3846. Diagnostic(ErrorCode.ERR_ConstValueRequired, ","),
  3847. // (12,99): error CS0145: A const field requires a value to be provided
  3848. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3849. Diagnostic(ErrorCode.ERR_ConstValueRequired, "i"),
  3850. // (12,101): error CS1002: ; expected
  3851. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3852. Diagnostic(ErrorCode.ERR_SemicolonExpected, "}"),
  3853. // (12,102): error CS1597: Semicolon after method or accessor block is not valid
  3854. // var query13 = from const in expr1 join i in expr2 on const equals i select new { const, i };
  3855. Diagnostic(ErrorCode.ERR_UnexpectedSemicolon, ";"),
  3856. // (14,1): error CS1022: Type or namespace definition, or end-of-file expected
  3857. // }
  3858. Diagnostic(ErrorCode.ERR_EOFExpected, "}")
  3859. );
  3860. }
  3861. [Fact]
  3862. public void PartialTypesBeforeVersionTwo()
  3863. {
  3864. var text = @"
  3865. partial class C
  3866. {
  3867. }
  3868. ";
  3869. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  3870. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  3871. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp1));
  3872. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  3873. // (2,1): error CS8022: Feature 'partial types' is not available in C# 1. Please use language version 2 or greater.
  3874. // partial class C
  3875. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion1, "partial").WithArguments("partial types", "2"));
  3876. }
  3877. [Fact]
  3878. public void PartialMethodsVersionThree()
  3879. {
  3880. var text = @"
  3881. class C
  3882. {
  3883. partial int Foo() { }
  3884. }
  3885. ";
  3886. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  3887. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  3888. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2));
  3889. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  3890. // (4,5): error CS8023: Feature 'partial method' is not available in C# 2. Please use language version 3 or greater.
  3891. // partial int Foo() { }
  3892. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "partial").WithArguments("partial method", "3"));
  3893. }
  3894. [Fact]
  3895. public void QueryBeforeVersionThree()
  3896. {
  3897. var text = @"
  3898. class C
  3899. {
  3900. void Foo()
  3901. {
  3902. var q = from a in b
  3903. select c;
  3904. }
  3905. }
  3906. ";
  3907. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  3908. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  3909. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2));
  3910. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  3911. // (6,17): error CS8023: Feature 'query expression' is not available in C# 2. Please use language version 3 or greater.
  3912. // var q = from a in b
  3913. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "from a in b").WithArguments("query expression", "3"),
  3914. // (6,17): error CS8023: Feature 'query expression' is not available in C# 2. Please use language version 3 or greater.
  3915. // var q = from a in b
  3916. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "from").WithArguments("query expression", "3"));
  3917. }
  3918. [Fact]
  3919. public void AnonymousTypeBeforeVersionThree()
  3920. {
  3921. var text = @"
  3922. class C
  3923. {
  3924. void Foo()
  3925. {
  3926. var q = new { };
  3927. }
  3928. }
  3929. ";
  3930. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  3931. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  3932. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2));
  3933. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  3934. // (6,17): error CS8023: Feature 'anonymous types' is not available in C# 2. Please use language version 3 or greater.
  3935. // var q = new { };
  3936. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "new").WithArguments("anonymous types", "3"));
  3937. }
  3938. [Fact]
  3939. public void ImplicitArrayBeforeVersionThree()
  3940. {
  3941. var text = @"
  3942. class C
  3943. {
  3944. void Foo()
  3945. {
  3946. var q = new [] { };
  3947. }
  3948. }
  3949. ";
  3950. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  3951. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  3952. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2));
  3953. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  3954. // (6,17): error CS8023: Feature 'implicitly typed array' is not available in C# 2. Please use language version 3 or greater.
  3955. // var q = new [] { };
  3956. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "new").WithArguments("implicitly typed array", "3"));
  3957. }
  3958. [Fact]
  3959. public void ObjectInitializerBeforeVersionThree()
  3960. {
  3961. var text = @"
  3962. class C
  3963. {
  3964. void Foo()
  3965. {
  3966. var q = new Foo { };
  3967. }
  3968. }
  3969. ";
  3970. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  3971. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  3972. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2));
  3973. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  3974. // (6,25): error CS8023: Feature 'object initializer' is not available in C# 2. Please use language version 3 or greater.
  3975. // var q = new Foo { };
  3976. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "{").WithArguments("object initializer", "3"));
  3977. }
  3978. [Fact]
  3979. public void LambdaBeforeVersionThree()
  3980. {
  3981. var text = @"
  3982. class C
  3983. {
  3984. void Foo()
  3985. {
  3986. var q = a => b;
  3987. }
  3988. }
  3989. ";
  3990. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  3991. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  3992. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2));
  3993. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  3994. // (6,19): error CS8023: Feature 'lambda expression' is not available in C# 2. Please use language version 3 or greater.
  3995. // var q = a => b;
  3996. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "=>").WithArguments("lambda expression", "3"));
  3997. }
  3998. [Fact]
  3999. public void ExceptionFilterBeforeVersionSix()
  4000. {
  4001. var text = @"
  4002. public class C
  4003. {
  4004. public static int Main()
  4005. {
  4006. try { } catch if (true) {}
  4007. }
  4008. }
  4009. ";
  4010. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6));
  4011. tree.GetDiagnostics().Verify();
  4012. tree = Parse(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  4013. tree.GetDiagnostics().Verify(
  4014. // (6,23): error CS8026: Feature 'exception filter' is not available in C# 5. Please use language version 6 or greater.
  4015. // try { } catch if (true) {}
  4016. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "if").WithArguments("exception filter", "6"));
  4017. }
  4018. #endregion
  4019. #region "Targeted Warning Tests - please arrange tests in the order of error code"
  4020. [Fact]
  4021. public void CS0440WRN_GlobalAliasDefn()
  4022. {
  4023. var test = @"
  4024. using global = MyClass; // CS0440
  4025. class MyClass
  4026. {
  4027. static void Main()
  4028. {
  4029. // Note how global refers to the global namespace
  4030. // even though it is redefined above.
  4031. global::System.Console.WriteLine();
  4032. }
  4033. }
  4034. ";
  4035. ParseAndValidate(test, Diagnostic(ErrorCode.WRN_GlobalAliasDefn, "global"));
  4036. }
  4037. [Fact]
  4038. public void CS0642WRN_PossibleMistakenNullStatement()
  4039. {
  4040. var test = @"
  4041. class MyClass
  4042. {
  4043. public static int Main()
  4044. {
  4045. for (int i = 0; i < 10; i += 1); // CS0642, semicolon intentional?
  4046. if(true);
  4047. while(false);
  4048. return 0;
  4049. }
  4050. }
  4051. ";
  4052. ParseAndValidate(test, Diagnostic(ErrorCode.WRN_PossibleMistakenNullStatement, ";"));
  4053. }
  4054. [Fact, WorkItem(529895, "DevDiv")]
  4055. public void AttributeInMethodBody()
  4056. {
  4057. var test = @"
  4058. public class Class1
  4059. {
  4060. int Meth2 (int parm) {[Foo(5)]return 0;}
  4061. }
  4062. ";
  4063. ParseAndValidate(test,
  4064. // (4,27): error CS1513: } expected
  4065. Diagnostic(ErrorCode.ERR_RbraceExpected, "[").WithLocation(4, 27),
  4066. // (4,35): error CS1519: Invalid token 'return' in class, struct, or interface member declaration
  4067. Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "return").WithArguments("return").WithLocation(4, 35),
  4068. // (4,35): error CS1519: Invalid token 'return' in class, struct, or interface member declaration
  4069. Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "return").WithArguments("return").WithLocation(4, 35),
  4070. // (5,1): error CS1022: Type or namespace definition, or end-of-file expected
  4071. Diagnostic(ErrorCode.ERR_EOFExpected, "}").WithLocation(5, 1));
  4072. }
  4073. // Preprocessor:
  4074. [Fact]
  4075. public void CS1030WRN_WarningDirectivepp()
  4076. {
  4077. //the single-line comment is handled differently from other trivia in the directive
  4078. var test = @"
  4079. class Test
  4080. {
  4081. static void Main()
  4082. {
  4083. #warning //This is a WARNING!
  4084. }
  4085. }
  4086. ";
  4087. ParseAndValidate(test, Diagnostic(ErrorCode.WRN_WarningDirective, "//This is a WARNING!").WithArguments("//This is a WARNING!"));
  4088. }
  4089. [Fact]
  4090. public void CS1522WRN_EmptySwitch()
  4091. {
  4092. var test = @"
  4093. class Test
  4094. {
  4095. public static int Main()
  4096. {
  4097. int i = 6;
  4098. switch(i) // CS1522
  4099. {}
  4100. return 0;
  4101. }
  4102. }
  4103. ";
  4104. ParseAndValidate(test, Diagnostic(ErrorCode.WRN_EmptySwitch, "{"));
  4105. }
  4106. [Fact]
  4107. public void PartialMethodInCSharp2()
  4108. {
  4109. var test = @"
  4110. partial class X
  4111. {
  4112. partial void M();
  4113. }
  4114. ";
  4115. CreateCompilationWithMscorlib(test, parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2)).VerifyDiagnostics(
  4116. // (4,5): error CS8023: Feature 'partial method' is not available in C# 2. Please use language version 3 or greater.
  4117. // partial void M();
  4118. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "partial").WithArguments("partial method", "3"));
  4119. }
  4120. [WorkItem(529870, "DevDiv")]
  4121. [Fact]
  4122. public void AsyncBeforeCSharp5()
  4123. {
  4124. var text = @"
  4125. class C
  4126. {
  4127. async void M() { }
  4128. }
  4129. ";
  4130. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  4131. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  4132. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp3));
  4133. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  4134. // (4,5): error CS8024: Feature 'async function' is not available in C# 3. Please use language version 5 or greater.
  4135. // async void M() { }
  4136. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion3, "async").WithArguments("async function", "5"));
  4137. }
  4138. [WorkItem(529870, "DevDiv")]
  4139. [Fact]
  4140. public void AsyncWithOtherModifiersBeforeCSharp5()
  4141. {
  4142. var text = @"
  4143. class C
  4144. {
  4145. async static void M() { }
  4146. }
  4147. ";
  4148. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  4149. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  4150. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp3));
  4151. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  4152. // (4,5): error CS8024: Feature 'async function' is not available in C# 3. Please use language version 5 or greater.
  4153. // async static void M() { }
  4154. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion3, "async").WithArguments("async function", "5"));
  4155. }
  4156. [Fact]
  4157. public void AsyncLambdaBeforeCSharp5()
  4158. {
  4159. var text = @"
  4160. class C
  4161. {
  4162. static void Main()
  4163. {
  4164. Func<int, Task<int>> f = async x => x;
  4165. }
  4166. }
  4167. ";
  4168. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  4169. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  4170. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp4));
  4171. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  4172. // (6,34): error CS8025: Feature 'async function' is not available in C# 4. Please use language version 5 or greater.
  4173. // Func<int, Task<int>> f = async x => x;
  4174. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion4, "async").WithArguments("async function", "5"));
  4175. }
  4176. [Fact]
  4177. public void AsyncDelegateBeforeCSharp5()
  4178. {
  4179. var text = @"
  4180. class C
  4181. {
  4182. static void Main()
  4183. {
  4184. Func<int, Task<int>> f = async delegate (int x) { return x; };
  4185. }
  4186. }
  4187. ";
  4188. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  4189. tree.GetCompilationUnitRoot().GetDiagnostics().Verify();
  4190. tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp4));
  4191. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  4192. // (6,34): error CS8025: Feature 'async function' is not available in C# 4. Please use language version 5 or greater.
  4193. // Func<int, Task<int>> f = async delegate (int x) { return x; };
  4194. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion4, "async").WithArguments("async function", "5"));
  4195. }
  4196. [Fact]
  4197. public void NamedArgumentBeforeCSharp4()
  4198. {
  4199. var text = @"
  4200. [Attr(x:1)]
  4201. class C
  4202. {
  4203. C()
  4204. {
  4205. M(y:2);
  4206. }
  4207. }
  4208. ";
  4209. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp4)).GetDiagnostics().Verify();
  4210. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp3)).GetDiagnostics().Verify(
  4211. // (2,7): error CS8024: Feature 'named argument' is not available in C# 3. Please use language version 4 or greater.
  4212. // [Attr(x:1)]
  4213. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion3, "x:").WithArguments("named argument", "4"),
  4214. // (7,11): error CS8024: Feature 'named argument' is not available in C# 3. Please use language version 4 or greater.
  4215. // M(y:2);
  4216. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion3, "y:").WithArguments("named argument", "4"));
  4217. }
  4218. [Fact]
  4219. public void GlobalKeywordBeforeCSharp2()
  4220. {
  4221. var text = @"
  4222. class C : global::B
  4223. {
  4224. }
  4225. ";
  4226. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify();
  4227. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp1)).GetDiagnostics().Verify(
  4228. // (2,11): error CS8022: Feature 'namespace alias qualifier' is not available in C# 1. Please use language version 2 or greater.
  4229. // class C : global::B
  4230. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion1, "global").WithArguments("namespace alias qualifier", "2"));
  4231. }
  4232. [Fact]
  4233. public void AliasQualifiedNameBeforeCSharp2()
  4234. {
  4235. var text = @"
  4236. class C : A::B
  4237. {
  4238. }
  4239. ";
  4240. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify();
  4241. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp1)).GetDiagnostics().Verify(
  4242. // (2,11): error CS8022: Feature 'namespace alias qualifier' is not available in C# 1. Please use language version 2 or greater.
  4243. // class C : A::B
  4244. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion1, "A").WithArguments("namespace alias qualifier", "2"));
  4245. }
  4246. [Fact]
  4247. public void OptionalParameterBeforeCSharp4()
  4248. {
  4249. var text = @"
  4250. class C
  4251. {
  4252. void M(int x = 1) { }
  4253. }
  4254. ";
  4255. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp4)).GetDiagnostics().Verify();
  4256. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp3)).GetDiagnostics().Verify(
  4257. // (4,18): error CS8024: Feature 'optional parameter' is not available in C# 3. Please use language version 4 or greater.
  4258. // void M(int x = 1) { }
  4259. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion3, "= 1").WithArguments("optional parameter", "4"));
  4260. }
  4261. [Fact]
  4262. public void ObjectInitializerBeforeCSharp3()
  4263. {
  4264. var text = @"
  4265. class C
  4266. {
  4267. void M()
  4268. {
  4269. return new C { Foo = 1 };
  4270. }
  4271. }
  4272. ";
  4273. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp3)).GetDiagnostics().Verify();
  4274. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify(
  4275. // (6,22): error CS8023: Feature 'object initializer' is not available in C# 2. Please use language version 3 or greater.
  4276. // return new C { Foo = 1 };
  4277. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "{").WithArguments("object initializer", "3"));
  4278. }
  4279. [Fact]
  4280. public void CollectionInitializerBeforeCSharp3()
  4281. {
  4282. var text = @"
  4283. class C
  4284. {
  4285. void M()
  4286. {
  4287. return new C { 1, 2, 3 };
  4288. }
  4289. }
  4290. ";
  4291. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp3)).GetDiagnostics().Verify();
  4292. SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify(
  4293. // (6,22): error CS8023: Feature 'collection initializer' is not available in C# 2. Please use language version 3 or greater.
  4294. // return new C { 1, 2, 3 };
  4295. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "{").WithArguments("collection initializer", "3"));
  4296. }
  4297. [Fact]
  4298. public void CrefGenericBeforeCSharp2()
  4299. {
  4300. var text = @"
  4301. /// <see cref='C{T}'/>
  4302. class C
  4303. {
  4304. }
  4305. ";
  4306. // NOTE: This actually causes an internal compiler error in dev12 (probably wasn't expecting an error from cref parsing).
  4307. SyntaxFactory.ParseSyntaxTree(text, options: TestOptions.RegularWithDocumentationComments.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify();
  4308. SyntaxFactory.ParseSyntaxTree(text, options: TestOptions.RegularWithDocumentationComments.WithLanguageVersion(LanguageVersion.CSharp1)).GetDiagnostics().Verify(
  4309. // (2,16): warning CS1584: XML comment has syntactically incorrect cref attribute 'C{T}'
  4310. // /// <see cref='C{T}'/>
  4311. Diagnostic(ErrorCode.WRN_BadXMLRefSyntax, "C{T}").WithArguments("C{T}"),
  4312. // (2,17): warning CS1658: Feature 'generics' is not available in C# 1. Please use language version 2 or greater.. See also error CS8022.
  4313. // /// <see cref='C{T}'/>
  4314. Diagnostic(ErrorCode.WRN_ErrorOverride, "{").WithArguments("error CS8022: Feature 'generics' is not available in C# 1. Please use language version 2 or greater.", "8022"));
  4315. }
  4316. [Fact]
  4317. public void CrefAliasQualifiedNameBeforeCSharp2()
  4318. {
  4319. var text = @"
  4320. /// <see cref='Alias::Foo'/>
  4321. /// <see cref='global::Foo'/>
  4322. class C { }
  4323. ";
  4324. // NOTE: This actually causes an internal compiler error in dev12 (probably wasn't expecting an error from cref parsing).
  4325. SyntaxFactory.ParseSyntaxTree(text, options: TestOptions.RegularWithDocumentationComments.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify();
  4326. SyntaxFactory.ParseSyntaxTree(text, options: TestOptions.RegularWithDocumentationComments.WithLanguageVersion(LanguageVersion.CSharp1)).GetDiagnostics().Verify(
  4327. // (2,16): warning CS1584: XML comment has syntactically incorrect cref attribute 'Alias::Foo'
  4328. // /// <see cref='Alias::Foo'/>
  4329. Diagnostic(ErrorCode.WRN_BadXMLRefSyntax, "Alias::Foo").WithArguments("Alias::Foo"),
  4330. // (2,16): warning CS1658: Feature 'namespace alias qualifier' is not available in C# 1. Please use language version 2 or greater.. See also error CS8022.
  4331. // /// <see cref='Alias::Foo'/>
  4332. Diagnostic(ErrorCode.WRN_ErrorOverride, "Alias").WithArguments("error CS8022: Feature 'namespace alias qualifier' is not available in C# 1. Please use language version 2 or greater.", "8022"),
  4333. // (3,16): warning CS1584: XML comment has syntactically incorrect cref attribute 'global::Foo'
  4334. // /// <see cref='global::Foo'/>
  4335. Diagnostic(ErrorCode.WRN_BadXMLRefSyntax, "global::Foo").WithArguments("global::Foo"),
  4336. // (3,16): warning CS1658: Feature 'namespace alias qualifier' is not available in C# 1. Please use language version 2 or greater.. See also error CS8022.
  4337. // /// <see cref='global::Foo'/>
  4338. Diagnostic(ErrorCode.WRN_ErrorOverride, "global").WithArguments("error CS8022: Feature 'namespace alias qualifier' is not available in C# 1. Please use language version 2 or greater.", "8022"));
  4339. }
  4340. [Fact]
  4341. public void PragmaBeforeCSharp2()
  4342. {
  4343. var text = @"
  4344. #pragma warning disable 1584
  4345. #pragma checksum ""file.txt"" ""{00000000-0000-0000-0000-000000000000}"" ""2453""
  4346. class C { }
  4347. ";
  4348. SyntaxFactory.ParseSyntaxTree(text, options: TestOptions.RegularWithDocumentationComments.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify();
  4349. SyntaxFactory.ParseSyntaxTree(text, options: TestOptions.RegularWithDocumentationComments.WithLanguageVersion(LanguageVersion.CSharp1)).GetDiagnostics().Verify(
  4350. // (2,2): error CS8022: Feature '#pragma' is not available in C# 1. Please use language version 2 or greater.
  4351. // #pragma warning disable 1584
  4352. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion1, "pragma").WithArguments("#pragma", "2"),
  4353. // (3,2): error CS8022: Feature '#pragma' is not available in C# 1. Please use language version 2 or greater.
  4354. // #pragma checksum "file.txt" "{00000000-0000-0000-0000-000000000000}" "2453"
  4355. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion1, "pragma").WithArguments("#pragma", "2"));
  4356. }
  4357. [Fact]
  4358. public void AwaitAsIdentifierInAsyncContext()
  4359. {
  4360. var text = @"
  4361. class C
  4362. {
  4363. async void f()
  4364. {
  4365. int await;
  4366. }
  4367. }
  4368. ";
  4369. var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
  4370. tree.GetCompilationUnitRoot().GetDiagnostics().Verify(
  4371. // (6,13): error CS4003: 'await' cannot be used as an identifier within an async method or lambda expression
  4372. // int await;
  4373. Diagnostic(ErrorCode.ERR_BadAwaitAsIdentifier, "await"));
  4374. }
  4375. // Note: Warnings covered in other test suite:
  4376. // 1) PreprocessorTests: CS1633WRN_IllegalPragma, CS1634WRN_IllegalPPWarning, CS1691WRN_BadWarningNumber, CS1692WRN_InvalidNumber,
  4377. // CS1695WRN_IllegalPPChecksum, CS1696WRN_EndOfPPLineExpected
  4378. [Fact]
  4379. public void WRN_NonECMAFeature()
  4380. {
  4381. var source = @"[module:Obsolete()]";
  4382. SyntaxFactory.ParseSyntaxTree(source, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2)).GetDiagnostics().Verify();
  4383. SyntaxFactory.ParseSyntaxTree(source, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp1)).GetDiagnostics().Verify(
  4384. // (1,2): warning CS1645: Feature 'module as an attribute target specifier' is not part of the standardized ISO C# language specification, and may not be accepted by other compilers
  4385. // [module:Obsolete()]
  4386. Diagnostic(ErrorCode.WRN_NonECMAFeature, "module:").WithArguments("module as an attribute target specifier"));
  4387. }
  4388. [Fact]
  4389. public void CSharp6Features()
  4390. {
  4391. var source =
  4392. @"class Foo(int z) // primary constructor
  4393. {
  4394. int L { get; } = 12; // auto property initializer
  4395. int M() => 12; // expression-bodied method
  4396. int N => 12; // expression-bodied property
  4397. int this[int a] => a + 1; // expression-bodied indexer
  4398. public static int operator +(Foo a, Foo b) => null; // expression-bodied operator
  4399. public static explicit operator bool(Foo a) => false; // expression-bodied conversion operator
  4400. void P(object o)
  4401. {
  4402. try {
  4403. } catch (Exception ex) if (ex.ToString() == null) { // exception filter
  4404. }
  4405. var s = o?.ToString(); // null propagating operator
  4406. }
  4407. }";
  4408. SyntaxFactory.ParseSyntaxTree(source, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Experimental)).GetDiagnostics().Verify();
  4409. SyntaxFactory.ParseSyntaxTree(source, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6)).GetDiagnostics().Verify(
  4410. // (1,10): error CS8058: Feature 'primary constructor' is only available in 'experimental' language version.
  4411. // class Foo(int z) // primary constructor
  4412. Diagnostic(ErrorCode.ERR_FeatureIsExperimental, "(int z)").WithArguments("primary constructor").WithLocation(1, 10));
  4413. SyntaxFactory.ParseSyntaxTree(source, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5)).GetDiagnostics().Verify(
  4414. // (1,10): error CS8058: Feature 'primary constructor' is only available in 'experimental' language version.
  4415. // class Foo(int z) // primary constructor
  4416. Diagnostic(ErrorCode.ERR_FeatureIsExperimental, "(int z)").WithArguments("primary constructor").WithLocation(1, 10),
  4417. // (3,20): error CS8026: Feature 'auto property initializer' is not available in C# 5. Please use language version 6 or greater.
  4418. // int L { get; } = 12; // auto property initializer
  4419. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "= 12").WithArguments("auto property initializer", "6").WithLocation(3, 20),
  4420. // (5,13): error CS8026: Feature 'expression-bodied method' is not available in C# 5. Please use language version 6 or greater.
  4421. // int M() => 12; // expression-bodied method
  4422. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "=> 12").WithArguments("expression-bodied method", "6").WithLocation(5, 13),
  4423. // (7,11): error CS8026: Feature 'expression-bodied property' is not available in C# 5. Please use language version 6 or greater.
  4424. // int N => 12; // expression-bodied property
  4425. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "=> 12").WithArguments("expression-bodied property", "6").WithLocation(7, 11),
  4426. // (9,21): error CS8026: Feature 'expression-bodied indexer' is not available in C# 5. Please use language version 6 or greater.
  4427. // int this[int a] => a + 1; // expression-bodied indexer
  4428. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "=> a + 1").WithArguments("expression-bodied indexer", "6").WithLocation(9, 21),
  4429. // (11,48): error CS8026: Feature 'expression-bodied method' is not available in C# 5. Please use language version 6 or greater.
  4430. // public static int operator +(Foo a, Foo b) => null; // expression-bodied operator
  4431. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "=> null").WithArguments("expression-bodied method", "6").WithLocation(11, 48),
  4432. // (13,49): error CS8026: Feature 'expression-bodied method' is not available in C# 5. Please use language version 6 or greater.
  4433. // public static explicit operator bool(Foo a) => false; // expression-bodied conversion operator
  4434. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "=> false").WithArguments("expression-bodied method", "6").WithLocation(13, 49),
  4435. // (18,32): error CS8026: Feature 'exception filter' is not available in C# 5. Please use language version 6 or greater.
  4436. // } catch (Exception ex) if (ex.ToString() == null) { // exception filter
  4437. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "if").WithArguments("exception filter", "6").WithLocation(18, 32),
  4438. // (21,17): error CS8026: Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater.
  4439. // var s = o?.ToString(); // null propagating operator
  4440. Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion5, "o?.ToString()").WithArguments("null propagating operator", "6").WithLocation(21, 17));
  4441. }
  4442. #endregion
  4443. #region "Helpers"
  4444. public static void ParseAndValidate(string text, params DiagnosticDescription[] expectedErrors)
  4445. {
  4446. var parsedTree = ParseWithRoundTripCheck(text);
  4447. var actualErrors = parsedTree.GetDiagnostics();
  4448. actualErrors.Verify(expectedErrors);
  4449. }
  4450. public static void ParseAndValidate(string text, CSharpParseOptions options, params DiagnosticDescription[] expectedErrors)
  4451. {
  4452. var parsedTree = ParseWithRoundTripCheck(text, options: options);
  4453. var actualErrors = parsedTree.GetDiagnostics();
  4454. actualErrors.Verify(expectedErrors);
  4455. }
  4456. public static void ParseAndValidateFirst(string text, DiagnosticDescription expectedFirstError)
  4457. {
  4458. var parsedTree = ParseWithRoundTripCheck(text);
  4459. var actualErrors = parsedTree.GetDiagnostics();
  4460. actualErrors.Take(1).Verify(expectedFirstError);
  4461. }
  4462. #endregion
  4463. }
  4464. }