Skip to content

Commit

Permalink
Add extract method support for ref struct interfaces
Browse files Browse the repository at this point in the history
The method extraction code uses ITypeParameterSymbol's constraints to generate the new method's text. This codepath had not yet been modified to support ITypeParameterSymbol.AllowsRefLikeType

This is in support of the "allows ref struct" on interfaces feature outlined here: #72124

This ref structs for interfaces feature was merged via this PR: #73567
  • Loading branch information
ToddGrun committed May 21, 2024
1 parent db31e78 commit 9f40e07
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ class Program
await TestExtractMethodAsync(code, expected);
}

[Fact]
public async Task SelectTypeParameterWithAllowsRefStructAntiConstraint()
{
var code = """
using System;

class Program
{
void MyMethod1<TT>(TT tt) where TT : IDisposable, allows ref struct
{
[|tt.Dispose();|]
}
}
""";
var expected = """
using System;

class Program
{
void MyMethod1<TT>(TT tt) where TT : IDisposable, allows ref struct
{
NewMethod(tt);
}

private static void NewMethod<TT>(TT tt) where TT : IDisposable, allows ref struct
{
tt.Dispose();
}
}
""";

await TestExtractMethodAsync(code, expected);
}
[Fact]
public async Task SelectTypeParameter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ protected ImmutableArray<ITypeParameterSymbol> CreateMethodTypeParameters()
typeParameters.Add(CodeGenerationSymbolFactory.CreateTypeParameter(
parameter.GetAttributes(), parameter.Variance, parameter.Name, [], parameter.NullableAnnotation,
parameter.HasConstructorConstraint, parameter.HasReferenceTypeConstraint, parameter.HasUnmanagedTypeConstraint,
parameter.HasValueTypeConstraint, parameter.HasNotNullConstraint, parameter.Ordinal));
parameter.HasValueTypeConstraint, parameter.AllowsRefLikeType, parameter.HasNotNullConstraint, parameter.Ordinal));
}

return typeParameters.ToImmutableAndFree();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private static ImmutableArray<ITypeParameterSymbol> RenameTypeParameters(
typeParameter.HasConstructorConstraint,
typeParameter.HasReferenceTypeConstraint,
typeParameter.HasValueTypeConstraint,
typeParameter.AllowsRefLikeType,
typeParameter.HasUnmanagedTypeConstraint,
typeParameter.HasNotNullConstraint,
typeParameter.Ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ private static void AddConstraintClauses(
constraints.Add(ConstructorConstraint());
}

if (typeParameter.AllowsRefLikeType)
{
// "allows ref struct" anti-constraint must be last
constraints.Add(AllowsConstraintClause([RefStructConstraint()]));
}

if (constraints.Count == 0)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ public static ITypeParameterSymbol CreateTypeParameterSymbol(string name, int or
attributes: default, varianceKind: VarianceKind.None,
name: name, constraintTypes: [],
hasConstructorConstraint: false, hasReferenceConstraint: false, hasValueConstraint: false,
hasUnmanagedConstraint: false, hasNotNullConstraint: false, ordinal: ordinal);
allowsRefLikeType: false, hasUnmanagedConstraint: false, hasNotNullConstraint: false,

Check failure on line 325 in src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs#L325

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs(325,98): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 325 in src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs#L325

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs(325,98): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 325 in src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs#L325

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/CodeGenerationSymbolFactory.cs(325,98): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
ordinal: ordinal);
}

/// <summary>
Expand All @@ -337,10 +338,11 @@ public static ITypeParameterSymbol CreateTypeParameter(
bool hasReferenceConstraint = false,
bool hasUnmanagedConstraint = false,
bool hasValueConstraint = false,
bool allowsRefLikeType = false,
bool hasNotNullConstraint = false,
int ordinal = 0)
{
return new CodeGenerationTypeParameterSymbol(null, attributes, varianceKind, name, nullableAnnotation, constraintTypes, hasConstructorConstraint, hasReferenceConstraint, hasValueConstraint, hasUnmanagedConstraint, hasNotNullConstraint, ordinal);
return new CodeGenerationTypeParameterSymbol(null, attributes, varianceKind, name, nullableAnnotation, constraintTypes, hasConstructorConstraint, hasReferenceConstraint, hasValueConstraint, hasUnmanagedConstraint, allowsRefLikeType, hasNotNullConstraint, ordinal);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class CodeGenerationTypeParameterSymbol(
bool hasConstructorConstraint,
bool hasReferenceConstraint,
bool hasValueConstraint,
bool allowsRefLikeType,
bool hasUnmanagedConstraint,
bool hasNotNullConstraint,
int ordinal) : CodeGenerationTypeSymbol(containingType?.ContainingAssembly, containingType, attributes, Accessibility.NotApplicable, default, name, SpecialType.None, nullableAnnotation), ITypeParameterSymbol
Expand All @@ -27,7 +28,7 @@ internal class CodeGenerationTypeParameterSymbol(
public bool HasConstructorConstraint { get; } = hasConstructorConstraint;
public bool HasReferenceTypeConstraint { get; } = hasReferenceConstraint;
public bool HasValueTypeConstraint { get; } = hasValueConstraint;
public bool AllowsRefLikeType => false;
public bool AllowsRefLikeType { get; } = allowsRefLikeType;
public bool HasUnmanagedTypeConstraint { get; } = hasUnmanagedConstraint;
public bool HasNotNullConstraint { get; } = hasNotNullConstraint;
public int Ordinal { get; } = ordinal;
Expand All @@ -37,7 +38,8 @@ protected override CodeGenerationTypeSymbol CloneWithNullableAnnotation(Nullable
return new CodeGenerationTypeParameterSymbol(
this.ContainingType, this.GetAttributes(), this.Variance, this.Name, nullableAnnotation,
this.ConstraintTypes, this.HasConstructorConstraint, this.HasReferenceTypeConstraint,
this.HasValueTypeConstraint, this.HasUnmanagedTypeConstraint, this.HasNotNullConstraint, this.Ordinal);
this.HasValueTypeConstraint, this.AllowsRefLikeType, this.HasUnmanagedTypeConstraint,

Check failure on line 41 in src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/Symbols/CodeGenerationTypeParameterSymbol.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/Symbols/CodeGenerationTypeParameterSymbol.cs#L41

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/Symbols/CodeGenerationTypeParameterSymbol.cs(41,98): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/Symbols/CodeGenerationTypeParameterSymbol.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/Symbols/CodeGenerationTypeParameterSymbol.cs#L41

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeGeneration/Symbols/CodeGenerationTypeParameterSymbol.cs(41,98): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
this.HasNotNullConstraint, this.Ordinal);
}

public new ITypeParameterSymbol OriginalDefinition => this;
Expand Down

0 comments on commit 9f40e07

Please sign in to comment.