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

Add version check to enable the pattern-based Index & Range indexers #35170

Merged
merged 1 commit into from
Apr 23, 2019
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
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