Skip to content

Fix type inference issues with generic method calls. #30

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

Open
wants to merge 1 commit into
base: metadata-provider
Choose a base branch
from
Open
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
27 changes: 24 additions & 3 deletions Backend/Analyses/TypeInferenceAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,30 @@ public override void Visit(CreateObjectInstruction instruction)

public override void Visit(MethodCallInstruction instruction)
{
if (instruction.HasResult)
{
instruction.Result.Type = instruction.Method.ReturnType;
if (instruction.HasResult)
{
/*
You have class A<T> with method T Get().
In IL a method call to A<int32>::Get() looks like: callvirt instance !0 class A`1<int32>::Get()
We want the int32 (generic argument) not the generic reference as the return type .
The returned value can also be used as the return value of the current method.
If the current method's return type is int32, then !0 is not compatible.

int Foo(){
A<int32> a = new A<int32>();
int b = a.Get();
return b;
}
*/

if (instruction.Method.ReturnType is IGenericParameterReference genericParamRef)
{
if (genericParamRef.Kind == GenericParameterKind.Type)
instruction.Result.Type = instruction.Method.ContainingType.GenericArguments.ElementAt(genericParamRef.Index);
else
instruction.Result.Type = instruction.Method.GenericArguments.ElementAt(genericParamRef.Index);
} else
instruction.Result.Type = instruction.Method.ReturnType;
}

// Skip implicit "this" parameter.
Expand Down