-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
Version Used: https://github.com/bernd5/roslyn/tree/d83425fde733763cd45c010209ce8ce2ae1185d3
Steps to Reproduce:
Try to compile the following code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
ObjectList o = [1, 2];
var x = (ObjectList)[3, 4]; //<-- this fails to compile
class CustomListBuilder
{
public static ObjectList Create(ReadOnlySpan<object> span)
{
var list = new ObjectList();
foreach (var l in span)
{
list.Add(l);
}
return list;
}
}
[CollectionBuilder(typeof(CustomListBuilder), "Create")]
class ObjectList : IEnumerable<object>
{
private readonly List<object> _list = new();
public IEnumerator<object> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _list.GetEnumerator();
}
public void Add(object item) => _list.Add(item);
//and more...
}Diagnostic Id:
// /Program.cs(7,10): error CS0119: 'ObjectList' is a type, which is not valid in the given context
// var x = (ObjectList)[3, 4];
Diagnostic(ErrorCode.ERR_BadSKunknown, "ObjectList").WithArguments("ObjectList", "type").WithLocation(7, 10),
// /Program.cs(7,10): error CS0119: 'ObjectList' is a type, which is not valid in the given context
// var x = (ObjectList)[3, 4];
Diagnostic(ErrorCode.ERR_BadSKunknown, "ObjectList").WithArguments("ObjectList", "type").WithLocation(7, 10)
Expected Behavior:
Should compile fine (like it does with generic types or global-qualified types)
Actual Behavior:
Fails to parse the cast which causes the error.
See the syntax tree:
LocalDeclarationStatement(
VariableDeclaration(
IdentifierName(
Identifier(
TriviaList(),
SyntaxKind.VarKeyword,
"var",
"var",
TriviaList())))
.WithVariables(
SingletonSeparatedList<VariableDeclaratorSyntax>(
VariableDeclarator(
Identifier("x"))
.WithInitializer(
EqualsValueClause(
ElementAccessExpression(
ParenthesizedExpression(
IdentifierName("ObjectList")))
.WithArgumentList(
BracketedArgumentList(
SeparatedList<ArgumentSyntax>(
new SyntaxNodeOrToken[]{
Argument(
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(3))),
Token(SyntaxKind.CommaToken),
Argument(
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(4)))}))))))))
.NormalizeWhitespace()It should be:
LocalDeclarationStatement(
VariableDeclaration(
IdentifierName(
Identifier(
TriviaList(),
SyntaxKind.VarKeyword,
"var",
"var",
TriviaList())))
.WithVariables(
SingletonSeparatedList<VariableDeclaratorSyntax>(
VariableDeclarator(
Identifier("x"))
.WithInitializer(
EqualsValueClause(
CastExpression(
IdentifierName("ObjectList"),
CollectionExpression(
SeparatedList<CollectionElementSyntax>(
new SyntaxNodeOrToken[]{
ExpressionElement(
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(3))),
Token(SyntaxKind.CommaToken),
ExpressionElement(
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(4)))}))))))))