- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.2k
Add tests verifying pointer types in type parameters require unsafe context #80776
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
Add tests verifying pointer types in type parameters require unsafe context #80776
Conversation
…ntext Co-authored-by: CyrusNajmabadi <[email protected]>
| { | ||
| void M() | ||
| { | ||
| C<int*> c = null; | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot  this is not the right test.  Specifically, we want to test C<int*[]>.  In this case, an array of pointers is fine (sincce an array is a maanged type).  But it's just the usage of hte pointer type that should or should not produce an error depending on the unsafe context we're in.
Fix this and hte the teest below.
Also, add appropriate [Workitem(...)] attributes to both tests. REmove superfluous explanatory comments on the methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: CyrusNajmabadi <[email protected]>
| @dotnet/roslyn-compiler ptal. | 
| { | ||
| void M() | ||
| { | ||
| C<int*[]> c = null; | 
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Co-authored-by: jjonescz <[email protected]>
* upstream/main: (332 commits) Cache lambdas in analyzer driver (dotnet#80759) Add information for NuGet package version 4.14 (dotnet#80870) Add missing search keywords to VB Advanced options page Fix IDE0031 false positive when preprocessor directives are used in if statements (dotnet#80878) Use core compiler on netfx hosts with toolset package (dotnet#80631) Make string concat assert more precise (dotnet#80619) Extensions: address some diagnostic quality issues (dotnet#80827) Add note on traversal order for bound nodes (dotnet#80872) Ensure that locals at the top level of a constructor have the same safe-context as parameters (dotnet#80807) Fix handling of SymbolDisplayCompilerInternalOptions.UseArityForGenericTypes option for non-native symbol implementations (dotnet#80826) Update src/Analyzers/CSharp/Tests/UseCollectionInitializer/UseCollectionInitializerTests.cs Add IsValidContainingStatement check to prevent collection initializers in using declarations Add back old DocumentSpan constructor (dotnet#80864) Add tests verifying pointer types in type parameters require unsafe context (dotnet#80776) Add regression test for Interlocked.Exchange with nullable types (dotnet#80796) Add regression test for ParseAttributeArgumentList with invalid input (fixes dotnet#8699) (dotnet#80705) Add regression test for compiler crash with syntax error in indexer declaration (dotnet#80772) Add runtime NullReferenceException validation to foreach null iteration tests (dotnet#80839) Update MicrosoftBuildTasksCoreVersionForMetrics to 17.11.48 (dotnet#80812) Mark CS4009 error as a "build only" error. (dotnet#80698) ...
This PR adds comprehensive tests to verify that the C# compiler correctly enforces unsafe context requirements when using pointer types in type parameters.
Background
An issue was reported suggesting that unsafe context might not be required for pointer types in type parameters. Investigation confirmed this is not a bug - the compiler correctly requires unsafe context and produces appropriate errors.
Tests Added
Four new tests in
UnsafeTests.csverify the correct behavior for both field and variable scenarios:1. PointerTypeInTypeParameter_Field_RequiresUnsafeContext
Verifies that using a pointer type syntax in a type parameter for a field without an unsafe context produces the expected error:
Expected error:
CS0214(ERR_UnsafeNeeded): Pointers and fixed size buffers may only be used in an unsafe context2. PointerTypeInTypeParameter_Field_UnsafeModifierRemovesUnsafeNeededError
Verifies that adding the
unsafemodifier to the class removes the unsafe context error for fields:Expected behavior:
CS0214error (unsafe context is provided by the class modifier)3. PointerTypeInTypeParameter_Variable_RequiresUnsafeContext
Verifies that using a pointer type syntax in a type parameter for a local variable without an unsafe context produces the expected error:
Expected error:
CS0214(ERR_UnsafeNeeded): Pointers and fixed size buffers may only be used in an unsafe context4. PointerTypeInTypeParameter_Variable_UnsafeModifierRemovesUnsafeNeededError
Verifies that adding the
unsafemodifier to the class removes the unsafe context error for local variables:Expected behavior:
CS0214error (unsafe context is provided by the class modifier)Implementation Details
TestOptions.UnsafeReleaseDllto enable unsafe code compilationWorkItemattributes linking to issue Unsafe not required for pointer type in type parameter #44088UnsafeTests.csNote:
int*[](array of pointers) is a valid type argument since arrays are managed types, but the pointer type syntaxint*itself requires an unsafe context.These tests document and verify the correct compiler behavior: the pointer type syntax requires an unsafe context in both field and variable scenarios, but arrays of pointers can be used as type arguments since arrays are managed types.
Fixes #44088
Original prompt
Fixes #44088
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.