提交 ad32a7a6 编写于 作者: N Neal Gafter

Allow VB Shared New to initialize a Shared field of another instance.

Fixes #1028
Closes #1116
上级 56a89273
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
' TODO: Strict Module
Imports System.Runtime.CompilerServices
Public Class TestOptions
Public Shared ReadOnly Script As New VisualBasicParseOptions(kind:=SourceCodeKind.Script)
Public Shared ReadOnly Interactive As New VisualBasicParseOptions(kind:=SourceCodeKind.Interactive)
......@@ -20,3 +21,15 @@ Public Class TestOptions
Public Shared ReadOnly ReleaseModule As New VisualBasicCompilationOptions(OutputKind.NetModule, optimizationLevel:=OptimizationLevel.Release)
Public Shared ReadOnly ReleaseWinMD As New VisualBasicCompilationOptions(OutputKind.WindowsRuntimeMetadata, optimizationLevel:=OptimizationLevel.Release)
End Class
Module TestOptionExtensions
<Extension()>
Public Function WithStrictMode(options As VisualBasicCompilationOptions) As VisualBasicCompilationOptions
Return options.WithFeatures(options.Features.Add("strict"))
End Function
<Extension()>
Public Function WithStrictMode(compilation As VisualBasicCompilation) As VisualBasicCompilation
Return compilation.WithOptions(compilation.Options.WithStrictMode())
End Function
End Module
......@@ -1551,11 +1551,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
' an LValue. In this case, containingMember will be a LambdaSymbol rather than a symbol for
' constructor.
If Me.ContainingMember.ContainingSymbol Is field.ContainingSymbol Then
Return True
End If
Return False
' We duplicate a bug in the native compiler for compatibility in non-strict mode
Return If(Me.Compilation.FeatureStrictEnabled,
Me.ContainingMember.ContainingSymbol Is field.ContainingSymbol,
Me.ContainingMember.ContainingSymbol.OriginalDefinition Is field.ContainingSymbol.OriginalDefinition)
End Function
''' <summary>
......
......@@ -1820,6 +1820,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return New SyntaxTreeSemanticModel(Me, DirectCast(Me.SourceModule, SourceModuleSymbol), syntaxTree, ignoreAccessibility)
End Function
Friend ReadOnly Property FeatureStrictEnabled As Boolean
Get
Return Me.Feature("strict") IsNot Nothing
End Get
End Property
#End Region
#Region "Diagnostics"
......
......@@ -1408,6 +1408,78 @@ BC30799: Field 'Clazz.b' has an invalid constant value.
</errors>)
End Sub
<Fact, WorkItem(1028, "https://github.com/dotnet/roslyn/issues/1028")>
Sub WriteOfReadonlySharedMemberOfAnotherInstantiation01()
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib(
<compilation>
<file name="a.vb">
Class Foo(Of T)
Shared Sub New()
Foo(Of Integer).X = 12
Foo(Of Integer).Y = 12
Foo(Of T).X = 12
Foo(Of T).Y = 12
End Sub
Public Shared ReadOnly X As Integer
Public Shared ReadOnly Property Y As Integer = 0
End Class
</file>
</compilation>, TestOptions.ReleaseDll)
CompilationUtils.AssertTheseDiagnostics(compilation, <expected>
BC30526: Property 'Y' is 'ReadOnly'.
Foo(Of Integer).Y = 12
~~~~~~~~~~~~~~~~~~~~~~
</expected>)
CompilationUtils.AssertTheseDiagnostics(compilation.WithStrictMode(), <expected>
BC30064: 'ReadOnly' variable cannot be the target of an assignment.
Foo(Of Integer).X = 12
~~~~~~~~~~~~~~~~~
BC30526: Property 'Y' is 'ReadOnly'.
Foo(Of Integer).Y = 12
~~~~~~~~~~~~~~~~~~~~~~
</expected>)
End Sub
<Fact, WorkItem(1028, "https://github.com/dotnet/roslyn/issues/1028")>
Public Sub WriteOfReadonlySharedMemberOfAnotherInstantiation02()
CompileAndVerify(
<compilation>
<file name="a.vb">
Imports System
Module M1
Sub Main()
Console.WriteLine(Foo(Of Long).X)
Console.WriteLine(Foo(Of Integer).X)
Console.WriteLine(Foo(Of String).X)
Console.WriteLine(Foo(Of Integer).X)
End Sub
End Module
Public Class Foo(Of T)
Shared Sub New()
Console.WriteLine("Initializing for {0}", GetType(T))
Foo(Of Integer).X = GetType(T).Name
End Sub
Public Shared ReadOnly X As String
End Class
</file>
</compilation>,
verify:=False,
expectedOutput:="Initializing for System.Int64
Initializing for System.Int32
Int64
Initializing for System.String
String
")
End Sub
#Region "Helpers"
Private Shared Function CompileAndExtractTypeSymbol(sources As Xml.Linq.XElement, Optional typeName As String = "C") As SourceNamedTypeSymbol
Dim compilation = CompilationUtils.CreateCompilationWithMscorlib(sources)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册