Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 14, 2025

Fixes #75147 (VB) and the related C# issue

Problem

SemanticModel.GetTypeInfo() was returning null Type when called on the Type property of an ObjectCreationExpressionSyntax, even though GetSymbolInfo() correctly returned the type symbol:

var objCreation = (ObjectCreationExpressionSyntax)...;
var typeInfo = semanticModel.GetTypeInfo(objCreation.Type);
// typeInfo.Type was null (unexpected)

var symbolInfo = semanticModel.GetSymbolInfo(objCreation.Type);
// symbolInfo.Symbol was correctly populated

This behavior was inconsistent - the Type node is in a "type-only context" (confirmed by SyntaxFacts.IsInTypeOnlyContext returning true), and other type-only contexts correctly return type information.

Solution

C# Changes

Removed the explicit check in CSharpSemanticModel.GetTypeInfoForNode() that was blocking type information from being returned for ObjectCreationExpressionSyntax.Type nodes. The check dates back to the early days of Roslyn but there was no clear reason for this restriction.

Visual Basic Changes

  1. Removed the equivalent blocking check in SemanticModel.GetTypeInfoForNode()
  2. Added special handling in CanGetSemanticInfo() to allow semantic information retrieval for the Type node of ObjectCreationExpression even when marked as IsMissing
    • VB's syntax tree marks simple identifier types (e.g., New MyClass()) as IsMissing, while qualified types (e.g., New N.MyClass()) are not marked as missing
    • This is a quirk of VB's syntax tree construction, and the special case ensures both work correctly

Testing

  • Added comprehensive tests for both C# and VB covering simple types, generic types, and qualified names
  • Updated existing VB tests that were expecting the old null behavior
  • All existing compiler tests pass (19,250+ C# semantic tests, all VB semantic tests)
Original prompt

This section details on the original issue you should resolve

<issue_title>SemanitModel.GetTypeInfo returns null Type on ObjectCreationExpressionSyntax.Type</issue_title>
<issue_description>I'd expect the type symbol to be resolved.

var objCreation = (ObjectCreationExpressionSyntax)...;
var typeInfo = semanticModel.GetTypeInfo(objCreation.Type);

typeInfo.Type is null

</issue_description>

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

@jaredpar @333fred for triage @CyrusNajmabadi I remember this being intentional in the early days of roslyn. but i can't remember why anymore. @CyrusNajmabadi GetTypeInfo works fine for TypeSyntaxes in other non-expression contexts though. @jcouv FWIW, VB has the same behavior: https://github.com//issues/75147 (we should consider fixing both at the same time if we decide to move ahead)

Fixes #78783


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Fix GetTypeInfo returning null Type for ObjectCreationExpressionSyntax Fix SemanticModel.GetTypeInfo to return type information for ObjectCreationExpression.Type Oct 14, 2025
@Copilot Copilot AI requested a review from jcouv October 14, 2025 21:35
Copilot finished work on behalf of jcouv October 14, 2025 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants