CastException in Microsoft.CodeAnalysis.CShartp.Features.dll

Topics: APIs
Apr 12, 2014 at 10:54 PM
I've cloned Roslyn and trying to implement https://roslyn.codeplex.com/discussions/541330

(I know, I know, there's no way on earth this will get into master, just as a challenge).

I've been reviewing the code:
And when I feel confident enough to break things, I've added in SyntaxKind just after SlashToken, my new and incredible AtToken. No more changes are necessary, just Play and the VS Hive throws and exception:

System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.TypeArgumentListSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax'.
Source=Microsoft.CodeAnalysis.CSharp.Features
StackTrace:
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.SimplifyTypeNames.CSharpSimplifyTypeNamesDiagnosticAnalyzer.CanSimplifyTypeNameExpression(SemanticModel model, SyntaxNode node, OptionSet optionSet, TextSpan& issueSpan, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.SimplifyTypeNames.CSharpSimplifyTypeNamesDiagnosticAnalyzer.CanSimplifyTypeNameExpressionCore(SemanticModel model, SyntaxNode node, OptionSet optionSet, TextSpan& issueSpan, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Diagnostics.SimplifyTypeNames.SimplifyTypeNamesDiagnosticAnalyzerBase`1.TrySimplifyTypeNameExpression(SemanticModel model, SyntaxNode node, Diagnostic& diagnostic, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.SimplifyTypeNames.CSharpSimplifyTypeNamesDiagnosticAnalyzer.<>c__DisplayClass0.<AnalyzeNode>b__2(SyntaxNode n)
   at Microsoft.CodeAnalysis.SyntaxNode.<DescendantNodesOnly>d__6.MoveNext()
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.SimplifyTypeNames.CSharpSimplifyTypeNamesDiagnosticAnalyzer.AnalyzeNode(SyntaxNode node, SemanticModel semanticModel, Action`1 addDiagnostic, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Diagnostics.AbstractSyntaxNodeAnalyzerService`1.ExecuteSyntaxNodeAnalyzer(IDiagnosticAnalyzer analyzer, IEnumerable`1 descendantNodes, SemanticModel model, Action`1 addDiagnostic, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzerDriver.<>c__DisplayClass11.<<GetSemanticDiagnosticsAsync>b__13>d__0.MoveNext()

The error happends in Microsoft.CodeAnalysis.CShartp.Features.dll and the stack trace is greyed out (external code).

Is Features dll included in the source code? Is there a way to solve this?
Developer
Apr 13, 2014 at 1:29 AM
It might be related to your addition of the new SyntaxKind. If you added it in the middle you will have changed the underlying numeric value of the other enum fields. This would make a binary breaking change that would confuse downstream components that were not recompiled with your change. Try moving your new value to the end of the enum.
Apr 13, 2014 at 9:34 AM
I think I've seen ConsumeToken checking that the token value is in a specific range, that could be an issue.

I try and I'll turn back if I can't solve it
Apr 13, 2014 at 10:15 AM
Yup, in SyntaxFacts, at least the following methods expect a proper SyntaxKind ordenation:

GetReservedKeywordKinds / IsReservedKeyword
GetPreprocessorKeywordKinds
IsPunctuation / GetPunctuationKinds
IsPunctuationOrKeyword
GetContextualKeywordKinds

Should I modify them or wait for a new version of Microsoft.CodeAnalysis.CShartp.Features.dll with ranges on SyntaxKind ?
 public enum SyntaxKind : ushort
    {
        None,
        List = GreenNode.ListKind,

        // punctuation
        TildeToken = 8193,  //Why?
        ExclamationToken,
        DollarToken,
        PercentToken,
         ...

        // additional xml tokens
        SlashGreaterThanToken = 9000, // xml empty element end
        LessThanSlashToken, // element end tag start token
        XmlCommentStartToken, // <!--
         ...

        // compound punctuation
        BarBarToken = 10000,
        AmpersandAmpersandToken,
        MinusMinusToken,
        PlusPlusToken,
        ...

        // Keywords
        BoolKeyword = 11000,
        ByteKeyword,
        SByteKeyword,
        ShortKeyword,
        UShortKeyword,
        ....

        // contextual keywords
        YieldKeyword = 12000,
        PartialKeyword,
        AliasKeyword,
        GlobalKeyword,
        AssemblyKeyword,
        ModuleKeyword,
        TypeKeyword,
        ....

        // Other
        OmittedTypeArgumentToken = 13000,
        OmittedArraySizeExpressionToken,
        EndOfDirectiveToken,
        EndOfDocumentationCommentToken,
        EndOfFileToken, //NB: this is assumed to be the last textless token
        ...

        ...
  }