Skip to content

Commit

Permalink
Add version check to enable the pattern-based Index & Range indexers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
agocke authored Apr 23, 2019
1 parent 50f76d5 commit b1fe112
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7192,6 +7192,7 @@ private BoundExpression BindIndexerAccess(ExpressionSyntax node, BoundExpression
node,
expr,
analyzedArguments.Arguments,
diagnostics,
out var patternIndexerAccess))
{
indexerAccessExpression = patternIndexerAccess;
Expand Down Expand Up @@ -7365,6 +7366,7 @@ private BoundExpression BindIndexerOrIndexedPropertyAccess(
syntax,
receiverOpt,
analyzedArguments.Arguments,
diagnostics,
out var patternIndexerAccess))
{
return patternIndexerAccess;
Expand Down Expand Up @@ -7464,6 +7466,7 @@ private bool TryBindIndexOrRangeIndexer(
SyntaxNode syntax,
BoundExpression receiverOpt,
ArrayBuilder<BoundExpression> arguments,
DiagnosticBag diagnostics,
out BoundIndexOrRangePatternIndexerAccess patternIndexerAccess)
{
// Verify a few things up-front, namely that we have a single argument
Expand Down Expand Up @@ -7542,6 +7545,7 @@ candidate is PropertySymbol property &&
property.OriginalDefinition is { ParameterCount: 1 } original &&
isIntNotByRef(original.Parameters[0]))
{
_ = MessageID.IDS_FeatureIndexOperator.CheckFeatureAvailability(diagnostics, syntax.Location);
patternIndexerAccess = new BoundIndexOrRangePatternIndexerAccess(
syntax,
receiverOpt,
Expand All @@ -7562,6 +7566,7 @@ candidate is PropertySymbol property &&
var substring = (MethodSymbol)Compilation.GetSpecialTypeMember(SpecialMember.System_String__Substring);
if (substring is { })
{
_ = MessageID.IDS_FeatureIndexOperator.CheckFeatureAvailability(diagnostics, syntax.Location);
patternIndexerAccess = new BoundIndexOrRangePatternIndexerAccess(
syntax,
receiverOpt,
Expand Down Expand Up @@ -7601,6 +7606,7 @@ method.OriginalDefinition is var original &&
isIntNotByRef(original.Parameters[0]) &&
isIntNotByRef(original.Parameters[1]))
{
_ = MessageID.IDS_FeatureIndexOperator.CheckFeatureAvailability(diagnostics, syntax.Location);
patternIndexerAccess = new BoundIndexOrRangePatternIndexerAccess(
syntax,
receiverOpt,
Expand Down
34 changes: 34 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/IndexAndRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ public class IndexAndRangeTests : CompilingTestBase
private const string RangeEndAtSignature = "System.Range System.Range.EndAt(System.Index end)";
private const string RangeAllSignature = "System.Range System.Range.All.get";

[Fact]
public void PatternIndexRangeLangVer()
{
var src = @"
using System;
struct S
{
public int Length => 0;
public int Slice(int x, int y) => 0;
}
class C
{
void M(string s, Index i, Range r)
{
_ = s[i];
_ = s[r];
_ = new S()[r];
}
}";
var comp = CreateCompilationWithIndexAndRange(src);
comp.VerifyDiagnostics();
comp = CreateCompilationWithIndexAndRange(src, parseOptions: TestOptions.Regular7_3);
comp.VerifyDiagnostics(
// (12,13): error CS8652: The feature 'index operator' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// _ = s[i];
Diagnostic(ErrorCode.ERR_FeatureInPreview, "s[i]").WithArguments("index operator").WithLocation(12, 13),
// (13,13): error CS8652: The feature 'index operator' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// _ = s[r];
Diagnostic(ErrorCode.ERR_FeatureInPreview, "s[r]").WithArguments("index operator").WithLocation(13, 13),
// (14,13): error CS8652: The feature 'index operator' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// _ = new S()[r];
Diagnostic(ErrorCode.ERR_FeatureInPreview, "new S()[r]").WithArguments("index operator").WithLocation(14, 13));
}

[Fact]
public void SpanPatternRangeDelegate()
{
Expand Down

0 comments on commit b1fe112

Please sign in to comment.