From 228416c26bb9705f62986b1598db9160662bccba Mon Sep 17 00:00:00 2001 From: Andrew Rondeau Date: Fri, 13 Jan 2023 15:43:35 -0500 Subject: [PATCH] Demo of changes that CSharpier makes --- AllInOne.cs | 236 +++++++++++++++++++++++++++++----------------------- 1 file changed, 132 insertions(+), 104 deletions(-) diff --git a/AllInOne.cs b/AllInOne.cs index 30dffe2..cb3fd59 100644 --- a/AllInOne.cs +++ b/AllInOne.cs @@ -1,4 +1,4 @@ -#error Error message +#error Error message #warning Warning message #pragma warning disable 414, 3021 #pragma warning restore 3021 @@ -40,9 +40,11 @@ #endregion -[assembly: System.Copyright(@"(C)"" +[assembly: System.Copyright( + @"(C)"" -2009")] +2009" +)] [module: System.Copyright("\n\t\u0123(C) \"2009" + "\u0123")] class TopLevelType : IDisposable @@ -55,8 +57,8 @@ namespace My using A.B; interface CoContra { } - delegate void CoContra2<[System.Obsolete()] out T, in K>() - where T : struct; + + delegate void CoContra2<[System.Obsolete()] out T, in K>() where T : struct; public unsafe partial class A : C, I { @@ -69,7 +71,7 @@ static A() { } public A(int foo) { - L: + L: { int i = sizeof(int); ++i; @@ -78,7 +80,7 @@ public A(int foo) } #if DEBUG - Console.WriteLine(export.iefSupplied.command); + Console.WriteLine(export.iefSupplied.command); #endif const int? local = int.MaxValue; const Guid? local0 = new Guid(r.ToString()); @@ -176,22 +178,20 @@ public A(int foo) var o1 = new MyObject(); var o2 = new MyObject(var); var o3 = new MyObject { A = i }; - var o4 = new MyObject(@dynamic) { A = 0, B = 0, C = 0 }; - var o5 = new { A = 0 }; - var dictionaryInitializer = new Dictionary + var o4 = new MyObject(@dynamic) { - { 1, "" }, - { 2, "a" } + A = 0, + B = 0, + C = 0 }; + var o5 = new { A = 0 }; + var dictionaryInitializer = new Dictionary { { 1, "" }, { 2, "a" } }; float[] a = new float[] { 0f, 1.1f }; - int[,,] cube = { - { { 111, 112 }, { 121, 122 } }, - { { 211, 212 }, { 221, 222 } } - }; + int[,,] cube = { { { 111, 112 }, { 121, 122 } }, { { 211, 212 }, { 221, 222 } } }; int[][] jagged = { { 111 }, { 121, 122 } }; int[][,] arr = new int[5][,]; // as opposed to new int[][5,5] - arr[0] = new int[5,5]; // as opposed to arr[0,0] = new int[5]; + arr[0] = new int[5, 5]; // as opposed to arr[0,0] = new int[5]; arr[0][0, 0] = 47; int[] arrayTypeInference = new[] { 0, 1 }; switch (3) { } @@ -229,11 +229,10 @@ public A(int foo) continue; } break; - } - while (i < 10); + } while (i < 10); for (int j = 0; j < 100; ++j) { - for (;;) + for (; ; ) { for (int i = 0, j = 0; i < length; i++, j++) { } if (true) @@ -270,12 +269,18 @@ public A(int foo) process(); using (var v = BeginScope()) using (A a = new A()) - using (A a = new A(), b = new A()) + using ( + A a = new A(), + b = new A() + ) using (BeginScope()) return; yield return this.items[3]; yield break; - fixed (int* p = stackalloc int[100], q = &y) + fixed ( + int* p = stackalloc int[100], + q = &y + ) { *intref = 1; } @@ -310,52 +315,48 @@ from c in customers let d = c where d != null join c1 in customers on c1.GetHashCode() equals c.GetHashCode() - join c1 in customers - on c1.GetHashCode() equals c.GetHashCode() - into e + join c1 in customers on c1.GetHashCode() equals c.GetHashCode() into e group c by c.Country into g orderby g.Count() ascending orderby g.Key descending select new { Country = g.Key, CustCount = g.Count() }; query = from c in customers select c into d select d; } + ~A() { } + private readonly int f1; + [Obsolete] [NonExisting] [Foo::NonExisting(var, 5)] [CLSCompliant(false)] - [ - Obsolete, - System.NonSerialized, - NonSerialized, - CLSCompliant(true || false & true) - ] + [Obsolete, System.NonSerialized, NonSerialized, CLSCompliant(true || false & true)] private volatile int f2; + [return: Obsolete] [method: Obsolete] public void Handler(object value) { } - public int m(T t) - where T : class, new() + + public int m(T t) where T : class, new() { base.m(t); return 1; } + public string P { get { return "A"; } set; } public abstract string P { get; } - public abstract int this[int index] - { - protected internal get; - internal protected set; - } + public abstract int this[int index] { protected internal get; internal protected set; } + [method: Obsolete] [field: Obsolete] [event: Obsolete] public readonly event Event E; + [event: Test] public event Action E1 { @@ -369,34 +370,42 @@ public event Action E1 E -= Handler; } } + public static A operator +(A first, A second) { Delegate handler = new Delegate(Handler); return first.Add(second); } + [method: Obsolete] [return: Obsolete] public static bool operator true(A a) { return true; } + public static bool operator false(A a) { return false; } + class C { } } + public struct S : I { public S() { } + private int f1; + [Obsolete("Use Script instead", error: false)] private volatile int f2; - public abstract int m(T t) - where T : struct + + public abstract int m(T t) where T : struct { return 1; } + public string P { get @@ -408,25 +417,26 @@ public string P } public abstract string P { get; } - public abstract int this[int index] - { - get; - internal protected set; - } + public abstract int this[int index] { get; internal protected set; } public event Event E; + public static A operator +(A first, A second) { return first.Add(second); } + fixed int field[10]; + class C { } } + public interface I { void A(int value); string Value { get; set; } - unsafe void UpdateSignatureByHashingContent([In]byte* buffer, int size); + unsafe void UpdateSignatureByHashingContent([In] byte* buffer, int size); } + [type: Flags] public enum E { @@ -435,18 +445,19 @@ public enum E C = 2 + A, #if DEBUG D, - } + } #else E, - } + } #endif - public delegate void Delegate(object P); + namespace Test { using System; using System.Collections; + public class Список { public static IEnumerable Power(int number, int exponent) @@ -457,10 +468,11 @@ public static IEnumerable Power(int number, int exponent) int אתר = 0; while (++counter++ < --exponent--) { - result = result * number + +number+++++number; + result = result * number + +number++++ + number; yield return result; } } + static void Main() { foreach (int i in Power(2, 8)) @@ -468,20 +480,18 @@ static void Main() Console.Write("{0} ", i); } } + async void Wait() { await System.Threading.Tasks.Task.Delay(0); } + void AsyncAnonymous() // C # 5 feature { - var task = Task.Factory.StartNew( - async () => - { - return await new WebClient().DownloadStringTaskAsync( - "http://example.com" - ); - } - ); + var task = Task.Factory.StartNew(async () => + { + return await new WebClient().DownloadStringTaskAsync("http://example.com"); + }); } } } @@ -491,28 +501,31 @@ namespace ConsoleApplication1 { namespace RecursiveGenericBaseType { - class A : B, A> - where T : A + class A : B, A> where T : A { protected virtual A M() { } + protected abstract B, A> N() { } + static B, A> O() { } } sealed class B : A> { protected override A M() { } + protected sealed override B, A> N() { } + new static A O() { } } } namespace Boo { - public class Bar - where T : IComparable + public class Bar where T : IComparable { public T f; + public class Foo : IEnumerable { public void Method(K k, T t, U u) @@ -540,12 +553,14 @@ public static implicit operator Test(string s) { return new ConsoleApplication1.Test(); } + public static explicit operator Test(string s = "") { return new Test(); } public int foo = 5; + void Bar2() { foo = 6; @@ -561,10 +576,11 @@ void Blah() int? j = 6; Expression> e = () => i; - Expression> e2 = b => () => - { - return; - }; + Expression> e2 = b => + () => + { + return; + }; Func f = async delegate(bool a) { return await !a; @@ -572,7 +588,7 @@ void Blah() Func f2 = (a, b) => 0; f2 = (int a, int b) => 1; Action a = Blah; - f2 = () => { }; + f2 = () => { }; f2 = () => { ; @@ -603,8 +619,7 @@ public Type Foo public void Constants() { int i = 1 + 2 + 3 + 5; - global::System.String s = - "a" + (System.String)"a" + "a" + "a" + "a" + "A"; + global::System.String s = "a" + (System.String)"a" + "a" + "a" + "a" + "A"; } public void ConstructedType() @@ -633,7 +648,8 @@ namespace Comments.XmlComments.UndocumentedKeywords /// /// /// - class /*///*/C + class /*///*/ + C { void M(T t, U u) { @@ -641,20 +657,28 @@ void M(T t, U u) /* *** / */ /* // */ - /*s*///comment + /*s*/ + //comment // /***/ - /*s*/int /*s*/intValue = 0; - intValue = intValue /*s*/+ 1; - string strValue = /*s*/"hello"; + /*s*/int /*s*/ + intValue = 0; + intValue = + intValue /*s*/ + + 1; + string strValue = /*s*/ + "hello"; /*s*/MyClass c = new MyClass(); - string verbatimStr = /*s*/@"\\\\"; + string verbatimStr = /*s*/ + @"\\\\"; } } //General Test F. Type a very long class name, verify colorization happens correctly only upto the correct size (118324) - class TestClassXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX22/*Scen9*/{ } + class TestClassXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX22 /*Scen9*/ + { } class yield { @@ -668,11 +692,10 @@ void Foo(__arglist) Params(a: t, b: t); Params(ref c, out c); } + void Params(ref dynamic a, out dynamic b, params dynamic[] c) { } - void Params( - out dynamic a = 2, - ref dynamic c = default(dynamic), - params dynamic[][] c) { } + + void Params(out dynamic a = 2, ref dynamic c = default(dynamic), params dynamic[][] c) { } public override string ToString() { @@ -728,6 +751,7 @@ struct Point { public int X; public int Y; + public void ThisAccess() { this = this; @@ -748,9 +772,11 @@ class CSharp6Features // Expression bodies on method-like members public Point Move(int dx, int dy) => new Point(x + dx, y + dy); + public static Complex operator +(Complex a, Complex b) => a.Add(b); - public static implicit operator string(Person p) => - p.First + " " + p.Last; + + public static implicit operator string(Person p) => p.First + " " + p.Last; + public void Print() => Console.WriteLine(First + " " + Last); // Expression bodies on property-like function members @@ -775,8 +801,9 @@ async void Test() // String interpolation string s = $"{p.Name, 20} is {p.Age:D3} year{{s}} old #"; s = $"{p.Name} is \"{p.Age} year{(p.Age == 1 ? "" : "s")} old"; - s = $"{(p.Age == 2 ? $"{new Person { }}" : "")}"; - s = $@"\{p.Name} + s = $"{(p.Age == 2 ? $"{new Person { }}" : "")}"; + s = + $@"\{p.Name} ""\"; s = $"Color [ R={func(b: 3):#0.##}, G={G:#0.##}, B={B:#0.##}, A={A:#0.##} ]"; @@ -820,6 +847,7 @@ async void Test() } } } + #line 6 #line 2 "test.cs" #line default @@ -906,6 +934,7 @@ readonly struct ReadonlyRef1 { Func s = (in int x) => x; ref TValue this[in TKey index] => null; + public static Vector3 operator +(in Vector3 x, in Vector3 y) => null; static readonly ref Vector3 M1_Trace() @@ -961,8 +990,7 @@ ref T Choice(bool condition, ref T consequence, ref T alternative) } } - public void DoSomething(bool isEmployed, string personName, int personAge) - { } + public void DoSomething(bool isEmployed, string personName, int personAge) { } public void NonTrailingNamedArguments() { @@ -980,24 +1008,23 @@ public void ConditionalRef() public void LeadingSeparator() { - var res = 0 - + 123 // permitted in C# 1.0 and later - + 1_2_3 // permitted in C# 7.0 and later - + 0x1_2_3 // permitted in C# 7.0 and later - + 0b101 // binary literals added in C# 7.0 - + 0b1_0_1 // permitted in C# 7.0 and later - - // in C# 7.2, _ is permitted after the `0x` or `0b` - + 0x_1_2 // permitted in C# 7.2 and later - + 0b_1_0_1 // permitted in C# 7.2 and later + var res = + 0 + + 123 // permitted in C# 1.0 and later + + 1_2_3 // permitted in C# 7.0 and later + + 0x1_2_3 // permitted in C# 7.0 and later + + 0b101 // binary literals added in C# 7.0 + + 0b1_0_1 // permitted in C# 7.0 and later + // in C# 7.2, _ is permitted after the `0x` or `0b` + + 0x_1_2 // permitted in C# 7.2 and later + + 0b_1_0_1 // permitted in C# 7.2 and later ; } } class CSharp73 { - void Blittable(T value) - where T : unmanaged + void Blittable(T value) where T : unmanaged { var unmanaged = 666; } @@ -1019,8 +1046,8 @@ public void PatternBasedFixed() { fixed (byte* ptr = byteArray) { - // ptr is a native pointer to the first element of the array - // byteArray is protected from being moved/collected by the GC for the duration of this block + // ptr is a native pointer to the first element of the array + // byteArray is protected from being moved/collected by the GC for the duration of this block } } @@ -1034,7 +1061,8 @@ public void StackallocArrayInitializer() public void TupleEquality() { - (int, (int, int)) t1, t2; + (int, (int, int)) t1, + t2; var res = t1 == (1, (2, 3)); } } @@ -1096,7 +1124,8 @@ void Ranges() void UsingDeclarators() { using var item = new FileStream("./.f"); - fixed (char* ch = "hell"); + fixed (char* ch = "hell") + ; item.Dispose(); } @@ -1151,4 +1180,3 @@ override void M() // implicitly named } } } -