Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -46,9 +47,8 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
// Returns null if nothing to do, Diagnostic if there's an error to report, or RegexType if the type was analyzed successfully.
private static object? GetRegexTypeToEmit(Compilation compilation, MethodDeclarationSyntax methodSyntax, CancellationToken cancellationToken)
{
// TODO: Use https://github.com/dotnet/runtime/pull/59092
INamedTypeSymbol? regexSymbol = compilation.GetTypeByMetadataName(RegexName);
INamedTypeSymbol? regexGeneratorAttributeSymbol = compilation.GetTypeByMetadataName(RegexGeneratorAttributeName);
INamedTypeSymbol? regexSymbol = compilation.GetBestTypeByMetadataName(RegexName);
INamedTypeSymbol? regexGeneratorAttributeSymbol = compilation.GetBestTypeByMetadataName(RegexGeneratorAttributeName);
if (regexSymbol is null || regexGeneratorAttributeSymbol is null)
{
// Required types aren't available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
<PackageReference Include="Microsoft.DotNet.Build.Tasks.Packaging" Version="$(MicrosoftDotNetBuildTasksPackagingVersion)" PrivateAssets="all" />
</ItemGroup>

<!-- Code included from System.Text.RegularExpressions -->
<ItemGroup>
<Compile Include="..\..\Common\src\System\HexConverter.cs" Link="Production\HexConverter.cs" />
<Compile Include="..\..\Common\src\System\Text\ValueStringBuilder.cs" Link="Production\ValueStringBuilder.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
<!-- Common generator support -->
<Compile Include="$(CommonPath)Roslyn\GetBestTypeByMetadataName.cs" Link="Common\Roslyn\GetBestTypeByMetadataName.cs" />

<!-- Code included from System.Text.RegularExpressions -->
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Production\HexConverter.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs" Link="Production\ValueStringBuilder.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
<Compile Include="..\src\System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexBoyerMoore.cs" Link="Production\RegexBoyerMoore.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexCharClass.cs" Link="Production\RegexCharClass.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,34 @@ public partial struct E
", compile: true));
}

[Fact]
public async Task MultipleTypeDefinitions_DoesntBreakGeneration()
{
byte[] referencedAssembly = CreateAssemblyImage(@"
namespace System.Text.RegularExpressions;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class RegexGeneratorAttribute : Attribute
{
public RegexGeneratorAttribute(string pattern){}
}", "TestAssembly");

Assert.Empty(await RunGenerator(@"
using System.Text.RegularExpressions;
partial class C
{
[RegexGenerator(""abc"")]
private static partial Regex Valid();
}", compile: true, additionalRefs: new[] { MetadataReference.CreateFromImage(referencedAssembly) }));
}

private async Task<IReadOnlyList<Diagnostic>> RunGenerator(
string code, bool compile = false, LanguageVersion langVersion = LanguageVersion.Preview, CancellationToken cancellationToken = default)
string code, bool compile = false, LanguageVersion langVersion = LanguageVersion.Preview, MetadataReference[]? additionalRefs = null, CancellationToken cancellationToken = default)
{
var proj = new AdhocWorkspace()
.AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()))
.AddProject("RegexGeneratorTest", "RegexGeneratorTest.dll", "C#")
.WithMetadataReferences(s_refs)
.WithMetadataReferences(additionalRefs is not null ? s_refs.Concat(additionalRefs) : s_refs)
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithNullableContextOptions(NullableContextOptions.Enable))
.WithParseOptions(new CSharpParseOptions(langVersion))
Expand Down Expand Up @@ -513,10 +534,33 @@ private async Task<IReadOnlyList<Diagnostic>> RunGenerator(
return generatorResults.Diagnostics.Concat(results.Diagnostics).Where(d => d.Severity != DiagnosticSeverity.Hidden).ToArray();
}

private static byte[] CreateAssemblyImage(string source, string assemblyName)
{
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview)) },
s_refs.ToArray(),
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

var ms = new MemoryStream();
if (compilation.Emit(ms).Success)
{
return ms.ToArray();
}

throw new InvalidOperationException();
}

private static readonly MetadataReference[] s_refs = CreateReferences();

private static MetadataReference[] CreateReferences()
{
if (PlatformDetection.IsBrowser)
{
// These tests that use Roslyn don't work well on browser wasm today
return new MetadataReference[0];
}

string corelibPath = typeof(object).Assembly.Location;
return new[]
{
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@
<!-- https://github.com/dotnet/runtime/issues/58226 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn3.11.Unit.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.0.Unit.Tests.csproj" />

<!-- hhttps://github.com/dotnet/runtime/issues/60048 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Text.RegularExpressions\tests\System.Text.RegularExpressions.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Text.RegularExpressions\tests\System.Text.RegularExpressions.Generators.Tests\System.Text.RegularExpressions.Generators.Tests.csproj" />
</ItemGroup>

<!-- Aggressive Trimming related failures -->
Expand Down