Nullable annotations shift after an anonymous type: NullableAttribute flags skip the anonymous type's type arguments
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 74/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- csharp
调研方向
从 ApplyAttributeTypeVisitor.cs 开始,重点查看第 240 行附近的 VisitParameterizedType 和 ExpectDummyNullabilityForGenericValueType。针对 AnonNullableRepro.dll 运行随附的 driver,并检查缓存 lambda 字段和 lambda 方法输出。当匿名类型参数不再导致后续的 NullableAttribute 标志发生偏移,并且已检查相关的 DynamicAttribute 和 TupleElementNamesAttribute 行为时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Whenever a signature contains an anonymous type, every nullable annotation after it lands on the wrong type argument. NullableAttribute's flag array has no entry for an anonymous type's type arguments, but ApplyAttributeTypeVisitor walks them and consumes a flag for each, so the rest of the type is read from the flag stream at the wrong index.
This is a Release-mode output bug, not just the Debug.Assert mentioned at the end.
Repro
Repro.cs, compiled as a net9.0 library with <Nullable>enable</Nullable> (Release):
using System;
using System.Collections.Generic;
using System.Linq;
public class Repro
{
public void M(IEnumerable<string> xs)
{
xs.Select(x => new { A = x, B = x }).Pick(a => new List<string?>());
}
}
public static class Ext
{
public static IEnumerable<T> Pick<TSource, T>(this IEnumerable<TSource> source,
Func<TSource, List<T?>> selector) where T : class => throw new NotSupportedException();
}
csc caches the second lambda in
.field public static class [System.Runtime]System.Func`2<class '<>f__AnonymousType0`2'<string,string>,
class [System.Collections]System.Collections.Generic.List`1<string>> '<>9__0_1'
.custom instance void ...NullableAttribute::.ctor(uint8[]) = ( 01 00 04 00 00 00 00 00 01 02 00 00 )
That is 4 flags for 4 positions: Func = 0, <>f__AnonymousType0<string,string> = 0, List = 1, string = 2. The anonymous type's two string arguments get no flag at all, so the real type is Func<<>f__AnonymousType0<string, string>, List<string?>>.
Actual
Decompiling Repro.<>c (select it in the tree, or use the driver below):
public static Func<<>f__AnonymousType0<string, string?>, List<string>> <>9__0_1;
internal List<string?> <M>b__0_1(<>f__AnonymousType0<string, string> a)
{
return new List<string>();
}
The ? moved off List's element onto the anonymous type's second property. The same two types are printed correctly on the lambda method right below it, whose parameter and return type each carry their own attribute.
Expected
public static Func<<>f__AnonymousType0<string, string>, List<string?>> <>9__0_1;
Driver (no GUI needed)
#:package [email protected]
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
var path = args[0];
var resolver = new UniversalAssemblyResolver(path, throwOnError: false, targetFramework: null);
var decompiler = new CSharpDecompiler(path, resolver, new DecompilerSettings());
var c = decompiler.TypeSystem.MainModule.TypeDefinitions.Single(t => t.Name == "<>c");
Console.WriteLine(decompiler.DecompileAsString(c.MetadataToken));
dotnet run driver.cs -- AnonNullableRepro.dll. Reproduces on the released 9.1.0.7988 and on master (3f59de268).
Cause
ApplyAttributeTypeVisitor.VisitParameterizedType visits the type arguments of every parameterized type, and each reference-typed argument consumes one nullability flag. Roslyn treats an anonymous type as a single opaque position in the flag stream: in its symbol model the anonymous type is not generic, the type arguments are an emit-time artifact of the template, so no flags are written for them (their nullability is not representable in metadata). Every position after the anonymous type is therefore read too late; once the array is exhausted the remainder falls back to the enclosing NullableContext.
Where it showed up
Found by fuzzing nuget packages through the decompiler. Real-world hits, all on cached-lambda fields:
- linq2db 6.4.0,
ConvertBuilder+<>c__DisplayClass17_0:Func<<>f__AnonymousType5<MapValue, MapValueAttribute>, IEnumerable<ValueTuple<string, int>>>withNullable([0,0,1,0,0]) - brightsword.squid 2.0.0,
BasicDataTransferObjectTypeCreator<T>+<>c::<>9__127_3:Func<<>f__AnonymousType6<KeyValuePair<Type, IBehaviour>, Type>, IEnumerable<Func<TypeBuilder, TypeBuilder>>>withNullable([0,0,1,0,0,0])
In both, the byte count matches only if the anonymous type's arguments are skipped, and the shifted stream lands on a generic value type, which additionally trips Debug.Assert in ApplyAttributeTypeVisitor.ExpectDummyNullabilityForGenericValueType (ApplyAttributeTypeVisitor.cs:240).
Worth checking whether DynamicAttribute and TupleElementNamesAttribute use the same skip before changing the shared walk.
Written by an AI agent (Claude) on Siegfried's behalf.
- 主要语言
- C#
- 星标
- 27.1k
- 派生
- 3.8k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 44
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
icsharpcode/ILSpy 的其他 Issue
-
External UI
难度 1/5 1-3 小时 新手友好度 75/100
icsharpcode/ILSpy#4284 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 55/100
icsharpcode/ILSpy#4357 ·
维护者通常 1 天内回复
-
Misdecompilation of a lazy initialization pattern可能已有人在做 @siegfriedpammer 于 5 天前认领。 未关闭Bug Decompiler
难度 4/5 3-5 天 新手友好度 20/100
icsharpcode/ILSpy#4349 ·
维护者通常 1 天内回复
-
Bug
难度 4/5 3-5 天 新手友好度 45/100
icsharpcode/ILSpy#4343 ·
维护者通常 1 天内回复
-
AI
难度 4/5 1-2 天 新手友好度 55/100
icsharpcode/ILSpy#4300 ·
维护者通常 1 天内回复
查看 icsharpcode/ILSpy 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 3 天内回复
-
bug python
难度 1/5 1 小时以内 新手友好度 88/100
ilia-sokolov/OfficeAgent.NET#89 ·
维护者通常 13 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 82/100
juandresrodca/AdminForge#66 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
umbraco/Umbraco.Automate#612 ·
维护者通常 1 天内回复