dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.06k stars 4.04k forks source link

Hard crash and/or gold bars when editing a file that contains aliases to pointer types #67989

Closed jhinder closed 1 year ago

jhinder commented 1 year ago

Version Used: VS 17.6 Preview 5

Steps to Reproduce: Perform some IDE operations (typing, saving, etc) in this code file:

using unsafe Alias = int*;

namespace N
{
    internal unsafe class C
    {
        const int Factor = 3;

        void M()
        {
            _ = Alias * Factor;
        }
    }
}

Expected Behavior: No crash, no exceptions

Actual Behavior: The first time devenv crashed with an exception. This was not reproducible, however the reproduction attempts led to several gold bars.

Crash log from `eventvwr` ``` Application: devenv.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.InvalidOperationException at Microsoft.CodeAnalysis.CSharp.Binder.BindNonMethod(Microsoft.CodeAnalysis.CSharp.Syntax.SimpleNameSyntax, Microsoft.CodeAnalysis.CSharp.Symbol, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Microsoft.CodeAnalysis.CSharp.LookupResultKind, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Binder.BindIdentifier(Microsoft.CodeAnalysis.CSharp.Syntax.SimpleNameSyntax, Boolean, Boolean, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Binder.BindSimpleBinaryOperator(Microsoft.CodeAnalysis.CSharp.Syntax.BinaryExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Binder.BindAssignment(Microsoft.CodeAnalysis.CSharp.Syntax.AssignmentExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, Boolean, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag) at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel+IncrementalBinder.BindStatement(Microsoft.CodeAnalysis.CSharp.Syntax.StatementSyntax, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetBoundNodes(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetBoundNodes(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode, Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode ByRef, Microsoft.CodeAnalysis.CSharp.BoundNode ByRef, Microsoft.CodeAnalysis.CSharp.BoundNode ByRef, Microsoft.CodeAnalysis.CSharp.BoundNode ByRef) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetSymbolInfoWorker(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode, SymbolInfoOptions, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetSymbolInfoWorker(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode, SymbolInfoOptions, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfo(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfoFromNode(Microsoft.CodeAnalysis.SyntaxNode, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfoCore(Microsoft.CodeAnalysis.SyntaxNode, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.CSharp.Classification.OperatorOverloadSyntaxClassifier.AddClassifications(Microsoft.CodeAnalysis.SyntaxNode, Microsoft.CodeAnalysis.SemanticModel, Microsoft.CodeAnalysis.Classification.ClassificationOptions, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder`1, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.Classification.AbstractSyntaxClassificationService+Worker.ClassifyNode(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.Classification.AbstractSyntaxClassificationService+Worker.ProcessNodes() at Microsoft.CodeAnalysis.Classification.AbstractSyntaxClassificationService+Worker.Classify(Microsoft.CodeAnalysis.SemanticModel, Microsoft.CodeAnalysis.Text.TextSpan, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder`1, System.Func`2>, System.Func`2>, Microsoft.CodeAnalysis.Classification.ClassificationOptions, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.Classification.AbstractSyntaxClassificationService.AddSemanticClassifications(Microsoft.CodeAnalysis.SemanticModel, Microsoft.CodeAnalysis.Text.TextSpan, System.Func`2>, System.Func`2>, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder`1, Microsoft.CodeAnalysis.Classification.ClassificationOptions, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.Classification.Classifier.GetClassifiedSpans(Microsoft.CodeAnalysis.Host.SolutionServices, Microsoft.CodeAnalysis.Project, Microsoft.CodeAnalysis.SemanticModel, Microsoft.CodeAnalysis.Text.TextSpan, Microsoft.CodeAnalysis.Classification.ClassificationOptions, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.Classification.Classifier.GetClassifiedSpans(Microsoft.CodeAnalysis.SemanticModel, Microsoft.CodeAnalysis.Text.TextSpan, Microsoft.CodeAnalysis.Workspace, System.Threading.CancellationToken) at Roslyn.SyntaxVisualizer.Control.SyntaxVisualizerControl.DisplaySyntaxTree(Microsoft.CodeAnalysis.SyntaxTree, Microsoft.CodeAnalysis.SemanticModel, Boolean, Microsoft.CodeAnalysis.Workspace) at Roslyn.SyntaxVisualizer.Extension.SyntaxVisualizerContainer.RefreshSyntaxVisualizer() at System.Windows.Threading.DispatcherTimer.FireTick(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) ```
Exception from gold bar ``` StreamJsonRpc.RemoteInvocationException: Unexpected value 'PointerType' of type 'Microsoft.CodeAnalysis.SymbolKind' at StreamJsonRpc.JsonRpc.d__151`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.CodeAnalysis.Remote.BrokeredServiceConnection`1.d__20`1.MoveNext() RPC server exception: System.InvalidOperationException: Unexpected value 'PointerType' of type 'Microsoft.CodeAnalysis.SymbolKind' at Microsoft.CodeAnalysis.CSharp.Binder.BindNonMethod(SimpleNameSyntax node, Symbol symbol, BindingDiagnosticBag diagnostics, LookupResultKind resultKind, Boolean indexed, Boolean isError) at Microsoft.CodeAnalysis.CSharp.Binder.BindIdentifier(SimpleNameSyntax node, Boolean invoked, Boolean indexed, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, BindingDiagnosticBag diagnostics, Boolean invoked, Boolean indexed) at Microsoft.CodeAnalysis.CSharp.Binder.BindSimpleBinaryOperator(BinaryExpressionSyntax node, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, BindingDiagnosticBag diagnostics, Boolean invoked, Boolean indexed) at Microsoft.CodeAnalysis.CSharp.Binder.BindAssignment(AssignmentExpressionSyntax node, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, BindingDiagnosticBag diagnostics, Boolean invoked, Boolean indexed) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(CSharpSyntaxNode node, ExpressionSyntax syntax, Boolean allowsAnyExpression, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionStatement(ExpressionStatementSyntax node, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.Binder.BindStatement(StatementSyntax node, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.IncrementalBinder.BindStatement(StatementSyntax node, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.Bind(Binder binder, CSharpSyntaxNode node, BindingDiagnosticBag diagnostics) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetBoundNodes(CSharpSyntaxNode node) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetLowerBoundNode(CSharpSyntaxNode node) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetBoundNodes(CSharpSyntaxNode node, CSharpSyntaxNode& bindableNode, BoundNode& lowestBoundNode, BoundNode& highestBoundNode, BoundNode& boundParent) at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetSymbolInfoWorker(CSharpSyntaxNode node, SymbolInfoOptions options, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetSymbolInfoWorker(CSharpSyntaxNode node, SymbolInfoOptions options, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfo(ExpressionSyntax expression, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfoFromNode(SyntaxNode node, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfoCore(SyntaxNode node, CancellationToken cancellationToken) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument) at Microsoft.CodeAnalysis.FindSymbols.FindReferenceCache.GetSymbolInfo(SyntaxNode node, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.FindSymbols.Finders.AbstractReferenceFinder.FindReferencesInTokensAsync(ISymbol symbol, FindReferencesDocumentState state, ImmutableArray`1 tokens, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.FindSymbols.Finders.AbstractReferenceFinder.FindReferencesInDocumentUsingIdentifierAsync(ISymbol symbol, String identifier, FindReferencesDocumentState state, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.FindSymbols.Finders.FieldSymbolReferenceFinder.FindReferencesInDocumentAsync(IFieldSymbol symbol, FindReferencesDocumentState state, FindReferencesSearchOptions options, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.<>c__DisplayClass28_0.<g__PerformSearchInDocumentWorkerAsync|4>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.<>c__DisplayClass28_0.<g__PerformSearchInDocumentAsync|3>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.<>c__DisplayClass28_0.<g__PerformSearchInProjectAsync|2>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.FindReferencesInDocumentsAsync(ISymbol originalSymbol, IImmutableSet`1 documents, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.GetTagsForReferencedSymbolAsync(ISymbol symbol, Document document, IImmutableSet`1 documentsToSearch, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.GetDocumentHighlightsInCurrentProcessAsync(Document document, Int32 position, IImmutableSet`1 documentsToSearch, HighlightingOptions options, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.DocumentHighlighting.AbstractDocumentHighlightsService.GetDocumentHighlightsAsync(Document document, Int32 position, IImmutableSet`1 documentsToSearch, HighlightingOptions options, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Remote.RemoteDocumentHighlightsService.<>c__DisplayClass2_0.<b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.<>c__DisplayClass9_0`1.<g__ProcessSolutionAsync|1>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Int32 workspaceVersion, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Int32 workspaceVersion, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunWithSolutionAsync[T](Checksum solutionChecksum, Func`2 implementation, CancellationToken cancellationToken) at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunServiceImplAsync[T](Func`2 implementation, CancellationToken cancellationToken) ```

Relates to alias any type: https://github.com/dotnet/roslyn/issues/56323

sharwell commented 1 year ago

SyntaxVisualizerControl

This one should be reported/fixed in dotnet/roslyn-sdk.

jcouv commented 1 year ago

@sharwell I see that you fixed something in SyntaxVisualizerControl that relates to this issue. Can we close this?

jhinder commented 1 year ago

@jcouv The fix by @sharwell relates only to the Syntax Visualizer and prevents the exception from bubbling up and taking down the entire IDE, right? The underlying exception thrown by the compiler (that also causes all the gold bars) is still there.

sharwell commented 1 year ago

Yes that's correct

jcouv commented 1 year ago

Assigned to @CyrusNajmabadi. Looks like an issue with semantic model on alias to pointer type. Let me know if you need help with setting up unittest to repro this.