Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2048,10 +2048,7 @@ internal CSharpTypeInfo GetTypeInfoForNode(
var boundExpr = lowestBoundNode as BoundExpression;
var highestBoundExpr = highestBoundNode as BoundExpression;

if (boundExpr != null &&
!(boundNodeForSyntacticParent != null &&
boundNodeForSyntacticParent.Syntax.Kind() == SyntaxKind.ObjectCreationExpression &&
((ObjectCreationExpressionSyntax)boundNodeForSyntacticParent.Syntax).Type == boundExpr.Syntax)) // Do not return any type information for a ObjectCreationExpressionSyntax.Type node.
if (boundExpr != null)
{
// TODO: Should parenthesized expression really not have symbols? At least for C#, I'm not sure that
// is right. For example, C# allows the assignment statement:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15434,5 +15434,93 @@ public void Z()
var info = model.GetSemanticInfoSummary(syntax);
Assert.Equal("C", info.Type.Name);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75147")]
public void GetTypeInfoOnObjectCreationExpressionType()
{
string sourceCode = @"
class MyClass { }
class Program
{
static void Main()
{
var obj = new /*<bind>*/MyClass/*</bind>*/();
}
}";
var semanticInfo = GetSemanticInfoForTest<IdentifierNameSyntax>(sourceCode);

// Verify that GetTypeInfo returns the type information for the Type node of ObjectCreationExpression
Assert.Equal("MyClass", semanticInfo.Type.ToTestDisplayString());
Assert.Equal(TypeKind.Class, semanticInfo.Type.TypeKind);
Assert.Equal("MyClass", semanticInfo.ConvertedType.ToTestDisplayString());
Assert.Equal(TypeKind.Class, semanticInfo.ConvertedType.TypeKind);
Assert.Equal(ConversionKind.Identity, semanticInfo.ImplicitConversion.Kind);

Assert.Equal("MyClass", semanticInfo.Symbol.ToTestDisplayString());
Assert.Equal(SymbolKind.NamedType, semanticInfo.Symbol.Kind);
Assert.Equal(0, semanticInfo.CandidateSymbols.Length);

Assert.False(semanticInfo.IsCompileTimeConstant);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75147")]
public void GetTypeInfoOnObjectCreationExpressionTypeWithGeneric()
{
string sourceCode = @"
using System.Collections.Generic;
class Program
{
static void Main()
{
var list = new /*<bind>*/List<int>/*</bind>*/();
}
}";
var semanticInfo = GetSemanticInfoForTest<GenericNameSyntax>(sourceCode);

Assert.Equal("System.Collections.Generic.List<System.Int32>", semanticInfo.Type.ToTestDisplayString());
Assert.Equal(TypeKind.Class, semanticInfo.Type.TypeKind);
Assert.Equal("System.Collections.Generic.List<System.Int32>", semanticInfo.ConvertedType.ToTestDisplayString());
Assert.Equal(TypeKind.Class, semanticInfo.ConvertedType.TypeKind);
Assert.Equal(ConversionKind.Identity, semanticInfo.ImplicitConversion.Kind);

Assert.Equal("System.Collections.Generic.List<System.Int32>", semanticInfo.Symbol.ToTestDisplayString());
Assert.Equal(SymbolKind.NamedType, semanticInfo.Symbol.Kind);
Assert.Equal(0, semanticInfo.CandidateSymbols.Length);

Assert.False(semanticInfo.IsCompileTimeConstant);
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/75147")]
public void GetTypeInfoOnObjectCreationExpressionTypeWithQualifiedName()
{
string sourceCode = @"
namespace N
{
class MyClass { }
}
class Program
{
static void Main()
{
var obj = new /*<bind>*/N.MyClass/*</bind>*/();
}
}";
var semanticInfo = GetSemanticInfoForTest<QualifiedNameSyntax>(sourceCode);

Assert.Equal("N.MyClass", semanticInfo.Type.ToTestDisplayString());
Assert.Equal(TypeKind.Class, semanticInfo.Type.TypeKind);
Assert.Equal("N.MyClass", semanticInfo.ConvertedType.ToTestDisplayString());
Assert.Equal(TypeKind.Class, semanticInfo.ConvertedType.TypeKind);
Assert.Equal(ConversionKind.Identity, semanticInfo.ImplicitConversion.Kind);

Assert.Equal("N.MyClass", semanticInfo.Symbol.ToTestDisplayString());
Assert.Equal(SymbolKind.NamedType, semanticInfo.Symbol.Kind);
Assert.Equal(0, semanticInfo.CandidateSymbols.Length);

Assert.False(semanticInfo.IsCompileTimeConstant);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return IsInCrefOrNameAttributeInterior(node)
End If

' Special case: Allow getting semantic info for the Type node of ObjectCreationExpression
' even if it's marked as IsMissing, since simple identifier types are incorrectly marked as missing.
If node.IsMissing AndAlso TypeOf node Is ExpressionSyntax AndAlso node.Parent IsNot Nothing AndAlso
node.Parent.Kind = SyntaxKind.ObjectCreationExpression AndAlso
DirectCast(node.Parent, ObjectCreationExpressionSyntax).Type Is node Then
Return True
End If

Return Not node.IsMissing AndAlso
(TypeOf (node) Is ExpressionSyntax AndAlso (allowNamedArgumentName OrElse Not SyntaxFacts.IsNamedArgumentName(node)) OrElse
TypeOf (node) Is AttributeSyntax OrElse
Expand Down Expand Up @@ -927,13 +935,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return Nothing
End If

' Do not return any type information for a ObjectCreationExpressionSyntax.Type node.
If boundNodes.LowestBoundNodeOfSyntacticParent IsNot Nothing AndAlso
boundNodes.LowestBoundNodeOfSyntacticParent.Syntax.Kind = SyntaxKind.ObjectCreationExpression AndAlso
DirectCast(boundNodes.LowestBoundNodeOfSyntacticParent.Syntax, ObjectCreationExpressionSyntax).Type Is lowestExpr.Syntax Then
Return Nothing
End If

Dim type As TypeSymbol

' Similar to a lambda expression, array literal doesn't have a type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5350,8 +5350,8 @@
Assert.Equal(0, memberGroup.Length)

Dim typeInfo As TypeInfo = model.GetTypeInfo(creation.Type)
Assert.Null(typeInfo.Type)
Assert.Null(typeInfo.ConvertedType)
Assert.Equal("Test.C", typeInfo.Type.ToTestDisplayString())
Assert.Equal("Test.C", typeInfo.ConvertedType.ToTestDisplayString())

Dim conv = model.GetConversion(creation.Type)
Assert.True(conv.IsIdentity)
Expand Down Expand Up @@ -5439,8 +5439,8 @@
Assert.Equal(0, memberGroup.Length)

Dim typeInfo As TypeInfo = model.GetTypeInfo(creation.Type)
Assert.Null(typeInfo.Type)
Assert.Null(typeInfo.ConvertedType)
Assert.Equal("Test.I", typeInfo.Type.ToTestDisplayString())
Assert.Equal("Test.I", typeInfo.ConvertedType.ToTestDisplayString())

Dim conv = model.GetConversion(creation.Type)
Assert.True(conv.IsIdentity)
Expand Down Expand Up @@ -5544,8 +5544,8 @@
Assert.Equal(0, memberGroup.Length)

Dim typeInfo As TypeInfo = model.GetTypeInfo(creation.Type)
Assert.Null(typeInfo.Type)
Assert.Null(typeInfo.ConvertedType)
Assert.Equal("Test.I", typeInfo.Type.ToTestDisplayString())
Assert.Equal("Test.I", typeInfo.ConvertedType.ToTestDisplayString())

Dim conv = model.GetConversion(creation.Type)
Assert.True(conv.IsIdentity)
Expand Down Expand Up @@ -6637,5 +6637,112 @@
OutConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null)
]]>.Value)
End Sub

<Fact>
<WorkItem("https://github.com/dotnet/roslyn/issues/75147")>
Public Sub GetTypeInfoOnObjectCreationExpressionType()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib40(
<compilation>
<file name="a.vb">
Class TestClass
End Class
Module TestModule
Sub Main()
Dim obj = New TestClass()
End Sub
End Module
</file>
</compilation>)

Dim tree = compilation.SyntaxTrees(0)
Dim model = compilation.GetSemanticModel(tree)
Dim objCreation = tree.GetRoot().DescendantNodes().OfType(Of ObjectCreationExpressionSyntax)().First()

Check failure on line 6660 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6660

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6660,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 6660 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6660

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6660,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
' Test getting TypeInfo on the Type property
Dim typeNode = objCreation.Type
Dim typeInfo = model.GetTypeInfo(typeNode)

Check failure on line 6664 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6664

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6664,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 6664 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6664

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6664,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
' Verify that GetTypeInfo returns the type information for the Type node of ObjectCreationExpression
Assert.NotNull(typeInfo.Type)
Assert.Equal("TestClass", typeInfo.Type.ToTestDisplayString())
Assert.Equal(TypeKind.Class, typeInfo.Type.TypeKind)
Assert.Equal("TestClass", typeInfo.ConvertedType.ToTestDisplayString())
Assert.Equal(TypeKind.Class, typeInfo.ConvertedType.TypeKind)

Check failure on line 6671 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6671

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6671,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 6671 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6671

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6671,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
' Test SymbolInfo as well
Dim symbolInfo = model.GetSymbolInfo(typeNode)
Assert.NotNull(symbolInfo.Symbol)
Assert.Equal("TestClass", symbolInfo.Symbol.ToTestDisplayString())
Assert.Equal(SymbolKind.NamedType, symbolInfo.Symbol.Kind)
End Sub

<Fact>
<WorkItem("https://github.com/dotnet/roslyn/issues/75147")>
Public Sub GetTypeInfoOnObjectCreationExpressionTypeWithGeneric()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib40(
<compilation>
<file name="a.vb">
Imports System.Collections.Generic
Module TestModule
Sub Main()
Dim list = New List(Of Integer)()
End Sub
End Module
</file>
</compilation>)

Dim tree = compilation.SyntaxTrees(0)
Dim model = compilation.GetSemanticModel(tree)
Dim objCreation = tree.GetRoot().DescendantNodes().OfType(Of ObjectCreationExpressionSyntax)().First()

Check failure on line 6697 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6697

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6697,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 6697 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6697

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6697,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
Dim typeNode = objCreation.Type
Dim typeInfo = model.GetTypeInfo(typeNode)

Check failure on line 6700 in src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb#L6700

src/Compilers/VisualBasic/Test/Semantic/Semantics/GetSemanticInfoTests.vb(6700,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
Assert.NotNull(typeInfo.Type)
Assert.Equal("System.Collections.Generic.List(Of System.Int32)", typeInfo.Type.ToTestDisplayString())
Assert.Equal(TypeKind.Class, typeInfo.Type.TypeKind)
Assert.Equal("System.Collections.Generic.List(Of System.Int32)", typeInfo.ConvertedType.ToTestDisplayString())

Dim symbolInfo = model.GetSymbolInfo(typeNode)
Assert.NotNull(symbolInfo.Symbol)
Assert.Equal("System.Collections.Generic.List(Of System.Int32)", symbolInfo.Symbol.ToTestDisplayString())
Assert.Equal(SymbolKind.NamedType, symbolInfo.Symbol.Kind)
End Sub

<Fact>
<WorkItem("https://github.com/dotnet/roslyn/issues/75147")>
Public Sub GetTypeInfoOnObjectCreationExpressionTypeWithQualifiedName()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib40(
<compilation>
<file name="a.vb">
Namespace N
Class TestClass
End Class
End Namespace
Module TestModule
Sub Main()
Dim obj = New N.TestClass()
End Sub
End Module
</file>
</compilation>)

Dim tree = compilation.SyntaxTrees(0)
Dim model = compilation.GetSemanticModel(tree)
Dim objCreation = tree.GetRoot().DescendantNodes().OfType(Of ObjectCreationExpressionSyntax)().First()

Dim typeNode = objCreation.Type
Dim typeInfo = model.GetTypeInfo(typeNode)

Assert.NotNull(typeInfo.Type)
Assert.Equal("N.TestClass", typeInfo.Type.ToTestDisplayString())
Assert.Equal(TypeKind.Class, typeInfo.Type.TypeKind)
Assert.Equal("N.TestClass", typeInfo.ConvertedType.ToTestDisplayString())

Dim symbolInfo = model.GetSymbolInfo(typeNode)
Assert.NotNull(symbolInfo.Symbol)
Assert.Equal("N.TestClass", symbolInfo.Symbol.ToTestDisplayString())
Assert.Equal(SymbolKind.NamedType, symbolInfo.Symbol.Kind)
End Sub
End Class
End Namespace
Loading