Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 16, 2025

Fix superfluous managed address errors for pointer to non-existent type

Summary

Fixed issue #38378 where declaring a pointer to a non-existent type would report both CS0246 (type not found) and CS8500/CS0208 (managed address warning/error). Now only CS0246 is reported.

Changes Made

  • Binder_Expressions.cs: Added error type check in CheckManagedAddr() to skip the warning only for truly missing types (LookupResultKind.Empty or NotATypeOrNamespace), but still report for other error scenarios like inaccessible types
  • Binder_Symbols.cs: Removed redundant check (now handled in CheckManagedAddr)
  • ConstraintsHelper.cs: Removed redundant check (now handled in CheckManagedAddr)
  • Added 4 comprehensive test cases in UnsafeTests.cs using raw string literals with C# pointer style (Type* instead of Type *)
  • Updated 2 existing tests (PointerTypeInDeconstruction, TypeNamedFile_03_CSharp11) to reflect new behavior for error types from parse errors

Test Results

  • ✅ All 4 originally failing tests now pass
  • ✅ All 4 new test cases pass
  • ✅ All 491 unsafe tests pass

Fix Pattern

Centralized the check in CheckManagedAddr with refined logic: skip warning only for types that are truly missing (Empty/NotATypeOrNamespace), but still report for other error scenarios (e.g., Inaccessible types). Uses modern C# pattern matching syntax for clean code.

Original prompt

This section details on the original issue you should resolve

<issue_title>Quality of implementation: superfluous errors for pointer to non-existent type</issue_title>
<issue_description>Compile the following program:

class Program
{
    static unsafe void Type(Type *type)
    {
    }
}

Expected

Program.cs(3,29): error CS0246: The type or namespace name 'Type' could not be found (are you missing a using directive or an assembly reference?) [/Users/hugh/Documents/GitHub/repo/repo.csproj]

A single error that we can't find the type

Actual

Program.cs(3,29): error CS0246: The type or namespace name 'Type' could not be found (are you missing a using directive or an assembly reference?) [/Users/hugh/Documents/GitHub/repo/repo.csproj]
Program.cs(3,29): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('Type') [/Users/hugh/Documents/GitHub/repo/repo.csproj]

The second error is pointless in this case as the type doesn't exist. I would suggest not checking if we can take the address, size or declare a pointer to a non-existent type</issue_description>

Comments on the Issue (you are @copilot in this section)

@RikkiGibson Currently we refrain from giving the ManagedAddr error (CS0208) for sizeof and stackalloc array creation when the type is an error type. It seems like we don't need to give the error in this case (pointer to an error type) either.

Fixes #38378


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 16, 2025 14:57
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix superfluous errors for pointer to non-existent type Fix superfluous managed address errors for pointer to non-existent type Oct 16, 2025
Copilot AI requested a review from CyrusNajmabadi October 16, 2025 15:08
Copilot finished work on behalf of CyrusNajmabadi October 16, 2025 15:08
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/36877")]
public void PointerToNonExistentType_NoSuperfluousError()
{
var source = @"
Copy link
Member

Choose a reason for hiding this comment

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

@copilot please use raw string literals.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use raw string literals in commit 7d8a30f.

var pointedAtType = ((PointerTypeSymbol)type).PointedAtType;
if (!pointedAtType.IsErrorType())
{
Binder.CheckManagedAddr(args.CurrentCompilation, pointedAtType, args.Location, args.Diagnostics);
Copy link
Member

Choose a reason for hiding this comment

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

@copilot would it make more sense to put the IsErrorType check inside CheckManaged address? If not, why not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion! Moved the IsErrorType check into CheckManagedAddr in commit 7d8a30f. This centralizes the fix and automatically handles all call sites, making the code cleaner and more maintainable.

// (4,31): error CS0246: The type or namespace name 'Type' could not be found (are you missing a using directive or an assembly reference?)
// static unsafe void Method(Type *type)
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "Type").WithArguments("Type").WithLocation(4, 31)
// Should not also report CS8500 or CS0208 for managed address of non-existent type
Copy link
Member

Choose a reason for hiding this comment

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

@copilot no need for this comment. And the same with the remainder of the tests. The last paren can also be pulled up to the same line that WithLocation(...) is on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed unnecessary comments and improved formatting in commit 7d8a30f.

…raw string literals, remove comments

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot finished work on behalf of CyrusNajmabadi October 16, 2025 15:30
Copilot AI requested a review from CyrusNajmabadi October 16, 2025 15:30
@CyrusNajmabadi
Copy link
Member

@copilot the following 4 tests are failing, now that that warning is no longer being reported. can you fix them all up:

Microsoft.CodeAnalysis.CSharp.UnitTests.FileModifierParsingTests.TypeNamedFile_03_CSharp11
Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics.DeconstructionTests.PointerTypeInDeconstruction
Microsoft.CodeAnalysis.CSharp.UnitTests.BaseClassTests.CyclePointer_UnsafeContext
Microsoft.CodeAnalysis.CSharp.UnitTests.BaseClassTests.CyclePointer

…ssing types

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 16, 2025

@copilot the following 4 tests are failing, now that that warning is no longer being reported. can you fix them all up:

Microsoft.CodeAnalysis.CSharp.UnitTests.FileModifierParsingTests.TypeNamedFi...

Fixed all 4 tests in commit 5c51e77. Refined the IsErrorType check to only skip the warning for truly missing types (LookupResultKind.Empty or NotATypeOrNamespace), while still reporting it for types that exist but have other issues (e.g., inaccessible types). Updated test expectations for PointerTypeInDeconstruction and TypeNamedFile_03_CSharp11 where error types from parse errors no longer get the warning.

Copilot finished work on behalf of CyrusNajmabadi October 16, 2025 21:04
var source = """
class Program
{
static unsafe void Method(Type *type)
Copy link
Member

Choose a reason for hiding this comment

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

@copilot unlike in C++ in c# we put the asterix next to the type not next to the name. so Type* type. Can you fixup all these tests to follow that style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed the pointer syntax style to use Type* type instead of Type *type in all 4 test cases in commit b8f9c2a.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI requested a review from CyrusNajmabadi October 16, 2025 21:13
Copilot finished work on behalf of CyrusNajmabadi October 16, 2025 21:13
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review October 20, 2025 17:21
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 20, 2025 17:22
@CyrusNajmabadi
Copy link
Member

@dotnet/roslyn-compiler ptal

{
// Skip the check for error types that represent truly missing types (not found),
// but still report for error types due to other issues (e.g., inaccessibility).
if (type is ErrorTypeSymbol { ResultKind: LookupResultKind.Empty or LookupResultKind.NotATypeOrNamespace })
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 3, 2025

Choose a reason for hiding this comment

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

LookupResultKind.Empty or LookupResultKind.NotATypeOrNamespace

Are there tests that observe significance of this condition? I.e. they would fail if the condition was removed. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Are there tests that observe significance of this condition? I.e. they would fail if the condition was removed.

Still would like to get an answer, pointing to specific test(s)

Copy link
Member

Choose a reason for hiding this comment

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

Added a couple of demonstrative tests in 7242579

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 11)

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 12)

@CyrusNajmabadi
Copy link
Member

Anything more needed here?

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 14)

@CyrusNajmabadi
Copy link
Member

@RikkiGibson @jjonescz ptal :)

@CyrusNajmabadi CyrusNajmabadi merged commit 4cdc624 into main Nov 13, 2025
25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 13, 2025
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/fix-superfluous-pointer-errors branch November 13, 2025 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quality of implementation: superfluous errors for pointer to non-existent type

4 participants