' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Collections.Immutable Imports Microsoft.CodeAnalysis.Test.Utilities Imports Microsoft.CodeAnalysis.VisualBasic.Syntax Imports Roslyn.Test.Utilities Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics Public Class TypeOfTests Inherits BasicTestBase Public Sub ProducesNoErrorsOnBoxedValueTypes_ObjectExpressionAndPrimitiveType() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesNoErrorsOnBoxedValueTypes_ObjectExpressionAndIComparableType() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesNoErrorsOnBoxedValueTypes_ValueTypeExpressionAndEnumType() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesNoErrorsOnBoxedValueTypes_EnumTypeExpressionAndEnumType() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesNoErrorsWithClassTypeUnconstrainedTypeParameterTargetTypes_ClassConstraint() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesNoErrorsWithClassTypeUnconstrainedTypeParameterTargetTypes_StructureConstraint() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesNoErrorsWithClassTypeUnconstrainedTypeParameterTargetTypes_StructureAndClassConstraint() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesNoErrorsWithClassTypeUnconstrainedTypeParameterTargetTypes_StringType() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesErrorsWhenNoReferenceConversionExists_BC30371() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesErrorsWhenNoReferenceConversionExists_BC31430() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesErrorsWhenNoReferenceConversionExists_BC31430_02() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesErrorsWhenNoReferenceConversionExists_BC30021() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = .Value VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ProducesErrorsWhenNoReferenceConversionExistsBetweenConstrainedTypeParameterTypes() Dim source = Option Strict On Imports System Module Program Sub Main() End Sub Class A : End Class Class B : End Class Sub M(Of T, TA As A, TB As B, TC As Structure, TD As IDisposable)(x As T, a As TA, b As TB, c As TC, d As TD, s As String) If TypeOf x Is TA OrElse TypeOf x Is TB OrElse TypeOf x Is TC OrElse TypeOf x Is TD OrElse TypeOf x Is String Then Console.WriteLine("Success!") End If If TypeOf a Is TB OrElse TypeOf a Is TC OrElse TypeOf a Is String Then Console.WriteLine("Fail!") ElseIf TypeOf a Is T OrElse TypeOf a Is TD Then Console.WriteLine("Success!") End If If TypeOf b Is TA OrElse TypeOf b Is TC OrElse TypeOf b Is String Then Console.WriteLine("Fail!") ElseIf TypeOf b Is T OrElse TypeOf b Is TD Then Console.WriteLine("Success!") End If If TypeOf c Is TA OrElse TypeOf c Is TB OrElse TypeOf c Is String Then Console.WriteLine("Fail!") ElseIf TypeOf c Is T OrElse TypeOf c Is TD Then Console.WriteLine("Success!") End If If TypeOf d Is T OrElse TypeOf d Is TA OrElse TypeOf d Is TB OrElse TypeOf d Is TC OrElse TypeOf d Is String Then Console.WriteLine("Success!") End If If TypeOf s Is TA OrElse TypeOf s Is TB OrElse TypeOf s Is TC Then Console.WriteLine("Fail!") ElseIf TypeOf s Is T OrElse TypeOf s Is T OrElse TypeOf s Is TD Then Console.WriteLine("Success!") End If End Sub End Module Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntimeAndReferences(source, , TestOptions.ReleaseExe) CompilationUtils.AssertTheseDiagnostics(compilation, BC31430: Expression of type 'TA' can never be of type 'TB'. If TypeOf a Is TB OrElse TypeOf a Is TC OrElse TypeOf a Is String Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'TA' can never be of type 'TC'. If TypeOf a Is TB OrElse TypeOf a Is TC OrElse TypeOf a Is String Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'TA' can never be of type 'String'. If TypeOf a Is TB OrElse TypeOf a Is TC OrElse TypeOf a Is String Then ~~~~~~~~~~~~~~~~~~ BC31430: Expression of type 'TB' can never be of type 'TA'. If TypeOf b Is TA OrElse TypeOf b Is TC OrElse TypeOf b Is String Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'TB' can never be of type 'TC'. If TypeOf b Is TA OrElse TypeOf b Is TC OrElse TypeOf b Is String Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'TB' can never be of type 'String'. If TypeOf b Is TA OrElse TypeOf b Is TC OrElse TypeOf b Is String Then ~~~~~~~~~~~~~~~~~~ BC31430: Expression of type 'TC' can never be of type 'TA'. If TypeOf c Is TA OrElse TypeOf c Is TB OrElse TypeOf c Is String Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'TC' can never be of type 'TB'. If TypeOf c Is TA OrElse TypeOf c Is TB OrElse TypeOf c Is String Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'TC' can never be of type 'String'. If TypeOf c Is TA OrElse TypeOf c Is TB OrElse TypeOf c Is String Then ~~~~~~~~~~~~~~~~~~ BC31430: Expression of type 'String' can never be of type 'TA'. If TypeOf s Is TA OrElse TypeOf s Is TB OrElse TypeOf s Is TC Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'String' can never be of type 'TB'. If TypeOf s Is TA OrElse TypeOf s Is TB OrElse TypeOf s Is TC Then ~~~~~~~~~~~~~~ BC31430: Expression of type 'String' can never be of type 'TC'. If TypeOf s Is TA OrElse TypeOf s Is TB OrElse TypeOf s Is TC Then ~~~~~~~~~~~~~~ ) End Sub Public Sub SeesThroughTypeAndNamespaceAliases() Dim source = Option Strict On Imports HRESULT = System.Int32 Imports CharacterSequence = System.String Module Program Sub Main(args As String()) Dim o As Object = "" Dim isString = TypeOf o Is CharacterSequence Dim isInteger = TypeOf o Is HRESULT Dim isNotString = TypeOf o IsNot CharacterSequence Dim isNotInteger = TypeOf o IsNot HRESULT System.Console.WriteLine(isString) System.Console.WriteLine(isInteger) System.Console.WriteLine(isNotString) System.Console.WriteLine(isNotInteger) End Sub End Module Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntimeAndReferences(source, , TestOptions.ReleaseExe) CompilationUtils.AssertNoErrors(compilation) CompileAndVerify(compilation, ) End Sub Public Sub SeesThroughTypeAndNamespaceAliases_IsStringAliasIOperationTest() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub SeesThroughTypeAndNamespaceAliases_IsIntegerAliasIOperationTest() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub SeesThroughTypeAndNamespaceAliases_IsNotStringAliasIOperationTest() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub SeesThroughTypeAndNamespaceAliases_IsNotIntegerAliasIOperationTest() Dim source = .Value Dim expectedOperationTree = .Value Dim expectedDiagnostics = String.Empty VerifyOperationTreeAndDiagnosticsForTest(Of TypeOfExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub Public Sub ReturnsExpectedValuesFromSemanticModelApi() Dim source = Option Strict On Imports HRESULT = System.Int32 Imports CharacterSequence = System.String Module Program Sub Main(args As String()) Dim o As Object = "" Dim isString = TypeOf o Is CharacterSequence '0 Dim isInteger = TypeOf o Is HRESULT '1 Dim isNotString = TypeOf o IsNot String '2 Dim isNotInteger As Object = TypeOf o IsNot Integer '3 If TypeOf CObj(isString) Is Boolean Then '4 System.Console.WriteLine(True) End If System.Console.WriteLine(isString) System.Console.WriteLine(isInteger) System.Console.WriteLine(isNotString) System.Console.WriteLine(isNotInteger) System.Console.WriteLine(TypeOf "" Is String) '5 End Sub End Module Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntimeAndReferences(source, , TestOptions.ReleaseExe) CompilationUtils.AssertNoErrors(compilation) Dim semantics = compilation.GetSemanticModel(compilation.SyntaxTrees(0)) Dim typeOfExpressions = compilation.SyntaxTrees(0).GetCompilationUnitRoot().DescendantNodes.OfType(Of TypeOfExpressionSyntax).ToArray() ' Dim isString = TypeOf o Is CharacterSequence '0 Assert.Equal("System.Boolean", semantics.GetTypeInfo(typeOfExpressions(0)).Type.ToDisplayString(SymbolDisplayFormat.TestFormat)) Assert.Equal("System.String", semantics.GetSymbolInfo(typeOfExpressions(0).Type).Symbol.ToDisplayString(SymbolDisplayFormat.TestFormat)) ' Dim isInteger = TypeOf o Is HRESULT '1 Dim aliasSymbol = semantics.GetAliasInfo(CType(typeOfExpressions(1).Type, IdentifierNameSyntax)) Assert.Equal(SymbolKind.Alias, aliasSymbol.Kind) Assert.Equal("HRESULT", aliasSymbol.Name) Assert.Equal("System.Int32", semantics.GetSymbolInfo(typeOfExpressions(1).Type).Symbol.ToDisplayString(SymbolDisplayFormat.TestFormat)) ' Dim isNotString = TypeOf o IsNot String '2 Assert.Equal("System.Boolean", semantics.GetTypeInfo(typeOfExpressions(2)).Type.ToDisplayString(SymbolDisplayFormat.TestFormat)) Assert.Equal("System.String", semantics.GetSymbolInfo(typeOfExpressions(2).Type).Symbol.ToDisplayString(SymbolDisplayFormat.TestFormat)) ' Dim isNotInteger As Object = TypeOf o IsNot Integer '3 Dim typeInfo = semantics.GetTypeInfo(typeOfExpressions(3)) Dim conv = semantics.GetConversion(typeOfExpressions(3)) Assert.Equal(ConversionKind.WideningValue, conv.Kind) Assert.Equal(SpecialType.System_Object, typeInfo.ConvertedType.SpecialType) ' If TypeOf CObj(isString) Is Boolean Then '4 Dim symbolInfo = semantics.GetSymbolInfo(CType(typeOfExpressions(4).Expression, PredefinedCastExpressionSyntax).Expression) Assert.Equal(SymbolKind.Local, symbolInfo.Symbol.Kind) Assert.Equal("isString", symbolInfo.Symbol.Name) Dim expressionAnalysis = semantics.AnalyzeDataFlow(typeOfExpressions(4)) Assert.DoesNotContain(symbolInfo.Symbol, expressionAnalysis.AlwaysAssigned) Assert.DoesNotContain(symbolInfo.Symbol, expressionAnalysis.Captured) Assert.Contains(symbolInfo.Symbol, expressionAnalysis.DataFlowsIn) Assert.DoesNotContain(symbolInfo.Symbol, expressionAnalysis.DataFlowsOut) Assert.Contains(symbolInfo.Symbol, expressionAnalysis.ReadInside) Assert.Contains(symbolInfo.Symbol, expressionAnalysis.ReadOutside) Assert.True(expressionAnalysis.Succeeded) Assert.DoesNotContain(symbolInfo.Symbol, expressionAnalysis.VariablesDeclared) Assert.DoesNotContain(symbolInfo.Symbol, expressionAnalysis.WrittenInside) Assert.Contains(symbolInfo.Symbol, expressionAnalysis.WrittenOutside) Dim statementDataAnalysis = semantics.AnalyzeDataFlow(CType(typeOfExpressions(4).Parent.Parent, StatementSyntax)) AssertSequenceEqual(expressionAnalysis.AlwaysAssigned, statementDataAnalysis.AlwaysAssigned) AssertSequenceEqual(expressionAnalysis.Captured, statementDataAnalysis.Captured) AssertSequenceEqual(expressionAnalysis.DataFlowsIn, statementDataAnalysis.DataFlowsIn) AssertSequenceEqual(expressionAnalysis.DataFlowsOut, statementDataAnalysis.DataFlowsOut) AssertSequenceEqual(expressionAnalysis.ReadInside, statementDataAnalysis.ReadInside) AssertSequenceEqual(expressionAnalysis.ReadOutside, statementDataAnalysis.ReadOutside) Assert.Equal(expressionAnalysis.Succeeded, statementDataAnalysis.Succeeded) AssertSequenceEqual(expressionAnalysis.VariablesDeclared, statementDataAnalysis.VariablesDeclared) AssertSequenceEqual(expressionAnalysis.WrittenInside, statementDataAnalysis.WrittenInside) AssertSequenceEqual(expressionAnalysis.WrittenOutside, statementDataAnalysis.WrittenOutside) Assert.False(semantics.GetConstantValue(typeOfExpressions(5)).HasValue) End Sub Private Shared Sub AssertSequenceEqual(Of TElement)(a1 As ImmutableArray(Of TElement), a2 As ImmutableArray(Of TElement)) Assert.Equal(a1.Length, a2.Length) For i As Integer = 0 To a1.Length - 1 Assert.Equal(a1(i), a2(i)) Next End Sub Public Sub ExecutesCorrectlyInExpression() Dim source = Option Strict On Imports System Module Program Sub Main(args As String()) Dim o As Object = "" Dim isString = TypeOf o Is String Dim isInteger = TypeOf o Is Integer Dim isNotString = TypeOf o IsNot String Dim isNotInteger = TypeOf o IsNot Integer Console.WriteLine(isString) Console.WriteLine(isInteger) Console.WriteLine(isNotString) Console.WriteLine(isNotInteger) End Sub End Module Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntimeAndReferences(source, , TestOptions.ReleaseExe) CompilationUtils.AssertNoErrors(compilation) CompileAndVerify(compilation, ) End Sub Public Sub ExecutesCorrectlyAsIfCondition() Dim source = Option Strict On Imports System Module Program Sub Main(args As String()) Dim o As Object = "" If TypeOf o Is String Then Console.WriteLine("It's a String") End If If TypeOf o Is Integer Then Console.WriteLine("It's an Integer") End If If TypeOf o IsNot String Then Console.WriteLine("It's NOT a String") End If If TypeOf o IsNot Integer Then Console.WriteLine("It's NOT an Integer") End If End Sub End Module Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntimeAndReferences(source, , TestOptions.ReleaseExe) CompilationUtils.AssertNoErrors(compilation) CompileAndVerify(compilation, ) End Sub Public Sub GeneratesILCorrectlyUnderRelease() Dim source = Option Strict On Imports System Module Program Sub Main(args As String()) Dim o As Object = "" Dim isString = TypeOf o Is String Console.WriteLine(isString) If TypeOf o IsNot Integer Then Console.WriteLine(True) Else Console.WriteLine(False) End If Dim isInteger = TypeOf o Is Integer Console.WriteLine(isInteger) If TypeOf o IsNot String Then Console.WriteLine(True) Else Console.WriteLine(False) End If Console.WriteLine(If(TypeOf o Is Decimal, True, False)) Console.WriteLine(If(TypeOf o IsNot Decimal, True, False)) End Sub Sub M(Of T, TRef As Class, TVal As Structure, TBase As Class, TDerived As TBase)() Dim oT As T = Nothing If TypeOf oT Is TRef Then Console.WriteLine(False) End If If TypeOf oT Is TVal Then Console.WriteLine(False) End If Dim vVal As TVal = Nothing If TypeOf vVal Is TRef Then Console.WriteLine(False) End If If TypeOf oT Is String Then Console.WriteLine(False) End If End Sub End Module CompileAndVerify(source).VerifyIL("Program.Main", ).VerifyIL("Program.M", ) End Sub ' For compatibility with Dev10, "TypeOf t" should be ' supported for type parameter with Structure constraint, ' and the generated IL should box the argument. Public Sub TypeParameterWithConstraints01() Dim source = Imports System Module M Sub M(Of T, U As Structure, V As Class)(x As T, y As U, z As V) If TypeOf x Is Integer Then Console.WriteLine("x") End If If TypeOf y Is Integer Then Console.WriteLine("y") End If If TypeOf z Is Integer Then Console.WriteLine("z") End If End Sub Sub Main() M(1, 2.0, DirectCast(3, Object)) M(1.0, 2, DirectCast(3.0, Object)) End Sub End Module CompileAndVerify(source, expectedOutput:=).VerifyIL("M.M(Of T, U, V)(T, U, V)", ) End Sub Public Sub TypeParameterWithConstraints02() Dim compilation = CompilationUtils.CreateCompilationWithMscorlib( Structure S1 End Structure Structure S2 End Structure MustInherit Class A0(Of T1, T2) Public MustOverride Sub M(Of U1 As T1, U2 As T2)(_1 As U1, _2 As U2) End Class Class B0 Inherits A0(Of S1, S2) Public Overloads Overrides Sub M(Of U1 As S1, U2 As S2)(_1 As U1, _2 As U2) If TypeOf _1 Is S2 Then ' B0.M End If If TypeOf _1 Is U2 Then ' B0.M End If End Sub End Class MustInherit Class A1(Of T1, T2) Public MustOverride Sub M(Of U1 As {T1, Structure}, U2 As {T2})(_1 As U1, _2 As U2) End Class Class B1 Inherits A1(Of S1, S2) Public Overloads Overrides Sub M(Of U1 As {Structure, S1}, U2 As {S2})(_1 As U1, _2 As U2) If TypeOf _1 Is S2 Then ' B1.M End If If TypeOf _1 Is U2 Then ' B1.M End If If TypeOf _2 Is S1 Then ' B1.M End If If TypeOf _2 Is U1 Then ' B1.M End If End Sub End Class MustInherit Class A2(Of T1, T2) Public MustOverride Sub M(Of U1 As {T1, Structure}, U2 As {T2, Structure})(_1 As U1, _2 As U2) End Class Class B2 Inherits A2(Of S1, S2) Public Overloads Overrides Sub M(Of U1 As {Structure, S1}, U2 As {Structure, S2})(_1 As U1, _2 As U2) If TypeOf _1 Is S2 Then ' B2.M End If If TypeOf _1 Is U2 Then ' B2.M (Dev10 no error) End If End Sub End Class ) compilation.AssertTheseDiagnostics( BC31430: Expression of type 'U1' can never be of type 'S2'. If TypeOf _1 Is S2 Then ' B0.M ~~~~~~~~~~~~~~~ BC31430: Expression of type 'U1' can never be of type 'U2'. If TypeOf _1 Is U2 Then ' B0.M ~~~~~~~~~~~~~~~ BC31430: Expression of type 'U1' can never be of type 'S2'. If TypeOf _1 Is S2 Then ' B1.M ~~~~~~~~~~~~~~~ BC31430: Expression of type 'U1' can never be of type 'U2'. If TypeOf _1 Is U2 Then ' B1.M ~~~~~~~~~~~~~~~ BC31430: Expression of type 'U2' can never be of type 'S1'. If TypeOf _2 Is S1 Then ' B1.M ~~~~~~~~~~~~~~~ BC31430: Expression of type 'U2' can never be of type 'U1'. If TypeOf _2 Is U1 Then ' B1.M ~~~~~~~~~~~~~~~ BC31430: Expression of type 'U1' can never be of type 'S2'. If TypeOf _1 Is S2 Then ' B2.M ~~~~~~~~~~~~~~~ ) End Sub Public Sub TypeParameterWithConstraints03() Dim compilation = CompilationUtils.CreateCompilationWithMscorlib( Interface I End Interface NotInheritable Class C End Class Structure S End Structure MustInherit Class A(Of T1, T2) Public MustOverride Sub M(Of U1 As T1, U2 As T2)(_1 As U1, _2 As U2) End Class Class B1 Inherits A(Of C, I) Public Overrides Sub M(Of U1 As C, U2 As I)(_1 As U1, _2 As U2) If TypeOf _1 Is I Then End If If TypeOf _1 Is U2 Then End If If TypeOf _2 Is C Then End If If TypeOf _2 Is U1 Then End If End Sub End Class Class B2 Inherits A(Of S, I) Public Overrides Sub M(Of U1 As S, U2 As I)(_1 As U1, _2 As U2) If TypeOf _1 Is I Then End If If TypeOf _1 Is U2 Then End If If TypeOf _2 Is S Then End If If TypeOf _2 Is U1 Then End If End Sub End Class ) compilation.AssertNoErrors() End Sub Public Sub TypeParameterWithConstraints04() Dim compilation = CompilationUtils.CreateCompilationWithMscorlib( MustInherit Class A(Of T) Public MustOverride Sub M(Of U As T)(_u As U) End Class Class B0 Inherits A(Of System.Array) Public Overrides Sub M(Of U As System.Array)(_u As U) If TypeOf _u Is String() Then End If End Sub End Class Class B1 Inherits A(Of String()) Public Overrides Sub M(Of U As String())(_u As U) If TypeOf _u Is System.Array Then End If If TypeOf _u Is Integer() Then End If End Sub End Class ) compilation.AssertTheseDiagnostics( BC31430: Expression of type 'U' can never be of type 'Integer()'. If TypeOf _u Is Integer() Then ~~~~~~~~~~~~~~~~~~~~~~ ) End Sub Public Sub TypeParameterWithConstraints05() Dim compilation = CompilationUtils.CreateCompilationWithMscorlib( Enum E A End Enum MustInherit Class A(Of T) Public MustOverride Sub M(Of U As T)(_u As U) End Class Class B1 Inherits A(Of Integer) Public Overrides Sub M(Of U As Integer)(_u As U) If TypeOf _u Is E Then End If End Sub End Class Class B2 Inherits A(Of E) Public Overrides Sub M(Of U As E)(_u As U) If TypeOf _u Is Integer Then End If End Sub End Class ) compilation.AssertTheseDiagnostics( BC31430: Expression of type 'U' can never be of type 'E'. If TypeOf _u Is E Then ~~~~~~~~~~~~~~ ) End Sub Public Sub BC30021ERR_TypeOfRequiresReferenceType1() Dim compilation = CompilationUtils.CreateCompilationWithMscorlib( Class C Shared Sub M() Dim i2 As Integer? 'COMPILEERROR: BC30021, "i2" If TypeOf i2 Is Integer Then End If End Sub End Class ) CompilationUtils.AssertTheseDiagnostics(compilation, BC30021: 'TypeOf ... Is' requires its left operand to have a reference type, but this operand has the value type 'Integer?'. If TypeOf i2 Is Integer Then ~~ ) End Sub ' For compatibility with Dev10, "TypeOf t" should be ' supported for type parameter T, regardless of constraints. Public Sub BC30021ERR_TypeOfRequiresReferenceType1_1() Dim compilation = CompilationUtils.CreateCompilationWithMscorlib( Interface I End Interface Class A End Class Class C Shared Sub M(Of T1, T2 As Class, T3 As Structure, T4 As New, T5 As I, T6 As A, T7 As U, U)(_1 As T1, _2 As T2, _3 As T3, _4 As T4, _5 As T5, _6 As T6, _7 As T7) If TypeOf _1 Is Object Then End If If TypeOf _2 Is Object Then End If If TypeOf _3 Is Object Then End If If TypeOf _4 Is Object Then End If If TypeOf _5 Is Object Then End If If TypeOf _6 Is Object Then End If If TypeOf _7 Is Object Then End If End Sub End Class ) CompilationUtils.AssertNoErrors(compilation) End Sub Public Sub BC31430ERR_TypeOfExprAlwaysFalse2() Dim compilation = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime( Module M Sub Goo(Of T As Structure)(ByVal x As T) Dim y = TypeOf x Is String End Sub End Module ) CompilationUtils.AssertTheseDiagnostics(compilation, BC31430: Expression of type 'T' can never be of type 'String'. Dim y = TypeOf x Is String ~~~~~~~~~~~~~~~~~~ ) End Sub End Class End Namespace