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

Fix field offset not being set for 0-offset #105894

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ private void WriteFields(TypeBuilderImpl typeBuilder, BlobBuilder fieldDataBuild
Debug.Assert(field._handle == handle);
WriteCustomAttributes(field._customAttributes, handle);

if (field._offset > 0 && (typeBuilder.Attributes & TypeAttributes.ExplicitLayout) != 0)
if (field._offset >= 0 && (typeBuilder.Attributes & TypeAttributes.ExplicitLayout) != 0)
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
{
AddFieldLayout(handle, field._offset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2740,5 +2740,22 @@ public void ValueTypeParentTest()
Assert.Equal(1, result);
}
}

[Fact]
public void ExplicitFieldOffsetSavesAndLoads()
{
PersistedAssemblyBuilder ab = new PersistedAssemblyBuilder(new AssemblyName("MyAssemblyForExplicitLayout"), typeof(object).Assembly);
ModuleBuilder mob = ab.DefineDynamicModule("MyModule");
TypeBuilder tb = mob.DefineType("ExplicitLayoutType", TypeAttributes.ExplicitLayout);
FieldBuilder f1 = tb.DefineField("field1", typeof(int), 0);
f1.SetOffset(0);
tb.CreateType();

using var stream = new MemoryStream();
ab.Save(stream);
stream.Seek(0, SeekOrigin.Begin);
var assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
var method = assembly.GetType("ExplicitLayoutType")!;
}
}
}
Loading