Skip to content

Commit 2e5a08d

Browse files
committed
fixes crash when resolving types in anonymous function expressions declaration (#331)
1 parent 37e2926 commit 2e5a08d

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

Cecilifier.Core.Tests/Tests/Unit/Miscellaneous.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,11 @@ public void TypeDeclarationResolverTests(string code)
347347
}
348348
}
349349

350-
[Test, Ignore("Issue #331")]
351-
public void Lambda_With_DefaultParameter()
350+
[TestCase("var f = (int i) => i + 1;")]
351+
[TestCase("System.Func<int, int> f = (int i) => i + 1;")]
352+
public void SimpleLambda_ToDelegateConversion_DoesNotCrash(string code)
352353
{
353-
var ctx = RunCecilifier("var f = (int i = 41) => i + 1;");
354+
var ctx = RunCecilifier(code);
354355
Assert.Pass();
355356
}
356357

Cecilifier.Core.Tests/Tests/Unit/NonCapturingLambdaProcessorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void Conversion_FromDelegates_OtherThanFuncAndAction_AreReported(string s
4949
{
5050
var context = RunProcessorOn(source);
5151
Assert.That(context.Output, Does.Not.Contains("//Synthetic method for lambda expression: i => i + 1"));
52-
Assert.That(context.Output, Contains.Substring("//Lambda to delegates conversion is only supported for Func<> and Action<>"));
52+
Assert.That(context.Output, Contains.Substring("#warning Lambda to delegates conversion is only supported for Func<> and Action<>"));
5353
}
5454

5555
[TestCase("using System; class Foo { void M(Func<int, int> a) { M(x => x + 1); } }", TestName = "Expression")]

Cecilifier.Core/AST/NonCapturingLambdaProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void InjectSyntheticMethodsForNonCapturingLambdas(IVisitorContext
2222

2323
if (!IsValidConversion(context, lambda))
2424
{
25-
context.WriteComment("Lambda to delegates conversion is only supported for Func<> and Action<>");
25+
context.EmitWarning($"Lambda to delegates conversion is only supported for Func<> and Action<>: {node.HumanReadableSummary()}", node);
2626
continue;
2727
}
2828

Cecilifier.Core/AST/SyntaxWalkerBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ protected static void WriteCecilExpression(IVisitorContext context, string value
374374

375375
protected string ResolveExpressionType(ExpressionSyntax expression)
376376
{
377-
var type = Context.GetTypeInfo(expression).Type.EnsureNotNull();
377+
var typeInfo = Context.GetTypeInfo(expression);
378+
var type = (typeInfo.Type ?? typeInfo.ConvertedType).EnsureNotNull();
378379
return Context.TypeResolver.Resolve(type);
379380
}
380381

@@ -941,11 +942,16 @@ private static IEnumerable<string> ProcessNormalMemberAttribute(IVisitorContext
941942

942943
protected void LogUnsupportedSyntax(SyntaxNode node)
943944
{
944-
Context.EmitWarning($"Syntax {node.Kind()} ({node.HumanReadableSummary()}) is not supported.\nGenerated code may not compile, or if it compiles, produce invalid results.", node);
945+
LogWarning($"Syntax {node.Kind()} ({node.HumanReadableSummary()}) is not supported.", node);
945946
var lineSpan = node.GetLocation().GetLineSpan();
946947
AddCecilExpression($"/* Syntax '{node.Kind()}' is not supported in {lineSpan.Path} ({lineSpan.Span.Start.Line + 1},{lineSpan.Span.Start.Character + 1}):\n------\n{node}\n----*/");
947948
}
948949

950+
private void LogWarning(string message, SyntaxNode node)
951+
{
952+
Context.EmitWarning($"{message}\nGenerated code may not compile, or if it compiles, produce invalid results.", node);
953+
}
954+
949955
// Methods implementing explicit interfaces, static abstract methods from interfaces and overriden methods with covariant return types
950956
// needs to explicitly specify which methods they override.
951957
protected void AddToOverridenMethodsIfAppropriated(string methodVar, IMethodSymbol method)

0 commit comments

Comments
 (0)