MissingSpecialMember.cs 99.6 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
P
Pilchie 已提交
4

5
using System;
P
Pilchie 已提交
6 7
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
8 9
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
P
Pilchie 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
    public class MissingSpecialMember : CSharpTestBase
    {
        [Fact]
        public void Missing_System_Collections_Generic_IEnumerable_T__GetEnumerator()
        {
            var source =
@"using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
    }

    public IEnumerable<int> M()
    {
        yield return 0;
        yield return 1;
    }
}";
J
Jared Parsons 已提交
34
            var comp = CreateCompilation(source, options: TestOptions.ReleaseDll);
35 36

            comp.MakeMemberMissing(SpecialMember.System_Collections_Generic_IEnumerable_T__GetEnumerator);
37

38
            comp.VerifyEmitDiagnostics(
P
Pilchie 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    // (10,5): error CS0656: Missing compiler required member 'System.Collections.Generic.IEnumerable`1.GetEnumerator'
    //     {
    Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"{
        yield return 0;
        yield return 1;
    }").WithArguments("System.Collections.Generic.IEnumerable`1", "GetEnumerator").WithLocation(10, 5)
                );
        }

        [Fact]
        public void Missing_System_IDisposable__Dispose()
        {
            var source =
@"using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
    }

    public IEnumerable<int> M()
    {
        yield return 0;
        yield return 1;
    }
}";
J
Jared Parsons 已提交
66
            var comp = CreateCompilation(source, options: TestOptions.ReleaseDll);
67 68 69 70

            comp.MakeMemberMissing(SpecialMember.System_IDisposable__Dispose);

            comp.VerifyEmitDiagnostics(
P
Pilchie 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    // (10,5): error CS0656: Missing compiler required member 'System.IDisposable.Dispose'
    //     {
    Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"{
        yield return 0;
        yield return 1;
    }").WithArguments("System.IDisposable", "Dispose").WithLocation(10, 5)
                );
        }

        [Fact]
        public void Missing_System_Diagnostics_DebuggerHiddenAttribute__ctor()
        {
            var source =
@"using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
    }

    public IEnumerable<int> M()
    {
        yield return 0;
        yield return 1;
    }
}";
J
Jared Parsons 已提交
98
            var comp = CreateCompilation(source, options: TestOptions.ReleaseDll);
99 100 101 102

            comp.MakeMemberMissing(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor);

            comp.VerifyEmitDiagnostics(
P
Pilchie 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                // the DebuggerHidden attribute is optional.
                );
        }

        [Fact]
        public void Missing_System_Runtime_CompilerServices_ExtensionAttribute__ctor()
        {
            var source =
@"using System.Collections.Generic;

public static class Program
{
    public static void Main(string[] args)
    {
    }

    public static void Extension(this string x) {}
}";
J
Jared Parsons 已提交
121
            var comp = CreateEmptyCompilation(source, new[] { MscorlibRef }, options: TestOptions.ReleaseDll);
122 123 124 125

            comp.MakeMemberMissing(WellKnownMember.System_Diagnostics_DebuggerHiddenAttribute__ctor);

            comp.VerifyEmitDiagnostics(
P
Pilchie 已提交
126 127 128 129 130
                // (9,34): error CS1110: Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll?
                //     public static void Extension(this string x) {}
                Diagnostic(ErrorCode.ERR_ExtensionAttrNotFound, "this").WithArguments("System.Runtime.CompilerServices.ExtensionAttribute").WithLocation(9, 34)
                );
        }
131

J
Jared Parsons 已提交
132
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        [Fact]
        public void NonPublicSpecialType()
        {
            var source = @"
namespace System
{
    public class Object
    {
        public Object() { }
    }

    internal class String : Object
    {
    }

    public class ValueType { }
    public struct Void { }
}
";
            Action<CSharpCompilation> validate = comp =>
            {
                var specialType = comp.GetSpecialType(SpecialType.System_String);
                Assert.Equal(TypeKind.Error, specialType.TypeKind);
                Assert.Equal(SpecialType.System_String, specialType.SpecialType);
                Assert.Equal(Accessibility.NotApplicable, specialType.DeclaredAccessibility);

                var lookupType = comp.GetTypeByMetadataName("System.String");
                Assert.Equal(TypeKind.Class, lookupType.TypeKind);
                Assert.Equal(SpecialType.None, lookupType.SpecialType);
                Assert.Equal(Accessibility.Internal, lookupType.DeclaredAccessibility);
            };

            ValidateSourceAndMetadata(source, validate);
        }

J
Jared Parsons 已提交
168
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        [Fact]
        public void NonPublicSpecialTypeMember()
        {
            var sourceTemplate = @"
namespace System
{{
    public class Object
    {{
        public Object() {{ }}

        {0} virtual String ToString() {{ return null; }}
    }}

    {0} class String : Object
    {{
        public static String Concat(String s1, String s2) {{ return null; }}
    }}

    public class ValueType {{ }}
    public struct Void {{ }}
}}
";
            Action<CSharpCompilation> validatePresent = comp =>
            {
                Assert.NotNull(comp.GetSpecialTypeMember(SpecialMember.System_Object__ToString));
                Assert.NotNull(comp.GetSpecialTypeMember(SpecialMember.System_String__ConcatStringString));
195
                comp.GetDiagnostics();
196 197 198 199 200 201
            };

            Action<CSharpCompilation> validateMissing = comp =>
            {
                Assert.Null(comp.GetSpecialTypeMember(SpecialMember.System_Object__ToString));
                Assert.Null(comp.GetSpecialTypeMember(SpecialMember.System_String__ConcatStringString));
202
                comp.GetDiagnostics();
203 204 205 206 207 208 209
            };

            ValidateSourceAndMetadata(string.Format(sourceTemplate, "public"), validatePresent);
            ValidateSourceAndMetadata(string.Format(sourceTemplate, "internal"), validateMissing);
        }

        // Document the fact that we don't reject type parameters with constraints (yet?).
J
Jared Parsons 已提交
210
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        [Fact]
        public void GenericConstraintsOnSpecialType()
        {
            var source = @"
namespace System
{
    public class Object
    {
        public Object() { }
    }

    public struct Nullable<T> where T : new()
    {
    }

    public class ValueType { }
    public struct Void { }
}
";
            Action<CSharpCompilation> validate = comp =>
            {
                var specialType = comp.GetSpecialType(SpecialType.System_Nullable_T);
                Assert.Equal(TypeKind.Struct, specialType.TypeKind);
                Assert.Equal(SpecialType.System_Nullable_T, specialType.SpecialType);

                var lookupType = comp.GetTypeByMetadataName("System.Nullable`1");
                Assert.Equal(TypeKind.Struct, lookupType.TypeKind);
                Assert.Equal(SpecialType.System_Nullable_T, lookupType.SpecialType);
            };

            ValidateSourceAndMetadata(source, validate);
        }

        // No special type members have type parameters that could (incorrectly) be constrained.

J
Jared Parsons 已提交
246
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        [Fact]
        public void NonPublicWellKnownType()
        {
            var source = @"
namespace System
{
    public class Object
    {
        public Object() { }
    }

    internal class Type : Object
    {
    }

    public class ValueType { }
    public struct Void { }
}
";
J
Jared Parsons 已提交
266
            var comp = CreateEmptyCompilation(source);
267

268 269 270
            var wellKnownType = comp.GetWellKnownType(WellKnownType.System_Type);
            Assert.Equal(TypeKind.Class, wellKnownType.TypeKind);
            Assert.Equal(Accessibility.Internal, wellKnownType.DeclaredAccessibility);
271

272 273 274
            var lookupType = comp.GetTypeByMetadataName("System.Type");
            Assert.Equal(TypeKind.Class, lookupType.TypeKind);
            Assert.Equal(Accessibility.Internal, lookupType.DeclaredAccessibility);
275 276
        }

J
Jared Parsons 已提交
277
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        [Fact]
        public void NonPublicWellKnownType_Nested()
        {
            var sourceTemplate = @"
namespace System.Diagnostics
{{
    {0} class DebuggableAttribute
    {{
        {1} enum DebuggingModes {{ }}
    }}
}}

namespace System
{{
    public class Object {{ }}
    public class ValueType {{ }}
    public class Enum : ValueType {{ }}
    public struct Void {{ }}
    public struct Int32 {{ }}
}}
";
            Action<CSharpCompilation> validate = comp =>
            {
                var wellKnownType = comp.GetWellKnownType(WellKnownType.System_Diagnostics_DebuggableAttribute__DebuggingModes);
                Assert.Equal(TypeKind.Error, wellKnownType.TypeKind);
                Assert.Equal(Accessibility.NotApplicable, wellKnownType.DeclaredAccessibility);

                var lookupType = comp.GetTypeByMetadataName("System.Diagnostics.DebuggableAttribute+DebuggingModes");
                Assert.Equal(TypeKind.Enum, lookupType.TypeKind);
                Assert.NotEqual(Accessibility.NotApplicable, lookupType.DeclaredAccessibility);
            };

310 311
            ValidateSourceAndMetadata(string.Format(sourceTemplate, "public", "protected"), validate);
            ValidateSourceAndMetadata(string.Format(sourceTemplate, "public", "private"), validate);
312 313
        }

