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

Attempted to access an element as a type incompatible with the array. #860

Closed
n9 opened this issue Oct 27, 2022 · 3 comments · Fixed by #879
Closed

Attempted to access an element as a type incompatible with the array. #860

n9 opened this issue Oct 27, 2022 · 3 comments · Fixed by #879
Labels

Comments

@n9
Copy link

n9 commented Oct 27, 2022

Version: 2.12.27 (current in nuget, but not as a release in this repo)
Caller: .NET 6 (Windows)
Target: .NET 6 (Android)

Exception: StreamJsonRpc.RemoteInvocationException
ErrorCode: -32000 (StreamJsonRpc.Protocol.JsonRpcErrorCode.InvocationError)
ErrorData.TypeName: System.ArrayTypeMismatchException
ErrorData.Message: Attempted to access an element as a type incompatible with the array.
ErrorData.Stack:

   at StreamJsonRpc.TargetMethod.TryGetArguments(JsonRpcRequest request, MethodSignature method, Span`1 arguments)
   at StreamJsonRpc.TargetMethod..ctor(JsonRpcRequest request, List`1 candidateMethodTargets, SynchronizationContext fallbackSynchronizationContext)
   at StreamJsonRpc.Reflection.RpcTargetInfo.TryGetTargetMethod(JsonRpcRequest request, TargetMethod& targetMethod)
   at StreamJsonRpc.JsonRpc.DispatchIncomingRequestAsync(JsonRpcRequest request)

Signature of the called method:

Task Start();

Not sure how mono is currently integrated into .NET 6 (for Android), there is already one ticket with the same error message: #387

@n9
Copy link
Author

n9 commented Oct 27, 2022

@AArnott Could you please release preview/testing nuget with modified "mono" check (that will also work on Android).

The current check is not working on Android:

internal static bool IsRunningOnMono => Type.GetType("Mono.Runtime") is not null;

But the following check is:

Type.GetType("Mono.RuntimeStructs") != null

Source:

https://github.com/dotnet/runtime/blob/9e1350eb6d8d6018d599b7fefbae6805dbd420ea/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs#L30

@n9
Copy link
Author

n9 commented Oct 28, 2022

The issue appears only when the method has no parameters. "Mono" (.net 6 on android) returns in that case RuntimeParameterInfo[] instead of ParameterInfo[].

In that case, AsSpan will fail, but ReadOnlySpan will work:

static void Main()
{
        var type = Type.GetType("System.Object");
        TestMethod(type, "ReferenceEquals");
        TestMethod(type, "ToString");
}

static void TestMethod(Type type, string name)
{
        var method = type.GetMethod(name);
        var parameters = method.GetParameters();
        new ReadOnlySpan<ParameterInfo>(parameters, 0, 0);
        Span<ParameterInfo> sp = parameters.AsSpan(0, 0); // <- this will fail on "mono" in case of method with 0 parameters
}

The following example will fail also in "vanilla" .net 6 (on windows):

class A { }
class B : A { }

void Main()
{
	new ReadOnlySpan<A>(Array.Empty<B>());
	((A[])Array.Empty<B>()).AsSpan(); // <- error here
}

I think it is correct that AsSpan will fail in that case.

So, the fix should not improve "mono" detection, but use ReadOnlySpan<T> instead of AsSpan.

Do you agree?

@AArnott
Copy link
Member

AArnott commented Jan 7, 2023

Excellent analysis, @n9. I'm preparing the change now.

@AArnott AArnott added the bug label Jan 7, 2023
AArnott added a commit to AArnott/vs-streamjsonrpc that referenced this issue Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants