From 1ab03508f5c1e548624f2ff3419958626798cb2a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 12 Dec 2023 20:15:21 +0100 Subject: [PATCH] #3134: A bug in DetermineEffectiveAccessibility prevented references from the enclosing type to public members of private nested types to be found. --- ILSpy/Analyzers/AnalyzerScope.cs | 38 +++++++++++++++++--------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/ILSpy/Analyzers/AnalyzerScope.cs b/ILSpy/Analyzers/AnalyzerScope.cs index 6377549b2..4560ce535 100644 --- a/ILSpy/Analyzers/AnalyzerScope.cs +++ b/ILSpy/Analyzers/AnalyzerScope.cs @@ -54,16 +54,7 @@ namespace ICSharpCode.ILSpy.Analyzers AssemblyList = assemblyList; assemblyListSnapshot = assemblyList.GetSnapshot(); AnalyzedSymbol = entity; - if (entity is ITypeDefinition type) - { - typeScope = type; - effectiveAccessibility = DetermineEffectiveAccessibility(ref typeScope, Accessibility.Public); - } - else - { - typeScope = entity.DeclaringTypeDefinition; - effectiveAccessibility = DetermineEffectiveAccessibility(ref typeScope, entity.Accessibility); - } + DetermineEffectiveAccessibility(entity, out typeScope, out effectiveAccessibility); IsLocal = effectiveAccessibility.LessThanOrEqual(Accessibility.Private); } @@ -112,20 +103,31 @@ namespace ICSharpCode.ILSpy.Analyzers } } - static Accessibility DetermineEffectiveAccessibility(ref ITypeDefinition typeScope, Accessibility memberAccessibility) + static void DetermineEffectiveAccessibility(IEntity input, out ITypeDefinition typeScope, out Accessibility accessibility) { - Accessibility accessibility = memberAccessibility; - var ts = typeScope; - while (ts != null && !accessibility.LessThanOrEqual(Accessibility.Private)) + if (input is ITypeDefinition td) + { + accessibility = Accessibility.Public; + typeScope = td; + } + else { - accessibility = accessibility.Intersect(ts.Accessibility); - typeScope = ts; - ts = ts.DeclaringTypeDefinition; + accessibility = input.Accessibility; + typeScope = input.DeclaringTypeDefinition; } // Once we reach a private entity, we leave the loop with typeScope set to the class that // contains the private entity = the scope that needs to be searched. // Otherwise (if we don't find a private entity) we return the top-level class. - return accessibility; + var prevTypeScope = typeScope; + while (typeScope != null && !accessibility.LessThanOrEqual(Accessibility.Private)) + { + accessibility = accessibility.Intersect(typeScope.Accessibility); + typeScope = prevTypeScope.DeclaringTypeDefinition; + } + if (typeScope == null) + { + typeScope = prevTypeScope; + } } #region Find modules