J
Jared Parsons 已提交
314
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        [Fact]
        public void NonPublicWellKnownTypeMember()
        {
            var sourceTemplate = @"
namespace System
{{
    public class Object
    {{
        public Object() {{ }}
    }}

    {0} class Type : Object
    {{
        public static readonly Object Missing = new Object();
    }}

    public static class Math : Object
    {{
        {0} static Double Round(Double d) {{ return d; }}
    }}

    public class ValueType {{ }}
    public struct Void {{ }}
    public struct Double {{ }}
}}
";
            Action<CSharpCompilation> validatePresent = comp =>
            {
                Assert.NotNull(comp.GetWellKnownTypeMember(WellKnownMember.System_Type__Missing));
                Assert.NotNull(comp.GetWellKnownTypeMember(WellKnownMember.System_Math__RoundDouble));
345
                comp.GetDiagnostics();
346 347
            };

J
Jared Parsons 已提交
348 349
            validatePresent(CreateEmptyCompilation(string.Format(sourceTemplate, "public")));
            validatePresent(CreateEmptyCompilation(string.Format(sourceTemplate, "internal")));
350 351 352
        }

        // Document the fact that we don't reject type parameters with constraints (yet?).
J
Jared Parsons 已提交
353
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        [Fact]
        public void GenericConstraintsOnWellKnownType()
        {
            var source = @"
namespace System
{
    public class Object
    {
        public Object() { }
    }

    namespace Threading.Tasks
    {
        public class Task<T> where T : new()
        {
        }
    }

    public class ValueType { }
    public struct Void { }
}
";
            Action<CSharpCompilation> validate = comp =>
            {
                var wellKnownType = comp.GetWellKnownType(WellKnownType.System_Threading_Tasks_Task_T);
                Assert.Equal(TypeKind.Class, wellKnownType.TypeKind);

                var lookupType = comp.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
                Assert.Equal(TypeKind.Class, lookupType.TypeKind);
            };

            ValidateSourceAndMetadata(source, validate);
        }

        // Document the fact that we don't reject type parameters with constraints (yet?).
J
Jared Parsons 已提交
389
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        [Fact]
        public void GenericConstraintsOnWellKnownTypeMember()
        {
            var sourceTemplate = @"
namespace System
{{
    public class Object
    {{
        public Object() {{ }}
    }}

    namespace Threading
    {{
        public static class Interlocked
        {{
            public static T CompareExchange<T>(ref T t1, T t2, T t3){0}
            {{
                return t1;
            }}
        }}
    }}

    public class ValueType {{ }}
    public struct Void {{ }}
}}
";
            Action<CSharpCompilation> validate = comp =>
            {
                Assert.NotNull(comp.GetWellKnownTypeMember(WellKnownMember.System_Threading_Interlocked__CompareExchange_T));
419
                comp.GetDiagnostics();
420 421 422 423 424 425
            };

            ValidateSourceAndMetadata(string.Format(sourceTemplate, ""), validate);
            ValidateSourceAndMetadata(string.Format(sourceTemplate, " where T : new()"), validate);
        }

J
Jared Parsons 已提交
426
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
        [Fact]
        public void PublicVersusInternalWellKnownType()
        {
            var corlibSource = @"
namespace System
{
    public class Object
    {
        public Object() { }
    }

    public class String
    {
    }

    public class Attribute 
    {
    }

    public class ValueType { }
    public struct Void { }
}

namespace System.Runtime.CompilerServices
{
    public class InternalsVisibleToAttribute : Attribute
    {
        public InternalsVisibleToAttribute(String s)
        {
        }
    }
}
";
            var libSourceTemplate = @"
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""Test"")]

namespace System
{{
    {0} class Type
    {{
    }}
}}
";

J
Jared Parsons 已提交
471
            var corlibRef = CreateEmptyCompilation(corlibSource).EmitToImageReference(expectedWarnings: new[]
472
            {
473
                // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
474 475 476
                Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1)
            });

J
Jared Parsons 已提交
477 478
            var publicLibRef = CreateEmptyCompilation(string.Format(libSourceTemplate, "public"), new[] { corlibRef }).EmitToImageReference();
            var internalLibRef = CreateEmptyCompilation(string.Format(libSourceTemplate, "internal"), new[] { corlibRef }).EmitToImageReference();
479

J
Jared Parsons 已提交
480
            var comp = CreateEmptyCompilation("", new[] { corlibRef, publicLibRef, internalLibRef }, assemblyName: "Test");
481 482 483 484 485 486 487 488 489 490 491 492

            var wellKnown = comp.GetWellKnownType(WellKnownType.System_Type);
            Assert.NotNull(wellKnown);
            Assert.Equal(TypeKind.Class, wellKnown.TypeKind);
            Assert.Equal(Accessibility.Public, wellKnown.DeclaredAccessibility);

            var lookup = comp.GetTypeByMetadataName("System.Type");
            Assert.Null(lookup); // Ambiguous
        }

        private static void ValidateSourceAndMetadata(string source, Action<CSharpCompilation> validate)
        {
J
Jared Parsons 已提交
493
            var comp1 = CreateEmptyCompilation(source);
494 495
            validate(comp1);

496 497
            var reference = comp1.EmitToImageReference(expectedWarnings: new[]
            {
498
                // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
499 500 501
                Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1)
            });

J
Jared Parsons 已提交
502
            var comp2 = CreateEmptyCompilation("", new[] { reference });
503 504
            validate(comp2);
        }
505

506
        [Fact]
J
Jared Parsons 已提交
507
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
508 509
        public void AllSpecialTypes()
        {
J
Jared Parsons 已提交
510
            var comp = CreateEmptyCompilation("", new[] { MscorlibRef_v4_0_30316_17626 });
511 512 513 514 515

            for (var special = SpecialType.None + 1; special <= SpecialType.Count; special++)
            {
                var symbol = comp.GetSpecialType(special);
                Assert.NotNull(symbol);
516 517 518 519 520 521 522 523 524

                if (special == SpecialType.System_Runtime_CompilerServices_RuntimeFeature)
                {
                    Assert.Equal(SymbolKind.ErrorType, symbol.Kind); // Not available
                }
                else
                {
                    Assert.NotEqual(SymbolKind.ErrorType, symbol.Kind);
                }
525 526 527 528
            }
        }

        [Fact]
J
Jared Parsons 已提交
529
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
530 531
        public void AllSpecialTypeMembers()
        {
J
Jared Parsons 已提交
532
            var comp = CreateEmptyCompilation("", new[] { MscorlibRef_v4_0_30316_17626 });
533 534 535 536 537 538

            foreach (SpecialMember special in Enum.GetValues(typeof(SpecialMember)))
            {
                if (special == SpecialMember.Count) continue; // Not a real value;

                var symbol = comp.GetSpecialTypeMember(special);
539 540 541 542 543 544 545 546
                if (special == SpecialMember.System_Runtime_CompilerServices_RuntimeFeature__DefaultImplementationsOfInterfaces)
                {
                    Assert.Null(symbol); // Not available
                }
                else
                {
                    Assert.NotNull(symbol);
                }
547 548 549 550
            }
        }

        [Fact]
J
Jared Parsons 已提交
551
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
552
        public void AllWellKnownTypes()
553 554
        {
            var refs = new[]
555 556 557 558 559 560 561 562 563
            {
                MscorlibRef_v4_0_30316_17626,
                SystemRef_v4_0_30319_17929,
                SystemCoreRef_v4_0_30319_17929,
                MsvbRef_v4_0_30319_17929,
                CSharpRef,
                SystemXmlRef,
                SystemXmlLinqRef,
                SystemWindowsFormsRef,
564
                ValueTupleRef
565
            }.Concat(WinRtRefs).ToArray();
J
Jared Parsons 已提交
566
            var comp = CreateEmptyCompilation("", refs);
567

C
Charles Stoner 已提交
568
            for (var wkt = WellKnownType.First; wkt < WellKnownType.NextAvailable; wkt++)
569 570 571 572 573 574 575
            {
                switch (wkt)
                {
                    case WellKnownType.Microsoft_VisualBasic_Embedded:
                    case WellKnownType.Microsoft_VisualBasic_CompilerServices_EmbeddedOperators:
                        // Not applicable in C#.
                        continue;
576 577
                    case WellKnownType.System_FormattableString:
                    case WellKnownType.System_Runtime_CompilerServices_FormattableStringFactory:
578
                    case WellKnownType.System_Runtime_CompilerServices_NullableAttribute:
579
                    case WellKnownType.System_Runtime_CompilerServices_NullableContextAttribute:
580
                    case WellKnownType.System_Runtime_CompilerServices_NullablePublicOnlyAttribute:
581
                    case WellKnownType.System_Runtime_CompilerServices_IsReadOnlyAttribute:
V
vsadov 已提交
582
                    case WellKnownType.System_Runtime_CompilerServices_IsByRefLikeAttribute:
583
                    case WellKnownType.System_Span_T:
V
vsadov 已提交
584
                    case WellKnownType.System_ReadOnlySpan_T:
585
                    case WellKnownType.System_Runtime_CompilerServices_IsUnmanagedAttribute:
O
Omar Tawfik 已提交
586 587
                    case WellKnownType.System_Index:
                    case WellKnownType.System_Range:
588
                    case WellKnownType.System_Runtime_CompilerServices_AsyncIteratorStateMachineAttribute:
589
                    case WellKnownType.System_IAsyncDisposable:
590 591
                    case WellKnownType.System_Collections_Generic_IAsyncEnumerable_T:
                    case WellKnownType.System_Collections_Generic_IAsyncEnumerator_T:
592
                    case WellKnownType.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T:
593 594 595
                    case WellKnownType.System_Threading_Tasks_Sources_ValueTaskSourceStatus:
                    case WellKnownType.System_Threading_Tasks_Sources_ValueTaskSourceOnCompletedFlags:
                    case WellKnownType.System_Threading_Tasks_Sources_IValueTaskSource_T:
596
                    case WellKnownType.System_Threading_Tasks_Sources_IValueTaskSource:
597
                    case WellKnownType.System_Threading_Tasks_ValueTask_T:
598
                    case WellKnownType.System_Threading_Tasks_ValueTask:
599
                    case WellKnownType.System_Runtime_CompilerServices_AsyncIteratorMethodBuilder:
600
                    case WellKnownType.System_Threading_CancellationToken:
601
                    case WellKnownType.System_Runtime_CompilerServices_SwitchExpressionException:
602
                    case WellKnownType.System_Runtime_CompilerServices_NativeIntegerAttribute:
603 604
                        // Not yet in the platform.
                        continue;
J
John Hamby 已提交
605
                    case WellKnownType.Microsoft_CodeAnalysis_Runtime_Instrumentation:
606
                    case WellKnownType.System_Runtime_CompilerServices_ITuple:
607 608
                    case WellKnownType.System_Runtime_CompilerServices_NonNullTypesAttribute:
                    case WellKnownType.Microsoft_CodeAnalysis_EmbeddedAttribute:
J
John Hamby 已提交
609
                        // Not always available.
610
                        continue;
611 612 613
                    case WellKnownType.ExtSentinel:
                        // Not a real type
                        continue;
614 615
                }

616 617
                switch (wkt)
                {
618
                    case WellKnownType.System_ValueTuple:
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
                    case WellKnownType.System_ValueTuple_T1:
                    case WellKnownType.System_ValueTuple_T2:
                    case WellKnownType.System_ValueTuple_T3:
                    case WellKnownType.System_ValueTuple_T4:
                    case WellKnownType.System_ValueTuple_T5:
                    case WellKnownType.System_ValueTuple_T6:
                    case WellKnownType.System_ValueTuple_T7:
                    case WellKnownType.System_ValueTuple_TRest:
                        Assert.True(wkt.IsValueTupleType());
                        break;

                    default:
                        Assert.False(wkt.IsValueTupleType());
                        break;
                }

635 636
                var symbol = comp.GetWellKnownType(wkt);
                Assert.NotNull(symbol);
637
                Assert.True(symbol.Kind != SymbolKind.ErrorType, $"{wkt} should not be an error type");
638 639 640
            }
        }

