Skip to content

Commit

Permalink
List patterns: change syntax to use square brackets (#54335)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrz authored Jul 13, 2021
1 parent 37d8075 commit 9961685
Show file tree
Hide file tree
Showing 55 changed files with 1,569 additions and 2,106 deletions.
4 changes: 4 additions & 0 deletions src/Compilers/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ dotnet_diagnostic.RS0102.severity = none
csharp_style_var_for_built_in_types = false:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = false:none

# XML files
[*.xml]
indent_size = 2
54 changes: 25 additions & 29 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7878,7 +7878,7 @@ private bool TryBindIndexOrRangeIndexer(

bool argIsIndex = argIsIndexNotRange.Value();
var useSiteInfo = CompoundUseSiteInfo<AssemblySymbol>.Discarded;
if (!TryFindIndexOrRangeIndexerPattern(syntax, receiverOpt, receiverType, argIsIndex: argIsIndex,
if (!TryFindIndexOrRangeIndexerPattern(receiverOpt, receiverType, argIsIndex: argIsIndex,
out PropertySymbol? lengthOrCountProperty, out Symbol? patternSymbol, diagnostics, ref useSiteInfo))
{
return false;
Expand All @@ -7892,6 +7892,16 @@ private bool TryBindIndexOrRangeIndexer(
BindToNaturalType(argument, diagnostics),
patternSymbol.GetTypeOrReturnType().Type);

ReportDiagnosticsIfObsolete(diagnostics, patternSymbol, syntax, hasBaseReceiver: false);
ReportDiagnosticsIfObsolete(diagnostics, lengthOrCountProperty, syntax, hasBaseReceiver: false);

if (!argIsIndex)
{
checkWellKnown(WellKnownMember.System_Range__get_Start);
checkWellKnown(WellKnownMember.System_Range__get_End);
}
checkWellKnown(WellKnownMember.System_Index__GetOffset);

_ = MessageID.IDS_FeatureIndexOperator.CheckFeatureAvailability(diagnostics, syntax);
if (arguments.Names.Count > 0)
{
Expand All @@ -7902,15 +7912,23 @@ private bool TryBindIndexOrRangeIndexer(
arguments.Names[0].GetLocation());
}
return true;

void checkWellKnown(WellKnownMember member)
{
// Check required well-known member. They may not be needed
// during lowering, but it's simpler to always require them to prevent
// the user from getting surprising errors when optimizations fail
_ = GetWellKnownTypeMember(member, diagnostics, syntax: syntax);
}
}

private bool TryFindIndexOrRangeIndexerPattern(
SyntaxNode syntax,
BoundExpression? receiverOpt,
TypeSymbol receiverType,
bool argIsIndex,
[NotNullWhen(true)] out PropertySymbol? lengthOrCountProperty,
[NotNullWhen(true)] out Symbol? patternSymbol,
// PROTOTYPE(list-patterns) We should take either of these and adjust the caller.
BindingDiagnosticBag diagnostics,
ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
Expand All @@ -7927,12 +7945,9 @@ private bool TryFindIndexOrRangeIndexerPattern(
var lookupResult = LookupResult.GetInstance();

if (TryLookupLengthOrCount(receiverType, lookupResult, out lengthOrCountProperty, ref useSiteInfo) &&
TryFindIndexOrRangeIndexerPattern(syntax, lookupResult, receiverOpt, receiverType, argIsIndex, out patternSymbol, diagnostics, ref useSiteInfo))
TryFindIndexOrRangeIndexerPattern(lookupResult, receiverOpt, receiverType, argIsIndex, out patternSymbol, diagnostics, ref useSiteInfo))
{
CheckImplicitThisCopyInReadOnlyMember(receiverOpt, lengthOrCountProperty.GetMethod, diagnostics);
// PROTOTYPE we should check whether lengthOrCountProperty.GetMethod might be obsolete
ReportDiagnosticsIfObsolete(diagnostics, lengthOrCountProperty, syntax, hasBaseReceiver: false);

lookupResult.Free();
return true;
}
Expand All @@ -7943,16 +7958,16 @@ private bool TryFindIndexOrRangeIndexerPattern(
}

private bool TryFindIndexOrRangeIndexerPattern(
SyntaxNode syntax,
LookupResult lookupResult,
BoundExpression? receiverOpt,
TypeSymbol receiverType,
bool argIsIndex,
[NotNullWhen(true)] out Symbol? patternSymbol,
// PROTOTYPE(list-patterns) We should take either of these and adjust the caller.
BindingDiagnosticBag diagnostics,
ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo,
bool forPattern = false)
ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
Debug.Assert(lookupResult.IsClear);
if (argIsIndex)
{
// Look for `T this[int i]` indexer
Expand All @@ -7978,10 +7993,8 @@ candidate is PropertySymbol property &&
property.OriginalDefinition is { ParameterCount: 1 } original &&
original.Parameters[0] is { Type: { SpecialType: SpecialType.System_Int32 }, RefKind: RefKind.None })
{
ReportDiagnosticsIfObsolete(diagnostics, property, syntax, hasBaseReceiver: false);
// note: implicit copy check on the indexer accessor happens in CheckPropertyValueKind
patternSymbol = property;
checkWellKnown(WellKnownMember.System_Index__GetOffset);
return true;
}
}
Expand All @@ -7996,9 +8009,6 @@ candidate is PropertySymbol property &&
if (substring is object)
{
patternSymbol = substring;
checkWellKnown(WellKnownMember.System_Range__get_Start);
checkWellKnown(WellKnownMember.System_Range__get_End);
checkWellKnown(WellKnownMember.System_Index__GetOffset);
return true;
}
}
Expand Down Expand Up @@ -8033,10 +8043,6 @@ method.OriginalDefinition is var original &&
{
patternSymbol = method;
CheckImplicitThisCopyInReadOnlyMember(receiverOpt, method, diagnostics);
ReportDiagnosticsIfObsolete(diagnostics, method, syntax, hasBaseReceiver: false);
checkWellKnown(WellKnownMember.System_Range__get_Start);
checkWellKnown(WellKnownMember.System_Range__get_End);
checkWellKnown(WellKnownMember.System_Index__GetOffset);
return true;
}
}
Expand All @@ -8045,17 +8051,6 @@ method.OriginalDefinition is var original &&

patternSymbol = null;
return false;

void checkWellKnown(WellKnownMember member)
{
// For list or slice patterns, there's no index or range expression in the source so we won't report missing members here.
if (forPattern)
return;
// Check required well-known member. They may not be needed
// during lowering, but it's simpler to always require them to prevent
// the user from getting surprising errors when optimizations fail
_ = GetWellKnownTypeMember(member, diagnostics, syntax: syntax);
}
}

private bool TryLookupLengthOrCount(
Expand All @@ -8064,6 +8059,7 @@ private bool TryLookupLengthOrCount(
[NotNullWhen(true)] out PropertySymbol? lengthOrCountProperty,
ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
Debug.Assert(lookupResult.IsClear);
// PROTOTYPE(list-patterns) Assert about whether diagnostics and dependencies are being accumulated.
return tryLookupLengthOrCount(WellKnownMemberNames.LengthPropertyName, out lengthOrCountProperty, ref useSiteInfo) ||
tryLookupLengthOrCount(WellKnownMemberNames.CountPropertyName, out lengthOrCountProperty, ref useSiteInfo);
Expand Down
Loading

0 comments on commit 9961685

Please sign in to comment.