Skip to content
Merged
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
76 changes: 76 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/UnsafeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14298,5 +14298,81 @@ class PointerImpl : IPointerTest
Diagnostic(ErrorCode.ERR_UnsafeNeeded, "void*").WithLocation(14, 55)
);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/44088")]
public void PointerTypeInTypeParameter_Field_RequiresUnsafeContext()
{
var code = """
class C<T>
{
C<int*[]> field;
}
""";

CreateCompilation(code, options: TestOptions.UnsafeReleaseDll).VerifyDiagnostics(
// (3,7): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
// C<int*[]> field;
Diagnostic(ErrorCode.ERR_UnsafeNeeded, "int*").WithLocation(3, 7),
// (3,15): warning CS0169: The field 'C<T>.field' is never used
// C<int*[]> field;
Diagnostic(ErrorCode.WRN_UnreferencedField, "field").WithArguments("C<T>.field").WithLocation(3, 15));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/44088")]
public void PointerTypeInTypeParameter_Field_UnsafeModifierRemovesUnsafeNeededError()
{
var code = """
unsafe class C<T>
{
C<int*[]> field;
}
""";

CreateCompilation(code, options: TestOptions.UnsafeReleaseDll).VerifyDiagnostics(
// (3,15): warning CS0169: The field 'C<T>.field' is never used
// C<int*[]> field;
Diagnostic(ErrorCode.WRN_UnreferencedField, "field").WithArguments("C<T>.field").WithLocation(3, 15));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/44088")]
public void PointerTypeInTypeParameter_Variable_RequiresUnsafeContext()
{
var code = """
class C<T>
{
void M()
{
C<int*[]> c = null;
Copy link
Member

Choose a reason for hiding this comment

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

@copilot, this doesn't look like the test from the issue - that one was for a field, this is a variable. I would keep this test though, just add the original one too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added field-based tests in commit d8c7f91. The original variable tests are now renamed with _Variable suffix, and new _Field tests match the original issue.

}
}
""";

CreateCompilation(code, options: TestOptions.UnsafeReleaseDll).VerifyDiagnostics(
// (5,11): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
// C<int*[]> c = null;
Diagnostic(ErrorCode.ERR_UnsafeNeeded, "int*").WithLocation(5, 11),
// (5,19): warning CS0219: The variable 'c' is assigned but its value is never used
// C<int*[]> c = null;
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "c").WithArguments("c").WithLocation(5, 19));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/44088")]
public void PointerTypeInTypeParameter_Variable_UnsafeModifierRemovesUnsafeNeededError()
{
var code = """
unsafe class C<T>
{
void M()
{
C<int*[]> c = null;
}
}
""";

CreateCompilation(code, options: TestOptions.UnsafeReleaseDll).VerifyDiagnostics(
// (5,19): warning CS0219: The variable 'c' is assigned but its value is never used
// C<int*[]> c = null;
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "c").WithArguments("c").WithLocation(5, 19));
}
}
}
Loading