forked from dotnet/roslyn
/
LocalFunctionTests.cs
3141 lines (2922 loc) · 116 KB
/
LocalFunctionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
66
67
68
69
70
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
168
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
310
311
312
313
314
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
345
346
347
348
349
350
351
352
353
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
389
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
419
420
421
422
423
424
425
426
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using System.Linq;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
public class LocalFunctionsTestBase : CSharpTestBase
{
internal static readonly CSharpParseOptions DefaultParseOptions = TestOptions.Regular;
internal void VerifyDiagnostics(string source, params DiagnosticDescription[] expected)
{
var comp = CreateCompilationWithMscorlib45AndCSruntime(source, options: TestOptions.ReleaseDll, parseOptions: DefaultParseOptions);
comp.VerifyDiagnostics(expected);
}
internal void VerifyDiagnostics(string source, CSharpCompilationOptions options, params DiagnosticDescription[] expected)
{
var comp = CreateCompilationWithMscorlib45AndCSruntime(source, options: options, parseOptions: DefaultParseOptions);
comp.VerifyDiagnostics(expected);
}
}
[CompilerTrait(CompilerFeature.LocalFunctions)]
public class LocalFunctionTests : LocalFunctionsTestBase
{
[Fact]
[WorkItem(17014, "https://github.com/dotnet/roslyn/pull/17014")]
public void RecursiveLocalFuncsAsParameterTypes()
{
var comp = CreateStandardCompilation(@"
class C
{
void M()
{
int L(L2 l2) => 0;
int L2(L l1) => 0;
}
}");
comp.VerifyDiagnostics(
// (6,15): error CS0246: The type or namespace name 'L2' could not be found (are you missing a using directive or an assembly reference?)
// int L(L2 l2) => 0;
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "L2").WithArguments("L2").WithLocation(6, 15),
// (7,16): error CS0246: The type or namespace name 'L' could not be found (are you missing a using directive or an assembly reference?)
// int L2(L l1) => 0;
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "L").WithArguments("L").WithLocation(7, 16),
// (6,13): warning CS0168: The variable 'L' is declared but never used
// int L(L2 l2) => 0;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "L").WithArguments("L").WithLocation(6, 13),
// (7,13): warning CS0168: The variable 'L2' is declared but never used
// int L2(L l1) => 0;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "L2").WithArguments("L2").WithLocation(7, 13));
}
[Fact]
[WorkItem(16451, "https://github.com/dotnet/roslyn/issues/16451")]
public void BadGenericConstraint()
{
var comp = CreateStandardCompilation(@"
class C
{
public void M<T>(T value) where T : object { }
}");
comp.VerifyDiagnostics(
// (4,41): error CS0702: Constraint cannot be special class 'object'
// public void M<T>(T value) where T : object { }
Diagnostic(ErrorCode.ERR_SpecialTypeAsBound, "object").WithArguments("object").WithLocation(4, 41));
}
[Fact]
[WorkItem(16451, "https://github.com/dotnet/roslyn/issues/16451")]
public void RecursiveDefaultParameter()
{
var comp = CreateStandardCompilation(@"
class C
{
public static void Main()
{
int Local(int j = Local()) => 0;
Local();
}
}");
comp.VerifyDiagnostics(
// (6,27): error CS1736: Default parameter value for 'j' must be a compile-time constant
// int Local(int j = Local()) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local()").WithArguments("j").WithLocation(6, 27));
comp.DeclarationDiagnostics.Verify();
}
[Fact]
[WorkItem(16451, "https://github.com/dotnet/roslyn/issues/16451")]
public void RecursiveDefaultParameter2()
{
var comp = CreateStandardCompilation(@"
using System;
class C
{
void M()
{
int Local(Action a = Local) => 0;
Local();
}
}");
comp.VerifyDiagnostics(
// (7,30): error CS1736: Default parameter value for 'a' must be a compile-time constant
// int Local(Action a = Local) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local").WithArguments("a").WithLocation(7, 30));
comp.DeclarationDiagnostics.Verify();
}
[Fact]
[WorkItem(16451, "https://github.com/dotnet/roslyn/issues/16451")]
public void MutuallyRecursiveDefaultParameters()
{
var comp = CreateStandardCompilation(@"
class C
{
void M()
{
int Local1(int p = Local2()) => 0;
int Local2(int p = Local1()) => 0;
Local1();
Local2();
}
}");
comp.VerifyDiagnostics(
// (6,28): error CS1736: Default parameter value for 'p' must be a compile-time constant
// int Local1(int p = Local2()) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local2()").WithArguments("p").WithLocation(6, 28),
// (7,28): error CS1736: Default parameter value for 'p' must be a compile-time constant
// int Local2(int p = Local1()) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local1()").WithArguments("p").WithLocation(7, 28));
comp.DeclarationDiagnostics.Verify();
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16652")]
public void FetchLocalFunctionSymbolThroughLocal()
{
var tree = SyntaxFactory.ParseSyntaxTree(@"
using System;
class C
{
public void M()
{
void Local<[A, B, CLSCompliant, D]T>()
{
var x = new object();
}
Local<int>();
}
}");
var comp = CreateStandardCompilation(tree);
comp.DeclarationDiagnostics.Verify();
comp.VerifyDiagnostics(
// (7,20): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[A, B, CLSCompliant, D]").WithLocation(7, 20),
// (7,21): error CS0246: The type or namespace name 'AAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("AAttribute").WithLocation(7, 21),
// (7,21): error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("A").WithLocation(7, 21),
// (7,24): error CS0246: The type or namespace name 'BAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("BAttribute").WithLocation(7, 24),
// (7,24): error CS0246: The type or namespace name 'B' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("B").WithLocation(7, 24),
// (7,41): error CS0246: The type or namespace name 'DAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "D").WithArguments("DAttribute").WithLocation(7, 41),
// (7,41): error CS0246: The type or namespace name 'D' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "D").WithArguments("D").WithLocation(7, 41),
// (7,27): error CS7036: There is no argument given that corresponds to the required formal parameter 'isCompliant' of 'CLSCompliantAttribute.CLSCompliantAttribute(bool)'
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "CLSCompliant").WithArguments("isCompliant", "System.CLSCompliantAttribute.CLSCompliantAttribute(bool)").WithLocation(7, 27));
var model = comp.GetSemanticModel(tree);
var x = tree.GetRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().Where(v => v.Identifier.ValueText == "x").Single();
var localSymbol = (LocalFunctionSymbol)model.GetDeclaredSymbol(x).ContainingSymbol;
var typeParam = localSymbol.TypeParameters.Single();
var attrs = typeParam.GetAttributes();
Assert.True(attrs[0].AttributeClass.IsErrorType());
Assert.True(attrs[1].AttributeClass.IsErrorType());
Assert.False(attrs[2].AttributeClass.IsErrorType());
Assert.Equal(comp.GlobalNamespace
.GetMember<NamespaceSymbol>("System")
.GetMember<NamedTypeSymbol>("CLSCompliantAttribute"),
attrs[2].AttributeClass);
Assert.True(attrs[3].AttributeClass.IsErrorType());
comp.DeclarationDiagnostics.Verify();
}
[Fact]
public void TypeParameterAttributesInSemanticModel()
{
var comp = CreateStandardCompilation(@"
using System;
class C
{
public void M()
{
void Local<[A]T, [CLSCompliant]U>() { }
}
}");
comp.VerifyDiagnostics(
// (7,21): error CS0246: The type or namespace name 'AAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A]T, [CLSCompliant]U>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("AAttribute").WithLocation(7, 21),
// (7,21): error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A]T, [CLSCompliant]U>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("A").WithLocation(7, 21),
// (7,27): error CS7036: There is no argument given that corresponds to the required formal parameter 'isCompliant' of 'CLSCompliantAttribute.CLSCompliantAttribute(bool)'
// void Local<[A]T, [CLSCompliant]U>() { }
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "CLSCompliant").WithArguments("isCompliant", "System.CLSCompliantAttribute.CLSCompliantAttribute(bool)").WithLocation(7, 27),
// (7,20): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local<[A]T, [CLSCompliant]U>() { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[A]").WithLocation(7, 20),
// (7,26): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local<[A]T, [CLSCompliant]U>() { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[CLSCompliant]").WithLocation(7, 26),
// (7,14): warning CS0168: The variable 'Local' is declared but never used
// void Local<[A]T, [CLSCompliant]U>() { }
Diagnostic(ErrorCode.WRN_UnreferencedVar, "Local").WithArguments("Local").WithLocation(7, 14));
var tree = comp.SyntaxTrees[0];
var root = tree.GetRoot();
var model = comp.GetSemanticModel(tree);
var a = root.DescendantNodes()
.OfType<IdentifierNameSyntax>()
.Where(id => id.Identifier.ValueText == "A")
.Single();
Assert.Null(model.GetDeclaredSymbol(a));
var aSymbolInfo = model.GetSymbolInfo(a);
Assert.Equal(0, aSymbolInfo.CandidateSymbols.Length);
Assert.Null(aSymbolInfo.Symbol);
var aTypeInfo = model.GetTypeInfo(a);
Assert.Equal(TypeKind.Error, aTypeInfo.Type.TypeKind);
Assert.Null(model.GetAliasInfo(a));
Assert.Empty(model.LookupNamespacesAndTypes(a.SpanStart, name: "A"));
var clsCompliant = root.DescendantNodes()
.OfType<IdentifierNameSyntax>()
.Where(id => id.Identifier.ValueText == "CLSCompliant")
.Single();
var clsCompliantSymbol = comp.GlobalNamespace
.GetMember<NamespaceSymbol>("System")
.GetTypeMember("CLSCompliantAttribute");
Assert.Null(model.GetDeclaredSymbol(clsCompliant));
// This should be null because there is no CLSCompliant ctor with no args
var clsCompliantSymbolInfo = model.GetSymbolInfo(clsCompliant);
Assert.Null(clsCompliantSymbolInfo.Symbol);
Assert.Equal(clsCompliantSymbol.GetMember<MethodSymbol>(".ctor"),
clsCompliantSymbolInfo.CandidateSymbols.Single());
Assert.Equal(CandidateReason.OverloadResolutionFailure, clsCompliantSymbolInfo.CandidateReason);
Assert.Equal(clsCompliantSymbol, model.GetTypeInfo(clsCompliant).Type);
Assert.Null(model.GetAliasInfo(clsCompliant));
Assert.Equal(clsCompliantSymbol,
model.LookupNamespacesAndTypes(clsCompliant.SpanStart, name: "CLSCompliantAttribute").Single());
comp.DeclarationDiagnostics.Verify();
}
[Fact]
public void ParameterAttributesInSemanticModel()
{
var comp = CreateStandardCompilation(@"
using System;
class C
{
public void M()
{
void Local([A]int x, [CLSCompliant]int y) { }
}
}");
comp.VerifyDiagnostics(
// (7,20): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local([A]int x, [CLSCompliant]int y) { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[A]").WithLocation(7, 20),
// (7,30): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local([A]int x, [CLSCompliant]int y) { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[CLSCompliant]").WithLocation(7, 30),
// (7,21): error CS0246: The type or namespace name 'AAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local([A]int x, [CLSCompliant]int y) { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("AAttribute").WithLocation(7, 21),
// (7,21): error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
// void Local([A]int x, [CLSCompliant]int y) { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("A").WithLocation(7, 21),
// (7,31): error CS7036: There is no argument given that corresponds to the required formal parameter 'isCompliant' of 'CLSCompliantAttribute.CLSCompliantAttribute(bool)'
// void Local([A]int x, [CLSCompliant]int y) { }
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "CLSCompliant").WithArguments("isCompliant", "System.CLSCompliantAttribute.CLSCompliantAttribute(bool)").WithLocation(7, 31),
// (7,14): warning CS0168: The variable 'Local' is declared but never used
// void Local([A]int x, [CLSCompliant]int y) { }
Diagnostic(ErrorCode.WRN_UnreferencedVar, "Local").WithArguments("Local").WithLocation(7, 14));
var tree = comp.SyntaxTrees[0];
var root = tree.GetRoot();
var model = comp.GetSemanticModel(tree);
var a = root.DescendantNodes()
.OfType<IdentifierNameSyntax>()
.Where(id => id.Identifier.ValueText == "A")
.Single();
Assert.Null(model.GetDeclaredSymbol(a));
var aSymbolInfo = model.GetSymbolInfo(a);
Assert.Equal(0, aSymbolInfo.CandidateSymbols.Length);
Assert.Null(aSymbolInfo.Symbol);
var aTypeInfo = model.GetTypeInfo(a);
Assert.Equal(TypeKind.Error, aTypeInfo.Type.TypeKind);
Assert.Null(model.GetAliasInfo(a));
Assert.Empty(model.LookupNamespacesAndTypes(a.SpanStart, name: "A"));
var clsCompliant = root.DescendantNodes()
.OfType<IdentifierNameSyntax>()
.Where(id => id.Identifier.ValueText == "CLSCompliant")
.Single();
var clsCompliantSymbol = comp.GlobalNamespace
.GetMember<NamespaceSymbol>("System")
.GetTypeMember("CLSCompliantAttribute");
Assert.Null(model.GetDeclaredSymbol(clsCompliant));
// This should be null because there is no CLSCompliant ctor with no args
var clsCompliantSymbolInfo = model.GetSymbolInfo(clsCompliant);
Assert.Null(clsCompliantSymbolInfo.Symbol);
Assert.Equal(clsCompliantSymbol.GetMember<MethodSymbol>(".ctor"),
clsCompliantSymbolInfo.CandidateSymbols.Single());
Assert.Equal(CandidateReason.OverloadResolutionFailure, clsCompliantSymbolInfo.CandidateReason);
Assert.Equal(clsCompliantSymbol, model.GetTypeInfo(clsCompliant).Type);
Assert.Null(model.GetAliasInfo(clsCompliant));
Assert.Equal(clsCompliantSymbol,
model.LookupNamespacesAndTypes(clsCompliant.SpanStart, name: "CLSCompliantAttribute").Single());
comp.DeclarationDiagnostics.Verify();
}
[Fact]
public void LocalFunctionAttributeOnTypeParameter()
{
var tree = SyntaxFactory.ParseSyntaxTree(@"
using System;
class C
{
public void M()
{
void Local<[A, B, CLSCompliant, D]T>() { }
Local<int>();
}
}");
var comp = CreateStandardCompilation(tree);
comp.DeclarationDiagnostics.Verify();
comp.VerifyDiagnostics(
// (7,20): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[A, B, CLSCompliant, D]").WithLocation(7, 20),
// (7,21): error CS0246: The type or namespace name 'AAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("AAttribute").WithLocation(7, 21),
// (7,21): error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("A").WithLocation(7, 21),
// (7,24): error CS0246: The type or namespace name 'BAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("BAttribute").WithLocation(7, 24),
// (7,24): error CS0246: The type or namespace name 'B' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("B").WithLocation(7, 24),
// (7,41): error CS0246: The type or namespace name 'DAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "D").WithArguments("DAttribute").WithLocation(7, 41),
// (7,41): error CS0246: The type or namespace name 'D' could not be found (are you missing a using directive or an assembly reference?)
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "D").WithArguments("D").WithLocation(7, 41),
// (7,27): error CS7036: There is no argument given that corresponds to the required formal parameter 'isCompliant' of 'CLSCompliantAttribute.CLSCompliantAttribute(bool)'
// void Local<[A, B, CLSCompliant, D]T>() { }
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "CLSCompliant").WithArguments("isCompliant", "System.CLSCompliantAttribute.CLSCompliantAttribute(bool)").WithLocation(7, 27));
var localDecl = tree.FindNodeOrTokenByKind(SyntaxKind.LocalFunctionStatement);
var model = comp.GetSemanticModel(tree);
var localSymbol = Assert.IsType<LocalFunctionSymbol>(model.GetDeclaredSymbol(localDecl.AsNode()));
var typeParam = localSymbol.TypeParameters.Single();
var attrs = typeParam.GetAttributes();
Assert.True(attrs[0].AttributeClass.IsErrorType());
Assert.True(attrs[1].AttributeClass.IsErrorType());
Assert.False(attrs[2].AttributeClass.IsErrorType());
Assert.Equal(comp.GlobalNamespace
.GetMember<NamespaceSymbol>("System")
.GetMember<NamedTypeSymbol>("CLSCompliantAttribute"),
attrs[2].AttributeClass);
Assert.True(attrs[3].AttributeClass.IsErrorType());
comp.DeclarationDiagnostics.Verify();
}
[Fact]
public void LocalFunctionAttributeOnParameters()
{
var tree = SyntaxFactory.ParseSyntaxTree(@"
using System;
class C
{
public void M()
{
void Local([A, B]int x, [CLSCompliant]string s = """") { }
Local(0);
}
}");
var comp = CreateStandardCompilation(tree);
comp.DeclarationDiagnostics.Verify();
comp.VerifyDiagnostics(
// (7,20): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local([A, B]int x, [CLSCompliant]string s = "") { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[A, B]").WithLocation(7, 20),
// (7,33): error CS8204: Attributes are not allowed on local function parameters or type parameters
// void Local([A, B]int x, [CLSCompliant]string s = "") { }
Diagnostic(ErrorCode.ERR_AttributesInLocalFuncDecl, "[CLSCompliant]").WithLocation(7, 33),
// (7,21): error CS0246: The type or namespace name 'AAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local([A, B]int x, [CLSCompliant]string s = "") { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("AAttribute").WithLocation(7, 21),
// (7,21): error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
// void Local([A, B]int x, [CLSCompliant]string s = "") { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("A").WithLocation(7, 21),
// (7,24): error CS0246: The type or namespace name 'BAttribute' could not be found (are you missing a using directive or an assembly reference?)
// void Local([A, B]int x, [CLSCompliant]string s = "") { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("BAttribute").WithLocation(7, 24),
// (7,24): error CS0246: The type or namespace name 'B' could not be found (are you missing a using directive or an assembly reference?)
// void Local([A, B]int x, [CLSCompliant]string s = "") { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("B").WithLocation(7, 24),
// (7,34): error CS7036: There is no argument given that corresponds to the required formal parameter 'isCompliant' of 'CLSCompliantAttribute.CLSCompliantAttribute(bool)'
// void Local([A, B]int x, [CLSCompliant]string s = "") { }
Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "CLSCompliant").WithArguments("isCompliant", "System.CLSCompliantAttribute.CLSCompliantAttribute(bool)").WithLocation(7, 34));
var localDecl = tree.FindNodeOrTokenByKind(SyntaxKind.LocalFunctionStatement);
var model = comp.GetSemanticModel(tree);
var localSymbol = Assert.IsType<LocalFunctionSymbol>(model.GetDeclaredSymbol(localDecl.AsNode()));
var param = localSymbol.Parameters[0];
var attrs = param.GetAttributes();
Assert.True(attrs[0].AttributeClass.IsErrorType());
Assert.True(attrs[1].AttributeClass.IsErrorType());
param = localSymbol.Parameters[1];
attrs = param.GetAttributes();
Assert.Equal(comp.GlobalNamespace
.GetMember<NamespaceSymbol>("System")
.GetMember<NamedTypeSymbol>("CLSCompliantAttribute"),
attrs[0].AttributeClass);
comp.DeclarationDiagnostics.Verify();
}
[Fact]
public void TypeParameterBindingScope()
{
var src = @"
class C
{
public void M()
{
{
int T = 0; // Should not have error
int Local<T>() => 0; // Should conflict with above
Local<int>();
T++;
}
{
int T<T>() => 0;
T<int>();
}
{
int Local<T, T>() => 0;
Local<int, int>();
}
}
public void M2<T>()
{
{
int Local<T>() => 0;
Local<int>();
}
{
int Local1<V>()
{
int Local2<V>() => 0;
return Local2<int>();
}
Local1<int>();
}
{
int T() => 0;
T();
}
{
int Local1<V>()
{
int V() => 0;
return V();
}
Local1<int>();
}
{
int Local1<V>()
{
int Local2<U>()
{
// Conflicts with method type parameter
int T() => 0;
return T();
}
return Local2<int>();
}
Local1<int>();
}
{
int Local1<V>()
{
int Local2<U>()
{
// Shadows M.2<T>
int Local3<T>() => 0;
return Local3<int>();
}
return Local2<int>();
}
Local1<int>();
}
}
public void V<V>() { }
}
";
var comp = CreateStandardCompilation(src);
comp.VerifyDiagnostics(
// (9,23): error CS0136: A local or parameter named 'T' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// int Local<T>() => 0; // Should conflict with above
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "T").WithArguments("T").WithLocation(9, 23),
// (14,19): error CS0136: A local or parameter named 'T' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// int T<T>() => 0;
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "T").WithArguments("T").WithLocation(14, 19),
// (18,26): error CS0692: Duplicate type parameter 'T'
// int Local<T, T>() => 0;
Diagnostic(ErrorCode.ERR_DuplicateTypeParameter, "T").WithArguments("T").WithLocation(18, 26),
// (25,23): warning CS0693: Type parameter 'T' has the same name as the type parameter from outer type 'C.M2<T>()'
// int Local<T>() => 0;
Diagnostic(ErrorCode.WRN_TypeParameterSameAsOuterTypeParameter, "T").WithArguments("T", "C.M2<T>()").WithLocation(25, 23),
// (31,28): warning CS0693: Type parameter 'V' has the same name as the type parameter from outer type 'Local1<V>()'
// int Local2<V>() => 0;
Diagnostic(ErrorCode.WRN_TypeParameterSameAsOuterTypeParameter, "V").WithArguments("V", "Local1<V>()").WithLocation(31, 28),
// (37,17): error CS0412: 'T': a parameter, local variable, or local function cannot have the same name as a method type parameter
// int T() => 0;
Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "T").WithArguments("T").WithLocation(37, 17),
// (43,21): error CS0412: 'V': a parameter, local variable, or local function cannot have the same name as a method type parameter
// int V() => 0;
Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "V").WithArguments("V").WithLocation(43, 21),
// (54,25): error CS0412: 'T': a parameter, local variable, or local function cannot have the same name as a method type parameter
// int T() => 0;
Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "T").WithArguments("T").WithLocation(54, 25),
// (67,32): warning CS0693: Type parameter 'T' has the same name as the type parameter from outer type 'C.M2<T>()'
// int Local3<T>() => 0;
Diagnostic(ErrorCode.WRN_TypeParameterSameAsOuterTypeParameter, "T").WithArguments("T", "C.M2<T>()").WithLocation(67, 32));
}
[Fact]
public void LocalFuncAndTypeParameterOnType()
{
var comp = CreateStandardCompilation(@"
class C2<T>
{
public void M()
{
{
int Local1()
{
int Local2<T>() => 0;
return Local2<int>();
}
Local1();
}
{
int Local1()
{
int Local2()
{
// Shadows type parameter
int T() => 0;
// Type parameter resolves in type only context
T t = default(T);
// Ambiguous context chooses local
T.M();
// Call chooses local
return T();
}
return Local2();
}
Local1();
}
}
}");
comp.VerifyDiagnostics(
// (9,28): warning CS0693: Type parameter 'T' has the same name as the type parameter from outer type 'C2<T>'
// int Local2<T>() => 0;
Diagnostic(ErrorCode.WRN_TypeParameterSameAsOuterTypeParameter, "T").WithArguments("T", "C2<T>").WithLocation(9, 28),
// (26,21): error CS0119: 'T()' is a method, which is not valid in the given context
// T.M();
Diagnostic(ErrorCode.ERR_BadSKunknown, "T").WithArguments("T()", "method").WithLocation(26, 21),
// (23,23): warning CS0219: The variable 't' is assigned but its value is never used
// T t = default(T);
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "t").WithArguments("t").WithLocation(23, 23));
}
[Fact]
public void RefArgsInIteratorLocalFuncs()
{
var src = @"
using System;
using System.Collections.Generic;
class C
{
public void M1()
{
IEnumerable<int> Local(ref int a) { yield break; }
int x = 0;
Local(ref x);
}
public void M2()
{
Action a = () =>
{
IEnumerable<int> Local(ref int x) { yield break; }
int y = 0;
Local(ref y);
return;
};
a();
}
public Func<int> M3() => (() =>
{
IEnumerable<int> Local(ref int a) { yield break; }
int x = 0;
Local(ref x);
return 0;
});
public IEnumerable<int> M4(ref int a)
{
yield return new Func<int>(() =>
{
IEnumerable<int> Local(ref int b) { yield break; }
int x = 0;
Local(ref x);
return 0;
})();
}
}";
VerifyDiagnostics(src,
// (8,40): error CS1623: Iterators cannot have ref or out parameters
// IEnumerable<int> Local(ref int a) { yield break; }
Diagnostic(ErrorCode.ERR_BadIteratorArgType, "a").WithLocation(8, 40),
// (17,44): error CS1623: Iterators cannot have ref or out parameters
// IEnumerable<int> Local(ref int x) { yield break; }
Diagnostic(ErrorCode.ERR_BadIteratorArgType, "x").WithLocation(17, 44),
// (27,40): error CS1623: Iterators cannot have ref or out parameters
// IEnumerable<int> Local(ref int a) { yield break; }
Diagnostic(ErrorCode.ERR_BadIteratorArgType, "a").WithLocation(27, 40),
// (33,40): error CS1623: Iterators cannot have ref or out parameters
// public IEnumerable<int> M4(ref int a)
Diagnostic(ErrorCode.ERR_BadIteratorArgType, "a").WithLocation(33, 40),
// (37,48): error CS1623: Iterators cannot have ref or out parameters
// IEnumerable<int> Local(ref int b) { yield break; }
Diagnostic(ErrorCode.ERR_BadIteratorArgType, "b").WithLocation(37, 48));
}
[Fact]
public void UnsafeArgsInIteratorLocalFuncs()
{
var src = @"
using System;
using System.Collections.Generic;
class C
{
public unsafe void M1()
{
IEnumerable<int> Local(int* a) { yield break; }
int x = 0;
Local(&x);
}
public unsafe void M2()
{
Action a = () =>
{
IEnumerable<int> Local(int* x) { yield break; }
int y = 0;
Local(&y);
return;
};
a();
}
public unsafe Func<int> M3() => (() =>
{
IEnumerable<int> Local(int* a) { yield break; }
int x = 0;
Local(&x);
return 0;
});
public unsafe IEnumerable<int> M4(int* a)
{
yield return new Func<int>(() =>
{
IEnumerable<int> Local(int* b) { yield break; }
int x = 0;
Local(&x);
return 0;
})();
}
}";
CreateStandardCompilation(src, options: TestOptions.UnsafeDebugDll)
.VerifyDiagnostics(
// (8,37): error CS1637: Iterators cannot have unsafe parameters or yield types
// IEnumerable<int> Local(int* a) { yield break; }
Diagnostic(ErrorCode.ERR_UnsafeIteratorArgType, "a").WithLocation(8, 37),
// (17,41): error CS1637: Iterators cannot have unsafe parameters or yield types
// IEnumerable<int> Local(int* x) { yield break; }
Diagnostic(ErrorCode.ERR_UnsafeIteratorArgType, "x").WithLocation(17, 41),
// (27,37): error CS1637: Iterators cannot have unsafe parameters or yield types
// IEnumerable<int> Local(int* a) { yield break; }
Diagnostic(ErrorCode.ERR_UnsafeIteratorArgType, "a").WithLocation(27, 37),
// (33,44): error CS1637: Iterators cannot have unsafe parameters or yield types
// public unsafe IEnumerable<int> M4(int* a)
Diagnostic(ErrorCode.ERR_UnsafeIteratorArgType, "a").WithLocation(33, 44),
// (33,36): error CS1629: Unsafe code may not appear in iterators
// public unsafe IEnumerable<int> M4(int* a)
Diagnostic(ErrorCode.ERR_IllegalInnerUnsafe, "M4").WithLocation(33, 36),
// (37,40): error CS1629: Unsafe code may not appear in iterators
// IEnumerable<int> Local(int* b) { yield break; }
Diagnostic(ErrorCode.ERR_IllegalInnerUnsafe, "int*").WithLocation(37, 40),
// (39,23): error CS1629: Unsafe code may not appear in iterators
// Local(&x);
Diagnostic(ErrorCode.ERR_IllegalInnerUnsafe, "&x").WithLocation(39, 23),
// (39,17): error CS1629: Unsafe code may not appear in iterators
// Local(&x);
Diagnostic(ErrorCode.ERR_IllegalInnerUnsafe, "Local(&x)").WithLocation(39, 17),
// (37,45): error CS1637: Iterators cannot have unsafe parameters or yield types
// IEnumerable<int> Local(int* b) { yield break; }
Diagnostic(ErrorCode.ERR_UnsafeIteratorArgType, "b").WithLocation(37, 45));
}
[Fact]
[WorkItem(13193, "https://github.com/dotnet/roslyn/issues/13193")]
public void LocalFunctionConflictingName()
{
var comp = CreateStandardCompilation(@"
class C
{
public void M<TLocal>()
{
void TLocal() { }
TLocal();
}
public void M(int Local)
{
void Local() { }
Local();
}
public void M()
{
int local = 0;
void local() { }
local();
}
}");
comp.VerifyDiagnostics(
// (6,14): error CS0412: 'TLocal': a parameter, local variable, or local function cannot have the same name as a method type parameter
// void TLocal() { }
Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "TLocal").WithArguments("TLocal").WithLocation(6, 14),
// (11,14): error CS0136: A local or parameter named 'Local' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// void Local() { }
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "Local").WithArguments("Local").WithLocation(11, 14),
// (18,14): error CS0128: A local variable or function named 'local' is already defined in this scope
// void local() { }
Diagnostic(ErrorCode.ERR_LocalDuplicate, "local").WithArguments("local").WithLocation(18, 14),
// (16,13): warning CS0219: The variable 'local' is assigned but its value is never used
// int local = 0;
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "local").WithArguments("local").WithLocation(16, 13));
}
[Fact]
public void ForgotSemicolonLocalFunctionsMistake()
{
var src = @"
class C
{
public void M1()
{
// forget closing brace
public void BadLocal1()
{
this.BadLocal2();
}
public void BadLocal2()
{
}
public int P => 0;
}";
VerifyDiagnostics(src,
// (8,5): error CS0106: The modifier 'public' is not valid for this item
// public void BadLocal1()
Diagnostic(ErrorCode.ERR_BadMemberFlag, "public").WithArguments("public").WithLocation(8, 5),
// (13,5): error CS0106: The modifier 'public' is not valid for this item
// public void BadLocal2()
Diagnostic(ErrorCode.ERR_BadMemberFlag, "public").WithArguments("public").WithLocation(13, 5),
// (15,6): error CS1513: } expected
// }
Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(15, 6),
// (10,14): error CS1061: 'C' does not contain a definition for 'BadLocal2' and no extension method 'BadLocal2' accepting a first argument of type 'C' could be found (are you missing a using directive or an assembly reference?)
// this.BadLocal2();
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "BadLocal2").WithArguments("C", "BadLocal2").WithLocation(10, 14),
// (8,17): warning CS0168: The variable 'BadLocal1' is declared but never used
// public void BadLocal1()
Diagnostic(ErrorCode.WRN_UnreferencedVar, "BadLocal1").WithArguments("BadLocal1").WithLocation(8, 17),
// (13,17): warning CS0168: The variable 'BadLocal2' is declared but never used
// public void BadLocal2()
Diagnostic(ErrorCode.WRN_UnreferencedVar, "BadLocal2").WithArguments("BadLocal2").WithLocation(13, 17));
}
[Fact]
public void VarLocalFunction()
{
var src = @"
class C
{
void M()
{
var local() => 0;
int x = local();
}
}";
VerifyDiagnostics(src,
// (6,9): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code
// var local() => 0;
Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(6, 9));
}
[Fact]
public void VarLocalFunction2()
{
var comp = CreateStandardCompilation(@"
class C
{
private class var
{
}
void M()
{
var local() => new var();
var x = local();
}
}", parseOptions: DefaultParseOptions);
comp.VerifyDiagnostics();
}
[Fact]
[CompilerTrait(CompilerFeature.Params)]
public void BadParams()
{
var source = @"
using System;
class Program
{
static void Main(string[] args)
{
void Params(params int x)
{
Console.WriteLine(x);
}
Params(2);
}
}
";
VerifyDiagnostics(source,
// (8,21): error CS0225: The params parameter must be a single dimensional array
// void Params(params int x)
Diagnostic(ErrorCode.ERR_ParamsMustBeArray, "params").WithLocation(8, 21)
);
}
[Fact]
public void BadRefWithDefault()
{
var source = @"
class Program
{
static void Main(string[] args)
{
void RefOut(ref int x = 2)
{
x++;
}
int y = 2;
RefOut(ref y);
}
}
";
VerifyDiagnostics(source,
// (6,21): error CS1741: A ref or out parameter cannot have a default value
// void RefOut(ref int x = 2)
Diagnostic(ErrorCode.ERR_RefOutDefaultValue, "ref").WithLocation(6, 21)
);
}
[Fact]
public void BadDefaultValueType()
{
var source = @"
using System;
class Program
{
static void Main(string[] args)
{
void NamedOptional(string x = 2)
{
Console.WriteLine(x);
}
NamedOptional(""2"");
}
}
";
VerifyDiagnostics(source,
// (8,35): error CS1750: A value of type 'int' cannot be used as a default parameter because there are no standard conversions to type 'string'
// void NamedOptional(string x = 2)
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "x").WithArguments("int", "string").WithLocation(8, 35)
);
}
[Fact]
public void CallerMemberName()
{
var comp = CreateCompilationWithMscorlib46(@"
using System;
using System.Runtime.CompilerServices;
class C
{
static void Main()
{
void CallerMemberName([CallerMemberName] string s = null)
{
Console.Write(s);
}
void LocalFuncName()
{
CallerMemberName();
}
LocalFuncName();
Console.Write(' ');