Skip to content

[CodePlex] Bug in SemanticModel.GetAwaitExpressionInfo and expression lambdas #323

@gafter

Description

@gafter

@erik-kallen reported: in http://roslyn.codeplex.com/workitem/400

The following program will output FAILURE, even though the program is valid and no diagnostics messages are reported (using 1.0-beta). It will also fail if the type of f is changed to Action. However, if the type of f is changed to Func<Task<int>>, there is no problem.

using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace RoslynTest {
    class Program {
        static void Main(string[] args) {
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
using System.Threading.Tasks;
public class C {
    public C() {
        Task<int> task = null;
        Func<Task> f = async() => await task;
    }
}");
            var compilation = CSharpCompilation.Create("Test", new[] { syntaxTree }, new[] { MetadataReference.CreateFromAssembly(typeof(object).Assembly), MetadataReference.CreateFromAssembly(typeof(Enumerable).Assembly) }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
            foreach (var d in compilation.GetDiagnostics())
                Console.WriteLine(d.GetMessage());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var root = syntaxTree.GetRoot();
            var expr = root.DescendantNodes().OfType<ConstructorDeclarationSyntax>().First().Body.Statements.OfType<LocalDeclarationStatementSyntax>().ElementAt(1).Declaration.Variables[0].Initializer.Value;
            var awaitExpr = expr.DescendantNodes().OfType<AwaitExpressionSyntax>().Single();
            var awaitInfo = semanticModel.GetAwaitExpressionInfo(awaitExpr);
            if (awaitInfo.GetAwaiterMethod != null)
                Console.WriteLine("Everything was OK");
            else
                Console.WriteLine("FAILURE: GetAwaitExpressionInfo returned missing data even though the program is valid");
        }
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions