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

Bind child expressions within bad stackalloc expression to natural type #72454

Merged
merged 6 commits into from
Mar 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4062,7 +4062,7 @@ private BoundExpression BindStackAllocArrayCreationExpression(
{
if (size.Kind() != SyntaxKind.OmittedArraySizeExpression)
{
builder.Add(BindExpression(size, BindingDiagnosticBag.Discarded));
builder.Add(BindToTypeForErrorRecovery(BindExpression(size, BindingDiagnosticBag.Discarded)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2677,5 +2677,97 @@ public void Method1()

Assert.Null(model.GetDeclaredSymbol(@stackalloc));
}

[WorkItem("https://github.com/dotnet/roslyn/issues/72448")]
[Fact]
public void UnconvertedExpression_01()
{
string source = """
stackalloc X[new(), new()];
""";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net70);
comp.VerifyDiagnostics(
// (1,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
// stackalloc X[new(), new()];
Diagnostic(ErrorCode.ERR_IllegalStatement, "stackalloc X[new(), new()]").WithLocation(1, 1),
// (1,12): error CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)
// stackalloc X[new(), new()];
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "X").WithArguments("X").WithLocation(1, 12),
// (1,12): error CS1575: A stackalloc expression requires [] after type
// stackalloc X[new(), new()];
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "X[new(), new()]").WithLocation(1, 12));

var tree = comp.SyntaxTrees[0];
var model = comp.GetSemanticModel(tree);
var operation = model.GetOperation(tree.GetRoot());
var actualOperationTree = GetOperationTreeForTest(comp, operation);
OperationTreeVerifier.Verify("""
IMethodBodyOperation (OperationKind.MethodBody, Type: null, IsInvalid) (Syntax: 'stackalloc ... (), new()];')
BlockBody:
IBlockOperation (1 statements) (OperationKind.Block, Type: null, IsInvalid, IsImplicit) (Syntax: 'stackalloc ... (), new()];')
IExpressionStatementOperation (OperationKind.ExpressionStatement, Type: null, IsInvalid) (Syntax: 'stackalloc ... (), new()];')
Expression:
IInvalidOperation (OperationKind.Invalid, Type: X*, IsInvalid) (Syntax: 'stackalloc ... w(), new()]')
Children(2):
IInvalidOperation (OperationKind.Invalid, Type: ?, IsInvalid) (Syntax: 'new()')
Children(0)
IInvalidOperation (OperationKind.Invalid, Type: ?, IsInvalid) (Syntax: 'new()')
Children(0)
ExpressionBody:
null
""",
actualOperationTree);
}

[WorkItem("https://github.com/dotnet/roslyn/issues/72448")]
[Fact]
public void UnconvertedExpression_02()
{
string source = """
object x;
stackalloc X[0, new(out x)];
object y = x;
""";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net70);
comp.VerifyDiagnostics(
// (2,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
// stackalloc X[0, new(out x)];
Diagnostic(ErrorCode.ERR_IllegalStatement, "stackalloc X[0, new(out x)]").WithLocation(2, 1),
// (2,12): error CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)
// stackalloc X[0, new(out x)];
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "X").WithArguments("X").WithLocation(2, 12),
// (2,12): error CS1575: A stackalloc expression requires [] after type
// stackalloc X[0, new(out x)];
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, "X[0, new(out x)]").WithLocation(2, 12),
// (2,25): error CS0165: Use of unassigned local variable 'x'
// stackalloc X[0, new(out x)];
Diagnostic(ErrorCode.ERR_UseDefViolation, "x").WithArguments("x").WithLocation(2, 25));
}

[WorkItem("https://github.com/dotnet/roslyn/issues/72448")]
[Theory]
[InlineData("default")]
[InlineData("(default, 1)")]
[InlineData("[]")]
[InlineData("$\"str\"")]
[InlineData("args.Length > 0 ? default : default")]
[InlineData("args.Length > 0 switch { true => default, false => default }")]
public void UnconvertedExpression_03(string expr)
{
string source = $$"""
stackalloc X[0, {{expr}}];
""";
var comp = CreateCompilation(source, targetFramework: TargetFramework.Net70);
comp.VerifyDiagnostics(
// (1,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
// stackalloc X[0, default];
Diagnostic(ErrorCode.ERR_IllegalStatement, $"stackalloc X[0, {expr}]").WithLocation(1, 1),
// (1,12): error CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)
// stackalloc X[0, default];
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "X").WithArguments("X").WithLocation(1, 12),
// (1,12): error CS1575: A stackalloc expression requires [] after type
// stackalloc X[0, default];
Diagnostic(ErrorCode.ERR_BadStackAllocExpr, $"X[0, {expr}]").WithLocation(1, 12));
}
}
}