641
        [Fact]
C
Charles Stoner 已提交
642
        public void AllWellKnownTypesBeforeCSharp7()
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
        {
            foreach (var type in new[] {
                            WellKnownType.System_Math,
                            WellKnownType.System_Array,
                            WellKnownType.System_Attribute,
                            WellKnownType.System_CLSCompliantAttribute,
                            WellKnownType.System_Convert,
                            WellKnownType.System_Exception,
                            WellKnownType.System_FlagsAttribute,
                            WellKnownType.System_FormattableString,
                            WellKnownType.System_Guid,
                            WellKnownType.System_IFormattable,
                            WellKnownType.System_RuntimeTypeHandle,
                            WellKnownType.System_RuntimeFieldHandle,
                            WellKnownType.System_RuntimeMethodHandle,
                            WellKnownType.System_MarshalByRefObject,
                            WellKnownType.System_Type,
                            WellKnownType.System_Reflection_AssemblyKeyFileAttribute,
                            WellKnownType.System_Reflection_AssemblyKeyNameAttribute,
                            WellKnownType.System_Reflection_MethodInfo,
                            WellKnownType.System_Reflection_ConstructorInfo,
                            WellKnownType.System_Reflection_MethodBase,
                            WellKnownType.System_Reflection_FieldInfo,
                            WellKnownType.System_Reflection_MemberInfo,
                            WellKnownType.System_Reflection_Missing,
                            WellKnownType.System_Runtime_CompilerServices_FormattableStringFactory,
                            WellKnownType.System_Runtime_CompilerServices_RuntimeHelpers,
                            WellKnownType.System_Runtime_ExceptionServices_ExceptionDispatchInfo,
                            WellKnownType.System_Runtime_InteropServices_StructLayoutAttribute,
                            WellKnownType.System_Runtime_InteropServices_UnknownWrapper,
                            WellKnownType.System_Runtime_InteropServices_DispatchWrapper,
                            WellKnownType.System_Runtime_InteropServices_CallingConvention,
                            WellKnownType.System_Runtime_InteropServices_ClassInterfaceAttribute,
                            WellKnownType.System_Runtime_InteropServices_ClassInterfaceType,
                            WellKnownType.System_Runtime_InteropServices_CoClassAttribute,
                            WellKnownType.System_Runtime_InteropServices_ComAwareEventInfo,
                            WellKnownType.System_Runtime_InteropServices_ComEventInterfaceAttribute,
                            WellKnownType.System_Runtime_InteropServices_ComInterfaceType,
                            WellKnownType.System_Runtime_InteropServices_ComSourceInterfacesAttribute,
                            WellKnownType.System_Runtime_InteropServices_ComVisibleAttribute,
                            WellKnownType.System_Runtime_InteropServices_DispIdAttribute,
                            WellKnownType.System_Runtime_InteropServices_GuidAttribute,
                            WellKnownType.System_Runtime_InteropServices_InterfaceTypeAttribute,
                            WellKnownType.System_Runtime_InteropServices_Marshal,
                            WellKnownType.System_Runtime_InteropServices_TypeIdentifierAttribute,
                            WellKnownType.System_Runtime_InteropServices_BestFitMappingAttribute,
                            WellKnownType.System_Runtime_InteropServices_DefaultParameterValueAttribute,
                            WellKnownType.System_Runtime_InteropServices_LCIDConversionAttribute,
                            WellKnownType.System_Runtime_InteropServices_UnmanagedFunctionPointerAttribute,
                            WellKnownType.System_Activator,
                            WellKnownType.System_Threading_Tasks_Task,
                            WellKnownType.System_Threading_Tasks_Task_T,
                            WellKnownType.System_Threading_Interlocked,
                            WellKnownType.System_Threading_Monitor,
                            WellKnownType.System_Threading_Thread,
                            WellKnownType.Microsoft_CSharp_RuntimeBinder_Binder,
                            WellKnownType.Microsoft_CSharp_RuntimeBinder_CSharpArgumentInfo,
                            WellKnownType.Microsoft_CSharp_RuntimeBinder_CSharpArgumentInfoFlags,
                            WellKnownType.Microsoft_CSharp_RuntimeBinder_CSharpBinderFlags,
                            WellKnownType.Microsoft_VisualBasic_CallType,
                            WellKnownType.Microsoft_VisualBasic_Embedded,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_Conversions,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_Operators,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_NewLateBinding,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_EmbeddedOperators,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_StandardModuleAttribute,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_Utils,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_LikeOperator,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_ProjectData,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_ObjectFlowControl,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_ObjectFlowControl_ForLoopControl,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_StaticLocalInitFlag,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_StringType,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_IncompleteInitialization,
                            WellKnownType.Microsoft_VisualBasic_CompilerServices_Versioned,
                            WellKnownType.Microsoft_VisualBasic_CompareMethod,
                            WellKnownType.Microsoft_VisualBasic_Strings,
                            WellKnownType.Microsoft_VisualBasic_ErrObject,
                            WellKnownType.Microsoft_VisualBasic_FileSystem,
                            WellKnownType.Microsoft_VisualBasic_ApplicationServices_ApplicationBase,
                            WellKnownType.Microsoft_VisualBasic_ApplicationServices_WindowsFormsApplicationBase,
                            WellKnownType.Microsoft_VisualBasic_Information,
                            WellKnownType.Microsoft_VisualBasic_Interaction,

                            WellKnownType.System_Func_T,
                            WellKnownType.System_Func_T2,
                            WellKnownType.System_Func_T3,
                            WellKnownType.System_Func_T4,
                            WellKnownType.System_Func_T5,
                            WellKnownType.System_Func_T6,
                            WellKnownType.System_Func_T7,
                            WellKnownType.System_Func_T8,
                            WellKnownType.System_Func_T9,
                            WellKnownType.System_Func_T10,
                            WellKnownType.System_Func_T11,
                            WellKnownType.System_Func_T12,
                            WellKnownType.System_Func_T13,
                            WellKnownType.System_Func_T14,
                            WellKnownType.System_Func_T15,
                            WellKnownType.System_Func_T16,
                            WellKnownType.System_Func_T17,

                            WellKnownType.System_Action,
                            WellKnownType.System_Action_T,
                            WellKnownType.System_Action_T2,
                            WellKnownType.System_Action_T3,
                            WellKnownType.System_Action_T4,
                            WellKnownType.System_Action_T5,
                            WellKnownType.System_Action_T6,
                            WellKnownType.System_Action_T7,
                            WellKnownType.System_Action_T8,
                            WellKnownType.System_Action_T9,
                            WellKnownType.System_Action_T10,
                            WellKnownType.System_Action_T11,
                            WellKnownType.System_Action_T12,
                            WellKnownType.System_Action_T13,
                            WellKnownType.System_Action_T14,
                            WellKnownType.System_Action_T15,
                            WellKnownType.System_Action_T16,

                            WellKnownType.System_AttributeUsageAttribute,
                            WellKnownType.System_ParamArrayAttribute,
                            WellKnownType.System_NonSerializedAttribute,
                            WellKnownType.System_STAThreadAttribute,
                            WellKnownType.System_Reflection_DefaultMemberAttribute,
                            WellKnownType.System_Runtime_CompilerServices_DateTimeConstantAttribute,
                            WellKnownType.System_Runtime_CompilerServices_DecimalConstantAttribute,
                            WellKnownType.System_Runtime_CompilerServices_IUnknownConstantAttribute,
                            WellKnownType.System_Runtime_CompilerServices_IDispatchConstantAttribute,
                            WellKnownType.System_Runtime_CompilerServices_ExtensionAttribute,
                            WellKnownType.System_Runtime_CompilerServices_INotifyCompletion,
                            WellKnownType.System_Runtime_CompilerServices_InternalsVisibleToAttribute,
                            WellKnownType.System_Runtime_CompilerServices_CompilerGeneratedAttribute,
                            WellKnownType.System_Runtime_CompilerServices_AccessedThroughPropertyAttribute,
                            WellKnownType.System_Runtime_CompilerServices_CompilationRelaxationsAttribute,
                            WellKnownType.System_Runtime_CompilerServices_RuntimeCompatibilityAttribute,
                            WellKnownType.System_Runtime_CompilerServices_UnsafeValueTypeAttribute,
                            WellKnownType.System_Runtime_CompilerServices_FixedBufferAttribute,
                            WellKnownType.System_Runtime_CompilerServices_DynamicAttribute,
                            WellKnownType.System_Runtime_CompilerServices_CallSiteBinder,
                            WellKnownType.System_Runtime_CompilerServices_CallSite,
                            WellKnownType.System_Runtime_CompilerServices_CallSite_T,

                            WellKnownType.System_Runtime_InteropServices_WindowsRuntime_EventRegistrationToken,
                            WellKnownType.System_Runtime_InteropServices_WindowsRuntime_EventRegistrationTokenTable_T,
                            WellKnownType.System_Runtime_InteropServices_WindowsRuntime_WindowsRuntimeMarshal,

                            WellKnownType.Windows_Foundation_IAsyncAction,
                            WellKnownType.Windows_Foundation_IAsyncActionWithProgress_T,
                            WellKnownType.Windows_Foundation_IAsyncOperation_T,
                            WellKnownType.Windows_Foundation_IAsyncOperationWithProgress_T2,

                            WellKnownType.System_Diagnostics_Debugger,
                            WellKnownType.System_Diagnostics_DebuggerDisplayAttribute,
                            WellKnownType.System_Diagnostics_DebuggerNonUserCodeAttribute,
                            WellKnownType.System_Diagnostics_DebuggerHiddenAttribute,
                            WellKnownType.System_Diagnostics_DebuggerBrowsableAttribute,
                            WellKnownType.System_Diagnostics_DebuggerStepThroughAttribute,
                            WellKnownType.System_Diagnostics_DebuggerBrowsableState,
                            WellKnownType.System_Diagnostics_DebuggableAttribute,
                            WellKnownType.System_Diagnostics_DebuggableAttribute__DebuggingModes,

                            WellKnownType.System_ComponentModel_DesignerSerializationVisibilityAttribute,

                            WellKnownType.System_IEquatable_T,

                            WellKnownType.System_Collections_IList,
                            WellKnownType.System_Collections_ICollection,
                            WellKnownType.System_Collections_Generic_EqualityComparer_T,
                            WellKnownType.System_Collections_Generic_List_T,
                            WellKnownType.System_Collections_Generic_IDictionary_KV,
                            WellKnownType.System_Collections_Generic_IReadOnlyDictionary_KV,
                            WellKnownType.System_Collections_ObjectModel_Collection_T,
                            WellKnownType.System_Collections_ObjectModel_ReadOnlyCollection_T,
                            WellKnownType.System_Collections_Specialized_INotifyCollectionChanged,
                            WellKnownType.System_ComponentModel_INotifyPropertyChanged,
                            WellKnownType.System_ComponentModel_EditorBrowsableAttribute,
                            WellKnownType.System_ComponentModel_EditorBrowsableState,

                            WellKnownType.System_Linq_Enumerable,
                            WellKnownType.System_Linq_Expressions_Expression,
                            WellKnownType.System_Linq_Expressions_Expression_T,
                            WellKnownType.System_Linq_Expressions_ParameterExpression,
                            WellKnownType.System_Linq_Expressions_ElementInit,
                            WellKnownType.System_Linq_Expressions_MemberBinding,
                            WellKnownType.System_Linq_Expressions_ExpressionType,
                            WellKnownType.System_Linq_IQueryable,
                            WellKnownType.System_Linq_IQueryable_T,

                            WellKnownType.System_Xml_Linq_Extensions,
                            WellKnownType.System_Xml_Linq_XAttribute,
                            WellKnownType.System_Xml_Linq_XCData,
                            WellKnownType.System_Xml_Linq_XComment,
                            WellKnownType.System_Xml_Linq_XContainer,
                            WellKnownType.System_Xml_Linq_XDeclaration,
                            WellKnownType.System_Xml_Linq_XDocument,
                            WellKnownType.System_Xml_Linq_XElement,
                            WellKnownType.System_Xml_Linq_XName,
                            WellKnownType.System_Xml_Linq_XNamespace,
                            WellKnownType.System_Xml_Linq_XObject,
                            WellKnownType.System_Xml_Linq_XProcessingInstruction,

                            WellKnownType.System_Security_UnverifiableCodeAttribute,
                            WellKnownType.System_Security_Permissions_SecurityAction,
                            WellKnownType.System_Security_Permissions_SecurityAttribute,
                            WellKnownType.System_Security_Permissions_SecurityPermissionAttribute,

                            WellKnownType.System_NotSupportedException,

                            WellKnownType.System_Runtime_CompilerServices_ICriticalNotifyCompletion,
                            WellKnownType.System_Runtime_CompilerServices_IAsyncStateMachine,
                            WellKnownType.System_Runtime_CompilerServices_AsyncVoidMethodBuilder,
                            WellKnownType.System_Runtime_CompilerServices_AsyncTaskMethodBuilder,
                            WellKnownType.System_Runtime_CompilerServices_AsyncTaskMethodBuilder_T,
                            WellKnownType.System_Runtime_CompilerServices_AsyncStateMachineAttribute,
                            WellKnownType.System_Runtime_CompilerServices_IteratorStateMachineAttribute,

                            WellKnownType.System_Windows_Forms_Form,
                            WellKnownType.System_Windows_Forms_Application,

                            WellKnownType.System_Environment,

                            WellKnownType.System_Runtime_GCLatencyMode,
                            WellKnownType.System_IFormatProvider }
                )
            {
                Assert.True(type <= WellKnownType.CSharp7Sentinel);
            }

            // There were 204 well-known types prior to CSharp7
            Assert.Equal(204, (int)(WellKnownType.CSharp7Sentinel - WellKnownType.First));
        }

