diff --git a/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs b/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs index 6d01367384f345bcc0fee6f8bf901693139507bc..51181fe1c68f6cb88941f56e57bce47ac3c16745 100644 --- a/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs +++ b/src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs @@ -581,6 +581,7 @@ private void CompileSynthesizedMethods(ImmutableArray additiona private void CompileSynthesizedMethods(TypeCompilationState compilationState) { Debug.Assert(_moduleBeingBuiltOpt != null); + Debug.Assert(compilationState.ModuleBuilderOpt == _moduleBeingBuiltOpt); if (!compilationState.HasSynthesizedMethods) { diff --git a/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs b/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs index c0e9b365493edbf32d088532474393606ad164c1..54f86480c90586a5c6f185db8f306f7a3f76704b 100644 --- a/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs +++ b/src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs @@ -324,6 +324,16 @@ private Location GetSmallestSourceLocationOrNull(Symbol symbol) return result; } + /// + /// Ignore accessibility when resolving well-known type + /// members, in particular for generic type arguments + /// (e.g.: binding to internal types in the EE). + /// + internal virtual bool IgnoreAccessibility + { + get { return false; } + } + internal virtual VariableSlotAllocator TryCreateVariableSlotAllocator(MethodSymbol method) { return null; diff --git a/src/Compilers/CSharp/Portable/Lowering/AsyncRewriter/AsyncRewriter.cs b/src/Compilers/CSharp/Portable/Lowering/AsyncRewriter/AsyncRewriter.cs index 5c88b7992829d48a08c069a75af51a0f2c9cf916..0866a3e1ce9e45f9098674fc9ba9cdb8e05a077d 100644 --- a/src/Compilers/CSharp/Portable/Lowering/AsyncRewriter/AsyncRewriter.cs +++ b/src/Compilers/CSharp/Portable/Lowering/AsyncRewriter/AsyncRewriter.cs @@ -13,6 +13,7 @@ internal partial class AsyncRewriter : StateMachineRewriter private readonly AsyncMethodBuilderMemberCollection _asyncMethodBuilderMemberCollection; private readonly bool _constructedSuccessfully; private readonly int _methodOrdinal; + private readonly bool _ignoreAccessibility; private FieldSymbol _builderField; @@ -37,6 +38,7 @@ internal partial class AsyncRewriter : StateMachineRewriter } _methodOrdinal = methodOrdinal; + _ignoreAccessibility = compilationState.ModuleBuilderOpt.IgnoreAccessibility; } /// @@ -166,7 +168,7 @@ protected override BoundStatement GenerateStateMachineCreation(LocalSymbol state bodyBuilder.Add( F.Assignment( F.Field(F.Local(stateMachineVariable), _builderField.AsMember(frameType)), - F.StaticCall(BinderFlags.IgnoreAccessibility, methodScopeAsyncMethodBuilderMemberCollection.BuilderType, "Create", ImmutableArray.Empty))); + F.StaticCall(_ignoreAccessibility ? BinderFlags.IgnoreAccessibility : BinderFlags.None, methodScopeAsyncMethodBuilderMemberCollection.BuilderType, "Create", ImmutableArray.Empty))); // local.$stateField = NotStartedStateMachine bodyBuilder.Add( diff --git a/src/Compilers/Core/CodeAnalysisTest/Text/EncodedStringTextTests.cs b/src/Compilers/Core/CodeAnalysisTest/Text/EncodedStringTextTests.cs index f6e1c250dfb8686f48be30cbd8b586d930e43d2d..b8daefacad05b9badba13d6cd0d719e51612e32f 100644 --- a/src/Compilers/Core/CodeAnalysisTest/Text/EncodedStringTextTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/Text/EncodedStringTextTests.cs @@ -97,8 +97,9 @@ public void CheckSum_SHA256() public void Decode_NonUtf8() { var utf8 = new UTF8Encoding(false, true); - var text = "abc def baz aeiouy " + Encoding.Default.GetString(new byte[] { 0x80, 0x92, 0xA4, 0xB6, 0xC9, 0xDB, 0xED, 0xFF }); - var bytes = Encoding.Default.GetBytesWithPreamble(text); + var encoding = Encoding.GetEncoding(name: "Latin1"); + var text = "abc def baz aeiouy " + encoding.GetString(new byte[] { 0x80, 0x92, 0xA4, 0xB6, 0xC9, 0xDB, 0xED, 0xFF }); + var bytes = encoding.GetBytesWithPreamble(text); // Encoding.Default should not decode to UTF-8 using (var stream = new MemoryStream(bytes)) @@ -116,7 +117,7 @@ public void Decode_NonUtf8() { var sourceText = EncodedStringText.Create(stream); Assert.Equal(text, sourceText.ToString()); - Assert.Equal(Encoding.Default, sourceText.Encoding); + Assert.Equal(encoding, sourceText.Encoding); Assert.True(stream.CanRead); } } diff --git a/src/Compilers/Core/Desktop/EncodedStringText.cs b/src/Compilers/Core/Desktop/EncodedStringText.cs index cf6fdb92592812154fad7980de2bcba8d040fa13..c9c7f318c24338076b4bb764bdc834ee7a259c9b 100644 --- a/src/Compilers/Core/Desktop/EncodedStringText.cs +++ b/src/Compilers/Core/Desktop/EncodedStringText.cs @@ -23,7 +23,7 @@ internal static class EncodedStringText /// from in two ways: /// 1. It attempts to minimize allocations by trying to read the stream into a byte array. /// 2. If is null, it will first try UTF8 and, if that fails, it will - /// try extended ASCII (code page 1252). + /// try extended ASCII (code page Latin1). /// /// The stream containing encoded text. /// @@ -57,7 +57,7 @@ internal static SourceText Create(Stream stream, Encoding defaultEncoding = null try { - return Decode(stream, defaultEncoding ?? Encoding.GetEncoding(codepage: 1252), checksumAlgorithm, throwIfBinaryDetected: detectEncoding); + return Decode(stream, defaultEncoding ?? Encoding.GetEncoding(name: "Latin1"), checksumAlgorithm, throwIfBinaryDetected: detectEncoding); } catch (DecoderFallbackException e) { diff --git a/src/Compilers/VisualBasic/Desktop/CommandLine/CommandLineParser.vb b/src/Compilers/VisualBasic/Desktop/CommandLine/CommandLineParser.vb index 3c128a7366b741968d18ce22e258e5fbba08f9c6..78896b6db9ca44e33785a496676e3cea7e4d7c5d 100644 --- a/src/Compilers/VisualBasic/Desktop/CommandLine/CommandLineParser.vb +++ b/src/Compilers/VisualBasic/Desktop/CommandLine/CommandLineParser.vb @@ -1051,12 +1051,12 @@ lVbRuntimePlus: End If ' Locate default 'mscorlib.dll' or 'System.Runtime.dll', if any. - Dim defaultCoreLibraryReference As CommandLineReference? = LoadCoreLibraryReference(sdkPaths, baseDirectory) + Dim defaultCoreLibraryReference As CommandLineReference? = LoadCoreLibraryReference(sdkPaths, baseDirectory, sdkDirectory) ' If /nostdlib is not specified, load System.dll ' Dev12 does it through combination of CompilerHost::InitStandardLibraryList and CompilerProject::AddStandardLibraries. If Not noStdLib Then - Dim systemDllPath As String = FindFileInSdkPath(sdkPaths, "System.dll", baseDirectory) + Dim systemDllPath As String = FindFileInSdkPath(sdkPaths, "System.dll", baseDirectory, sdkDirectory) If systemDllPath Is Nothing Then AddDiagnostic(diagnostics, ERRID.WRN_CannotFindStandardLibrary1, "System.dll") Else @@ -1069,7 +1069,7 @@ lVbRuntimePlus: ' Add reference to 'Microsoft.VisualBasic.dll' if needed If includeVbRuntimeReference Then If vbRuntimePath Is Nothing Then - Dim msVbDllPath As String = FindFileInSdkPath(sdkPaths, "Microsoft.VisualBasic.dll", baseDirectory) + Dim msVbDllPath As String = FindFileInSdkPath(sdkPaths, "Microsoft.VisualBasic.dll", baseDirectory, sdkDirectory) If msVbDllPath Is Nothing Then AddDiagnostic(diagnostics, ERRID.ERR_LibNotFound, "Microsoft.VisualBasic.dll") Else @@ -1209,7 +1209,7 @@ lVbRuntimePlus: } End Function - Private Function LoadCoreLibraryReference(sdkPaths As List(Of String), baseDirectory As String) As CommandLineReference? + Private Function LoadCoreLibraryReference(sdkPaths As List(Of String), baseDirectory As String, sdkDirectory As String) As CommandLineReference? ' Load Core library in Dev11: ' Traditionally VB compiler has hard-coded the name of mscorlib.dll. In the Immersive profile the ' library is called System.Runtime.dll. Ideally we should get rid of the dependency on the name and @@ -1219,8 +1219,8 @@ lVbRuntimePlus: ' There is an extra check to only pick an assembly with no other assembly refs. This is so that is an ' user drops a user-defined binary called System.runtime.dll into the fx directory we still want to pick ' mscorlib. - Dim msCorLibPath As String = FindFileInSdkPath(sdkPaths, "mscorlib.dll", baseDirectory) - Dim systemRuntimePath As String = FindFileInSdkPath(sdkPaths, "System.Runtime.dll", baseDirectory) + Dim msCorLibPath As String = FindFileInSdkPath(sdkPaths, "mscorlib.dll", baseDirectory, sdkDirectory) + Dim systemRuntimePath As String = FindFileInSdkPath(sdkPaths, "System.Runtime.dll", baseDirectory, sdkDirectory) If systemRuntimePath IsNot Nothing Then If msCorLibPath Is Nothing Then @@ -1252,9 +1252,9 @@ lVbRuntimePlus: Return Nothing End Function - Private Function FindFileInSdkPath(sdkPaths As List(Of String), fileName As String, baseDirectory As String) As String + Private Function FindFileInSdkPath(sdkPaths As List(Of String), fileName As String, baseDirectory As String, sdkDirectory As String) As String For Each path In sdkPaths - Dim absolutePath = FileUtilities.ResolveRelativePath(If(path, RuntimeEnvironment.GetRuntimeDirectory()), baseDirectory) + Dim absolutePath = FileUtilities.ResolveRelativePath(If(path, sdkDirectory), baseDirectory) If absolutePath IsNot Nothing Then Dim filePath = PathUtilities.CombineAbsoluteAndRelativePaths(absolutePath, fileName) If File.Exists(filePath) Then diff --git a/src/Compilers/VisualBasic/Portable/BasicCodeAnalysis.vbproj b/src/Compilers/VisualBasic/Portable/BasicCodeAnalysis.vbproj index a1ef81ed7dc807d6d44aa106e1e19369b0e17f43..1d87a1b08c70c53a2d35fd92099ea125d5fce0d9 100644 --- a/src/Compilers/VisualBasic/Portable/BasicCodeAnalysis.vbproj +++ b/src/Compilers/VisualBasic/Portable/BasicCodeAnalysis.vbproj @@ -181,6 +181,7 @@ + diff --git a/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/Binders/IgnoreAccessibilityBinder.vb b/src/Compilers/VisualBasic/Portable/Binding/IgnoreAccessibilityBinder.vb similarity index 56% rename from src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/Binders/IgnoreAccessibilityBinder.vb rename to src/Compilers/VisualBasic/Portable/Binding/IgnoreAccessibilityBinder.vb index 0d771523ccbf34ea898c28c4caf7466fab5e9fe6..f832aba3931a31ccf31947afd45d4d0a96190319 100644 --- a/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/Binders/IgnoreAccessibilityBinder.vb +++ b/src/Compilers/VisualBasic/Portable/Binding/IgnoreAccessibilityBinder.vb @@ -1,6 +1,9 @@ ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -Namespace Microsoft.CodeAnalysis.VisualBasic.ExpressionEvaluator +Imports System.Runtime.InteropServices +Imports Microsoft.CodeAnalysis.VisualBasic.Symbols + +Namespace Microsoft.CodeAnalysis.VisualBasic Friend NotInheritable Class IgnoreAccessibilityBinder Inherits Binder @@ -11,5 +14,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ExpressionEvaluator Friend Overrides Function BinderSpecificLookupOptions(options As LookupOptions) As LookupOptions Return ContainingBinder.BinderSpecificLookupOptions(options) Or LookupOptions.IgnoreAccessibility End Function + + Public Overrides Function CheckAccessibility(sym As Symbol, <[In]> ByRef useSiteDiagnostics As HashSet(Of DiagnosticInfo), Optional accessThroughType As TypeSymbol = Nothing, Optional basesBeingResolved As ConsList(Of Symbol) = Nothing) As AccessCheckResult + Return AccessCheckResult.Accessible + End Function End Class End Namespace diff --git a/src/Compilers/VisualBasic/Portable/Emit/PEModuleBuilder.vb b/src/Compilers/VisualBasic/Portable/Emit/PEModuleBuilder.vb index 48b4a6d5927467dc5139b666c49de34cd4029555..68db314d3475bc08fb05298b9e4c2be756599f32 100644 --- a/src/Compilers/VisualBasic/Portable/Emit/PEModuleBuilder.vb +++ b/src/Compilers/VisualBasic/Portable/Emit/PEModuleBuilder.vb @@ -300,6 +300,17 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Emit Return result End Function + ''' + ''' Ignore accessibility when resolving well-known type + ''' members, in particular for generic type arguments + ''' (e.g.: binding to internal types in the EE). + ''' + Friend Overridable ReadOnly Property IgnoreAccessibility As Boolean + Get + Return False + End Get + End Property + Friend Overridable Function TryCreateVariableSlotAllocator(method As MethodSymbol) As VariableSlotAllocator Return Nothing End Function diff --git a/src/Compilers/VisualBasic/Portable/Lowering/AsyncRewriter/AsyncRewriter.vb b/src/Compilers/VisualBasic/Portable/Lowering/AsyncRewriter/AsyncRewriter.vb index a7a1a1c0247dace560ba742385b40f12a5f847c2..c6adafb1ddc7f6dced972eec5725431111dc941b 100644 --- a/src/Compilers/VisualBasic/Portable/Lowering/AsyncRewriter/AsyncRewriter.vb +++ b/src/Compilers/VisualBasic/Portable/Lowering/AsyncRewriter/AsyncRewriter.vb @@ -2,14 +2,9 @@ Imports System.Collections.Generic Imports System.Collections.Immutable -Imports System.Runtime.CompilerServices Imports System.Runtime.InteropServices -Imports System.Threading -Imports Microsoft.Cci Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.CodeGen -Imports Microsoft.CodeAnalysis.Collections -Imports Microsoft.CodeAnalysis.Text Imports Microsoft.CodeAnalysis.VisualBasic.Symbols Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -19,6 +14,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Inherits StateMachineRewriter(Of CapturedSymbolOrExpression) Private ReadOnly _binder As Binder + Private ReadOnly _lookupOptions As LookupOptions Private ReadOnly _asyncMethodKind As AsyncMethodKind Private ReadOnly _builderType As NamedTypeSymbol Private ReadOnly _resultType As TypeSymbol @@ -37,6 +33,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic MyBase.New(body, method, stateMachineType, slotAllocatorOpt, compilationState, diagnostics) Me._binder = CreateMethodBinder(method) + Me._lookupOptions = LookupOptions.AllMethodsOfAnyArity Or LookupOptions.IgnoreExtensionMethods Or LookupOptions.NoBaseClassLookup + + If compilationState.ModuleBuilderOpt.IgnoreAccessibility Then + Me._binder = New IgnoreAccessibilityBinder(Me._binder) + Me._lookupOptions = Me._lookupOptions Or LookupOptions.IgnoreAccessibility + End If Debug.Assert(asyncKind <> AsyncMethodKind.None) Debug.Assert(asyncKind = GetAsyncMethodKind(method)) @@ -498,19 +500,12 @@ lCaptureRValue: typeArgs As ImmutableArray(Of TypeSymbol), ParamArray arguments As BoundExpression()) As BoundExpression - Dim anyArgumentsWithErrors = False - - ' Check if we have any bad arguments. - For Each a In arguments - If a.HasErrors Then - anyArgumentsWithErrors = True - End If - Next - ' Get the method group Dim methodGroup = FindMethodAndReturnMethodGroup(receiver, type, methodName, typeArgs) - If methodGroup Is Nothing OrElse anyArgumentsWithErrors OrElse (receiver IsNot Nothing AndAlso receiver.HasErrors) Then + If methodGroup Is Nothing OrElse + arguments.Any(Function(a) a.HasErrors) OrElse + (receiver IsNot Nothing AndAlso receiver.HasErrors) Then Return Me.F.BadExpression(arguments) End If @@ -534,9 +529,8 @@ lCaptureRValue: Dim group As BoundMethodGroup = Nothing Dim result = LookupResult.GetInstance() - Dim options As LookupOptions = LookupOptions.AllMethodsOfAnyArity Or LookupOptions.IgnoreExtensionMethods Or LookupOptions.NoBaseClassLookup Dim useSiteDiagnostics As HashSet(Of DiagnosticInfo) = Nothing - Me._binder.LookupMember(result, type, methodName, arity:=0, options:=options, useSiteDiagnostics:=useSiteDiagnostics) + Me._binder.LookupMember(result, type, methodName, arity:=0, options:=_lookupOptions, useSiteDiagnostics:=useSiteDiagnostics) Me.Diagnostics.Add(Me.F.Syntax, useSiteDiagnostics) If result.IsGood Then @@ -594,9 +588,8 @@ lCaptureRValue: Dim group As BoundPropertyGroup = Nothing Dim result = LookupResult.GetInstance() - Dim options As LookupOptions = LookupOptions.AllMethodsOfAnyArity Or LookupOptions.IgnoreExtensionMethods Or LookupOptions.NoBaseClassLookup Dim useSiteDiagnostics As HashSet(Of DiagnosticInfo) = Nothing - Me._binder.LookupMember(result, type, propertyName, arity:=0, options:=options, useSiteDiagnostics:=useSiteDiagnostics) + Me._binder.LookupMember(result, type, propertyName, arity:=0, options:=_lookupOptions, useSiteDiagnostics:=useSiteDiagnostics) Me.Diagnostics.Add(Me.F.Syntax, useSiteDiagnostics) If result.IsGood Then diff --git a/src/Compilers/VisualBasic/Portable/Lowering/Rewriter.vb b/src/Compilers/VisualBasic/Portable/Lowering/Rewriter.vb index 73de0a82cd1e820cb080905ac934e2b8503a5f4b..badbd6746cd73b72587d4b5e355590feef5b47bf 100644 --- a/src/Compilers/VisualBasic/Portable/Lowering/Rewriter.vb +++ b/src/Compilers/VisualBasic/Portable/Lowering/Rewriter.vb @@ -24,6 +24,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic isBodySynthesized As Boolean) As BoundBlock Debug.Assert(Not body.HasErrors) + Debug.Assert(compilationState.ModuleBuilderOpt IsNot Nothing) ' performs node-specific lowering. Dim sawLambdas As Boolean @@ -89,6 +90,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic slotAllocatorOpt As VariableSlotAllocator, ByRef stateMachineTypeOpt As StateMachineTypeSymbol) As BoundBlock + Debug.Assert(compilationState.ModuleBuilderOpt IsNot Nothing) + Dim iteratorStateMachine As IteratorStateMachine = Nothing Dim bodyWithoutIterators = IteratorRewriter.Rewrite(bodyWithoutLambdas, method, diff --git a/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/EEAssemblyBuilder.cs b/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/EEAssemblyBuilder.cs index ad26997f0f23fdbfea5e16474a0a3bd4a594fa90..cf10476dbf06ff61fbe50cc0886ef20ec52236e0 100644 --- a/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/EEAssemblyBuilder.cs +++ b/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/EEAssemblyBuilder.cs @@ -61,6 +61,11 @@ protected override IModuleReference TranslateModule(ModuleSymbol symbol, Diagnos return base.TranslateModule(symbol, diagnostics); } + internal override bool IgnoreAccessibility + { + get { return true; } + } + public override int CurrentGenerationOrdinal => 0; internal override VariableSlotAllocator TryCreateVariableSlotAllocator(MethodSymbol symbol) diff --git a/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/BasicExpressionCompiler.vbproj b/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/BasicExpressionCompiler.vbproj index ad1b58eb796906410920fee3dfe9360a23ba69f1..fe391af088f198de924e9a69a27da09632752889 100644 --- a/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/BasicExpressionCompiler.vbproj +++ b/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/BasicExpressionCompiler.vbproj @@ -66,7 +66,6 @@ - diff --git a/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/EEAssemblyBuilder.vb b/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/EEAssemblyBuilder.vb index 2e2cb613ed860b07c8864670614f0256beea30c8..bf10fcd0e4c036028018dc29ac4fd815adb3c817 100644 --- a/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/EEAssemblyBuilder.vb +++ b/src/ExpressionEvaluator/VisualBasic/Source/ExpressionCompiler/EEAssemblyBuilder.vb @@ -60,6 +60,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ExpressionEvaluator Return MyBase.TranslateModule(symbol, diagnostics) End Function + Friend Overrides ReadOnly Property IgnoreAccessibility As Boolean + Get + Return True + End Get + End Property + Public Overrides ReadOnly Property CurrentGenerationOrdinal As Integer Get Return 0 diff --git a/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/ExpressionCompilerTests.vb b/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/ExpressionCompilerTests.vb index 94eefb8ae64f819baa6d4da31d0dab56f812ec90..bd39c4a267c1e7368ea716233ec2680b56c687c7 100644 --- a/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/ExpressionCompilerTests.vb +++ b/src/ExpressionEvaluator/VisualBasic/Test/ExpressionCompiler/ExpressionCompilerTests.vb @@ -4154,7 +4154,7 @@ End Class" ''' Ignore accessibility in async rewriter. ''' - + Public Sub AsyncRewriterIgnoreAccessibility() Const source = "Imports System