Skip to content

Commit 34080ef

Browse files
authored
Merge pull request #46043 from CyrusNajmabadi/backport
Backport 45942 to dev16.7. Include information why symbol key resolution has failed to help with debugging
2 parents 11b7e4b + 6a6327e commit 34080ef

24 files changed

+449
-177
lines changed

src/Workspaces/Core/Portable/Remote/RemoteArguments.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ public async Task<ISymbol> TryRehydrateAsync(
107107
// The server and client should both be talking about the same compilation. As such
108108
// locations in symbols are save to resolve as we rehydrate the SymbolKey.
109109
var symbol = SymbolKey.ResolveString(
110-
SymbolKeyData, compilation, cancellationToken: cancellationToken).GetAnySymbol();
110+
SymbolKeyData, compilation, out var failureReason, cancellationToken).GetAnySymbol();
111111

112112
if (symbol == null)
113113
{
114114
try
115115
{
116116
throw new InvalidOperationException(
117-
$"We should always be able to resolve a symbol back on the host side:\r\n{SymbolKeyData}");
117+
$"We should always be able to resolve a symbol back on the host side:\r\n{SymbolKeyData}\r\n{failureReason}");
118118
}
119119
catch (Exception ex) when (FatalError.ReportWithoutCrash(ex))
120120
{

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AliasSymbolKey.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ public static void Create(IAliasSymbol symbol, SymbolKeyWriter visitor)
1818
visitor.WriteString(FirstOrDefault(symbol.DeclaringSyntaxReferences)?.SyntaxTree.FilePath ?? "");
1919
}
2020

21-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
21+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
2222
{
2323
var name = reader.ReadString();
24-
var targetResolution = reader.ReadSymbolKey();
24+
var targetResolution = reader.ReadSymbolKey(out var targetFailureReason);
2525
var filePath = reader.ReadString();
2626

27+
if (targetFailureReason != null)
28+
{
29+
failureReason = $"({nameof(AliasSymbolKey)} {nameof(targetResolution)} failed -> {targetFailureReason})";
30+
return default;
31+
}
32+
2733
var syntaxTree = reader.GetSyntaxTree(filePath);
2834
if (syntaxTree != null)
2935
{
@@ -34,11 +40,13 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
3440
var result = Resolve(semanticModel, syntaxTree.GetRoot(reader.CancellationToken), name, target, reader.CancellationToken);
3541
if (result.HasValue)
3642
{
43+
failureReason = null;
3744
return result.Value;
3845
}
3946
}
4047
}
4148

49+
failureReason = $"({nameof(AliasSymbolKey)} '{name}' not found)";
4250
return default;
4351
}
4452

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousFunctionOrDelegateSymbolKey.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,21 @@ public static void Create(ISymbol symbol, SymbolKeyWriter visitor)
3131
visitor.WriteLocation(FirstOrDefault(symbol.Locations));
3232
}
3333

34-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
34+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
3535
{
3636
var isAnonymousDelegateType = reader.ReadBoolean();
37-
var location = reader.ReadLocation();
37+
var location = reader.ReadLocation(out var locationFailureReason);
38+
39+
if (locationFailureReason != null)
40+
{
41+
failureReason = $"({nameof(AnonymousFunctionOrDelegateSymbolKey)} {nameof(location)} failed -> {locationFailureReason})";
42+
return default;
43+
}
3844

3945
var syntaxTree = location.SourceTree;
4046
if (syntaxTree == null)
4147
{
48+
failureReason = $"({nameof(AnonymousFunctionOrDelegateSymbolKey)} {nameof(SyntaxTree)} failed)";
4249
return default;
4350
}
4451

@@ -58,6 +65,7 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
5865
symbol = anonymousDelegate;
5966
}
6067

68+
failureReason = null;
6169
return new SymbolKeyResolution(symbol);
6270
}
6371
}

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AnonymousTypeSymbolKey.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor)
2828
visitor.WriteLocationArray(propertyLocations);
2929
}
3030

