yanghuan / CSharp.lua

The C# to Lua compiler
Other
1.23k stars 202 forks source link

Aggressive inlining in indexer causes compiler null reference exception #485

Closed joelverhagen closed 10 months ago

joelverhagen commented 10 months ago

Repro:

using System.Runtime.CompilerServices;

public class LocationIntDictionary
{
    public int this[Location index]
    {
        get => GetIndex(index);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int GetIndex(Location location)
    {
        return location.X;
    }
}

public struct Location
{
    public Location(int x)
    {
        X = x;
    }

    public int X;
}

Error:

CSharpLua.BugErrorException: SourceLocation(C:\Users\joelv\Desktop\LuaTest\LuaTest\ClassA.cs@5:1)"public class LocationIntDictionary
{
    public int this[Location index]
    {
        get => GetIndex(index);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int GetIndex(Location location)
    {
        return location.X;
    }
}": Compiler has a bug, thanks to commit issue at https://github.com/yanghuan/CSharp.lua/issue
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at CSharpLua.LuaSyntaxNodeTransform.InliningInvocationExpression(SyntaxNode root, IMethodSymbol symbol, Func`1 invocationFn, LuaExpressionSyntax& inlineExpression) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.Helper.cs:line 2110        
   at CSharpLua.LuaSyntaxNodeTransform.InliningInvocationExpression(SyntaxNode root, IMethodSymbol symbol, LuaInvocationExpressionSyntax invocation, LuaExpressionSyntax& inlineExpression) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.Helper.cs:line 2285
   at CSharpLua.LuaSyntaxNodeTransform.VisitInvocationExpression(InvocationExpressionSyntax node) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 2426
   at Microsoft.CodeAnalysis.CSharp.Syntax.InvocationExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
   at CSharpLua.Utility.Accept[T](CSharpSyntaxNode node, LuaSyntaxNodeTransform transform) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\Utils.cs:line 1494
   at CSharpLua.Utility.AcceptExpression(CSharpSyntaxNode node, LuaSyntaxNodeTransform transform) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\Utils.cs:line 1498
   at CSharpLua.LuaSyntaxNodeTransform.VisitExpression(ExpressionSyntax node) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.Helper.cs:line 1441
   at CSharpLua.LuaSyntaxNodeTransform.VisitArrowExpressionClause(ArrowExpressionClauseSyntax node) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.Object.cs:line 1330
   at Microsoft.CodeAnalysis.CSharp.Syntax.ArrowExpressionClauseSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
   at CSharpLua.Utility.Accept[T](CSharpSyntaxNode node, LuaSyntaxNodeTransform transform) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\Utils.cs:line 1494
   at CSharpLua.Utility.AcceptExpression(CSharpSyntaxNode node, LuaSyntaxNodeTransform transform) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\Utils.cs:line 1498
   at CSharpLua.LuaSyntaxNodeTransform.<>c__DisplayClass114_2.<VisitIndexerDeclaration>b__2(LuaFunctionExpressionSyntax function, LuaPropertyOrEventIdentifierNameSyntax name) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 1261       
   at CSharpLua.LuaSyntaxNodeTransform.<>c__DisplayClass114_0.<VisitIndexerDeclaration>g__Fill|0(Action`2 action, <>c__DisplayClass114_1&) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 1248
   at CSharpLua.LuaSyntaxNodeTransform.VisitIndexerDeclaration(IndexerDeclarationSyntax node) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 1255
   at Microsoft.CodeAnalysis.CSharp.Syntax.IndexerDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
   at CSharpLua.LuaSyntaxNodeTransform.BuildTypeMembers(LuaTypeDeclarationSyntax typeDeclaration, TypeDeclarationSyntax node) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 298
   at CSharpLua.LuaSyntaxNodeTransform.BuildTypeDeclaration(INamedTypeSymbol typeSymbol, TypeDeclarationSyntax node, LuaTypeDeclarationSyntax typeDeclaration) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 404
   at CSharpLua.LuaSyntaxNodeTransform.VisitTypeDeclaration(INamedTypeSymbol typeSymbol, TypeDeclarationSyntax node, LuaTypeDeclarationSyntax typeDeclaration) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 507
   at CSharpLua.LuaSyntaxNodeTransform.VisitClassDeclaration(ClassDeclarationSyntax node) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 574
   at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
   at CSharpLua.LuaSyntaxNodeTransform.BlockCommonNode.Visit(LuaSyntaxNodeTransform transform) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxNodeTransform.cs:line 1351
   --- End of inner exception stack trace ---
   at CSharpLua.LuaSyntaxGenerator.Create(Boolean isSingleFile) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxGenerator.cs:line 240
   at CSharpLua.LuaSyntaxGenerator.GenerateSingleFile(String outFile, String outFolder, IEnumerable`1 luaSystemLibs) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\LuaSyntaxGenerator.cs:line 286
   at CSharpLua.Compiler.Compile() in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua\Compiler.cs:line 129
   at CSharpLua.Program.Main(String[] args) in C:\z\Git\joelverhagen\CSharp.lua\CSharp.lua.Launcher\Program.cs:line 90
require done
C:\Users\joelv\Downloads\lua-5.3.6_Win64_bin\lua53.exe: .\main.lua:6: attempt to index a nil value (global 'Knapcode')
stack traceback:
        .\main.lua:6: in main chunk
        [C]: in ?