-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Renamer throws InvalidCastException #1031
Comments
We don't currently support renaming namespaces in ways that add or remove namespace parts. In the Inline Rename UI, we block periods from being typed in the identifier being renamed, so this exception only happens when the API is called directly with an invalid (i.e. dotted) newName argument. For now, we should make this limitation clearer in the documentation and throw a better exception. |
There are Windows Error Reporting reports of this crash. If I copy a dot into the clipboard, I can crash VS by starting an inline rename and pasting the dot into the file. Looking at one crash report, they also invoked paste to get the dot in there. |
Fixes dotnet#1031 In both the C# and VB RenameRewriterLanguageServices, we were assuming the new rename text would parse as an IdentifierNameSyntax, but if the rename text is invalid it could parse as anything. For example, if the user tries to rename "A" to "A.B", then it will parse as a QualifiedNameSyntax. We now accommodate these cases.
Fixes dotnet#1031 In both the C# and VB RenameRewriterLanguageServices, we were assuming the new rename text would parse as an IdentifierNameSyntax, but if the rename text is invalid it could parse as anything. For example, if the user tries to rename "A" to "A.B", then it will parse as a QualifiedNameSyntax. We now accommodate these cases.
@brettfo Can you verify this? |
Building a CodeFix to adjust namespace to match folder structure, I initially tried to delegate the renaming work to the Renamer class which unfortunately throws InvalidCastException Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.QualifiedNameSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax'.
The repro are steps are super easy, just pass to the Renamer a symbol representing a namespace and a name with . (dot) e.g
Renamer.RenameSymbolAsync(solution, symbol, "My.New.Namespace", options, cancellationToken)
I'm using VS2015 CTP 6 running on Win 10 TP.
Full Stack Trace is:
Application: devenv.exe
Framework Version: v4.0.30319
Description: The application requested process termination through
System.Environment.FailFast(string message). Message: System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.QualifiedNameSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax'. at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService.RenameRewriter.RenameToken(SyntaxToken oldToken, SyntaxToken newToken, String suffix, Boolean isAccessorLocation) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService.RenameRewriter.<RenameAndAnnotateAsync>d__1.MoveNext() Stack: at System.Environment.FailFast(System.String, System.Exception) at Microsoft.CodeAnalysis.FailFast.OnFatalException(System.Exception) at Microsoft.CodeAnalysis.ErrorReporting.FatalError.Report(System.Exception, System.Action
1<System.Exception>)at Microsoft.CodeAnalysis.ErrorReporting.FatalError.ReportUnlessCanceled(System.Exception)
at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter+d__1.MoveNext()
at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter.RenameToken(Microsoft.CodeAnalysis.SyntaxToken, Microsoft.CodeAnalysis.SyntaxToken, System.String, Boolean)
at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter+d__1.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1[[Microsoft.CodeAnalysis.SyntaxToken, Microsoft.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]].Start[[Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter+<RenameAndAnnotateAsync>d__1, Microsoft.CodeAnalysis.CSharp.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<RenameAndAnnotateAsync>d__1 ByRef) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter.RenameAndAnnotateAsync(Microsoft.CodeAnalysis.SyntaxToken, Microsoft.CodeAnalysis.SyntaxToken, Boolean, Boolean) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter.VisitToken(Microsoft.CodeAnalysis.SyntaxToken) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitIdentifierName(Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax) at Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax.Accept[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor
1<System.__Canon>)at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter.Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitQualifiedName(Microsoft.CodeAnalysis.CSharp.Syntax.QualifiedNameSyntax) at Microsoft.CodeAnalysis.CSharp.Syntax.QualifiedNameSyntax.Accept[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor
1<System.__Canon>)at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter.Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitUsingDirective(Microsoft.CodeAnalysis.CSharp.Syntax.UsingDirectiveSyntax) at Microsoft.CodeAnalysis.CSharp.Syntax.UsingDirectiveSyntax.Accept[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor
1<System.__Canon>)at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter.Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitListElement[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.__Canon) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitList[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Microsoft.CodeAnalysis.SyntaxList
1<System.__Canon>)at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitCompilationUnit(Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax)
at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.Accept[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService+RenameRewriter.Visit(Microsoft.CodeAnalysis.SyntaxNode) at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService.AnnotateAndRename(Microsoft.CodeAnalysis.Rename.RenameRewriterParameters) at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver+Session+<AnnotateAndRename_WorkerAsync>d__1.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Start[[Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver+Session+<AnnotateAndRename_WorkerAsync>d__1, Microsoft.CodeAnalysis.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<AnnotateAndRename_WorkerAsync>d__1 ByRef)at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver+Session.AnnotateAndRename_WorkerAsync(Microsoft.CodeAnalysis.Solution, Microsoft.CodeAnalysis.Solution, System.Collections.Generic.HashSet
1<Microsoft.CodeAnalysis.DocumentId>, System.Collections.Generic.IEnumerable
1<Microsoft.CodeAnalysis.Rename.RenameLocation>, Microsoft.CodeAnalysis.Rename.ConflictEngine.RenamedSpansTracker, Boolean)at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver+Session+d__1.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Start[[Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver+Session+<ResolveConflictsAsync>d__1, Microsoft.CodeAnalysis.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]](<ResolveConflictsAsync>d__1 ByRef) at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver+Session.ResolveConflictsAsync() at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver.ResolveConflictsAsync(Microsoft.CodeAnalysis.Rename.RenameLocationSet, System.String, System.String, Microsoft.CodeAnalysis.Options.OptionSet, System.Threading.CancellationToken) at Microsoft.CodeAnalysis.Rename.Renamer+<RenameSymbolAsync>d__1.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(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.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c__DisplayClass1.<OutputAsyncCausalityEvents>b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper.Invoke() at System.Runtime.CompilerServices.TaskAwaiter+<>c__DisplayClass1.<OutputWaitEtwEvents>b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper.Invoke() at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action, Boolean, System.Threading.Tasks.Task ByRef) at System.Threading.Tasks.Task.FinishContinuations() at System.Threading.Tasks.Task.FinishStageThree() at System.Threading.Tasks.Task
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)at System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].SetResult(System.__Canon) at Microsoft.CodeAnalysis.Rename.RenameLocationSet+<FindAsync>d__1.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(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.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c__DisplayClass1.<OutputAsyncCausalityEvents>b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper.Invoke() at System.Runtime.CompilerServices.TaskAwaiter+<>c__DisplayClass1.<OutputWaitEtwEvents>b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper.Invoke() at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action, Boolean, System.Threading.Tasks.Task ByRef) at System.Threading.Tasks.Task.FinishContinuations() at System.Threading.Tasks.Task.FinishStageThree() at System.Threading.Tasks.Task
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)at System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].SetResult(System.__Canon) at Microsoft.CodeAnalysis.Rename.RenameLocationSet+<AddLocationsReferenceSymbolsAsync>d__1.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext(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.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c__DisplayClass1.<OutputAsyncCausalityEvents>b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper.Invoke() at System.Runtime.CompilerServices.TaskAwaiter+<>c__DisplayClass1.<OutputWaitEtwEvents>b__0() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper.Invoke() at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action, Boolean, System.Threading.Tasks.Task ByRef) at System.Threading.Tasks.Task.FinishContinuations() at System.Threading.Tasks.Task.FinishStageThree() at System.Threading.Tasks.Task.FinishStageTwo() at System.Threading.Tasks.Task.Finish(Boolean) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef) at System.Threading.Tasks.Task.ExecuteEntry(Boolean) at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
The text was updated successfully, but these errors were encountered: