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

Implicitly-typed lambda parameters have RefKind.None #65350

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private ImmutableArray<ParameterSymbol> MakeParameters(
else if (p < numDelegateParameters)
{
type = parameterTypes[p];
refKind = parameterRefKinds[p];
refKind = RefKind.None;
scope = DeclarationScope.Unscoped;
}
else
Expand Down
8 changes: 3 additions & 5 deletions src/Compilers/CSharp/Test/Semantic/Semantics/LambdaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6951,7 +6951,7 @@ public DisplayAttribute() { }
);
}

[Fact]
[Fact, WorkItem(64985, "https://github.com/dotnet/roslyn/issues/64985")]
public void DelegateConversions_ImplicitlyTypedParameter_RefParameter()
{
var source = """
Expand Down Expand Up @@ -6992,12 +6992,10 @@ static void M(D1 d1) { }
Assert.Equal("? r1", lambdaParameter1.ToTestDisplayString());
Assert.Equal(RefKind.None, lambdaParameter1.RefKind);

// Implicitly-typed lambda parameters can get a type, but they cannot get a different ref-kind (or scoped-ness) during anonymous function conversion
// Tracked by https://github.com/dotnet/roslyn/issues/64985
Assert.Equal("r2 => r2", lambdas[1].ToString());
var lambdaParameter2 = model.GetSymbolInfo(lambdas[1]).Symbol.GetParameters()[0];
Assert.Equal("ref R r2", lambdaParameter2.ToTestDisplayString());
Assert.Equal(RefKind.Ref, lambdaParameter2.RefKind);
Assert.Equal("R r2", lambdaParameter2.ToTestDisplayString());
Assert.Equal(RefKind.None, lambdaParameter2.RefKind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,61 @@ class C
CreateCompilation(source).VerifyDiagnostics(
// (4,17): error CS1988: Async methods cannot have ref, in or out parameters
jcouv marked this conversation as resolved.
Show resolved Hide resolved
// D d = async delegate { };
Diagnostic(ErrorCode.ERR_BadAsyncArgType, "delegate"),
jcouv marked this conversation as resolved.
Show resolved Hide resolved
Diagnostic(ErrorCode.ERR_BadAsyncArgType, "delegate").WithLocation(4, 17),
// (4,17): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
// D d = async delegate { };
Diagnostic(ErrorCode.WRN_AsyncLacksAwaits, "delegate").WithLocation(4, 17));
}

[Fact]
public void AnonymousMethodWithRefParameter()
{
var source =
@"delegate void D(ref int x);
class C
{
D d = delegate { };
}";
var comp = CreateCompilation(source).VerifyDiagnostics();

var syntaxTree = comp.SyntaxTrees.Single();
var root = syntaxTree.GetRoot();
var model = comp.GetSemanticModel(syntaxTree);

var anonymousMethod = root.DescendantNodes().OfType<AnonymousMethodExpressionSyntax>().Single();

Assert.Equal("delegate { }", anonymousMethod.ToString());
var parameter = model.GetSymbolInfo(anonymousMethod).Symbol.GetParameters()[0];
Assert.Equal("ref System.Int32 <p0>", parameter.ToTestDisplayString());
Assert.Equal(RefKind.Ref, parameter.RefKind);
}

[Fact]
public void AnonymousMethodWithOutParameter()
{
var source =
@"delegate void D(out int x);
class C
{
D d = delegate { };
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (4,11): error CS1688: Cannot convert anonymous method block without a parameter list to delegate type 'D' because it has one or more out parameters
// D d = delegate { };
Diagnostic(ErrorCode.ERR_CantConvAnonMethNoParams, "delegate { }").WithArguments("D").WithLocation(4, 11)
);

var syntaxTree = comp.SyntaxTrees.Single();
var root = syntaxTree.GetRoot();
var model = comp.GetSemanticModel(syntaxTree);

var anonymousMethod = root.DescendantNodes().OfType<AnonymousMethodExpressionSyntax>().Single();

Assert.Equal("delegate { }", anonymousMethod.ToString());
Assert.Empty(model.GetSymbolInfo(anonymousMethod).Symbol.GetParameters());
}

[Fact]
public void RefReturningDelegate()
{
Expand Down