Skip to content

Commit cdc829e

Browse files
authored
Avoid crash from Script class instantiation (#79879)
1 parent 6c1b31b commit cdc829e

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5227,16 +5227,14 @@ BoundExpression bindObjectCreationExpression(ObjectCreationExpressionSyntax node
52275227
case TypeKind.TypeParameter:
52285228
return BindTypeParameterCreationExpression(node, (TypeParameterSymbol)type, diagnostics);
52295229

5230-
case TypeKind.Submission:
5231-
// script class is synthesized and should not be used as a type of a new expression:
5232-
throw ExceptionUtilities.UnexpectedValue(type.TypeKind);
5233-
52345230
case TypeKind.Pointer:
52355231
case TypeKind.FunctionPointer:
52365232
type = new ExtendedErrorTypeSymbol(type, LookupResultKind.NotCreatable,
52375233
diagnostics.Add(ErrorCode.ERR_UnsafeTypeInObjectCreation, node.Location, type));
52385234
goto case TypeKind.Class;
52395235

5236+
case TypeKind.Submission:
5237+
// script class is synthesized and should not be used as a type of a new expression:
52405238
case TypeKind.Dynamic:
52415239
// we didn't find any type called "dynamic" so we are using the builtin dynamic type, which has no constructors:
52425240
case TypeKind.Array:

src/Scripting/CSharpTest/ScriptTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,19 @@ public async Task Function_ReturningPartialType_CSharp13()
10951095
Assert.Equal("partial", result.GetType().Name);
10961096
}
10971097

1098+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79146")]
1099+
public void ScriptInstantiation()
1100+
{
1101+
var parseOptions = CSharpParseOptions.Default.WithKind(SourceCodeKind.Script);
1102+
var compilation = CSharpCompilation.CreateScriptCompilation("Test",
1103+
CSharpSyntaxTree.ParseText("var x = new Script();", parseOptions),
1104+
[NetStandard20Ref]);
1105+
compilation.VerifyDiagnostics(
1106+
// (1,13): error CS8386: Invalid object creation
1107+
// var x = new Script();
1108+
Diagnostic(ErrorCode.ERR_InvalidObjectCreation, "Script").WithLocation(1, 13));
1109+
}
1110+
10981111
private class StreamOffsetResolver : SourceReferenceResolver
10991112
{
11001113
public override bool Equals(object other) => ReferenceEquals(this, other);

0 commit comments

Comments
 (0)