KirillOsenkov / RoslynQuoter

Roslyn tool that for a given C# program shows syntax tree API calls to construct its syntax tree
http://roslynquoter.azurewebsites.net
Apache License 2.0
918 stars 118 forks source link

Support C# 7.1 default literal #32

Closed svick closed 6 years ago

svick commented 6 years ago

When trying to use a C# 7.1 feature, like default literal, RoslynQuoter fails with an exception. I think it should set language version to latest, to avoid this kind of problem.

As an example, this code:

class C
{
    void A(int? x = default)
    {
    }
}

Produces this exception:

Microsoft.CodeAnalysis.Scripting.CompilationErrorException: (4,17): error CS8107: Feature 'default literal' is not available in C# 7.
Please use language version 7.1 or greater.
 at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.ThrowIfAnyCompilationErrors(DiagnosticBag diagnostics, DiagnosticFormatter formatter)
 at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.CreateExecutor[T](ScriptCompiler compiler, Compilation compilation, Boolean emitDebugInformation, CancellationToken cancellationToken)
 at Microsoft.CodeAnalysis.Scripting.Script`1.GetExecutor(CancellationToken cancellationToken)
 at Microsoft.CodeAnalysis.Scripting.Script`1.RunAsync(Object globals, Func`2 catchException, CancellationToken cancellationToken)
 at Quoter.Evaluate(String apiCallString, Boolean normalizeWhitespace)
 at Quoter.AddModifyingCall(ApiCall apiCall, MethodCall methodCall)
 at Quoter.AddModifyingCalls(Object treeElement, ApiCall apiCall, List`1 values)
 at Quoter.QuoteNode(SyntaxNode node, String name)
 at Quoter.QuotePropertyValue(SyntaxNode node, PropertyInfo property)
 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
 at Quoter.QuotePropertyValues(SyntaxNode node)
 at Quoter.QuoteNode(SyntaxNode node, String name) at Quoter.Quote(Object treeElement, String name)
 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
 at Quoter.QuoteList(IEnumerable syntaxList, String name)
 at Quoter.QuotePropertyValue(SyntaxNode node, PropertyInfo property)
 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
 at Quoter.QuotePropertyValues(SyntaxNode node)
 at Quoter.QuoteNode(SyntaxNode node, String name)
 at Quoter.QuotePropertyValue(SyntaxNode node, PropertyInfo property)
 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
 at Quoter.QuotePropertyValues(SyntaxNode node)
 at Quoter.QuoteNode(SyntaxNode node, String name)
 at Quoter.Quote(Object treeElement, String name)
 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
 at Quoter.QuoteList(IEnumerable syntaxList, String name)
 at Quoter.QuotePropertyValue(SyntaxNode node, PropertyInfo property)
 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
 at Quoter.QuotePropertyValues(SyntaxNode node)
 at Quoter.QuoteNode(SyntaxNode node, String name)
 at Quoter.Quote(Object treeElement, String name)
 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
 at Quoter.QuoteList(IEnumerable syntaxList, String name)
 at Quoter.QuotePropertyValue(SyntaxNode node, PropertyInfo property)
 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
 at Quoter.QuotePropertyValues(SyntaxNode node)
 at Quoter.QuoteNode(SyntaxNode node, String name)
 at Quoter.Quote(Object treeElement, String name)
 at Quoter.Quote(SyntaxNode node)
 at QuoterService.Controllers.QuoterController.Get(String sourceText, NodeKind nodeKind, Boolean openCurlyOnNewLine, Boolean closeCurlyOnNewLine, Boolean preserveOriginalWhitespace, Boolean keepRedundantApiCalls, Boolean avoidUsingStatic) in C:\RoslynQuoter\src\QuoterServiceOld\Controllers\QuoterController.cs:line 37
svick commented 6 years ago

Turns out the exception message was misleading: the issue and fix is specific to the default literal: https://github.com/KirillOsenkov/RoslynQuoter/pull/33.