Skip to content

LuaMember AllowNil support - #312

Merged
akeit0 merged 2 commits into
nuskey8:mainfrom
ashtonmeuser:nil-support
Sep 22, 2026
Merged

akeit0 merged 2 commits into
nuskey8:mainfrom
ashtonmeuser:nil-support

Conversation

@ashtonmeuser

Copy link
Copy Markdown
Contributor

Allows LuaMembers that hold LuaObjects to accept nil assignment from Lua via [LuaMember("my_prop", AllowNil = true)]. This aligns with the C# concept of said LuaObjects being reference types, and thus, accepting null

[LuaObject]
public partial class AllowNilReferencedObject
{
    [LuaMember("label")]
    public string Label { get; set; } = "";
}

[LuaObject]
public partial class AllowNilMemberContainer
{
    [LuaMember("optionalObject", AllowNil = true)]
    public AllowNilReferencedObject OptionalObject { get; set; } = null!;

    [LuaMember("requiredObject")]
    public AllowNilReferencedObject RequiredObject { get; set; } = null!;
}

state.Environment["target"] = new AllowNilMemberContainer();
state.Environment["reference"] = new AllowNilReferencedObject();
target.optionalObject = reference -- Works
target.optionalObject = nil -- Works
target.requiredObject = reference -- Works
target.requiredObject = nil -- Fails

In C#, state.Environment["target"]. OptionalObject is null.

@ashtonmeuser

Copy link
Copy Markdown
Contributor Author

Thoughts, @nuskey8 @akeit0?

@akeit0

akeit0 commented May 29, 2026

Copy link
Copy Markdown
Collaborator

I am wondering if it should be applied automatically with nullable in nullable enable context.

@ashtonmeuser

Copy link
Copy Markdown
Contributor Author

I am wondering if it should be applied automatically with nullable in nullable enable context.

I considered this and originally used nullable annotations to determine whether or not nil was accepted. However, nullable context support in Unity — where I’d assume a fair share of Lua-CSharp usage is — is spotty, and it typically isn’t used. If you want to go down that route, I think it's a great addition to this PR but doesn't necessarily preclude merging this one.

@akeit0

akeit0 commented May 30, 2026

Copy link
Copy Markdown
Collaborator

I am also uncertain about consistency.
If we provide AllowNil for properties, I think it would be insufficient if we do not also have AllowNil for function parameters.

@ashtonmeuser

Copy link
Copy Markdown
Contributor Author

Good point. Although, with a large usage share not having nullable context, there's really only so much you can do. Wholesale accepting nil for reference types would be a large breaking change.

@ashtonmeuser

Copy link
Copy Markdown
Contributor Author

@akeit0 Any chance we can get this merged?

I know you raised the issue of nil function params, but that's 1) a huge lift, 2) will surely be a breaking change, and 3) won't be usable in a Unity context. Further, nil can be accepted as function params, albeit a little awkwardly, using LuaValue and then TryRead<mytype>.

This PR provides a minimal change that is opt in only. I'd love to see this merged.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved review issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Adds AllowNil support for LuaObject reference-type members, allowing Lua nil assignments to map to C# null while preserving required-member validation.

Changes:

  • Adds AllowNil attribute and metadata support.
  • Generates nullable assignment handling.
  • Adds diagnostics and tests for allowed and rejected nil assignments.
File Description
tests/​Lua.Tests/​LuaObjectTests.cs Tests optional and required LuaObject members.
src/​Lua.SourceGenerator/​PropertyMetadata.cs Reads AllowNil metadata.
src/​Lua.SourceGenerator/​LuaObjectGenerator.Emit.cs Emits nullable assignment handling.
src/​Lua.SourceGenerator/​DiagnosticDescriptors.cs Adds invalid-use diagnostics.
src/​Lua.Annotations/​Attributes.cs Adds the AllowNil attribute property.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@akeit0
akeit0 merged commit 3ff9e97 into nuskey8:main Sep 22, 2026
@akeit0

akeit0 commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks!
Nuskey and I discussed this and decided to merge this PR for now.
We plan to handle nullables, including value types, later.

@akeit0

akeit0 commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

@ashtonmeuser
I created a pr which replaces AllowNil to standard AllowNull.

#341

Any thoughts on this?

@ashtonmeuser

Copy link
Copy Markdown
Contributor Author

We plan to handle nullables, including value types, later.

That's great to hear! That will be a very welcome feature.

I created a pr which replaces AllowNil to standard AllowNull.

That's fine by me if that's your preference. It's a bit of a judgement call as it's "nil" in Lua and "null" in C#.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants