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

Infer delegate types with -langversion:10 (and above) only #55232

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ private BoundExpression CreateAnonymousFunctionConversion(SyntaxNode syntax, Bou
// UNDONE: is converted to a delegate that does not match. What to surface then?

var unboundLambda = (UnboundLambda)source;
if (destination.SpecialType == SpecialType.System_Delegate || destination.IsNonGenericExpressionType())
if ((destination.SpecialType == SpecialType.System_Delegate || destination.IsNonGenericExpressionType()) &&
syntax.IsFeatureEnabled(MessageID.IDS_FeatureInferredDelegateType))
{
CheckFeatureAvailability(syntax, MessageID.IDS_FeatureInferredDelegateType, diagnostics);
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics);
var delegateType = unboundLambda.InferDelegateType(ref useSiteInfo);
BoundLambda boundLambda;
Expand Down Expand Up @@ -628,9 +628,9 @@ private BoundExpression CreateMethodGroupConversion(SyntaxNode syntax, BoundExpr
hasErrors = true;
}

if (destination.SpecialType == SpecialType.System_Delegate)
if (destination.SpecialType == SpecialType.System_Delegate &&
syntax.IsFeatureEnabled(MessageID.IDS_FeatureInferredDelegateType))
{
CheckFeatureAvailability(syntax, MessageID.IDS_FeatureInferredDelegateType, diagnostics);
// https://github.com/dotnet/roslyn/issues/52869: Avoid calculating the delegate type multiple times during conversion.
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics);
var delegateType = GetMethodGroupDelegateType(group, ref useSiteInfo);
Expand Down
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,8 @@ void reportMethodGroupErrors(BoundMethodGroup methodGroup, bool fromAddressOf)
{
errorCode = ErrorCode.ERR_AddressOfToNonFunctionPointer;
}
else if (targetType.SpecialType == SpecialType.System_Delegate)
else if (targetType.SpecialType == SpecialType.System_Delegate &&
syntax.IsFeatureEnabled(MessageID.IDS_FeatureInferredDelegateType))
{
Error(diagnostics, ErrorCode.ERR_CannotInferDelegateType, location);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override ConversionsBase WithNullabilityCore(bool includeNullability)
public override Conversion GetMethodGroupDelegateConversion(BoundMethodGroup source, TypeSymbol destination, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
// Must be a bona fide delegate type, not an expression tree type.
if (!(destination.IsDelegateType() || destination.SpecialType == SpecialType.System_Delegate))
if (!(destination.IsDelegateType() || (destination.SpecialType == SpecialType.System_Delegate && IsFeatureInferredDelegateTypeEnabled(source))))
{
return Conversion.NoConversion;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ private static MethodGroupResolution ResolveDelegateOrFunctionPointerMethodGroup
return (signature, true, new CallingConventionInfo(signature.CallingConvention, signature.GetCallingConventionModifiers()));
}

var delegateType = (type.SpecialType == SpecialType.System_Delegate) ?
var delegateType = (type.SpecialType == SpecialType.System_Delegate) && methodGroup.Syntax.IsFeatureEnabled(MessageID.IDS_FeatureNullableReferenceTypes) ?
// https://github.com/dotnet/roslyn/issues/52869: Avoid calculating the delegate type multiple times during conversion.
_binder.GetMethodGroupDelegateType(methodGroup, ref useSiteInfo) :
type.GetDelegateType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1407,20 +1407,31 @@ public static LambdaConversionResult IsAnonymousFunctionCompatibleWithType(Unbou

if (type.SpecialType == SpecialType.System_Delegate)
{
return LambdaConversionResult.Success;
if (IsFeatureInferredDelegateTypeEnabled(anonymousFunction))
{
return LambdaConversionResult.Success;
}
}
else if (type.IsDelegateType())
{
return IsAnonymousFunctionCompatibleWithDelegate(anonymousFunction, type, isTargetExpressionTree: false);
}
else if (type.IsGenericOrNonGenericExpressionType(out bool _))
else if (type.IsGenericOrNonGenericExpressionType(out bool isGenericType))
{
return IsAnonymousFunctionCompatibleWithExpressionTree(anonymousFunction, (NamedTypeSymbol)type);
if (isGenericType || IsFeatureInferredDelegateTypeEnabled(anonymousFunction))
{
return IsAnonymousFunctionCompatibleWithExpressionTree(anonymousFunction, (NamedTypeSymbol)type);
}
}

return LambdaConversionResult.BadTargetType;
}

internal static bool IsFeatureInferredDelegateTypeEnabled(BoundExpression expr)
{
return expr.Syntax.IsFeatureEnabled(MessageID.IDS_FeatureInferredDelegateType);
}

private static bool HasAnonymousFunctionConversion(BoundExpression source, TypeSymbol destination)
{
Debug.Assert(source != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Linq;

namespace Microsoft.CodeAnalysis.CSharp
Expand All @@ -12,7 +10,12 @@ internal static class CSharpCompilationExtensions
{
internal static bool IsFeatureEnabled(this CSharpCompilation compilation, MessageID feature)
{
return ((CSharpParseOptions)compilation.SyntaxTrees.FirstOrDefault()?.Options)?.IsFeatureEnabled(feature) == true;
return ((CSharpParseOptions?)compilation.SyntaxTrees.FirstOrDefault()?.Options)?.IsFeatureEnabled(feature) == true;
}

internal static bool IsFeatureEnabled(this SyntaxNode? syntax, MessageID feature)
{
return ((CSharpParseOptions?)syntax?.SyntaxTree.Options)?.IsFeatureEnabled(feature) == true;
}
}
}
60 changes: 27 additions & 33 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenTupleEqualityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,12 +1358,9 @@ static void Main()
// (6,30): error CS0034: Operator '==' is ambiguous on operands of type '<null>' and 'default'
// System.Console.Write((null, () => 1) == (default, default));
Diagnostic(ErrorCode.ERR_AmbigBinaryOps, "(null, () => 1) == (default, default)").WithArguments("==", "<null>", "default").WithLocation(6, 30),
// (6,37): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
// (6,30): error CS0019: Operator '==' cannot be applied to operands of type 'lambda expression' and 'default'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if later on (i.e. not in this PR/release) it will be practical to devise some way to still give LangVersion diagnostics. For example, by checking again at the point we give a conversion error if a valid conversion "would have" existed in the later language version.

// System.Console.Write((null, () => 1) == (default, default));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "() => 1").WithArguments("inferred delegate type", "10.0").WithLocation(6, 37),
// (6,37): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
// System.Console.Write((null, () => 1) == (default, default));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "() => 1").WithArguments("inferred delegate type", "10.0").WithLocation(6, 37),
Diagnostic(ErrorCode.ERR_BadBinaryOps, "(null, () => 1) == (default, default)").WithArguments("==", "lambda expression", "default").WithLocation(6, 30),
// (7,30): error CS0034: Operator '==' is ambiguous on operands of type '(<null>, lambda expression)' and 'default'
// System.Console.Write((null, () => 2) == default);
Diagnostic(ErrorCode.ERR_AmbigBinaryOps, "(null, () => 2) == default").WithArguments("==", "(<null>, lambda expression)", "default").WithLocation(7, 30)
Expand Down Expand Up @@ -1672,31 +1669,16 @@ static void Main()
}";
var comp = CreateCompilation(source, parseOptions: TestOptions.Regular9);
comp.VerifyDiagnostics(
// (6,65): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "x => x").WithArguments("inferred delegate type", "10.0").WithLocation(6, 65),
// (6,65): error CS8917: The delegate type could not be inferred.
// (6,30): error CS0019: Operator '==' cannot be applied to operands of type '<null>' and 'lambda expression'
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_CannotInferDelegateType, "x => x").WithLocation(6, 65),
// (6,65): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
Diagnostic(ErrorCode.ERR_BadBinaryOps, "(null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; })").WithArguments("==", "<null>", "lambda expression").WithLocation(6, 30),
// (6,30): error CS0019: Operator '==' cannot be applied to operands of type '<null>' and 'method group'
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "x => x").WithArguments("inferred delegate type", "10.0").WithLocation(6, 65),
// (6,65): error CS8917: The delegate type could not be inferred.
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_CannotInferDelegateType, "x => x").WithLocation(6, 65),
// (6,73): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
Diagnostic(ErrorCode.ERR_BadBinaryOps, "(null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; })").WithArguments("==", "<null>", "method group").WithLocation(6, 30),
// (6,30): error CS0019: Operator '==' cannot be applied to operands of type '<null>' and 'lambda expression'
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "Main").WithArguments("inferred delegate type", "10.0").WithLocation(6, 73),
// (6,73): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "Main").WithArguments("inferred delegate type", "10.0").WithLocation(6, 73),
// (6,79): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "(int i) => { int j = 0; return i + j; }").WithArguments("inferred delegate type", "10.0").WithLocation(6, 79),
// (6,79): error CS8773: Feature 'inferred delegate type' is not available in C# 9.0. Please use language version 10.0 or greater.
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion9, "(int i) => { int j = 0; return i + j; }").WithArguments("inferred delegate type", "10.0").WithLocation(6, 79));
verify(comp);
Diagnostic(ErrorCode.ERR_BadBinaryOps, "(null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; })").WithArguments("==", "<null>", "lambda expression").WithLocation(6, 30));
verify(comp, inferDelegate: false);

