Skip to content

Commit

Permalink
Merge branch 'features/FirstClassSpan' into FirstClassSpan-03-Betterness
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed Jun 13, 2024
2 parents 89674c3 + c24c886 commit e57cb21
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void DiagnosticAnalyzerAllInOne()
missingSyntaxKinds.Add(SyntaxKind.SpreadElement);

var analyzer = new CSharpTrackingDiagnosticAnalyzer();
var options = new AnalyzerOptions(new[] { new TestAdditionalText() }.ToImmutableArray<AdditionalText>());
var options = new AnalyzerOptions([new TestAdditionalText()]);
CreateCompilationWithMscorlib45(source).VerifyAnalyzerDiagnostics(new[] { analyzer }, options);
analyzer.VerifyAllAnalyzerMembersWereCalled();
analyzer.VerifyAnalyzeSymbolCalledForAllSymbolKinds();
Expand Down Expand Up @@ -99,7 +99,7 @@ public class C
public void AnalyzerDriverIsSafeAgainstAnalyzerExceptions()
{
var compilation = CreateCompilationWithMscorlib45(TestResource.AllInOneCSharpCode);
var options = new AnalyzerOptions(new[] { new TestAdditionalText() }.ToImmutableArray<AdditionalText>());
var options = new AnalyzerOptions([new TestAdditionalText()]);

ThrowingDiagnosticAnalyzer<SyntaxKind>.VerifyAnalyzerEngineIsSafeAgainstExceptions(analyzer =>
compilation.GetAnalyzerDiagnostics(new[] { analyzer }, options));
Expand All @@ -111,7 +111,7 @@ public void AnalyzerOptionsArePassedToAllAnalyzers()
var text = new StringText(string.Empty, encodingOpt: null);
AnalyzerOptions options = new AnalyzerOptions
(
new[] { new TestAdditionalText("myfilepath", text) }.ToImmutableArray<AdditionalText>()
[new TestAdditionalText("myfilepath", text)]
);

var compilation = CreateCompilationWithMscorlib45(TestResource.AllInOneCSharpCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
Expand Down
27 changes: 26 additions & 1 deletion src/Compilers/CSharp/Test/Emit3/FirstClassSpanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ class C
Diagnostic(ErrorCode.ERR_NoImplicitConv, "arg").WithArguments("int[]", "System.Span<string>").WithLocation(3, 41));
}

// PROTOTYPE: Revisit all nullable analysis tests.
[Fact]
public void Conversion_Array_Span_Implicit_NullableAnalysis()
{
Expand Down Expand Up @@ -1001,6 +1002,30 @@ .locals init (string[] V_0)
""");
}

[Theory, MemberData(nameof(LangVersions))]
public void Conversion_Array_Span_Implicit_RefReturn(LanguageVersion langVersion)
{
var source = """
using System;
class C
{
ref Span<string> M1(ref string[] arg) => ref arg;
ref ReadOnlySpan<string> M2(ref string[] arg) => ref arg;
ref ReadOnlySpan<object> M3(ref string[] arg) => ref arg;
}
""";
CreateCompilationWithSpan(source, parseOptions: TestOptions.Regular.WithLanguageVersion(langVersion)).VerifyDiagnostics(
// (4,50): error CS8151: The return expression must be of type 'Span<string>' because this method returns by reference
// ref Span<string> M1(ref string[] arg) => ref arg;
Diagnostic(ErrorCode.ERR_RefReturnMustHaveIdentityConversion, "arg").WithArguments("System.Span<string>").WithLocation(4, 50),
// (5,58): error CS8151: The return expression must be of type 'ReadOnlySpan<string>' because this method returns by reference
// ref ReadOnlySpan<string> M2(ref string[] arg) => ref arg;
Diagnostic(ErrorCode.ERR_RefReturnMustHaveIdentityConversion, "arg").WithArguments("System.ReadOnlySpan<string>").WithLocation(5, 58),
// (6,58): error CS8151: The return expression must be of type 'ReadOnlySpan<object>' because this method returns by reference
// ref ReadOnlySpan<object> M3(ref string[] arg) => ref arg;
Diagnostic(ErrorCode.ERR_RefReturnMustHaveIdentityConversion, "arg").WithArguments("System.ReadOnlySpan<object>").WithLocation(6, 58));
}

[Fact]
public void Conversion_Array_Span_Implicit_Value()
{
Expand Down Expand Up @@ -1641,7 +1666,7 @@ class C<T, U> {{constraints}}
Diagnostic(ErrorCode.ERR_NoExplicitConv, "(Span<U>)x").WithArguments("T[]", "System.Span<U>").WithLocation(7, 26));
}

// PROTOTYPE: User-defined conversions should not be considered? Note: this is not the only test affected.
// PROTOTYPE: User-defined conversions should not be considered? Note: this is not the only test affected. In particular, nullable analysis tests should be revisited.
[Fact]
public void Conversion_Array_Span_Variance_02()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void InitializeTest()
var parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Regular, documentationMode: DocumentationMode.None)
.WithFeatures(new[] { new KeyValuePair<string, string>("IOperation", "true") });
var compilation = CreateCompilation(code, parseOptions: parseOptions);
var options = new AnalyzerOptions(new[] { new TestAdditionalText() }.ToImmutableArray<AdditionalText>());
var options = new AnalyzerOptions([new TestAdditionalText()]);

Verify(compilation, options, nameof(AnalysisContext.RegisterCodeBlockAction));
Verify(compilation, options, nameof(AnalysisContext.RegisterCodeBlockStartAction));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ internal static T[] Copy<T>(this T[] array, int start, int length)
return newArray;
}

public static int IndexOf<T>(this T[] array, T value)
=> Array.IndexOf(array, value);

public static bool Contains<T>(this T[] array, T value)
=> Array.IndexOf(array, value) >= 0;

internal static T[] InsertAt<T>(this T[] array, int position, T item)
{
T[] newArray = new T[array.Length + 1];
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/Test/Core/TargetFrameworkUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.CodeAnalysis.CodeGen;
using System.Reflection;
using System.Collections.Concurrent;
using Roslyn.Utilities;

namespace Roslyn.Test.Utilities
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.QuickInfo;
using Roslyn.LanguageServer.Protocol;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.LanguageServer.Handler
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tools/GenerateRulesMissingDocumentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ await checkHelpLinkAsync(helpLinkUri).ConfigureAwait(false))
{
// We consider having "extra" entries as valid. This is to prevent CI failures due to rules being documented.
// However, we consider "missing" entries as invalid. This is to force updating the file when new rules are added.
if (!actualContent.Contains(line))
if (Array.IndexOf(actualContent, line) < 0)
{
await Console.Error.WriteLineAsync($"Missing entry in '{fileWithPath}'. Please add the below entry to this file to fix the build:").ConfigureAwait(false);
await Console.Error.WriteLineAsync(line).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CodeRefactorings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using System.Reflection;
using System.Threading;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Host.Mef;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
using Xunit.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.CodeAnalysis.Shared.Extensions;
Expand All @@ -11,7 +10,4 @@ internal static class ArrayExtensions
{
public static bool IsNullOrEmpty<T>([NotNullWhen(returnValue: false)] this T[]? array)
=> array == null || array.Length == 0;

public static bool Contains<T>(this T[] array, T item)
=> Array.IndexOf(array, item) >= 0;
}

0 comments on commit e57cb21

Please sign in to comment.