Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix SemanticModel.GetTypeInfo to return type information for ObjectCreationExpression.Type #80721

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

Draft
Copilot wants to merge 2 commits into main
base: main
Choose a base branch
Loading
from copilot/fix-gettypeinfo-null-type

Conversation

Copy link
Contributor

@Copilot Copilot AI commented Oct 14, 2025
edited
Loading

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.

...on.Type in C# and VB
Co-authored-by: jcouv <12466233+jcouv@users.noreply.github.com>
@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

Reviewers

@jcouv jcouv Awaiting requested review from jcouv

At least 1 approving review is required to merge this pull request.

Labels

None yet

Projects

None yet

Milestone

No milestone

2 participants

AltStyle によって変換されたページ (->オリジナル) /