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

Another cause for CS1677 (and CS1661) #43779

Open
RexJaeschke opened this issue Nov 26, 2024 · 0 comments
Open

Another cause for CS1677 (and CS1661) #43779

RexJaeschke opened this issue Nov 26, 2024 · 0 comments
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc errors-warnings/subsvc Pri2

Comments

@RexJaeschke
Copy link

RexJaeschke commented Nov 26, 2024

Type of issue

Missing information

Description

I'm working on the Ecma C# spec with lambda/anonymous function parameters [where I'm adding scoped], and my tests generated two CS errors, CS1677 and CS1661, from the same code. While I'll document the issue here, it likely will require tweaking the description for CS1661 as well.

Basically, the explanation and example you currently provide does not handle my situation.

Consider the following:

Action<int> lam3a = (in int i) => Mz2(in i);   // CS1677 on `i` in `in int i`, and CS1661 on `=>`
        var lam3b = (in int i) => Mz2(in i);   // no errors, and call below works fine.
int xx = 123;
lam3b(in xx);

public static void Mz2(in int v) { Console.WriteLine("In Mz2"); }

The CS1677 message I get is

Parameter 1 should not be declared with the 'in' keyword

The CS1661 message I get is

Cannot convert lambda expression to type 'Action<int>' because the parameter types do not match the delegate parameter types.

It appears that the explicit declaration type of lam3a is the problem, yet I don't see any way to change that (you can't include in in the type parameter), but when I let the compiler figure out the delegate type by using var, it works. This suggests that var can allow code that cannot be expressed directly using explicit types.

It seems there are multiple scenarios that can result in CS1661, as the message I got in the test above does not match the one in your Help entry, which states,

Cannot convert anonymous method block to delegate type 'delegate type' because the specified block's parameter types do not match the delegate parameter types

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1677?f1url=%3FappId%3Droslyn%26k%3Dk(CS1677)

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/misc/cs1677.md

Document Version Independent Id

9f888ae8-1183-8c38-f6d2-0dc075026016

Article author

@BillWagner

Metadata

  • ID: 802df120-d60d-3838-0ad6-e1437197950a
  • Service: dotnet-csharp
  • Sub-service: errors-warnings

Related Issues

@dotnetrepoman dotnetrepoman bot added ⌚ Not Triaged Not triaged labels Nov 26, 2024
@BillWagner BillWagner added the doc-enhancement Improve the current content [org][type][category] label Nov 27, 2024
@dotnetrepoman dotnetrepoman bot removed the ⌚ Not Triaged Not triaged label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc errors-warnings/subsvc Pri2
Projects
None yet
Development

No branches or pull requests

2 participants