Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid returning default from MethodSymbol.Parameters #54179

Merged
merged 4 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public sealed override ImmutableArray<ParameterSymbol> Parameters
{
if (_parameters.IsDefault)
{
ImmutableInterlocked.InterlockedCompareExchange(ref _parameters, MakeParameters(), default(ImmutableArray<ParameterSymbol>));
ImmutableInterlocked.InterlockedInitialize(ref _parameters, MakeParameters());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand how these two lines are different? I'm missing the distinction here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➡️ They are semantically identical. The simpler form allows a reader to understand the behavior without the mental burden of relating the third parameter of InterlockedCompareExchange to the default state for the field.

}
return _parameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.PooledObjects;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
Expand All @@ -25,13 +26,13 @@ internal AnonymousTypeConstructorSymbol(NamedTypeSymbol container, ImmutableArra
int fieldsCount = properties.Length;
if (fieldsCount > 0)
{
ParameterSymbol[] paramsArr = new ParameterSymbol[fieldsCount];
var paramsArr = ArrayBuilder<ParameterSymbol>.GetInstance(fieldsCount);
for (int index = 0; index < fieldsCount; index++)
{
PropertySymbol property = properties[index];
paramsArr[index] = SynthesizedParameterSymbol.Create(this, property.TypeWithAnnotations, index, RefKind.None, property.Name);
paramsArr.Add(SynthesizedParameterSymbol.Create(this, property.TypeWithAnnotations, index, RefKind.None, property.Name));
}
_parameters = paramsArr.AsImmutableOrNull();
_parameters = paramsArr.ToImmutableAndFree();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public override ImmutableArray<ParameterSymbol> Parameters
{
if (_lazyParameters.IsDefault)
{
ImmutableInterlocked.InterlockedCompareExchange(ref _lazyParameters, this.MakeParameters(), default(ImmutableArray<ParameterSymbol>));
ImmutableInterlocked.InterlockedInitialize(ref _lazyParameters, this.MakeParameters());
}
return _lazyParameters;
}
Expand Down Expand Up @@ -557,13 +557,13 @@ private ImmutableArray<ParameterSymbol> MakeParameters()
}
else
{
var parameters = new ParameterSymbol[count - 1];
var parameters = ArrayBuilder<ParameterSymbol>.GetInstance(count - 1);
for (int i = 0; i < count - 1; i++)
{
parameters[i] = new ReducedExtensionMethodParameterSymbol(this, reducedFromParameters[i + 1]);
parameters.Add(new ReducedExtensionMethodParameterSymbol(this, reducedFromParameters[i + 1]));
}

return parameters.AsImmutableOrNull();
return parameters.ToImmutableAndFree();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public override ImmutableArray<ParameterSymbol> Parameters
{
if (_lazyParameters.IsDefault)
{
ImmutableInterlocked.InterlockedCompareExchange(ref _lazyParameters, this.RetargetParameters(), default(ImmutableArray<ParameterSymbol>));
ImmutableInterlocked.InterlockedInitialize(ref _lazyParameters, this.RetargetParameters());
}

return _lazyParameters;
Expand All @@ -172,14 +172,14 @@ private ImmutableArray<ParameterSymbol> RetargetParameters()
}
else
{
ParameterSymbol[] parameters = new ParameterSymbol[count];
var parameters = ArrayBuilder<ParameterSymbol>.GetInstance(count);

for (int i = 0; i < count; i++)
{
parameters[i] = new RetargetingMethodParameterSymbol(this, list[i]);
parameters.Add(new RetargetingMethodParameterSymbol(this, list[i]));
}

return parameters.AsImmutableOrNull();
return parameters.ToImmutableAndFree();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public sealed override ImmutableArray<ParameterSymbol> Parameters
{
get
{
return _parameters;
Debug.Assert(!_parameters.IsDefault, $"Expected {nameof(InitializeParameters)} prior to accessing this property.");
return _parameters.NullToEmpty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public sealed override ImmutableArray<ParameterSymbol> Parameters
{
if (_lazyParameters.IsDefault)
{
ImmutableInterlocked.InterlockedCompareExchange(ref _lazyParameters, SubstituteParameters(), default(ImmutableArray<ParameterSymbol>));
ImmutableInterlocked.InterlockedInitialize(ref _lazyParameters, SubstituteParameters());
}

return _lazyParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
Expand Down Expand Up @@ -125,16 +126,17 @@ internal InvokeMethod(SynthesizedDelegateSymbol containingType, BitVector byRefP
// if we are given Void type the method returns Void, otherwise its return type is the last type parameter of the delegate:
_returnType = voidReturnTypeOpt ?? typeParams.Last();

var parameters = new ParameterSymbol[typeParams.Length - ((object)voidReturnTypeOpt != null ? 0 : 1)];
for (int i = 0; i < parameters.Length; i++)
int parameterCount = typeParams.Length - ((object)voidReturnTypeOpt != null ? 0 : 1);
var parameters = ArrayBuilder<ParameterSymbol>.GetInstance(parameterCount);
for (int i = 0; i < parameterCount; i++)
{
// we don't need to distinguish between out and ref since this is an internal synthesized symbol:
var refKind = !byRefParameters.IsNull && byRefParameters[i] ? RefKind.Ref : RefKind.None;

parameters[i] = SynthesizedParameterSymbol.Create(this, TypeWithAnnotations.Create(typeParams[i]), i, refKind);
parameters.Add(SynthesizedParameterSymbol.Create(this, TypeWithAnnotations.Create(typeParams[i]), i, refKind));
}

_parameters = parameters.AsImmutableOrNull();
_parameters = parameters.ToImmutableAndFree();
}

public override string Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public override ImmutableArray<ParameterSymbol> Parameters
{
get
{
if (_parameters.IsEmpty)
Debug.Assert(!_parameters.IsDefault, $"Expected {nameof(SetParameters)} prior to accessing this property.");
if (_parameters.IsDefault)
{
return ImmutableArray<ParameterSymbol>.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public SynthesizedIntrinsicOperatorSymbol(TypeSymbol leftType, string name, Type
Debug.Assert((leftType.IsDynamic() || rightType.IsDynamic()) == returnType.IsDynamic());
Debug.Assert(_containingType.IsDynamic() == returnType.IsDynamic());

_parameters = (new ParameterSymbol[] {new SynthesizedOperatorParameterSymbol(this, leftType, 0, "left"),
new SynthesizedOperatorParameterSymbol(this, rightType, 1, "right")}).AsImmutableOrNull();
_parameters = ImmutableArray.Create<ParameterSymbol>(new SynthesizedOperatorParameterSymbol(this, leftType, 0, "left"),
new SynthesizedOperatorParameterSymbol(this, rightType, 1, "right"));
_isCheckedBuiltin = isCheckedBuiltin;
}

Expand All @@ -52,7 +52,7 @@ public SynthesizedIntrinsicOperatorSymbol(TypeSymbol container, string name, Typ
_containingType = container;
_name = name;
_returnType = returnType;
_parameters = (new ParameterSymbol[] { new SynthesizedOperatorParameterSymbol(this, container, 0, "value") }).AsImmutableOrNull();
_parameters = ImmutableArray.Create<ParameterSymbol>(new SynthesizedOperatorParameterSymbol(this, container, 0, "value"));
_isCheckedBuiltin = isCheckedBuiltin;
}

Expand Down