comp = CreateCompilation(source, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
Expand All @@ -1706,9 +1688,9 @@ static void Main()
// (6,65): error CS8917: The delegate type could not be inferred.
// System.Console.Write((null, null, null, null) == (null, x => x, Main, (int i) => { int j = 0; return i + j; }));
Diagnostic(ErrorCode.ERR_CannotInferDelegateType, "x => x").WithLocation(6, 65));
verify(comp);
verify(comp, inferDelegate: true);

static void verify(CSharpCompilation comp)
static void verify(CSharpCompilation comp, bool inferDelegate)
{
var tree = comp.SyntaxTrees[0];
var model = comp.GetSemanticModel(tree);
Expand All @@ -1732,19 +1714,19 @@ static void verify(CSharpCompilation comp)
// ... its first lambda ...
var firstLambda = tuple2.Arguments[1].Expression;
Assert.Null(model.GetTypeInfo(firstLambda).Type);
Assert.Equal("System.Delegate", model.GetTypeInfo(firstLambda).ConvertedType.ToTestDisplayString());
verifyType("System.Delegate", model.GetTypeInfo(firstLambda).ConvertedType, inferDelegate);

// ... its method group ...
var methodGroup = tuple2.Arguments[2].Expression;
Assert.Null(model.GetTypeInfo(methodGroup).Type);
Assert.Equal("System.Delegate", model.GetTypeInfo(methodGroup).ConvertedType.ToTestDisplayString());
verifyType("System.Delegate", model.GetTypeInfo(methodGroup).ConvertedType, inferDelegate);
Assert.Null(model.GetSymbolInfo(methodGroup).Symbol);
Assert.Equal(new[] { "void C.Main()" }, model.GetSymbolInfo(methodGroup).CandidateSymbols.Select(s => s.ToTestDisplayString()));

// ... its second lambda and the symbols it uses
var secondLambda = tuple2.Arguments[3].Expression;
Assert.Equal("System.Func<System.Int32, System.Int32>", model.GetTypeInfo(secondLambda).Type.ToTestDisplayString());
Assert.Equal("System.Delegate", model.GetTypeInfo(secondLambda).ConvertedType.ToTestDisplayString());
verifyType("System.Func<System.Int32, System.Int32>", model.GetTypeInfo(secondLambda).Type, inferDelegate);
verifyType("System.Delegate", model.GetTypeInfo(secondLambda).ConvertedType, inferDelegate);

var addition = tree.GetCompilationUnitRoot().DescendantNodes().OfType<BinaryExpressionSyntax>().Last();
Assert.Equal("i + j", addition.ToString());
Expand All @@ -1755,6 +1737,18 @@ static void verify(CSharpCompilation comp)
var j = addition.Right;
Assert.Equal("System.Int32 j", model.GetSymbolInfo(j).Symbol.ToTestDisplayString());
}

static void verifyType(string expectedType, ITypeSymbol type, bool inferDelegate)
{
if (inferDelegate)
{
Assert.Equal(expectedType, type.ToTestDisplayString());
}
else
{
Assert.Null(type);
}
}
}

[Fact]
Expand Down
Loading