Lambda parameter 'params' and default values are dropped below C# 12

未关闭
#4,058 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
57/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
csharp

调研方向

首先检查已关闭的 #4004 分支中的 "Downgrade params and default values to attributes on pre-C# 12 lambdas" 提交及其 Ugly 测试,然后使用 CSharp11_0 验证示例。完成意味着在低于 C# 12 时默认值会生成 Optional/DefaultParameterValue 属性,params 仍被删除,并且有一个测试可防止无效的 ParamArray 形式。

由索引模型根据 Issue 内容生成。

描述

Decompiler

Split out of #4057, which fixes the case where params and parameter default values were printed
on syntax that cannot carry them. Below C# 12 they are now dropped instead, and this issue tracks
that remaining loss.

What happens

public delegate int ParamsDel(params int[] values);
public delegate int OptDel(int x = 42);

public static ParamsDel MakeParams(int seed) => (params int[] values) => values.Length + seed;
public static OptDel MakeOpt(int seed) => (int x = 42) => x + seed;

Decompiled with --languageversion CSharp11_0:

public static ParamsDel MakeParams(int seed)
{
    return (int[] values) => values.Length + seed;
}

public static OptDel MakeOpt(int seed)
{
    return (int x) => x + seed;
}

Both modifiers are gone. The output still compiles and still binds the way the input did - the
delegate type declares params and the default, and that is what call sites bind against - so
this is a fidelity loss, not a correctness bug. A reader of the C# 11 output cannot tell the
parameter was params or had a default.

The obvious fix only works for one of the two

C# 10 allows attributes on lambda parameters, so the natural repair is to downgrade the modifiers
to the attributes the compiler would have emitted anyway.

That works for the default value. Verified compiling at LangVersion 11:

return ([Optional][DefaultParameterValue(5)] int x) => x * 2;

It does not work for params:

return ([ParamArray] int[] xs) => xs.Length;
// error CS0674: Do not use 'System.ParamArrayAttribute'/
// 'System.Runtime.CompilerServices.ParamCollectionAttribute'. Use the 'params' keyword instead.

CS0674 rejects [ParamArray] on any parameter, and anonymous methods cannot declare params
either (CS1670), so below C# 12 there is no legal way to spell it on the anonymous function at
all. Dropping it may simply be the correct answer for that half.

Worth knowing: the closed #4004 branch (natural-type-lambdas-methods) carries a commit
"Downgrade params and default values to attributes on pre-C# 12 lambdas" which emits exactly the
[ParamArray] form above, with an Ugly test pinning it. That output does not compile, so the
commit should not be lifted as-is if this is picked up.

Suggested scope

  • Emit [Optional]/[DefaultParameterValue] for a default value below C# 12.
  • Leave params dropped, and pin that with a test so the CS0674 form cannot come back.

Written by an AI agent (Claude) on Siegfried's behalf.

主要语言
C#
星标
27.1k
派生
3.8k
平均合并
1 天 23 小时
30 天内合并 PR
44

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

icsharpcode/ILSpy 的其他 Issue

查看 icsharpcode/ILSpy 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。