Skip to content

Fix #3892: preserve unsafe for pointer constructors#3893

Merged
siegfriedpammer merged 1 commit into
icsharpcode:masterfrom
sailro:fix-3892-unsafe-object-creation
Jul 20, 2026
Merged

Fix #3892: preserve unsafe for pointer constructors#3893
siegfriedpammer merged 1 commit into
icsharpcode:masterfrom
sailro:fix-3892-unsafe-object-creation

Conversation

@sailro

@sailro sailro commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #3892.

Problem

IntroduceUnsafeModifier detects pointer parameters on ordinary invocation expressions, but not
on object creation expressions.

If the constructor argument itself contains no pointer syntax, such as new NativeBuffer(null),
the required unsafe context is therefore lost. A field that originally had an unsafe modifier
is emitted without one, and the output fails to recompile with CS0214.

Solution

Apply the existing HasUnsafeResolveResult check to ObjectCreateExpression, matching the
handling already used for InvocationExpression.

This uses the selected constructor's resolved parameter types, so it remains general and does
not depend on a particular struct or argument shape.

Tests

Added a Pretty regression case where a static field constructs a value through a void*
constructor while passing null.

Validation:

  • UnsafeCode Pretty matrix: 20 passed

  • Release solution build: 0 warnings, 0 errors

  • Full solution test suite: 4,885 passed, 15 existing skips

  • At least one test covering the code changed

Object creation can require an unsafe context solely because the selected
constructor has a pointer parameter. Apply the existing unsafe-signature
check to object creation nodes so the emitted declaration remains compilable.

Assisted-by: Copilot:gpt-5.6-sol:GitHub Copilot CLI
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 86d2918e-5a24-48b4-9a86-41d331ec3720
@siegfriedpammer
siegfriedpammer merged commit 9ff3ac9 into icsharpcode:master Jul 20, 2026
7 checks passed
@siegfriedpammer

Copy link
Copy Markdown
Member

Thank you for your contribution!

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.

Field initializer calling a pointer-parameter constructor loses unsafe (CS0214)

2 participants