Skip to content
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

Closed
ilmax opened this issue Mar 4, 2015 · 4 comments · Fixed by #3069
Closed

Renamer throws InvalidCastException #1031

ilmax opened this issue Mar 4, 2015 · 4 comments · Fixed by #3069

Comments

@ilmax
Copy link

ilmax commented Mar 4, 2015

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.Action1<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.AsyncTaskMethodBuilder1[[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.CSharpSyntaxVisitor1<System.__Canon>)
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor1[[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.CSharpSyntaxVisitor1<System.__Canon>)
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor1[[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.CSharpSyntaxVisitor1<System.__Canon>)
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor1[[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.SyntaxList1<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.CSharpSyntaxVisitor1[[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.AsyncTaskMethodBuilder1[[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.HashSet1<Microsoft.CodeAnalysis.DocumentId>, System.Collections.Generic.IEnumerable1<Microsoft.CodeAnalysis.Rename.RenameLocation>, Microsoft.CodeAnalysis.Rename.ConflictEngine.RenamedSpansTracker, Boolean)
at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver+Session+d__1.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[[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.Task1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[[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.Task1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1[[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()

@dpoeschl
Copy link
Contributor

dpoeschl commented Mar 5, 2015

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.

@Pilchie Pilchie added this to the Unknown milestone Mar 5, 2015
@Pilchie Pilchie added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Mar 5, 2015
@jasonmalinowski
Copy link
Member

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.

@jasonmalinowski jasonmalinowski removed this from the Unknown milestone May 20, 2015
@jasonmalinowski jasonmalinowski removed the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label May 20, 2015
@Pilchie Pilchie added this to the 1.0 (stable) milestone May 21, 2015
dpoeschl pushed a commit to dpoeschl/roslyn that referenced this issue May 24, 2015
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.
dpoeschl pushed a commit to dpoeschl/roslyn that referenced this issue May 27, 2015
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.
@dpoeschl
Copy link
Contributor

@ilmax Thanks again for reporting this. We no longer crash, but the rename result will be a solution where the rename has basically failed. Getting this failure information out of the rename call is part of #197, which is not yet implemented, so for now continue to avoid passing in dotted names.

@dpoeschl
Copy link
Contributor

dpoeschl commented Jun 1, 2015

@brettfo Can you verify this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants