提交 ab691fd4 编写于 作者: N Neal Gafter

Fix compiler crash when binding missing property pattern member.

上级 7f815b54
......@@ -658,14 +658,16 @@ private BoundPattern BindPropertyPattern(PropertyPatternSyntax node, TypeSymbol
{
Symbol symbol = BindPropertyPatternMember(inputType, name, ref hasErrors, diagnostics);
if (inputType.IsErrorType() || hasErrors)
if (inputType.IsErrorType() || hasErrors || symbol == (object)null)
{
memberType = CreateErrorType();
return null;
}
memberType = symbol.GetTypeOrReturnType();
return symbol;
else
{
memberType = symbol.GetTypeOrReturnType();
return symbol;
}
}
private Symbol BindPropertyPatternMember(
......@@ -726,16 +728,18 @@ private BoundPattern BindPropertyPattern(PropertyPatternSyntax node, TypeSymbol
default:
Error(diagnostics, ErrorCode.ERR_PropertyLacksGet, memberName, name);
hasErrors = true;
break;
}
}
hasErrors = true;
return null;
}
if (hasErrors || !CheckValueKind(node: memberName.Parent, expr: boundMember, valueKind: BindValueKind.RValue,
checkingReceiver: false, diagnostics: diagnostics))
{
hasErrors = true;
return null;
}
......
......@@ -1254,6 +1254,59 @@ class Cat {}
var comp = CompileAndVerify(compilation, expectedOutput: @"Fox Cat");
}
[Fact]
public void PropertyPatternMemberMissing01()
{
var source =
@"class Program
{
static void Main(string[] args)
{
Blah b = null;
if (b is Blah { X: int i })
{
}
}
}
class Blah
{
}";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (6,25): error CS0117: 'Blah' does not contain a definition for 'X'
// if (b is Blah { X: int i })
Diagnostic(ErrorCode.ERR_NoSuchMember, "X").WithArguments("Blah", "X").WithLocation(6, 25)
);
}
[Fact]
public void PropertyPatternMemberMissing02()
{
var source =
@"class Program
{
static void Main(string[] args)
{
Blah b = null;
if (b is Blah { X: int i })
{
}
}
}
class Blah
{
public int X { set {} }
}";
var compilation = CreatePatternCompilation(source);
compilation.VerifyDiagnostics(
// (6,25): error CS0154: The property or indexer 'Blah.X' cannot be used in this context because it lacks the get accessor
// if (b is Blah { X: int i })
Diagnostic(ErrorCode.ERR_PropertyLacksGet, "X:").WithArguments("Blah.X").WithLocation(6, 25)
);
}
// PROTOTYPE(patterns2): Need to have tests that exercise:
// PROTOTYPE(patterns2): Building the decision tree for the var-pattern
// PROTOTYPE(patterns2): Definite assignment for the var-pattern
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册