Flow analysis
还没有人认领这个 Issue。
评估
调研方向
首先定位 issue 中描述的当前 Roslyn 流分析集成,并跟踪 GetNode 示例如何生成图边。当流状态辅助节点能够跨分支跟踪局部变量的可空性,并且返回值取决于推断出的非空映射类型而不是声明的 nullable 类型时,这项工作就完成了。
由索引模型根据 Issue 内容生成。
描述
Currently our inference re-uses Roslyn's flow-analysis.
However, this has some fundamental problems.
9: Dictionary<T, Node> mapping = new Dictionary<T, Node>();
11: Node? GetNode(T element)
{
13: Node? node;
14: if (!mapping.TryGetValue(element, out node))
15: {
16: node = new Node();
17: mapping.Add(element, node);
18: }
19: return node;
}

There's no edges created for line 16/17 because here Roslyn knows that node is non-null.
Line 14 creates two edges: one from <nullable> because TryGetValue will assign null when returning false; the other from mapping!1#2 (the mapping field's Node type argument) when TryGetValue returns true.
The return statement creates an edge from the variable's type, because Roslyn's flow analysis can't guarantee us that the variable is non-null -- our Roslyn code analysis runs under the pessimistic assumption that all types-to-be-inferred might end up nullable, so it considers mapping to be Dictionary<T, Node?>, which leaves open the possibility that GetNode returns null.
However, after our inference decides that mapping!1#2 is non-null, it would be correct to also indicate that the GetNode return value is non-null. After all, if no node exists yet, the function will create one.
The issue here is that Roslyn's flow analysis isn't aware of our types-to-be-inferred.
It would be better if, instead of using Roslyn's flow analysis, we had our own that keeps track of node's nullability.
The idea would be to create additional "helper" graph nodes for the different flow states of a local variable of reference type.
After TryGetValue initializes node, it's flow-state would be (true: mapping!1#2, false: <nullable>). Within the if body, the flow-state would initially be <nullable>, but after line 16 would change to <nonnull>.
After the if, the flow-state from both alternatives could be re-combined by creating a new node "node-after-line-18" and edges from the nodes from the two if-branches -- in this case <nonnull> from the then-branch and mapping!1#2 from the else branch.
Then the return statement would create an edge from this "node-after-line-18" instead of the node for the variable's declared type.
All flow-state nodes associated with a variable would have an edge to the variable's declared type node.
We'd end up with a graph somewhat like this:

Thus in the end, node would be inferred as nullable, but the GetNode return type would only depend on mapping!1#2 and thus can be inferred depending on whether there's a mapping.Add(x, null) access somewhere else in the program.
- 主要语言
- C#
- 星标
- 38
- 派生
- 6
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
icsharpcode/NullabilityInference 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 35/100
icsharpcode/NullabilityInference#13 · 1 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 42/100
-
难度 5/5 一周以上 新手友好度 15/100
-
难度 5/5 一周以上 新手友好度 35/100
-
难度 5/5 一周以上 新手友好度 25/100
icsharpcode/NullabilityInference#6 · 2 条评论 ·
查看 icsharpcode/NullabilityInference 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
umbraco/Umbraco.Automate#612 ·
维护者通常 1 天内回复
-
崩溃日志只按 CRLF 分行未关闭
难度 2/5 1-3 小时 新手友好度 72/100
PCL-Community/PCL-CE#3872 ·
维护者通常 1 天内回复
-
type:task ux
难度 2/5 1-3 小时 新手友好度 72/100
elsa-workflows/elsa-foundation#5253 ·
维护者通常 1 天内回复