|
| 1 | +// P-014 bounded design — D2 isolation falsifiers (research only, not product code). |
| 2 | +// Builds tiny in-memory "projects" and compares ONE merged CSharpCompilation (today's extractor shape) |
| 3 | +// with ONE compilation PER project (project references as CompilationReference). Prints, per probe site, |
| 4 | +// the bound symbol, its containing assembly, or the candidate reason. Same Roslyn as the extractor (4.9.2). |
| 5 | +// Framework references: the Microsoft.NETCore.App.Ref 8.0.31 reference pack passed as args[0]. |
| 6 | +using System.Collections.Immutable; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | + |
| 11 | +var packDir = args[0]; |
| 12 | +var fw = Directory.EnumerateFiles(packDir, "*.dll").OrderBy(p => p, StringComparer.Ordinal) |
| 13 | + .Select(p => (MetadataReference)MetadataReference.CreateFromFile(p)).ToList(); |
| 14 | +var opts = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); |
| 15 | + |
| 16 | +SyntaxTree T(string proj, string file, string code) => |
| 17 | + CSharpSyntaxTree.ParseText(code, path: $"{proj}/{file}"); |
| 18 | + |
| 19 | +// A metadata-only "package" assembly with an explicit identity, so two versions share a simple name. |
| 20 | +MetadataReference Lib(string name, string version, string body) |
| 21 | +{ |
| 22 | + var c = CSharpCompilation.Create(name, |
| 23 | + new[] { CSharpSyntaxTree.ParseText($"[assembly: System.Reflection.AssemblyVersion(\"{version}\")]\n{body}") }, |
| 24 | + fw, opts); |
| 25 | + using var ms = new MemoryStream(); |
| 26 | + var r = c.Emit(ms); |
| 27 | + if (!r.Success) throw new Exception(string.Join("\n", r.Diagnostics)); |
| 28 | + return MetadataReference.CreateFromImage(ms.ToArray()); |
| 29 | +} |
| 30 | + |
| 31 | +// Probe sites are invocations / member accesses / identifier names carrying a `/*probe:ID*/` comment on the same line. |
| 32 | +void Report(string label, Compilation comp) |
| 33 | +{ |
| 34 | + var errs = comp.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error) |
| 35 | + .GroupBy(d => d.Id).OrderBy(g => g.Key).Select(g => $"{g.Key}x{g.Count()}"); |
| 36 | + Console.WriteLine($" [{label}] errors: {string.Join(" ", errs)}"); |
| 37 | + foreach (var tree in comp.SyntaxTrees) |
| 38 | + { |
| 39 | + var model = comp.GetSemanticModel(tree); |
| 40 | + var text = tree.GetText(); |
| 41 | + foreach (var line in text.Lines) |
| 42 | + { |
| 43 | + var s = line.ToString(); |
| 44 | + var i = s.IndexOf("/*probe:", StringComparison.Ordinal); |
| 45 | + if (i < 0) continue; |
| 46 | + var id = s.Substring(i + 8, s.IndexOf("*/", i, StringComparison.Ordinal) - i - 8); |
| 47 | + var node = tree.GetRoot().DescendantNodes(line.Span) |
| 48 | + .Where(n => line.Span.Contains(n.Span)) |
| 49 | + .OrderBy(n => n is AssignmentExpressionSyntax ? 0 : n is InvocationExpressionSyntax ? 1 : 2) |
| 50 | + .FirstOrDefault(n => n is InvocationExpressionSyntax or AssignmentExpressionSyntax or ObjectCreationExpressionSyntax); |
| 51 | + if (node is AssignmentExpressionSyntax a) node = a.Left; |
| 52 | + if (node is null) { Console.WriteLine($" {id}: no node"); continue; } |
| 53 | + var si = model.GetSymbolInfo(node); |
| 54 | + string what = si.Symbol is { } sym |
| 55 | + ? $"{sym.Kind} {sym.ToDisplayString()} @ {sym.ContainingAssembly?.Identity.ToString() ?? "-"}" |
| 56 | + : si.CandidateSymbols.Length > 0 |
| 57 | + ? $"null, {si.CandidateReason}: " + string.Join(" | ", si.CandidateSymbols.Select(c => $"{c.ToDisplayString()} @ {c.ContainingAssembly?.Name}")) |
| 58 | + : "UNRESOLVED"; |
| 59 | + var siteDiags = comp.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error |
| 60 | + && d.Location.SourceTree == tree && line.Span.IntersectsWith(d.Location.SourceSpan)).Select(d => d.Id).Distinct(); |
| 61 | + Console.WriteLine($" {tree.FilePath} {id}: {what} site-errors=[{string.Join(",", siteDiags)}]"); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +CSharpCompilation Merged(IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> refs) => |
| 67 | + CSharpCompilation.Create("own", trees, fw.Concat(refs), opts); |
| 68 | +CSharpCompilation Proj(string name, IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> refs) => |
| 69 | + CSharpCompilation.Create(name, trees, fw.Concat(refs), opts); |
| 70 | + |
| 71 | +// SDK default global usings for Microsoft.NET.Sdk (C# 10 ImplicitUsings=enable). |
| 72 | +const string ImplicitUsings = """ |
| 73 | +global using global::System; |
| 74 | +global using global::System.Collections.Generic; |
| 75 | +global using global::System.IO; |
| 76 | +global using global::System.Linq; |
| 77 | +global using global::System.Net.Http; |
| 78 | +global using global::System.Threading; |
| 79 | +global using global::System.Threading.Tasks; |
| 80 | +"""; |
| 81 | + |
| 82 | +// ---------- F2.1 two projects declare Dup.T ---------- |
| 83 | +Console.WriteLine("F2.1 Dup.T declared in two projects"); |
| 84 | +{ |
| 85 | + var p1 = new[] { T("P1", "a.cs", "namespace Dup { public class T { public int One() => 1; } }"), |
| 86 | + T("P1", "use1.cs", "class U1 { int M() => new Dup.T().One(); /*probe:p1-One*/ }") }; |
| 87 | + var p2 = new[] { T("P2", "b.cs", "namespace Dup { public class T { public int Two() => 2; } }"), |
| 88 | + T("P2", "use2.cs", @"class U2 { |
| 89 | + int M() => new Dup.T().Two(); /*probe:p2-Two*/ |
| 90 | + int N() => new Dup.T().One(); /*probe:p2-One-must-fail*/ |
| 91 | +}") }; |
| 92 | + var p3 = new[] { T("P3", "use3.cs", "class U3 { object M() => new Dup.T(); /*probe:p3-refs-both*/ }") }; |
| 93 | + Report("merged", Merged(p1.Concat(p2).Concat(p3), Array.Empty<MetadataReference>())); |
| 94 | + var c1 = Proj("P1", p1, Array.Empty<MetadataReference>()); |
| 95 | + var c2 = Proj("P2", p2, Array.Empty<MetadataReference>()); |
| 96 | + Report("per-project P1", c1); |
| 97 | + Report("per-project P2", c2); |
| 98 | + Report("per-project P3 -> P1,P2", Proj("P3", p3, new[] { c1.ToMetadataReference(), c2.ToMetadataReference() })); |
| 99 | +} |
| 100 | + |
| 101 | +// ---------- F2.2 same simple assembly name, two versions ---------- |
| 102 | +Console.WriteLine("F2.2 Lib 1.0 in P1, Lib 2.0 in P2"); |
| 103 | +{ |
| 104 | + var v1 = Lib("Lib", "1.0.0.0", "namespace L { public static class C { public static int V1Only() => 1; } }"); |
| 105 | + var v2 = Lib("Lib", "2.0.0.0", "namespace L { public static class C { public static int V2Only() => 2; } }"); |
| 106 | + var p1 = new[] { T("P1", "a.cs", "class A { int M() => L.C.V1Only(); /*probe:p1-V1Only*/ }") }; |
| 107 | + var p2 = new[] { T("P2", "b.cs", "class B { int M() => L.C.V2Only(); /*probe:p2-V2Only*/ }") }; |
| 108 | + Report("merged, first-name-wins (v1 only)", Merged(p1.Concat(p2), new[] { v1 })); |
| 109 | + Report("merged, both versions", Merged(p1.Concat(p2), new[] { v1, v2 })); |
| 110 | + Report("per-project P1 (v1)", Proj("P1", p1, new[] { v1 })); |
| 111 | + Report("per-project P2 (v2)", Proj("P2", p2, new[] { v2 })); |
| 112 | +} |
| 113 | + |
| 114 | +// ---------- F2.3 ImplicitUsings enable in P1, disable in P2 ---------- |
| 115 | +Console.WriteLine("F2.3 ImplicitUsings: P1 enable, P2 disable"); |
| 116 | +{ |
| 117 | + var p1 = new[] { T("P1", "GlobalUsings.g.cs", ImplicitUsings), |
| 118 | + T("P1", "a.cs", "class A { bool M() => File.Exists(\"x\"); /*probe:p1-File*/ }") }; |
| 119 | + var p2 = new[] { T("P2", "b.cs", "class B { bool M() => File.Exists(\"x\"); /*probe:p2-File-must-fail*/ }") }; |
| 120 | + Report("merged", Merged(p1.Concat(p2), Array.Empty<MetadataReference>())); |
| 121 | + Report("per-project P1", Proj("P1", p1, Array.Empty<MetadataReference>())); |
| 122 | + Report("per-project P2", Proj("P2", p2, Array.Empty<MetadataReference>())); |
| 123 | +} |
| 124 | + |
| 125 | +// ---------- F2.4 Timer: System.Timers vs System.Threading ---------- |
| 126 | +Console.WriteLine("F2.4 Timer: P1 implicit usings (System.Threading), P2 `using System.Timers;` only"); |
| 127 | +{ |
| 128 | + var p1 = new[] { T("P1", "GlobalUsings.g.cs", ImplicitUsings), |
| 129 | + T("P1", "a.cs", "class A { void M() { var t = new Timer(_ => { }); /*probe:p1-Timer*/ } }") }; |
| 130 | + var p2 = new[] { T("P2", "b.cs", """ |
| 131 | + using System.Timers; |
| 132 | + class B { Timer t = new Timer(100); /*probe:p2-Timer*/ |
| 133 | + void M() { t.Elapsed += H; /*probe:p2-Elapsed*/ } |
| 134 | + void H(object s, ElapsedEventArgs e) { } } |
| 135 | + """) }; |
| 136 | + Report("merged", Merged(p1.Concat(p2), Array.Empty<MetadataReference>())); |
| 137 | + Report("per-project P1", Proj("P1", p1, Array.Empty<MetadataReference>())); |
| 138 | + Report("per-project P2", Proj("P2", p2, Array.Empty<MetadataReference>())); |
| 139 | +} |
| 140 | + |
| 141 | +// ---------- F2.5 one project with a missing dependency ---------- |
| 142 | +Console.WriteLine("F2.5 P3 misses its dependency; P1 is complete"); |
| 143 | +{ |
| 144 | + var dep = Lib("Dep", "1.0.0.0", "namespace D { public class Src { public event System.EventHandler Changed; public void Raise() => Changed?.Invoke(this, System.EventArgs.Empty); } }"); |
| 145 | + var p1 = new[] { T("P1", "a.cs", @"class A { D.Src s = new D.Src(); |
| 146 | + void M() { s.Changed += H; /*probe:p1-Changed*/ } void H(object o, System.EventArgs e) { } }") }; |
| 147 | + var p3 = new[] { T("P3", "c.cs", @"class C { D.Src s = new D.Src(); |
| 148 | + void M() { s.Changed += H; /*probe:p3-Changed-missing*/ } void H(object o, System.EventArgs e) { } }") }; |
| 149 | + Report("merged (Dep referenced, P3 did not declare it)", Merged(p1.Concat(p3), new[] { dep })); |
| 150 | + Report("per-project P1 (Dep)", Proj("P1", p1, new[] { dep })); |
| 151 | + Report("per-project P3 (no Dep)", Proj("P3", p3, Array.Empty<MetadataReference>())); |
| 152 | +} |
0 commit comments