876
        [Fact]
J
Jared Parsons 已提交
877
        [WorkItem(530436, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530436")]
878 879 880 881 882 883 884 885
        public void AllWellKnownTypeMembers()
        {
            var refs = new[]
            {
                MscorlibRef_v4_0_30316_17626,
                SystemRef_v4_0_30319_17929,
                SystemCoreRef_v4_0_30319_17929,
                MsvbRef_v4_0_30319_17929,
886
                CSharpDesktopRef,
887 888 889
                SystemXmlRef,
                SystemXmlLinqRef,
                SystemWindowsFormsRef,
890
                ValueTupleRef
891
            }.Concat(WinRtRefs).ToArray();
J
Jared Parsons 已提交
892
            var comp = CreateEmptyCompilation("", refs);
893 894 895

            foreach (WellKnownMember wkm in Enum.GetValues(typeof(WellKnownMember)))
            {
896
                switch (wkm)
897
                {
898
                    case WellKnownMember.Count:
899 900 901 902 903 904
                        // Not a real value;
                        continue;
                    case WellKnownMember.Microsoft_VisualBasic_Embedded__ctor:
                    case WellKnownMember.Microsoft_VisualBasic_CompilerServices_EmbeddedOperators__CompareStringStringStringBoolean:
                        // C# can't embed VB core.
                        continue;
905
                    case WellKnownMember.System_Array__Empty:
906
                    case WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorByte:
907
                    case WellKnownMember.System_Runtime_CompilerServices_NullableAttribute__ctorTransformFlags:
908
                    case WellKnownMember.System_Runtime_CompilerServices_NullableContextAttribute__ctor:
909
                    case WellKnownMember.System_Runtime_CompilerServices_NullablePublicOnlyAttribute__ctor:
910
                    case WellKnownMember.System_Span_T__ctor:
V
vsadov 已提交
911 912
                    case WellKnownMember.System_Span_T__get_Item:
                    case WellKnownMember.System_Span_T__get_Length:
913
                    case WellKnownMember.System_ReadOnlySpan_T__ctor:
V
vsadov 已提交
914 915
                    case WellKnownMember.System_ReadOnlySpan_T__get_Item:
                    case WellKnownMember.System_ReadOnlySpan_T__get_Length:
O
Omar Tawfik 已提交
916
                    case WellKnownMember.System_Index__ctor:
917
                    case WellKnownMember.System_Index__GetOffset:
918
                    case WellKnownMember.System_Range__ctor:
919 920 921
                    case WellKnownMember.System_Range__StartAt:
                    case WellKnownMember.System_Range__EndAt:
                    case WellKnownMember.System_Range__get_All:
922 923
                    case WellKnownMember.System_Range__get_Start:
                    case WellKnownMember.System_Range__get_End:
924
                    case WellKnownMember.System_Runtime_CompilerServices_RuntimeHelpers__GetSubArray_T:
925
                    case WellKnownMember.System_Runtime_CompilerServices_AsyncIteratorStateMachineAttribute__ctor:
926
                    case WellKnownMember.System_IAsyncDisposable__DisposeAsync:
927
                    case WellKnownMember.System_Collections_Generic_IAsyncEnumerable_T__GetAsyncEnumerator:
928 929
                    case WellKnownMember.System_Collections_Generic_IAsyncEnumerator_T__MoveNextAsync:
                    case WellKnownMember.System_Collections_Generic_IAsyncEnumerator_T__get_Current:
930 931 932 933 934 935 936
                    case WellKnownMember.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T__get_Version:
                    case WellKnownMember.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T__GetResult:
                    case WellKnownMember.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T__GetStatus:
                    case WellKnownMember.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T__OnCompleted:
                    case WellKnownMember.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T__Reset:
                    case WellKnownMember.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T__SetResult:
                    case WellKnownMember.System_Threading_Tasks_Sources_ManualResetValueTaskSourceCore_T__SetException:
937
                    case WellKnownMember.System_Threading_Tasks_Sources_IValueTaskSource_T__GetResult:
938 939
                    case WellKnownMember.System_Threading_Tasks_Sources_IValueTaskSource_T__GetStatus:
                    case WellKnownMember.System_Threading_Tasks_Sources_IValueTaskSource_T__OnCompleted:
940 941 942
                    case WellKnownMember.System_Threading_Tasks_Sources_IValueTaskSource__GetResult:
                    case WellKnownMember.System_Threading_Tasks_Sources_IValueTaskSource__GetStatus:
                    case WellKnownMember.System_Threading_Tasks_Sources_IValueTaskSource__OnCompleted:
943 944
                    case WellKnownMember.System_Threading_Tasks_ValueTask_T__ctorSourceAndToken:
                    case WellKnownMember.System_Threading_Tasks_ValueTask_T__ctorValue:
945
                    case WellKnownMember.System_Threading_Tasks_ValueTask__ctor:
946 947 948 949 950 951
                    case WellKnownMember.System_Runtime_CompilerServices_AsyncIteratorMethodBuilder__AwaitOnCompleted:
                    case WellKnownMember.System_Runtime_CompilerServices_AsyncIteratorMethodBuilder__AwaitUnsafeOnCompleted:
                    case WellKnownMember.System_Runtime_CompilerServices_AsyncIteratorMethodBuilder__Complete:
                    case WellKnownMember.System_Runtime_CompilerServices_AsyncIteratorMethodBuilder__Create:
                    case WellKnownMember.System_Runtime_CompilerServices_AsyncIteratorMethodBuilder__MoveNext_T:
                    case WellKnownMember.System_Runtime_CompilerServices_AsyncStateMachineAttribute__ctor:
952 953
                    case WellKnownMember.System_Runtime_CompilerServices_SwitchExpressionException__ctor:
                    case WellKnownMember.System_Runtime_CompilerServices_SwitchExpressionException__ctorObject:
954 955
                    case WellKnownMember.System_Runtime_CompilerServices_NativeIntegerAttribute__ctor:
                    case WellKnownMember.System_Runtime_CompilerServices_NativeIntegerAttribute__ctorTransformFlags:
956
                        // Not yet in the platform.
957
                        continue;
958 959
                    case WellKnownMember.Microsoft_CodeAnalysis_Runtime_Instrumentation__CreatePayloadForMethodsSpanningSingleFile:
                    case WellKnownMember.Microsoft_CodeAnalysis_Runtime_Instrumentation__CreatePayloadForMethodsSpanningMultipleFiles:
960
                    case WellKnownMember.System_Runtime_CompilerServices_IsReadOnlyAttribute__ctor:
V
vsadov 已提交
961
                    case WellKnownMember.System_Runtime_CompilerServices_IsByRefLikeAttribute__ctor:
962
                    case WellKnownMember.System_Runtime_CompilerServices_IsUnmanagedAttribute__ctor:
963 964
                    case WellKnownMember.System_Runtime_CompilerServices_ITuple__get_Item:
                    case WellKnownMember.System_Runtime_CompilerServices_ITuple__get_Length:
J
John Hamby 已提交
965 966
                        // Not always available.
                        continue;
967 968 969 970
                }
                if (wkm == WellKnownMember.Count) continue; // Not a real value.

                var symbol = comp.GetWellKnownTypeMember(wkm);
971
                Assert.True((object)symbol != null, $"Unexpected null for {wkm}");
972 973
            }
        }
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988

        [Fact, WorkItem(377890, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=377890")]
        public void System_IntPtr__op_Explicit_FromInt32()
        {
            string source = @"
using System;

public class MyClass
{
    static void Main()
    {
        ((IntPtr)0).GetHashCode();
    }
}
";
J
Jared Parsons 已提交
989
            var comp = CreateCompilation(source);
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
            comp.MakeMemberMissing(SpecialMember.System_IntPtr__op_Explicit_FromInt32);
            comp.VerifyEmitDiagnostics(
                // (8,10): error CS0656: Missing compiler required member 'System.IntPtr.op_Explicit'
                //         ((IntPtr)0).GetHashCode();
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "(IntPtr)0").WithArguments("System.IntPtr", "op_Explicit").WithLocation(8, 10)
                );
        }

        [Fact]
        public void System_Delegate__Combine()
        {
            var source =
@"
using System;
using System.Threading.Tasks;

namespace RoslynAsyncDelegate
{
    class Program
    {
        static EventHandler MyEvent;

        static void Main(string[] args)
        {
           MyEvent += async delegate { await Task.Delay(0); };
        }
    }
}

";
            var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe);
            compilation.MakeMemberMissing(SpecialMember.System_Delegate__Combine);
            compilation.VerifyEmitDiagnostics(
                // (13,12): error CS0656: Missing compiler required member 'System.Delegate.Combine'
                //            MyEvent += async delegate { await Task.Delay(0); };
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "MyEvent += async delegate { await Task.Delay(0); }").WithArguments("System.Delegate", "Combine").WithLocation(13, 12)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_01()
        {
            string source = @"
using System;

public struct S
{
    public static implicit operator int(S n) // 1 native compiler
    {
        Console.WriteLine(1);
        return 0;
    }

    public static implicit operator int?(S n) // 2 Roslyn compiler
    {
        Console.WriteLine(2);
        return null;
    }

    public static void Main()
    {
        int? qa = 5;
        S b = default(S);
        var sum = qa + b;
    }
}
";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (20,19): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         int? qa = 5;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "5").WithArguments("System.Nullable`1", ".ctor").WithLocation(20, 19),
                // (22,19): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         var sum = qa + b;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "qa + b").WithArguments("System.Nullable`1", ".ctor").WithLocation(22, 19)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_01()
        {
            string source = @"
using System;

public struct S
{
    public static implicit operator int(S n) // 1 native compiler
    {
        Console.WriteLine(1);
        return 0;
    }

    public static implicit operator int?(S n) // 2 Roslyn compiler
    {
        Console.WriteLine(2);
        return null;
    }

    public static void Main()
    {
        int? qa = 5;
        S b = default(S);
        var sum = qa + b;
    }
}
";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (22,19): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         var sum = qa + b;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "qa + b").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(22, 19),
                // (22,19): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         var sum = qa + b;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "qa + b").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(22, 19)
                );
        }

        [Fact]
        public void System_Nullable_T_get_HasValue_01()
        {
            string source = @"
using System;

public struct S
{
    public static implicit operator int(S n) // 1 native compiler
    {
        Console.WriteLine(1);
        return 0;
    }

    public static implicit operator int?(S n) // 2 Roslyn compiler
    {
        Console.WriteLine(2);
        return null;
    }

    public static void Main()
    {
        int? qa = 5;
        S b = default(S);
        var sum = qa + b;
    }
}
";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_get_HasValue);
            compilation.VerifyEmitDiagnostics(
                // (22,19): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //         var sum = qa + b;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "qa + b").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(22, 19),
                // (22,19): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //         var sum = qa + b;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "qa + b").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(22, 19)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_02()
        {
            string source = @"
using System;
namespace Test
{
    static class Program
    {
        static void Main()
        {
            int? i = 123;
            C c = (C)i;
        }
    }

    public class C
    {
        public readonly int v;
        public C(int v) { this.v = v; }
        public static implicit operator C(int v)
        {
            Console.Write(v);
            return new C(v);
        }
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (10,19): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //             C c = (C)i;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "(C)i").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(10, 19)
                );
        }

        [Fact]
        public void System_Nullable_T_get_Value()
        {
            var source =
@"
using System;

class C
{
    static void Test()
    {
        byte? b = 0;
        IntPtr p = (IntPtr)b;
        Console.WriteLine(p);
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_get_Value);
            compilation.VerifyEmitDiagnostics(
                // (9,28): error CS0656: Missing compiler required member 'System.Nullable`1.get_Value'
                //         IntPtr p = (IntPtr)b;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "b").WithArguments("System.Nullable`1", "get_Value").WithLocation(9, 28)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_02()
        {
            var source =
@"
using System;

class C
{
    static void Main()
    {
        Console.WriteLine((IntPtr?)M_int());
        Console.WriteLine((IntPtr?)M_int(42));
        Console.WriteLine((IntPtr?)M_long());
        Console.WriteLine((IntPtr?)M_long(300));
    }

    static int? M_int(int? p = null) { return p; } 
    static long? M_long(long? p = null) { return p; } 
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (8,27): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Console.WriteLine((IntPtr?)M_int());
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "(IntPtr?)M_int()").WithArguments("System.Nullable`1", ".ctor").WithLocation(8, 27),
                // (9,42): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Console.WriteLine((IntPtr?)M_int(42));
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "42").WithArguments("System.Nullable`1", ".ctor").WithLocation(9, 42),
                // (9,27): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Console.WriteLine((IntPtr?)M_int(42));
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "(IntPtr?)M_int(42)").WithArguments("System.Nullable`1", ".ctor").WithLocation(9, 27),
                // (10,27): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Console.WriteLine((IntPtr?)M_long());
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "(IntPtr?)M_long()").WithArguments("System.Nullable`1", ".ctor").WithLocation(10, 27),
                // (11,43): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Console.WriteLine((IntPtr?)M_long(300));
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "300").WithArguments("System.Nullable`1", ".ctor").WithLocation(11, 43),
                // (11,27): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Console.WriteLine((IntPtr?)M_long(300));
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "(IntPtr?)M_long(300)").WithArguments("System.Nullable`1", ".ctor").WithLocation(11, 27)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_03()
        {
            var source =
@"

using System;

class Class1
{
    static void Main()
    {
        MyClass b = (int?)1;
    }
}

class MyClass
{
    public static implicit operator MyClass(decimal Value)
    {
        return new MyClass();
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (9,21): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         MyClass b = (int?)1;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "(int?)1").WithArguments("System.Nullable`1", ".ctor").WithLocation(9, 21)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_04()
        {
            var source1 = @"
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public static class Test
{
    public static void Generic<T>([Optional][DecimalConstant(0, 0, 0, 0, 50)] T x)
    {
        Console.WriteLine(x == null ? ""null"" : x.ToString());
    }

    public static void Decimal([Optional][DecimalConstant(0, 0, 0, 0, 50)] Decimal x)
    {
        Console.WriteLine(x.ToString());
    }

    public static void NullableDecimal([Optional][DecimalConstant(0, 0, 0, 0, 50)] Decimal? x)
    {
        Console.WriteLine(x == null ? ""null"" : x.ToString());
    }

    public static void Object([Optional][DecimalConstant(0, 0, 0, 0, 50)] object x)
    {
        Console.WriteLine(x == null ? ""null"" : x.ToString());
    }

    public static void String([Optional][DecimalConstant(0, 0, 0, 0, 50)] string x)
    {
        Console.WriteLine(x == null ? ""null"" : x.ToString());
    }

    public static void Int32([Optional][DecimalConstant(0, 0, 0, 0, 50)] int x)
    {
        Console.WriteLine(x.ToString());
    }

    public static void IComparable([Optional][DecimalConstant(0, 0, 0, 0, 50)] IComparable x)
    {
        Console.WriteLine(x == null ? ""null"" : x.ToString());
    }

    public static void ValueType([Optional][DecimalConstant(0, 0, 0, 0, 50)] ValueType x)
    {
        Console.WriteLine(x == null ? ""null"" : x.ToString());
    }
}
";

            var source2 = @"
class Program
{
    public static void Main()
    {
        // Respects default value
        Test.Generic<decimal>();    
        Test.Generic<decimal?>();   
        Test.Generic<object>();             
        Test.Decimal();                    
        Test.NullableDecimal();            
        Test.Object();                      
        Test.IComparable();                 
        Test.ValueType();                   
        Test.Int32();                       

        // Null, since not convertible
        Test.Generic<string>();             
        Test.String();                      
    }
}
";

            var compilation = CreateCompilationWithMscorlib45(source1 + source2);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (55,9): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Test.Generic<decimal?>();   
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "Test.Generic<decimal?>()").WithArguments("System.Nullable`1", ".ctor").WithLocation(55, 9),
                // (58,9): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         Test.NullableDecimal();            
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "Test.NullableDecimal()").WithArguments("System.Nullable`1", ".ctor").WithLocation(58, 9)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_04()
        {
            var source =
@"

using System;

class Class1
{
    static void Main()
    {
        int? a = 1;
        a.ToString();
        MyClass b = a;
        b.ToString();
    }
}

class MyClass
{
    public static implicit operator MyClass(decimal Value)
    {
        return new MyClass();
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (11,21): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         MyClass b = a;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "a").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(11, 21),
                // (11,21): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         MyClass b = a;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "a").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(11, 21)
                );
        }

        [Fact]
        public void System_Nullable_T_get_HasValue_02()
        {
            var source =
@"

using System;

class Class1
{
    static void Main()
    {
        int? a = 1;
        a.ToString();
        MyClass b = a;
        b.ToString();
    }
}

class MyClass
{
    public static implicit operator MyClass(decimal Value)
    {
        Console.WriteLine(""Value is: "" + Value);
        return new MyClass();
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_get_HasValue);
            compilation.VerifyEmitDiagnostics(
                // (11,21): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //         MyClass b = a;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "a").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(11, 21)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_03()
        {
            string source = @"using System;

namespace Test
{
    static class Program
    {
        static void Main()
        {
            S.v = 0;
            S? S2 = 123;                  // not lifted, int=>int?, int?=>S, S=>S?
            Console.WriteLine(S.v == 123);
        }
    }

    public struct S
    {
        public static int v;
        // s == null, return v = -1
        public static implicit operator S(int? s)
        {
            Console.Write(""Imp S::int? -> S "");
            S ss = new S();
            S.v = s ?? -1;
            return ss;
        }
    }
}
";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (23,19): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //             S.v = s ?? -1;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "s").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(23, 19)
                );
        }

1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
        [Fact]
        public void System_String__ConcatObjectObject()
        {
            var source =
@"
using System;
using System.Linq.Expressions;

class Class1
{
    static void Main()
    {
        Expression<Func<object, string>> e = x => ""X = "" + x;
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef });
            compilation.MakeMemberMissing(SpecialMember.System_String__ConcatObjectObject);
            compilation.VerifyEmitDiagnostics(
                // (9,51): error CS0656: Missing compiler required member 'System.String.Concat'
                //         Expression<Func<object, string>> e = x => "X = " + x;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"""X = "" + x").WithArguments("System.String", "Concat").WithLocation(9, 51)
                );
        }

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
        [Fact]
        public void System_String__ConcatStringStringString()
        {
            string source = @"
using System;
struct S
{
    private string str;
    public S(char chr) { this.str = chr.ToString(); }
    public S(string str) { this.str = str; }
1531
    public static S operator + (S x, S y) { return new S(x.str + '+' + y.str); }
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
}

class C
{
    static void Main()
    {
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_String__ConcatStringStringString);
            compilation.VerifyEmitDiagnostics(
                // (8,58): error CS0656: Missing compiler required member 'System.String.Concat'
1544 1545
                //     public static S operator + (S x, S y) { return new S(x.str + '+' + y.str); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "x.str + '+' + y.str").WithArguments("System.String", "Concat").WithLocation(8, 58)
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
                );
        }

        [Fact]
        public void System_String__ConcatStringStringStringString()
        {
            string source = @"
using System;
struct S
{
    private string str;
    public S(char chr) { this.str = chr.ToString(); }
    public S(string str) { this.str = str; }
    public static S operator + (S x, S y) { return new S('(' + x.str + '+' + y.str + ')'); }
}

class C
{
    static void Main()
    {
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_String__ConcatStringStringStringString);
            compilation.VerifyEmitDiagnostics(
                // (8,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator + (S x, S y) { return new S('(' + x.str + '+' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '+' + y.str").WithArguments("System.String", "Concat").WithLocation(8, 58)
                );
        }

        [Fact]
        public void System_String__ConcatStringArray()
        {
            string source = @"
using System;
struct S
{
    private string str;
    public S(char chr) { this.str = chr.ToString(); }
    public S(string str) { this.str = str; }
    public static S operator + (S x, S y) { return new S('(' + x.str + '+' + y.str + ')'); }
    public static S operator - (S x, S y) { return new S('(' + x.str + '-' + y.str + ')'); }
    public static S operator % (S x, S y) { return new S('(' + x.str + '%' + y.str + ')'); }
    public static S operator / (S x, S y) { return new S('(' + x.str + '/' + y.str + ')'); }
    public static S operator * (S x, S y) { return new S('(' + x.str + '*' + y.str + ')'); }
    public static S operator & (S x, S y) { return new S('(' + x.str + '&' + y.str + ')'); }
    public static S operator | (S x, S y) { return new S('(' + x.str + '|' + y.str + ')'); }
    public static S operator ^ (S x, S y) { return new S('(' + x.str + '^' + y.str + ')'); }
    public static S operator << (S x, int y) { return new S('(' + x.str + '<' + '<' + y.ToString() + ')'); }
    public static S operator >> (S x, int y) { return new S('(' + x.str + '>' + '>' + y.ToString() + ')'); }
    public static S operator >= (S x, S y) { return new S('(' + x.str + '>' + '=' + y.str + ')'); }
    public static S operator <= (S x, S y) { return new S('(' + x.str + '<' + '=' + y.str + ')'); }
    public static S operator > (S x, S y) { return new S('(' + x.str + '>' + y.str + ')'); }
    public static S operator < (S x, S y) { return new S('(' + x.str + '<' + y.str + ')'); }
    public override string ToString() { return this.str; }
}

class C
{
    static void Main()
    {
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_String__ConcatStringArray);
            compilation.VerifyEmitDiagnostics(
                // (8,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator + (S x, S y) { return new S('(' + x.str + '+' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '+' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(8, 58),
                // (9,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator - (S x, S y) { return new S('(' + x.str + '-' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '-' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(9, 58),
                // (10,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator % (S x, S y) { return new S('(' + x.str + '%' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '%' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(10, 58),
                // (11,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator / (S x, S y) { return new S('(' + x.str + '/' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '/' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(11, 58),
                // (12,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator * (S x, S y) { return new S('(' + x.str + '*' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '*' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(12, 58),
                // (13,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator & (S x, S y) { return new S('(' + x.str + '&' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '&' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(13, 58),
                // (14,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator | (S x, S y) { return new S('(' + x.str + '|' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '|' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(14, 58),
                // (15,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator ^ (S x, S y) { return new S('(' + x.str + '^' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '^' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(15, 58),
                // (16,61): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator << (S x, int y) { return new S('(' + x.str + '<' + '<' + y.ToString() + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '<' + '<' + y.ToString() + ')'").WithArguments("System.String", "Concat").WithLocation(16, 61),
                // (17,61): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator >> (S x, int y) { return new S('(' + x.str + '>' + '>' + y.ToString() + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '>' + '>' + y.ToString() + ')'").WithArguments("System.String", "Concat").WithLocation(17, 61),
                // (18,59): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator >= (S x, S y) { return new S('(' + x.str + '>' + '=' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '>' + '=' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(18, 59),
                // (19,59): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator <= (S x, S y) { return new S('(' + x.str + '<' + '=' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '<' + '=' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(19, 59),
                // (20,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator > (S x, S y) { return new S('(' + x.str + '>' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '>' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(20, 58),
                // (21,58): error CS0656: Missing compiler required member 'System.String.Concat'
                //     public static S operator < (S x, S y) { return new S('(' + x.str + '<' + y.str + ')'); }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "'(' + x.str + '<' + y.str + ')'").WithArguments("System.String", "Concat").WithLocation(21, 58)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_05()
        {
            string source =
@"
struct S
{
    public static int operator +(S s) { return 1; }
    public static void Main()
    {
        S s = new S();
        S? sq = s;
        var j = +sq;
        System.Console.WriteLine(j);
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (9,17): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         var j = +sq;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "+sq").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(9, 17)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_05()
        {
            string source =
@"
struct S
{
    public static int operator +(S s) { return 1; }
    public static void Main()
    {
        S s = new S();
        S? sq = s;
        var j = +sq;
        System.Console.WriteLine(j);
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (8,17): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         S? sq = s;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "s").WithArguments("System.Nullable`1", ".ctor").WithLocation(8, 17),
                // (9,17): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         var j = +sq;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "+sq").WithArguments("System.Nullable`1", ".ctor").WithLocation(9, 17)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_06()
        {
            string source =
@"
class C
{
  public readonly int? i;
  public C(int? i) { this.i = i; }
  public static implicit operator int?(C c) { return c.i; }
  public static implicit operator C(int? s) { return new C(s); }
  static void Main()
  {
    C c = new C(null);
    c++;
    System.Console.WriteLine(object.ReferenceEquals(c, null) ? 1 : 0);
  }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (11,5): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //     c++;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "c++").WithArguments("System.Nullable`1", ".ctor").WithLocation(11, 5)
                );
        }

        [Fact]
        public void System_Decimal__op_Multiply()
        {
            string source = @"
using System;
class Program
{       
    static void Main()
    {
        Func<decimal?, decimal?> lambda = a => { return checked(a * a); };
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Decimal__op_Multiply);
            compilation.VerifyEmitDiagnostics(
                // (7,65): error CS0656: Missing compiler required member 'System.Decimal.op_Multiply'
                //         Func<decimal?, decimal?> lambda = a => { return checked(a * a); };
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "a * a").WithArguments("System.Decimal", "op_Multiply").WithLocation(7, 65)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_06()
        {
            string source = @"
using System;

struct S : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine(123);
    }

    static void Main()
    {
        using (S? r = new S())
        {
            Console.Write(r);
        }
    }
}
";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (13,9): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         using (S? r = new S())
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"using (S? r = new S())
        {
            Console.Write(r);
        }").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(13, 9)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_07()
        {
            string source = @"
using System;
class C
{
  static void Main()
  {
    decimal q = 10;
    decimal? x = 10;

    T(2, (x++).Value == (q++));
  }

  static void T(int line, bool b)
  {
  }
}";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (10,11): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //     T(2, (x++).Value == (q++));
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "x++").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(10, 11)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_07()
        {
            string source = @"
using System;
class C
{
  static void Main()
  {
    decimal q = 10;
    decimal? x = 10;

    T(2, (x++).Value == (q++));
  }

  static void T(int line, bool b)
  {
  }
}";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (8,18): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //     decimal? x = 10;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "10").WithArguments("System.Nullable`1", ".ctor").WithLocation(8, 18),
                // (10,11): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //     T(2, (x++).Value == (q++));
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "x++").WithArguments("System.Nullable`1", ".ctor").WithLocation(10, 11),
                // (10,11): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //     T(2, (x++).Value == (q++));
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "x++").WithArguments("System.Nullable`1", ".ctor").WithLocation(10, 11)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_08()
        {
            string source = @"
using System;
struct S
{
  public int x;
  public S(int x) { this.x = x; }
  public static S operator ++(S s) { return new S(s.x + 1); }
  public static S operator --(S s) { return new S(s.x - 1); }
}

class C
{
  static void Main()
  {
    S? n = new S(1);
    S s = new S(1);

    T(2, (n++).Value.x == (s++).x);
  }

  static void T(int line, bool b)
  {
  }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (18,11): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //     T(2, (n++).Value.x == (s++).x);
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "n++").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(18, 11)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_08()
        {
            string source = @"
using System;
struct S
{
  public int x;
  public S(int x) { this.x = x; }
  public static S operator ++(S s) { return new S(s.x + 1); }
  public static S operator --(S s) { return new S(s.x - 1); }
}

class C
{
  static void Main()
  {
    S? n = new S(1);
    S s = new S(1);

    T(2, (n++).Value.x == (s++).x);
  }

  static void T(int line, bool b)
  {
  }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (15,12): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //     S? n = new S(1);
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "new S(1)").WithArguments("System.Nullable`1", ".ctor").WithLocation(15, 12),
                // (18,11): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //     T(2, (n++).Value.x == (s++).x);
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "n++").WithArguments("System.Nullable`1", ".ctor").WithLocation(18, 11)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_09()
        {
            string source = @"
using System;
class C
{
    
    static void T(int x, bool? b) {}

    static void Main()
    {
        bool bt = true;
        bool? bnt = bt;

        T(1, true & bnt);
    }
}";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (11,21): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         bool? bnt = bt;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "bt").WithArguments("System.Nullable`1", ".ctor").WithLocation(11, 21),
                // (13,14): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         T(1, true & bnt);
1964
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "true").WithArguments("System.Nullable`1", ".ctor").WithLocation(13, 14)
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_09()
        {
            string source = @"
using System;
class C
{
    
    static void T(int x, bool? b) {}

    static void Main()
    {
        bool bt = true;
        bool? bnt = bt;

        T(13, bnt & bnt);
    }
}";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (13,15): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         T(13, bnt & bnt);
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "bnt & bnt").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(13, 15),
                // (13,15): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         T(13, bnt & bnt);
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "bnt & bnt").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(13, 15)
                );
        }

        [Fact]
        public void System_String__op_Equality_01()
        {
            string source = @"
using System;
struct SZ
{
    public string str;
    public SZ(string str) { this.str = str; }
    public SZ(char c) { this.str = c.ToString(); }
    public static bool operator ==(SZ sz1, SZ sz2) { return sz1.str == sz2.str; }
    public static bool operator !=(SZ sz1, SZ sz2) { return sz1.str != sz2.str; }
    public override bool Equals(object x) { return true; }
    public override int GetHashCode() { return 0; }
}
class C
{
    static void Main()
    {
    }
}
";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_String__op_Equality);
            compilation.VerifyEmitDiagnostics(
                // (8,61): error CS0656: Missing compiler required member 'System.String.op_Equality'
                //     public static bool operator ==(SZ sz1, SZ sz2) { return sz1.str == sz2.str; }
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "sz1.str == sz2.str").WithArguments("System.String", "op_Equality").WithLocation(8, 61)
                );
        }

        [Fact]
        public void System_Nullable_T_get_HasValue_03()
        {
            var source = @"
using System;

static class LiveList
{
    struct WhereInfo<TSource>
    {
        public int Key { get; set; }
    }

    static void Where<TSource>()
    {
        Action subscribe = () =>
        {
            WhereInfo<TSource>? previous = null;

            var previousKey = previous?.Key;
        };
    }
}";

            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_get_HasValue);
            compilation.VerifyEmitDiagnostics(
                // (17,31): error CS0656: Missing compiler required member 'System.Nullable`1.get_HasValue'
                //             var previousKey = previous?.Key;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "previous?.Key").WithArguments("System.Nullable`1", "get_HasValue").WithLocation(17, 31)
                );
        }

2064
        [Fact]
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
        public void System_Nullable_T_GetValueOrDefault_10()
        {
            var source =
@"using System;
public class X
{
    public static void Main()
    {
        var s = nameof(Main);
        if (s is string t) Console.WriteLine(""1. {0}"", t);
        s = null;
        Console.WriteLine(""2. {0}"", s is string w ? w : nameof(X));
        int? x = 12;
        {if (x is var y) Console.WriteLine(""3. {0}"", y);}
        {if (x is int y) Console.WriteLine(""4. {0}"", y);}
        x = null;
        {if (x is var y) Console.WriteLine(""5. {0}"", y);}
        {if (x is int y) Console.WriteLine(""6. {0}"", y);}
        Console.WriteLine(""7. {0}"", (x is bool is bool));
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (16,38): warning CS0184: The given expression is never of the provided ('bool') type
                //         Console.WriteLine("7. {0}", (x is bool is bool));
                Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "x is bool").WithArguments("bool").WithLocation(16, 38),
                // (16,38): warning CS0183: The given expression is always of the provided ('bool') type
                //         Console.WriteLine("7. {0}", (x is bool is bool));
                Diagnostic(ErrorCode.WRN_IsAlwaysTrue, "x is bool is bool").WithArguments("bool").WithLocation(16, 38),
                // (12,19): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         {if (x is int y) Console.WriteLine("4. {0}", y);}
2097 2098 2099 2100
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int y").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(12, 19),
                // (15,19): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //         {if (x is int y) Console.WriteLine("6. {0}", y);}
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "int y").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(15, 19)
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
                );
        }

        [Fact]
        public void System_String__op_Equality_02()
        {
            var source =
@"
using System;
public class X
{
    public static void Main()
    {
    }

    public static void M(object o)
    {
        switch (o)
        {
            case ""hmm"":
                Console.WriteLine(""hmm""); break;
            case null:
                Console.WriteLine(""null""); break;
            case 1:
                Console.WriteLine(""int 1""); break;
            case ((byte)1):
                Console.WriteLine(""byte 1""); break;
            case ((short)1):
                Console.WriteLine(""short 1""); break;
            case ""bar"":
                Console.WriteLine(""bar""); break;
            case object t when t != o:
                Console.WriteLine(""impossible""); break;
            case 2:
                Console.WriteLine(""int 2""); break;
            case ((byte)2):
                Console.WriteLine(""byte 2""); break;
            case ((short)2):
                Console.WriteLine(""short 2""); break;
            case ""baz"":
                Console.WriteLine(""baz""); break;
            default:
                Console.WriteLine(""other "" + o); break;
        }
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_String__op_Equality);
            compilation.VerifyEmitDiagnostics(
2151 2152 2153
                // (13,13): error CS0656: Missing compiler required member 'System.String.op_Equality'
                //             case "hmm":
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"case ""hmm"":").WithArguments("System.String", "op_Equality").WithLocation(13, 13),
2154 2155 2156
                // (33,13): error CS0656: Missing compiler required member 'System.String.op_Equality'
                //             case "baz":
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"case ""baz"":").WithArguments("System.String", "op_Equality").WithLocation(33, 13)
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
                );
        }

        [Fact]
        public void System_String__Chars()
        {
            var source =
@"using System;

class Program
{
    public static void Main(string[] args)
    {
        bool hasB = false;
        foreach (var c in ""ab"")
        {
           switch (c)
           {
              case char b when IsB(b):
                 hasB = true;
                 break;

              default:
                 hasB = false;
                 break;
           }
        }
        Console.WriteLine(hasB);
    }

    public static bool IsB(char value)
    {
        return value == 'b';
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_String__Chars);
            compilation.VerifyEmitDiagnostics(
                // (8,9): error CS0656: Missing compiler required member 'System.String.get_Chars'
                //         foreach (var c in "ab")
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"foreach (var c in ""ab"")
        {
           switch (c)
           {
              case char b when IsB(b):
                 hasB = true;
                 break;

              default:
                 hasB = false;
                 break;
           }
        }").WithArguments("System.String", "get_Chars").WithLocation(8, 9)
                );
        }

        [Fact]
        public void System_Nullable_T_GetValueOrDefault_11()
        {
            var source =
@"using System;
class Program
{
  static void Main(string[] args)
  {
  }
  static void M(X? x)
  {
    switch (x)
    {
      case null:
        Console.WriteLine(""null"");
        break;
      case 1:
        Console.WriteLine(1);
        break;
    }
  }
}
struct X
{
    public static implicit operator int? (X x)
    {
        return 1;
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T_GetValueOrDefault);
            compilation.VerifyEmitDiagnostics(
                // (9,13): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //     switch (x)
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "x").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(9, 13),
2250 2251 2252
                // (14,7): error CS0656: Missing compiler required member 'System.Nullable`1.GetValueOrDefault'
                //       case 1:
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "case 1:").WithArguments("System.Nullable`1", "GetValueOrDefault").WithLocation(14, 7)
2253 2254 2255
                );
        }

2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
        [Fact]
        public void System_String__ConcatObject()
        {
            // It isn't possible to trigger this diagnostic, as we don't use String.Concat(object)

            var source = @"
using System;
public class Test
{
    private static string S = ""F"";
    private static object O = ""O"";
    static void Main()
    {
        Console.WriteLine(O + null);
        Console.WriteLine(S + null);
    }
}
    ";
            var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe);
            compilation.MakeMemberMissing(SpecialMember.System_String__ConcatObject);
            compilation.VerifyEmitDiagnostics(); // We don't expect any
            CompileAndVerify(compilation, expectedOutput: @"O
F");
        }

2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
        [Fact]
        public void System_Object__ToString()
        {
            var source = @"
using System;

public class Test
{
    static void Main()
    {
        char c = 'c';
        Console.WriteLine(c + ""3"");
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Object__ToString);
            compilation.VerifyEmitDiagnostics(
                // (9,27): error CS0656: Missing compiler required member 'System.Object.ToString'
                //         Console.WriteLine(c + "3");
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"c + ""3""").WithArguments("System.Object", "ToString").WithLocation(9, 27)
                );
        }

        [Fact]
        public void System_String__ConcatStringString()
        {
            var source = @"
using System;
using System.Linq;
using System.Linq.Expressions;

class Test
{
    public static void Main()
    {
        Expression<Func<string, string, string>> testExpr = (x, y) => x + y;
        var result = testExpr.Compile()(""Hello "", ""World!"");
        Console.WriteLine(result);
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef });
            compilation.MakeMemberMissing(SpecialMember.System_String__ConcatStringString);
            compilation.VerifyEmitDiagnostics(
                // (10,71): error CS0656: Missing compiler required member 'System.String.Concat'
                //         Expression<Func<string, string, string>> testExpr = (x, y) => x + y;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "x + y").WithArguments("System.String", "Concat").WithLocation(10, 71)
                );
        }

        [Fact]
        public void System_Array__GetLowerBound()
        {
            var source = @"
class C
{
    static void Main()
    {
        double[,] values = {
            { 1.2, 2.3, 3.4, 4.5 },
            { 5.6, 6.7, 7.8, 8.9 },
        };

        foreach (var x in values)
        {
            System.Console.WriteLine(x);
        }
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Array__GetLowerBound);
            compilation.VerifyEmitDiagnostics(
                // (11,9): error CS0656: Missing compiler required member 'System.Array.GetLowerBound'
                //         foreach (var x in values)
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"foreach (var x in values)
        {
            System.Console.WriteLine(x);
        }").WithArguments("System.Array", "GetLowerBound").WithLocation(11, 9)
                );
        }

        [Fact]
        public void System_Array__GetUpperBound()
        {
            var source = @"
class C
{
    static void Main()
    {
        double[,] values = {
            { 1.2, 2.3, 3.4, 4.5 },
            { 5.6, 6.7, 7.8, 8.9 },
        };

        foreach (var x in values)
        {
            System.Console.WriteLine(x);
        }
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source);
            compilation.MakeMemberMissing(SpecialMember.System_Array__GetUpperBound);
            compilation.VerifyEmitDiagnostics(
                // (11,9): error CS0656: Missing compiler required member 'System.Array.GetUpperBound'
                //         foreach (var x in values)
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, @"foreach (var x in values)
        {
            System.Console.WriteLine(x);
        }").WithArguments("System.Array", "GetUpperBound").WithLocation(11, 9)
                );
        }

        [Fact]
        public void System_Decimal__op_Implicit_FromInt32()
        {
            var source =
@"using System;
using System.Linq.Expressions;

public struct SampStruct
{
    public static implicit operator int(SampStruct ss1)
    {
        return 1;
    }
}

public class Test
{
    static void Main()
    {
        Expression<Func<SampStruct?, decimal, decimal>> testExpr = (x, y) => x ?? y;
    }
}";
            var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef });
            compilation.MakeMemberMissing(SpecialMember.System_Decimal__op_Implicit_FromInt32);
            compilation.VerifyEmitDiagnostics(
                // (16,78): error CS0656: Missing compiler required member 'System.Decimal.op_Implicit'
                //         Expression<Func<SampStruct?, decimal, decimal>> testExpr = (x, y) => x ?? y;
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "x ?? y").WithArguments("System.Decimal", "op_Implicit").WithLocation(16, 78)
                );
        }

        [Fact]
        public void System_Nullable_T__ctor_10()
        {
            string source = @"
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System;

class Test {
    static void LogCallerLineNumber5([CallerLineNumber] int? lineNumber   = 5) { Console.WriteLine(""line: "" + lineNumber); }

    public static void Main() {
        LogCallerLineNumber5();
    }
}";

            var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemRef });
            compilation.MakeMemberMissing(SpecialMember.System_Nullable_T__ctor);
            compilation.VerifyEmitDiagnostics(
                // (10,9): error CS0656: Missing compiler required member 'System.Nullable`1..ctor'
                //         LogCallerLineNumber5();
                Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "LogCallerLineNumber5()").WithArguments("System.Nullable`1", ".ctor").WithLocation(10, 9)
                );
        }
P
Pilchie 已提交
2449
    }
2450
}