31-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
31+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
3232
{
33-
using var propertyTypes = reader.ReadSymbolKeyArray<ITypeSymbol>();
33+
using var propertyTypes = reader.ReadSymbolKeyArray<ITypeSymbol>(out var propertyTypesFailureReason);
3434
using var propertyNames = reader.ReadStringArray();
3535
using var propertyIsReadOnly = reader.ReadBooleanArray();
36-
using var propertyLocations = reader.ReadLocationArray();
36+
using var propertyLocations = reader.ReadLocationArray(out var propertyLocationsFailureReason);
37+
38+
if (propertyTypesFailureReason != null)
39+
{
40+
failureReason = $"({nameof(AnonymousTypeSymbolKey)} {nameof(propertyTypes)} failed -> {propertyTypesFailureReason})";
41+
return default;
42+
}
43+
44+
if (propertyLocationsFailureReason != null)
45+
{
46+
failureReason = $"({nameof(AnonymousTypeSymbolKey)} {nameof(propertyLocations)} failed -> {propertyLocationsFailureReason})";
47+
return default;
48+
}
3749

3850
if (!propertyTypes.IsDefault)
3951
{
@@ -42,13 +54,15 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
4254
var anonymousType = reader.Compilation.CreateAnonymousTypeSymbol(
4355
propertyTypes.ToImmutable(), propertyNames.ToImmutable(),
4456
propertyIsReadOnly.ToImmutable(), propertyLocations.ToImmutable());
57+
failureReason = null;
4558
return new SymbolKeyResolution(anonymousType);
4659
}
4760
catch (ArgumentException)
4861
{
4962
}
5063
}
5164

65+
failureReason = null;
5266
return new SymbolKeyResolution(reader.Compilation.ObjectType);
5367
}
5468
}

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.ArrayTypeSymbolKey.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@ public static void Create(IArrayTypeSymbol symbol, SymbolKeyWriter visitor)
1414
visitor.WriteInteger(symbol.Rank);
1515
}
1616

17-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
17+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
1818
{
19-
var elementTypeResolution = reader.ReadSymbolKey();
19+
var elementTypeResolution = reader.ReadSymbolKey(out var elementTypeFailureReason);
2020
var rank = reader.ReadInteger();
2121

22+
if (elementTypeFailureReason != null)
23+
{
24+
failureReason = $"({nameof(ArrayTypeSymbolKey)} {nameof(elementTypeResolution)} failed -> {elementTypeFailureReason})";
25+
return default;
26+
}
27+
2228
using var result = PooledArrayBuilder<IArrayTypeSymbol>.GetInstance(elementTypeResolution.SymbolCount);
2329
foreach (var typeSymbol in elementTypeResolution.OfType<ITypeSymbol>())
24-
{
2530
result.AddIfNotNull(reader.Compilation.CreateArrayTypeSymbol(typeSymbol, rank));
26-
}
2731

28-
return CreateResolution(result);
32+
return CreateResolution(result, $"({nameof(ArrayTypeSymbolKey)})", out failureReason);
2933
}
3034
}
3135
}

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.AssemblySymbolKey.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void Create(IAssemblySymbol symbol, SymbolKeyWriter visitor)
1717
visitor.WriteString(symbol.Identity.Name);
1818
}
1919

20-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
20+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
2121
{
2222
var assemblyName = reader.ReadString();
2323
var compilation = reader.Compilation;
@@ -38,7 +38,7 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
3838
}
3939
}
4040

41-
return CreateResolution(result);
41+
return CreateResolution(result, $"({nameof(AssemblySymbolKey)} '{assemblyName}' not found)", out failureReason);
4242
}
4343
}
4444
}

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.BodyLevelSymbolKey.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,21 @@ private static bool TryGetSemanticModel(
116116
return false;
117117
}
118118

119-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
119+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string? failureReason)
120120
{
121121
var cancellationToken = reader.CancellationToken;
122122

123123
var name = reader.ReadString();
124124
var kind = (SymbolKind)reader.ReadInteger();
125-
var locations = reader.ReadLocationArray();
125+
var locations = reader.ReadLocationArray(out var locationsFailureReason);
126126
var ordinal = reader.ReadInteger();
127127

128+
if (locationsFailureReason != null)
129+
{
130+
failureReason = $"({nameof(BodyLevelSymbolKey)} {nameof(locations)} failed -> {locationsFailureReason})";
131+
return default;
132+
}
133+
128134
// First check if we can recover the symbol just through the original location.
129135
foreach (var loc in locations)
130136
{
@@ -136,6 +142,7 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
136142
if (symbol?.Kind == kind &&
137143
SymbolKey.Equals(reader.Compilation, name, symbol.Name))
138144
{
145+
failureReason = null;
139146
return resolution;
140147
}
141148
}
@@ -148,10 +155,14 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
148155
foreach (var symbol in EnumerateSymbols(semanticModel, kind, name, cancellationToken))
149156
{
150157
if (symbol.ordinal == ordinal)
158+
{
159+
failureReason = null;
151160
return new SymbolKeyResolution(symbol.symbol);
161+
}
152162
}
153163
}
154164

165+
failureReason = $"({nameof(BodyLevelSymbolKey)} '{name}' not found)";
155166
return default;
156167
}
157168

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.DynamicTypeSymbolKey.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ public static void Create(SymbolKeyWriter _)
1414
// per compilation.
1515
}
1616

17-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
18-
=> new SymbolKeyResolution(reader.Compilation.DynamicType);
17+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
18+
{
19+
failureReason = null;
20+
return new SymbolKeyResolution(reader.Compilation.DynamicType);
21+
}
1922
}
2023
}
2124
}

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.ErrorTypeSymbolKey.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,30 @@ private static ImmutableArray<string> GetContainingNamespaceNamesInReverse(IName
5757
return builder.ToImmutable();
5858
}
5959

60-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
60+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
6161
{
6262
var name = reader.ReadString();
63-
var containingSymbolResolution = ResolveContainer(reader);
63+
64+
var containingSymbolResolution = ResolveContainer(reader, out var containingSymbolFailureReason);
6465
var arity = reader.ReadInteger();
6566

66-
using var typeArguments = reader.ReadSymbolKeyArray<ITypeSymbol>();
67+
using var typeArguments = reader.ReadSymbolKeyArray<ITypeSymbol>(out var typeArgumentsFailureReason);
68+
69+
if (containingSymbolFailureReason != null)
70+
{
71+
failureReason = $"({nameof(ErrorTypeSymbolKey)} {nameof(containingSymbolResolution)} failed -> {containingSymbolFailureReason})";
72+
return default;
73+
}
74+
75+
if (typeArgumentsFailureReason != null)
76+
{
77+
failureReason = $"({nameof(ErrorTypeSymbolKey)} {nameof(typeArguments)} failed -> {typeArgumentsFailureReason})";
78+
return default;
79+
}
80+
6781
if (typeArguments.IsDefault)
6882
{
83+
failureReason = $"({nameof(ErrorTypeSymbolKey)} {nameof(typeArguments)} failed)";
6984
return default;
7085
}
7186

@@ -85,15 +100,15 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
85100
reader, container: null, name, arity, typeArgumentsArray));
86101
}
87102

88-
return CreateResolution(result);
103+
return CreateResolution(result, $"({nameof(ErrorTypeSymbolKey)} failed)", out failureReason);
89104
}
90105

91-
private static SymbolKeyResolution ResolveContainer(SymbolKeyReader reader)
106+
private static SymbolKeyResolution ResolveContainer(SymbolKeyReader reader, out string failureReason)
92107
{
93108
var type = reader.ReadInteger();
94109

95110
if (type == 0)
96-
return reader.ReadSymbolKey();
111+
return reader.ReadSymbolKey(out failureReason);
97112

98113
if (type == 1)
99114
{
@@ -104,11 +119,15 @@ private static SymbolKeyResolution ResolveContainer(SymbolKeyReader reader)
104119
for (var i = namespaceNames.Count - 1; i >= 0; i--)
105120
currentNamespace = reader.Compilation.CreateErrorNamespaceSymbol(currentNamespace, namespaceNames[i]);
106121

122+
failureReason = null;
107123
return new SymbolKeyResolution(currentNamespace);
108124
}
109125

110126
if (type == 2)
127+
{
128+
failureReason = null;
111129
return default;
130+
}
112131

113132
throw ExceptionUtilities.UnexpectedValue(type);
114133
}

src/Workspaces/Core/Portable/SymbolKey/SymbolKey.EventSymbolKey.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ public static void Create(IEventSymbol symbol, SymbolKeyWriter visitor)
1414
visitor.WriteSymbolKey(symbol.ContainingType);
1515
}
1616

17-
public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
17+
public static SymbolKeyResolution Resolve(SymbolKeyReader reader, out string failureReason)
1818
{
1919
var metadataName = reader.ReadString();
20-
var containingTypeResolution = reader.ReadSymbolKey();
20+
var containingTypeResolution = reader.ReadSymbolKey(out var containingTypeFailureReason);
21+
22+
if (containingTypeFailureReason != null)
23+
{
24+
failureReason = $"({nameof(EventSymbolKey)} {nameof(containingTypeResolution)} failed -> {containingTypeFailureReason})";
25+
return default;
26+
}
2127

2228
using var result = GetMembersOfNamedType<IEventSymbol>(containingTypeResolution, metadataName);
23-
return CreateResolution(result);
29+
return CreateResolution(result, $"({nameof(EventSymbolKey)} '{metadataName}' not found)", out failureReason);
2430
}
2531
}
2632
}

0 commit comments

Comments
 (0)