Skip to content

Commit 3a723bb

Browse files
Update code to use GetFirstLocation() instead of Locations[0] or Locations.First() (#80709)
1 parent 4c279b2 commit 3a723bb

37 files changed

+188
-159
lines changed

src/Compilers/CSharp/Portable/Symbols/Source/ExtensionGroupingInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static void checkCollisions(IEnumerable<SourceNamedTypeSymbol> extensions, Func<
474474
alreadyReportedExtensions ??= PooledHashSet<SourceNamedTypeSymbol>.GetInstance();
475475
if (alreadyReportedExtensions.Add(extension))
476476
{
477-
diagnostics.Add(ErrorCode.ERR_ExtensionBlockCollision, extension.Locations[0]);
477+
diagnostics.Add(ErrorCode.ERR_ExtensionBlockCollision, extension.GetFirstLocation());
478478
}
479479
}
480480
}

src/Compilers/Core/Portable/Emit/EditAndContinue/DefinitionMap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ protected abstract void GetStateMachineFieldMapFromMetadata(
353353
{
354354
diagnostics.Add(MessageProvider.CreateDiagnostic(
355355
MessageProvider.ERR_InvalidDebugInfo,
356-
method.Locations.First(),
356+
method.GetFirstLocation(),
357357
method,
358358
MetadataTokens.GetToken(methodHandle),
359359
method.ContainingAssembly,
@@ -438,7 +438,7 @@ protected abstract void GetStateMachineFieldMapFromMetadata(
438438
{
439439
diagnostics.Add(MessageProvider.CreateDiagnostic(
440440
MessageProvider.ERR_InvalidDebugInfo,
441-
method.Locations.First(),
441+
method.GetFirstLocation(),
442442
method,
443443
MetadataTokens.GetToken(localSignature),
444444
method.ContainingAssembly,
@@ -479,7 +479,7 @@ private void ReportMissingStateMachineAttribute(DiagnosticBag diagnostics, IMeth
479479
{
480480
diagnostics.Add(MessageProvider.CreateDiagnostic(
481481
MessageProvider.ERR_EncUpdateFailedMissingSymbol,
482-
method.Locations.First(),
482+
method.GetFirstLocation(),
483483
CodeAnalysisResources.Attribute,
484484
stateMachineAttributeFullName));
485485
}

src/Compilers/Core/Portable/PEWriter/MetadataWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ private static Location GetNamedEntityLocation(INamedEntity errorEntity)
14531453

14541454
protected static Location GetSymbolLocation(ISymbolInternal symbolOpt)
14551455
{
1456-
return symbolOpt != null && !symbolOpt.Locations.IsDefaultOrEmpty ? symbolOpt.Locations[0] : Location.None;
1456+
return symbolOpt?.GetFirstLocationOrNone() ?? Location.None;
14571457
}
14581458

14591459
internal TypeAttributes GetTypeAttributes(ITypeDefinition typeDef)

src/Compilers/Core/Portable/Symbols/ISymbolInternal.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Immutable;
67
using System.Reflection.Metadata;
78
using System.Threading;
@@ -89,6 +90,21 @@ internal interface ISymbolInternal
8990
/// </summary>
9091
bool IsDefinition { get; }
9192

93+
/// <summary>
94+
/// Similar to getting the first location from <see cref="Locations"/>. However, this can be more efficient as
95+
/// an intermediary array may not need to be created. This can often be advantageous for perf as most symbols
96+
/// only have a single location, and most clients only need the first location for some purpose (like error
97+
/// reporting).
98+
/// </summary>
99+
/// <exception cref="InvalidOperationException">If the symbol has no locations.</exception>
100+
Location GetFirstLocation();
101+
102+
/// <summary>
103+
/// Equivalent to calling <see cref="GetFirstLocation"/>, except that if <see cref="Locations"/> is empty this
104+
/// will return <see cref="Location.None"/>.
105+
/// </summary>
106+
Location GetFirstLocationOrNone();
107+
92108
/// <summary>
93109
/// Gets the locations where the symbol was originally defined, either in source or
94110
/// metadata. Some symbols (for example, partial classes) may be defined in more than one

src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/DataFlowPass.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
197197
If Not local.IsFunctionValue AndAlso Not String.IsNullOrEmpty(local.Name) Then
198198
If _writtenVariables.Contains(local) Then
199199
If local.IsConst Then
200-
Me.diagnostics.Add(ERRID.WRN_UnusedLocalConst, local.Locations(0), If(local.Name, "dummy"))
200+
Me.diagnostics.Add(ERRID.WRN_UnusedLocalConst, local.GetFirstLocation(), If(local.Name, "dummy"))
201201
End If
202202
Else
203-
Me.diagnostics.Add(ERRID.WRN_UnusedLocal, local.Locations(0), If(local.Name, "dummy"))
203+
Me.diagnostics.Add(ERRID.WRN_UnusedLocal, local.GetFirstLocation(), If(local.Name, "dummy"))
204204
End If
205205
End If
206206
End Sub

src/Compilers/VisualBasic/Portable/Binding/Binder_Conversions.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ DoneWithDiagnostics:
15691569
If delegateParam.IsByRef OrElse delegateParam.OriginalDefinition.Type.IsTypeParameter() Then
15701570
Dim restrictedType As TypeSymbol = Nothing
15711571
If delegateParam.Type.IsRestrictedTypeOrArrayType(restrictedType) Then
1572-
ReportDiagnostic(diagnostics, lambda.LambdaSymbol.Parameters(delegateParam.Ordinal).Locations(0),
1572+
ReportDiagnostic(diagnostics, lambda.LambdaSymbol.Parameters(delegateParam.Ordinal).GetFirstLocation(),
15731573
ERRID.ERR_RestrictedType1, restrictedType)
15741574
End If
15751575
End If

src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
117117
unboundType,
118118
unboundParam.IsByRef,
119119
unboundParam.Syntax,
120-
unboundParam.Locations(0))
120+
unboundParam.GetFirstLocation())
121121
Next
122122

123123
If parameters.Length <> targetSignature.ParameterTypes.Length Then
@@ -142,7 +142,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
142142
unboundType,
143143
unboundParam.IsByRef,
144144
unboundParam.Syntax,
145-
unboundParam.Locations(0))
145+
unboundParam.GetFirstLocation())
146146
Next
147147
End If
148148

src/Compilers/VisualBasic/Portable/Binding/ExecutableCodeBinder.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
136136
Dim bestLocation As Location = Nothing
137137
For Each symbol In labels
138138
Debug.Assert(symbol.Locations.Length = 1)
139-
Dim sourceLocation As Location = symbol.Locations(0)
139+
Dim sourceLocation As Location = symbol.GetFirstLocation()
140140
If bestSymbol Is Nothing OrElse Me.Compilation.CompareSourceLocations(bestLocation, sourceLocation) > 0 Then
141141
bestSymbol = symbol
142142
bestLocation = sourceLocation

src/Compilers/VisualBasic/Portable/Compilation/ClsComplianceChecker.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
763763
If attributeData.IsTargetAttribute(AttributeDescription.CLSCompliantAttribute) Then
764764
Dim attributeClass = attributeData.AttributeClass
765765
If attributeClass IsNot Nothing Then
766-
_diagnostics.ReportUseSite(attributeClass, If(symbol.Locations.IsEmpty, NoLocation.Singleton, symbol.Locations(0)))
766+
_diagnostics.ReportUseSite(attributeClass, If(symbol.Locations.IsEmpty, NoLocation.Singleton, symbol.GetFirstLocation()))
767767
End If
768768

769769
If Not attributeData.HasErrors Then
@@ -831,7 +831,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
831831
End Function
832832

833833
Private Sub AddDiagnostic(symbol As Symbol, code As ERRID, ParamArray args As Object())
834-
Dim location = If(symbol.Locations.IsEmpty, NoLocation.Singleton, symbol.Locations(0))
834+
Dim location = If(symbol.Locations.IsEmpty, NoLocation.Singleton, symbol.GetFirstLocation())
835835
Me.AddDiagnostic(symbol, code, location, args)
836836
End Sub
837837

src/Compilers/VisualBasic/Portable/Compilation/DocumentationComments/DocumentationCommentCompiler.Includes.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
664664
' can be found by the name, just deterministically choose which one to use
665665
If symbolCommentId Is Nothing OrElse
666666
Me._compilation.CompareSourceLocations(
667-
smallestSymbol.Locations(0), symbol.Locations(0)) > 0 Then
667+
smallestSymbol.GetFirstLocation(), symbol.GetFirstLocation()) > 0 Then
668668

669669
symbolCommentId = id
670670
smallestSymbol = symbol

0 commit comments

Comments
 (0)