Skip to content

Type inference fails for Func<Expression<Func<T>>> #57517

@CameronAavik

Description

@CameronAavik

Version Used:

dotnet: 6.0.100-rc.2.21505.57

Steps to Reproduce:

Compile the following code (sharplab link):

using System;
using System.Linq.Expressions;

void Test1<T>(Func<T> exp) {}
void Test2<T>(Expression<Func<T>> exp) {}
void Test3<T>(Func<Func<T>> exp) {}
void Test4<T>(Func<Expression<Func<T>>> exp) {}

Test1(() => 2); // works
Test2(() => 2); // works
Test3(() => () => 2); // works
Test4(() => () => 2); // error: CS0411

Expected Behavior:

The T generic type parameter is correctly inferred for all 4 cases.

Actual Behavior:

In Test4, the T is unable to be inferred, producing the following error:

error CS0411: The type arguments for method 'Test4(Func<Expression<Func>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Given that type inference works for Func<T>, Expression<Func<T>>, and Func<Func<T>>, I thought it might be a bug that Func<Expression<Func<T>>> doesn't work.

EDIT: As of .NET 6.0.101, the behaviour has changed and now inference fails also for Func<Func<T>>, the reason this behaviour was reverted is explained in #57713 (comment). An issue #57779 was created to consider addressing this scenario.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions