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

Calls to Object.GetType() on value types generates incorrect code #298

Closed
adrianoc opened this issue Jun 9, 2024 · 0 comments
Closed

Calls to Object.GetType() on value types generates incorrect code #298

adrianoc opened this issue Jun 9, 2024 · 0 comments
Labels
area:value-types 🐛 bug Something isn't working
Milestone

Comments

@adrianoc
Copy link
Owner

adrianoc commented Jun 9, 2024

int i = 10;
Console.WriteLine(i.GetType().Name);

generates

//int i = 10;
var lv_i_6 = new VariableDefinition(assembly.MainModule.TypeSystem.Int32);
md_TopLevelStatements_1.Body.Variables.Add(lv_i_6);
il_TopLevelMain_3.Emit(OpCodes.Ldc_I4, 10);
il_TopLevelMain_3.Emit(OpCodes.Stloc, lv_i_6);

// All good so far

//Console.WriteLine(i.GetType().Name);
// The following instruction is wrong.We should box the value instead.
il_TopLevelMain_3.Emit(OpCodes.Ldloca, lv_i_6); 

il_TopLevelMain_3.Emit(OpCodes.Callvirt, assembly.MainModule.ImportReference(TypeHelpers.ResolveMethod(typeof(System.Object), "GetType",System.Reflection.BindingFlags.Default|System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Public)));

correct code:

...
// All good so far

//Console.WriteLine(i.GetType().Name);
il_TopLevelMain_3.Emit(OpCodes.Ldloc, lv_i_6);  // load the integer value
il_TopLevelMain_3.Emit(OpCodes.Box, assembly.MainModule.TypeSystem.Int32);  // box

il_TopLevelMain_3.Emit(OpCodes.Callvirt, assembly.MainModule.ImportReference(TypeHelpers.ResolveMethod(typeof(System.Object), "GetType",System.Reflection.BindingFlags.Default|System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Public)));
@adrianoc adrianoc added 🐛 bug Something isn't working area:value-types labels Jun 9, 2024
@adrianoc adrianoc added this to the Future milestone Jun 9, 2024
@adrianoc adrianoc modified the milestones: Future, 2.15 Sep 11, 2024
@adrianoc adrianoc added the wip Work In Progress label Oct 3, 2024
@adrianoc adrianoc added fixed-in-dev-branch and removed wip Work In Progress labels Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:value-types 🐛 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant