未验证 提交 b1fe1127 编写于 作者: A Andy Gocke 提交者: GitHub

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

上级 50f76d59
......@@ -7192,6 +7192,7 @@ private BoundExpression BindIndexerAccess(ExpressionSyntax node, BoundExpression
node,
expr,
analyzedArguments.Arguments,
diagnostics,
out var patternIndexerAccess))
{
indexerAccessExpression = patternIndexerAccess;
......@@ -7365,6 +7366,7 @@ private BoundExpression BindIndexedPropertyAccess(SyntaxNode syntax, BoundExpres
syntax,
receiverOpt,
analyzedArguments.Arguments,
diagnostics,
out var patternIndexerAccess))
{
return patternIndexerAccess;
......@@ -7464,6 +7466,7 @@ private BoundExpression BindIndexedPropertyAccess(SyntaxNode syntax, BoundExpres
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
......@@ -7542,6 +7545,7 @@ private BoundExpression BindIndexedPropertyAccess(SyntaxNode syntax, BoundExpres
property.OriginalDefinition is { ParameterCount: 1 } original &&
isIntNotByRef(original.Parameters[0]))
{
_ = MessageID.IDS_FeatureIndexOperator.CheckFeatureAvailability(diagnostics, syntax.Location);
patternIndexerAccess = new BoundIndexOrRangePatternIndexerAccess(
syntax,
receiverOpt,
......@@ -7562,6 +7566,7 @@ private BoundExpression BindIndexedPropertyAccess(SyntaxNode syntax, BoundExpres
var substring = (MethodSymbol)Compilation.GetSpecialTypeMember(SpecialMember.System_String__Substring);
if (substring is { })
{
_ = MessageID.IDS_FeatureIndexOperator.CheckFeatureAvailability(diagnostics, syntax.Location);
patternIndexerAccess = new BoundIndexOrRangePatternIndexerAccess(
syntax,
receiverOpt,
......@@ -7601,6 +7606,7 @@ private BoundExpression BindIndexedPropertyAccess(SyntaxNode syntax, BoundExpres
isIntNotByRef(original.Parameters[0]) &&
isIntNotByRef(original.Parameters[1]))
{
_ = MessageID.IDS_FeatureIndexOperator.CheckFeatureAvailability(diagnostics, syntax.Location);
patternIndexerAccess = new BoundIndexOrRangePatternIndexerAccess(
syntax,
receiverOpt,
......
......@@ -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()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册