@@ -2949,74 +2949,42 @@ public void DataSectionStringLiterals_MissingMembers()
2949
2949
2950
2950
var comp = CreateCompilation ( source , parseOptions : TestOptions . Regular . WithFeature ( "experimental-data-section-string-literals" , "0" ) ) ;
2951
2951
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__get_UTF8 ) ;
2952
- comp . VerifyEmitDiagnostics (
2953
- // (1,22): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2954
- // System.Console.Write("a");
2955
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""a""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 1 , 22 ) ,
2956
- // (2,22): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2957
- // System.Console.Write("bb");
2958
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""bb""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 2 , 22 ) ,
2959
- // (3,22): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2960
- // System.Console.Write("ccc");
2961
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""ccc""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 3 , 22 ) ) ;
2952
+ comp . VerifyDiagnostics (
2953
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2954
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 1 , 1 ) ) ;
2962
2955
2963
2956
comp = CreateCompilation ( source , parseOptions : TestOptions . Regular . WithFeature ( "experimental-data-section-string-literals" , "0" ) ) ;
2964
2957
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__GetString ) ;
2965
- comp . VerifyEmitDiagnostics (
2966
- // (1,22): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2967
- // System.Console.Write("a");
2968
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""a""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 1 , 22 ) ,
2969
- // (2,22): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2970
- // System.Console.Write("bb");
2971
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""bb""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 2 , 22 ) ,
2972
- // (3,22): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2973
- // System.Console.Write("ccc");
2974
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""ccc""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 3 , 22 ) ) ;
2958
+ comp . VerifyDiagnostics (
2959
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2960
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 1 , 1 ) ) ;
2975
2961
2976
2962
comp = CreateCompilation ( source , parseOptions : TestOptions . Regular . WithFeature ( "experimental-data-section-string-literals" , "0" ) ) ;
2977
2963
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__get_UTF8 ) ;
2978
2964
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__GetString ) ;
2979
- comp . VerifyEmitDiagnostics (
2980
- // (1,22): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2981
- // System.Console.Write("a");
2982
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""a""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 1 , 22 ) ,
2983
- // (1,22): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2984
- // System.Console.Write("a");
2985
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""a""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 1 , 22 ) ,
2986
- // (2,22): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2987
- // System.Console.Write("bb");
2988
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""bb""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 2 , 22 ) ,
2989
- // (2,22): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2990
- // System.Console.Write("bb");
2991
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""bb""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 2 , 22 ) ,
2992
- // (3,22): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2993
- // System.Console.Write("ccc");
2994
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""ccc""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 3 , 22 ) ,
2995
- // (3,22): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2996
- // System.Console.Write("ccc");
2997
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""ccc""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 3 , 22 ) ) ;
2965
+ comp . VerifyDiagnostics (
2966
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2967
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 1 , 1 ) ,
2968
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2969
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 1 , 1 ) ) ;
2998
2970
2999
2971
comp = CreateCompilation ( source , parseOptions : TestOptions . Regular . WithFeature ( "experimental-data-section-string-literals" , "1" ) ) ;
3000
2972
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__get_UTF8 ) ;
3001
2973
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__GetString ) ;
3002
- comp . VerifyEmitDiagnostics (
3003
- // (2,26): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
3004
- // System.Console.Write("bb");
3005
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""bb""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 2 , 22 ) ,
3006
- // (2,26): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
3007
- // System.Console.Write("bb");
3008
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""bb""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 2 , 22 ) ,
3009
- // (3,22): error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
3010
- // System.Console.Write("ccc");
3011
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""ccc""" ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 3 , 22 ) ,
3012
- // (3,22): error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
3013
- // System.Console.Write("ccc");
3014
- Diagnostic ( ErrorCode . ERR_MissingPredefinedMember , @"""ccc""" ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 3 , 22 ) ) ;
2974
+ comp . VerifyDiagnostics (
2975
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2976
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 1 , 1 ) ,
2977
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2978
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 1 , 1 ) ) ;
3015
2979
3016
2980
comp = CreateCompilation ( source , parseOptions : TestOptions . Regular . WithFeature ( "experimental-data-section-string-literals" , "3" ) ) ;
3017
2981
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__get_UTF8 ) ;
3018
2982
comp . MakeMemberMissing ( WellKnownMember . System_Text_Encoding__GetString ) ;
3019
- CompileAndVerify ( comp , expectedOutput : "abbccc" ) . VerifyDiagnostics ( ) ;
2983
+ comp . VerifyDiagnostics (
2984
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
2985
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 1 , 1 ) ,
2986
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
2987
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 1 , 1 ) ) ;
3020
2988
}
3021
2989
3022
2990
[ Fact ]
@@ -3204,6 +3172,68 @@ .locals init (int V_0,
3204
3172
""" ) ;
3205
3173
}
3206
3174
3175
+ [ Theory ]
3176
+ [ InlineData ( """public static string M() => "abc";""" ) ]
3177
+ [ InlineData ( """public static void M(string s) { switch (s) { case "abc": break; } }""" ) ]
3178
+ public void DataSectionStringLiterals_UsedAssemblyReferences ( string code )
3179
+ {
3180
+ var source1 = """
3181
+ namespace System
3182
+ {
3183
+ public class Object;
3184
+ public class String
3185
+ {
3186
+ public static bool op_Equality(string a, string b) => false;
3187
+ }
3188
+ public class ValueType;
3189
+ public struct Void;
3190
+ public struct Byte;
3191
+ public struct Int16;
3192
+ public struct Int32;
3193
+ public struct Int64;
3194
+ public struct Boolean;
3195
+ public class Attribute;
3196
+ public class Enum;
3197
+ public enum AttributeTargets;
3198
+ public class AttributeUsageAttribute
3199
+ {
3200
+ public AttributeUsageAttribute(AttributeTargets validOn) { }
3201
+ public bool AllowMultiple { get; set; }
3202
+ public bool Inherited { get; set; }
3203
+ }
3204
+ }
3205
+ """ ;
3206
+ var ref1 = CreateEmptyCompilation ( source1 , assemblyName : "MinimalCoreLib" ) . VerifyDiagnostics ( ) . EmitToImageReference ( ) ;
3207
+
3208
+ var source2 = """
3209
+ namespace System.Text
3210
+ {
3211
+ public class Encoding
3212
+ {
3213
+ public static Encoding UTF8 => null;
3214
+ public unsafe string GetString(byte* bytes, int byteCount) => null;
3215
+ }
3216
+ }
3217
+ """ ;
3218
+ var ref2 = CreateEmptyCompilation ( source2 , [ ref1 ] , options : TestOptions . UnsafeDebugDll , assemblyName : "Encoding" )
3219
+ . VerifyDiagnostics ( ) . EmitToImageReference ( ) ;
3220
+
3221
+ var source3 = $$ """
3222
+ public static class C
3223
+ {
3224
+ {{ code }}
3225
+ }
3226
+ """ ;
3227
+ var comp = CreateEmptyCompilation ( source3 , [ ref1 , ref2 ] , assemblyName : "Lib1" ) ;
3228
+ AssertEx . SetEqual ( [ ref1 ] , comp . GetUsedAssemblyReferences ( ) ) ;
3229
+ comp . VerifyEmitDiagnostics ( ) ;
3230
+
3231
+ comp = CreateEmptyCompilation ( source3 , [ ref1 , ref2 ] , assemblyName : "Lib2" ,
3232
+ parseOptions : TestOptions . Regular . WithFeature ( "experimental-data-section-string-literals" , "0" ) ) ;
3233
+ AssertEx . SetEqual ( [ ref1 , ref2 ] , comp . GetUsedAssemblyReferences ( ) ) ;
3234
+ comp . VerifyEmitDiagnostics ( ) ;
3235
+ }
3236
+
3207
3237
[ Fact ]
3208
3238
public void DataSectionStringLiterals_InvalidUtf8 ( )
3209
3239
{
@@ -3561,8 +3591,7 @@ public class InvalidOperationException();
3561
3591
""" ;
3562
3592
3563
3593
var parseOptions = TestOptions . RegularPreview
3564
- . WithNoRefSafetyRulesAttribute ( )
3565
- . WithFeature ( "experimental-data-section-string-literals" , "0" ) ;
3594
+ . WithNoRefSafetyRulesAttribute ( ) ;
3566
3595
3567
3596
CompileAndVerify ( CreateEmptyCompilation ( source , parseOptions : parseOptions ) ,
3568
3597
verify : Verification . Skipped ,
@@ -3587,6 +3616,16 @@ public class InvalidOperationException();
3587
3616
. VerifyDiagnostics (
3588
3617
// warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
3589
3618
Diagnostic ( ErrorCode . WRN_NoRuntimeMetadataVersion ) . WithLocation ( 1 , 1 ) ) ;
3619
+
3620
+ // NOTE: If the feature is enabled by default in the future, it should not fail in case of missing Encoding members
3621
+ // (it should be automatically disabled instead and could warn) to avoid regressing the scenario above.
3622
+ CreateEmptyCompilation ( source ,
3623
+ parseOptions : parseOptions . WithFeature ( "experimental-data-section-string-literals" , "0" ) )
3624
+ . VerifyDiagnostics (
3625
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.get_UTF8'
3626
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "get_UTF8" ) . WithLocation ( 1 , 1 ) ,
3627
+ // error CS0656: Missing compiler required member 'System.Text.Encoding.GetString'
3628
+ Diagnostic ( ErrorCode . ERR_MissingPredefinedMember ) . WithArguments ( "System.Text.Encoding" , "GetString" ) . WithLocation ( 1 , 1 ) ) ;
3590
3629
}
3591
3630
3592
3631
[ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/76707" ) ]
0 commit comments