Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception when using funcs and take another approach to escape assembly name #1836

Merged
merged 2 commits into from
Oct 18, 2024
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
4 changes: 2 additions & 2 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
provider.IsCsWinRTCcwLookupTableGeneratorEnabled())
);

var assemblyName = context.CompilationProvider.Select(static (compilation, _) => GeneratorHelper.EscapeTypeNameForIdentifier(compilation.AssemblyName));
var assemblyName = context.CompilationProvider.Select(static (compilation, _) => GeneratorHelper.EscapeAssemblyNameForIdentifier(compilation.AssemblyName));

var propertiesAndAssemblyName = properties.Combine(assemblyName);

Expand Down Expand Up @@ -1107,7 +1107,7 @@ private static EquatableArray<VtableAttribute> GetVtableAttributesToAddOnLookupT

if (context.Node is InvocationExpressionSyntax invocation)
{
var invocationSymbol = context.SemanticModel.GetSymbolInfo(invocation.Expression).Symbol;
var invocationSymbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol;
if (invocationSymbol is IMethodSymbol methodSymbol &&
// Filter checks for boxing and casts to ones calling CsWinRT projected functions and
// functions within same assembly. Functions within same assembly can take a boxed value
Expand Down
5 changes: 5 additions & 0 deletions src/Authoring/WinRT.SourceGenerator/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,11 @@ public static string GetAbiMarshalerType(string type, string abiType, TypeKind k
throw new ArgumentException();
}

public static string EscapeAssemblyNameForIdentifier(string typeName)
{
return Regex.Replace(typeName, """[^a-zA-Z0-9_]""", "_");
}

public static string EscapeTypeNameForIdentifier(string typeName)
{
return Regex.Replace(typeName, """[(\ |:<>,\.\-@;+'^!`)]""", "_");
Expand Down
2 changes: 1 addition & 1 deletion src/Authoring/WinRT.SourceGenerator/WinRTAotCodeFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public override void Initialize(AnalysisContext context)

if (context.Node is InvocationExpressionSyntax invocation)
{
var invocationSymbol = context.SemanticModel.GetSymbolInfo(invocation.Expression).Symbol;
var invocationSymbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol;
if (invocationSymbol is IMethodSymbol methodSymbol)
{
var taskAdapter = GeneratorHelper.GetTaskAdapterIfAsyncMethod(methodSymbol);
Expand Down
11 changes: 11 additions & 0 deletions src/Tests/FunctionalTests/Collections/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,23 @@
return 101;
}

// Testing to ensure no exceptions from any of the analyzers while building.
Action<int, int> s = (a, b) => { _ = a + b; };
ActionToFunction(s)(2, 3);

return 100;

static bool SequencesEqual<T>(IEnumerable<T> x, params IEnumerable<T>[] list) => list.All((y) => x.SequenceEqual(y));

static bool AllEqual<T>(T[] x, params T[][] list) => list.All((y) => x.SequenceEqual(y));

static Func<TA1, TA2, TA1> ActionToFunction<TA1, TA2>(Action<TA1, TA2> action) =>
(a1, a2) =>
{
action(a1, a2);
return a1;
};

sealed partial class CustomClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
Expand Down