Skip to content

Commit ce12420

Browse files
333fredAlekseyTs
andauthored
2 parents 087e571 + a555cad commit ce12420

File tree

21 files changed

+998
-652
lines changed

21 files changed

+998
-652
lines changed

docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -97,59 +97,6 @@ A possible workaround is to switch to using `>>>` operator:
9797
static C1 Test1(C1 x, int y) => x >>> y;
9898
```
9999

100-
## UTF8 String Literal conversion
101-
102-
***Introduced in .NET SDK 6.0.400, Visual Studio 2022 version 17.3.***
103-
The language added conversions between `string` constants and `byte` sequences
104-
where the text is converted into the equivalent UTF8 byte representation.
105-
Specifically the compiler allowed an implicit conversions from **`string` constants**
106-
to `byte[]`, `Span<byte>`, and `ReadOnlySpan<byte>` types.
107-
108-
The conversions can lead to an overload resolution failure due to an ambiguity for a code
109-
that compiled successfully before. For example:
110-
``` C#
111-
Test("s"); // error CS0121: The call is ambiguous between the following methods or properties: 'C.Test(ReadOnlySpan<char>)' and 'C.Test(byte[])'
112-
113-
static string Test(ReadOnlySpan<char> a) => "ReadOnlySpan";
114-
static string Test(byte[] a) => "array";
115-
```
116-
117-
A possible workaround is to apply an explicit cast to the constant string argument.
118-
119-
The conversions can lead to an invocation of a different member. For example:
120-
``` C#
121-
Test("s", (int)1); // Used to call `Test(ReadOnlySpan<char> a, long x)`, but calls `Test(byte[] a, int x)` now
122-
123-
static string Test(ReadOnlySpan<char> a, long x) => "ReadOnlySpan";
124-
static string Test(byte[] a, int x) => "array";
125-
```
126-
127-
A possible workaround is to apply an explicit cast to the constant string argument.
128-
129-
The conversions can lead to an invocation of an instance member where an extension method used to be invoked.
130-
For example:
131-
``` C#
132-
class Program
133-
{
134-
static void Main()
135-
{
136-
var p = new Program();
137-
p.M(""); // Used to call E.M, but calls Program.M now
138-
}
139-
140-
public string M(byte[] b) => "byte[]";
141-
}
142-
143-
static class E
144-
{
145-
public static string M(this object o, string s) => "string";
146-
}
147-
```
148-
149-
Possible workarounds are:
150-
1. Apply an explicit cast to the constant string argument.
151-
2. Call the extension method by using static method invocation syntax.
152-
153100
## Foreach enumerator as a ref struct
154101

155102
***Introduced in .NET SDK 6.0.300, Visual Studio 2022 version 17.2.*** A `foreach` using a ref struct enumerator type reports an error if the language version is set to 7.3 or earlier.

src/Analyzers/CSharp/Analyzers/UseUTF8StringLiteral/UseUTF8StringLiteralDiagnosticAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ protected override void InitializeWorker(AnalysisContext context)
5151

5252
var expressionType = context.Compilation.GetTypeByMetadataName(typeof(System.Linq.Expressions.Expression<>).FullName!);
5353

54-
context.RegisterOperationAction(c => AnalyzeOperation(c, expressionType), OperationKind.ArrayCreation);
54+
// Temporarily disabling, https://github.com/dotnet/roslyn/issues/61517 tracks the follow up work
55+
// context.RegisterOperationAction(c => AnalyzeOperation(c, expressionType), OperationKind.ArrayCreation);
5556
});
5657

5758
private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol? expressionType)

src/Analyzers/CSharp/Tests/UseUTF8StringLiteral/UseUTF8StringLiteralTests.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void M()
183183
}.RunAsync();
184184
}
185185

186-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
186+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
187187
public async Task TestSimpleByteArray()
188188
{
189189
await new VerifyCS.Test
@@ -211,7 +211,7 @@ public void M()
211211
}.RunAsync();
212212
}
213213

214-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
214+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
215215
public async Task TestConstant()
216216
{
217217
await new VerifyCS.Test
@@ -241,7 +241,7 @@ public void M()
241241
}.RunAsync();
242242
}
243243

244-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
244+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
245245
public async Task TestImplicitArray()
246246
{
247247
await new VerifyCS.Test
@@ -269,7 +269,7 @@ public void M()
269269
}.RunAsync();
270270
}
271271

272-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
272+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
273273
public async Task TestExplicitCast()
274274
{
275275
await new VerifyCS.Test
@@ -297,7 +297,7 @@ public void M()
297297
}.RunAsync();
298298
}
299299

300-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
300+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
301301
public async Task TestHexLiteral()
302302
{
303303
await new VerifyCS.Test
@@ -325,7 +325,7 @@ public void M()
325325
}.RunAsync();
326326
}
327327

328-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
328+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
329329
public async Task TestBinaryExpression()
330330
{
331331
await new VerifyCS.Test
@@ -353,7 +353,7 @@ public void M()
353353
}.RunAsync();
354354
}
355355

356-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
356+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
357357
public async Task TestEmptyArray()
358358
{
359359
await new VerifyCS.Test
@@ -380,7 +380,7 @@ public void M()
380380
}.RunAsync();
381381
}
382382

383-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
383+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
384384
public async Task TestTrivia1()
385385
{
386386
await new VerifyCS.Test
@@ -408,7 +408,7 @@ public void M()
408408
}.RunAsync();
409409
}
410410

411-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
411+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
412412
public async Task TestTrivia2()
413413
{
414414
await new VerifyCS.Test
@@ -436,7 +436,7 @@ public void M(byte[] b)
436436
}.RunAsync();
437437
}
438438

439-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
439+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
440440
public async Task TestMultiple()
441441
{
442442
await new VerifyCS.Test
@@ -468,7 +468,7 @@ public void M()
468468
}.RunAsync();
469469
}
470470

471-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
471+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
472472
public async Task TestEscapeChars()
473473
{
474474
await new VerifyCS.Test
@@ -496,7 +496,7 @@ public void M()
496496
}.RunAsync();
497497
}
498498

499-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
499+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
500500
public async Task TestEmoji()
501501
{
502502
await new VerifyCS.Test
@@ -581,7 +581,7 @@ public void M()
581581
}.RunAsync();
582582
}
583583

584-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
584+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
585585
public async Task TestUnicodeReplacementChar()
586586
{
587587
// The unicode replacement character is what is returned when, for example, an unpaired
@@ -612,7 +612,7 @@ public void M()
612612
}.RunAsync();
613613
}
614614

615-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
615+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
616616
public async Task TestCollectionInitializer()
617617
{
618618
await new VerifyCS.Test
@@ -716,7 +716,7 @@ public void Dispose(int a = 1, bool b = true, params byte[] others) { }
716716
}.RunAsync();
717717
}
718718

719-
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
719+
[Theory(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
720720
// Various cases copied from https://github.com/dotnet/runtime/blob/main/src/libraries/Common/tests/Tests/System/Net/aspnetcore/Http3/QPackDecoderTest.cs
721721
[InlineData(new byte[] { 0x37, 0x02, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65 }, "7translate")]
722722
[InlineData(new byte[] { 0x3f, 0x01 }, "?")]
@@ -782,7 +782,7 @@ public class C
782782
Assert.NotEqual(bytes, newBytes);
783783
}
784784

785-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
785+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
786786
public async Task TestParamArray1()
787787
{
788788
await new VerifyCS.Test
@@ -812,7 +812,7 @@ public void M(params byte[] b)
812812
}.RunAsync();
813813
}
814814

815-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
815+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
816816
public async Task TestParamArray2()
817817
{
818818
await new VerifyCS.Test
@@ -842,7 +842,7 @@ public void M(int i, params byte[] b)
842842
}.RunAsync();
843843
}
844844

845-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
845+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
846846
public async Task TestParamArray3()
847847
{
848848
await new VerifyCS.Test
@@ -872,7 +872,7 @@ public void M(params byte[] b)
872872
}.RunAsync();
873873
}
874874

875-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
875+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
876876
public async Task TestParamArray4()
877877
{
878878
await new VerifyCS.Test
@@ -902,7 +902,7 @@ public void M(params byte[] b)
902902
}.RunAsync();
903903
}
904904

905-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
905+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
906906
public async Task TestParamArray5()
907907
{
908908
await new VerifyCS.Test
@@ -932,7 +932,7 @@ public void M(params byte[] b)
932932
}.RunAsync();
933933
}
934934

935-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
935+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
936936
public async Task TestParamArray6()
937937
{
938938
await new VerifyCS.Test
@@ -981,7 +981,7 @@ public void M(int x, params byte[] b)
981981
}.RunAsync();
982982
}
983983

984-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
984+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
985985
public async Task TestParamArray8()
986986
{
987987
await new VerifyCS.Test
@@ -1011,7 +1011,7 @@ public void M(int x, params byte[] b)
10111011
}.RunAsync();
10121012
}
10131013

1014-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1014+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
10151015
public async Task TestParamArray9()
10161016
{
10171017
await new VerifyCS.Test
@@ -1041,7 +1041,7 @@ public void M(int x, params byte[] b)
10411041
}.RunAsync();
10421042
}
10431043

1044-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1044+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
10451045
public async Task TestParamArray10()
10461046
{
10471047
await new VerifyCS.Test
@@ -1071,7 +1071,7 @@ public void M(int x, params byte[] b)
10711071
}.RunAsync();
10721072
}
10731073

1074-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1074+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
10751075
public async Task TestParamArray11()
10761076
{
10771077
await new VerifyCS.Test
@@ -1101,7 +1101,7 @@ public void M(int x, int y, int z, params byte[] b)
11011101
}.RunAsync();
11021102
}
11031103

1104-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1104+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
11051105
public async Task TestParamArray12()
11061106
{
11071107
await new VerifyCS.Test
@@ -1131,7 +1131,7 @@ public C(params byte[] b)
11311131
}.RunAsync();
11321132
}
11331133

1134-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1134+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
11351135
public async Task TestParamArray13()
11361136
{
11371137
await new VerifyCS.Test
@@ -1171,7 +1171,7 @@ public void M()
11711171
}.RunAsync();
11721172
}
11731173

1174-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1174+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
11751175
public async Task TestParamArray14()
11761176
{
11771177
await new VerifyCS.Test
@@ -1225,7 +1225,7 @@ public sealed class IsExternalInit
12251225
}.RunAsync();
12261226
}
12271227

1228-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1228+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
12291229
public async Task TestParamArray15()
12301230
{
12311231
await new VerifyCS.Test
@@ -1313,7 +1313,7 @@ public B(params byte[] bytes)
13131313
}.RunAsync();
13141314
}
13151315

1316-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1316+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
13171317
public async Task TestParamArray16()
13181318
{
13191319
await new VerifyCS.Test
@@ -1343,7 +1343,7 @@ public void M(int[] i, byte[] b)
13431343
}.RunAsync();
13441344
}
13451345

1346-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1346+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
13471347
public async Task TestParamArray17()
13481348
{
13491349
await new VerifyCS.Test
@@ -1373,7 +1373,7 @@ public void M(int[] i, params byte[] b)
13731373
}.RunAsync();
13741374
}
13751375

1376-
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
1376+
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/61517"), Trait(Traits.Feature, Traits.Features.CodeActionsUseUTF8StringLiteral)]
13771377
public async Task TestMultidimensionalArray()
13781378
{
13791379
await new VerifyCS.Test

src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,7 @@ internal static uint GetValEscape(BoundExpression expr, uint scopeOfTheContainin
26132613
case BoundKind.DefaultExpression:
26142614
case BoundKind.Parameter:
26152615
case BoundKind.ThisReference:
2616+
case BoundKind.UTF8String:
26162617
// always returnable
26172618
return Binder.ExternalScope;
26182619

@@ -3018,6 +3019,7 @@ internal static bool CheckValEscape(SyntaxNode node, BoundExpression expr, uint
30183019
case BoundKind.DefaultExpression:
30193020
case BoundKind.Parameter:
30203021
case BoundKind.ThisReference:
3022+
case BoundKind.UTF8String:
30213023
// always returnable
30223024
return true;
30233025

src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6058,7 +6058,7 @@ private BoundUTF8String BindUTF8StringLiteral(LiteralExpressionSyntax node, Bind
60586058
CheckFeatureAvailability(node, MessageID.IDS_FeatureUTF8StringLiterals, diagnostics);
60596059

60606060
var value = (string)node.Token.Value;
6061-
var type = ArrayTypeSymbol.CreateSZArray(Compilation.Assembly, TypeWithAnnotations.Create(GetSpecialType(SpecialType.System_Byte, diagnostics, node)));
6061+
var type = GetWellKnownType(WellKnownType.System_ReadOnlySpan_T, diagnostics, node).Construct(GetSpecialType(SpecialType.System_Byte, diagnostics, node));
60626062

60636063
return new BoundUTF8String(node, value, type);
60646064
}

0 commit comments

Comments
 (0)