Skip to content

Commit

Permalink
[mcs] C#7 throw expression
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-safar committed Jun 5, 2017
1 parent ed9fb93 commit 51b93b6
Show file tree
Hide file tree
Showing 27 changed files with 434 additions and 34 deletions.
10 changes: 10 additions & 0 deletions mcs/errors/cs0019-72.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// CS0019: Operator `??' cannot be applied to operands of type `void' and `throw expression'
// Line: 20

class C
{
public static void Main ()
{
var s = Main () ?? throw null;
}
}
7 changes: 7 additions & 0 deletions mcs/errors/cs0155-5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// CS0155: The type caught or thrown must be derived from System.Exception
// Line: 9

class X
{
public int Test () => throw "";
}
19 changes: 19 additions & 0 deletions mcs/errors/cs0162-21.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// CS0162: Unreachable code detected
// Line: 12
// Compiler options: -warnaserror -warn:2

using System;

class X
{
void Test ()
{
var x = true ? throw new NullReferenceException () : 1;
x = 2;
return;
}

static void Main ()
{
}
}
10 changes: 10 additions & 0 deletions mcs/errors/cs0173-6.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between `throw expression' and `throw expression'
// Line: 8

class C
{
public static void Test (bool b)
{
var s = b ? throw null : throw null;
}
}
16 changes: 16 additions & 0 deletions mcs/errors/cs0411-25.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// CS0411: The type arguments for method `C.Foo<T>(System.Func<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
// Line: 10

using System;

public class C
{
public static void Main ()
{
Foo (() => throw null);
}

static void Foo<T> (Func<T> arg)
{
}
}
18 changes: 18 additions & 0 deletions mcs/errors/cs1061-18.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// CS1061: No overload for method `Call' takes `0' arguments
// Line: 11

using System;

class Program
{
static void Main ()
{
Action<dynamic, object> action = delegate { };
Foo (action).NoDynamicBinding ();
}

static T Foo<T>(Action<T, T> x)
{
throw new NotImplementedException ();
}
}
2 changes: 1 addition & 1 deletion mcs/errors/cs1617.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// CS1617: Invalid -langversion option `ISO'. It must be `ISO-1', `ISO-2', Default or value in range 1 to 6
// CS1617: Invalid -langversion option `ISO'. It must be `ISO-1', `ISO-2', Default or value in range 1 to 7
// Line: 0
// Compiler options: -langversion:ISO
8 changes: 8 additions & 0 deletions mcs/errors/cs1644-51.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// CS1644: Feature `throw expression' cannot be used because it is not part of the C# 6.0 language specification
// Line: 5
// Compiler options: -langversion:6

static class Class
{
int Prop => throw null;
}
12 changes: 12 additions & 0 deletions mcs/errors/cs4001-3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// CS4001: Cannot await `void' expression
// Line: 10

using System;

class A
{
static async void Test ()
{
await Console.WriteLine;
}
}
13 changes: 13 additions & 0 deletions mcs/errors/cs8188.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// CS8188: An expression tree cannot not contain a throw expression
// Line: 11

using System;
using System.Linq.Expressions;

class C
{
public static void Main ()
{
Expression<Func<object>> l = () => throw null;
}
}
3 changes: 2 additions & 1 deletion mcs/mcs/anonymous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,9 +1652,10 @@ public override void FlowAnalysis (FlowAnalysisContext fc)
fc.TryFinally = prev_tf;
}

public override void MarkReachable (Reachability rc)
public override Reachability MarkReachable (Reachability rc)
{
block.MarkReachable (rc);
return rc;
}

public void SetHasThisAccess ()
Expand Down
6 changes: 2 additions & 4 deletions mcs/mcs/assign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,9 @@ public override void FlowAnalysis (FlowAnalysisContext fc)
}
}

public override void MarkReachable (Reachability rc)
public override Reachability MarkReachable (Reachability rc)
{
var es = source as ExpressionStatement;
if (es != null)
es.MarkReachable (rc);
return source.MarkReachable (rc);
}
}

Expand Down
8 changes: 4 additions & 4 deletions mcs/mcs/async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ public override void EmitStatement (EmitContext ec)
stmt.EmitStatement (ec);
}

public override void MarkReachable (Reachability rc)
public override Reachability MarkReachable (Reachability rc)
{
base.MarkReachable (rc);
stmt.MarkReachable (rc);
return stmt.MarkReachable (rc);
}

public override object Accept (StructuralVisitor visitor)
Expand Down Expand Up @@ -514,11 +513,12 @@ public override void EmitStatement (EmitContext ec)
ec.Emit (OpCodes.Ret);
}

public override void MarkReachable (Reachability rc)
public override Reachability MarkReachable (Reachability rc)
{
//
// Reachability has been done in AsyncInitializerStatement
//
return rc;
}
}

Expand Down
9 changes: 8 additions & 1 deletion mcs/mcs/convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ public static bool ImplicitStandardConversionExists (Expression expr, TypeSpec t
if (expr_type == target_type)
return true;

if (expr_type == InternalType.ThrowExpr)
return target_type.Kind != MemberKind.InternalCompilerType;

if (target_type.IsNullableType)
return ImplicitNulableConversion (null, expr, target_type) != null;

Expand Down Expand Up @@ -1370,7 +1373,7 @@ static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr
Expression e;

if (expr_type == target_type) {
if (expr_type != InternalType.NullLiteral && expr_type != InternalType.AnonymousMethod)
if (expr_type != InternalType.NullLiteral && expr_type != InternalType.AnonymousMethod && expr_type != InternalType.ThrowExpr)
return expr;
return null;
}
Expand All @@ -1396,6 +1399,10 @@ static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr
return null;
}

if (expr_type == InternalType.ThrowExpr) {
return target_type.Kind == MemberKind.InternalCompilerType ? null : EmptyCast.Create (expr, target_type);
}

if (target_type.IsNullableType)
return ImplicitNulableConversion (ec, expr, target_type);

Expand Down
18 changes: 16 additions & 2 deletions mcs/mcs/cs-parser.jay
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ namespace Mono.CSharp
%token STRUCT
%token SWITCH
%token THIS
%token THROW
%token THROW
%token TRUE
%token TRY
%token TYPEOF
Expand Down Expand Up @@ -261,6 +261,7 @@ namespace Mono.CSharp
%token WHEN
%token INTERPOLATED_STRING
%token INTERPOLATED_STRING_END
%token THROW_EXPR

/* C# keywords which are not really keywords */
%token GET
Expand Down Expand Up @@ -4310,6 +4311,13 @@ unary_expression

$$ = new Await ((Expression) $2, GetLocation ($1));
}
| THROW_EXPR prefixed_unary_expression
{
if (lang_version < LanguageVersion.V_7)
FeatureIsNotAvailable (lexer.Location, "throw expression");

$$ = new ThrowExpression ((Expression) $2, GetLocation ($1));
}
| BANG error
{
Error_SyntaxError (yyToken);
Expand Down Expand Up @@ -6366,11 +6374,16 @@ return_statement
;

throw_statement
: THROW opt_expression SEMICOLON
: THROW expression SEMICOLON
{
$$ = new Throw ((Expression) $2, GetLocation ($1));
lbag.AddStatement ($$, GetLocation ($3));
}
| THROW SEMICOLON
{
$$ = new Throw (null, GetLocation ($1));
lbag.AddStatement ($$, GetLocation ($2));
}
| THROW expression error
{
Error_SyntaxError (yyToken);
Expand Down Expand Up @@ -8081,6 +8094,7 @@ static string GetTokenName (int token)
case Token.THIS:
return "this";
case Token.THROW:
case Token.THROW_EXPR:
return "throw";
case Token.TRUE:
return "true";
Expand Down
12 changes: 12 additions & 0 deletions mcs/mcs/cs-tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ int GetKeyword (char[] id, int id_len)
if (parsing_block == 0)
res = -1;

break;
case Token.THROW:
switch (current_token) {
case Token.ARROW:
case Token.OP_COALESCING:
case Token.INTERR:
case Token.COLON:
res = Token.THROW_EXPR;
break;
}

break;
}

Expand Down Expand Up @@ -1345,6 +1356,7 @@ int TokenizePossibleNullableType ()
case Token.THIS:
case Token.NEW:
case Token.INTERPOLATED_STRING:
case Token.THROW:
next_token = Token.INTERR;
break;

Expand Down
19 changes: 15 additions & 4 deletions mcs/mcs/ecore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,11 @@ public virtual void FlowAnalysis (FlowAnalysisContext fc)
{
}

public virtual Reachability MarkReachable (Reachability rc)
{
return rc;
}

//
// Special version of flow analysis for expressions which can return different
// on-true and on-false result. Used by &&, ||, ?: expressions
Expand Down Expand Up @@ -1295,10 +1300,6 @@ public virtual object Accept (StructuralVisitor visitor)
/// </summary>
public abstract class ExpressionStatement : Expression
{
public virtual void MarkReachable (Reachability rc)
{
}

public virtual ExpressionStatement ResolveStatement (BlockContext ec)
{
Expression e = Resolve (ec);
Expand Down Expand Up @@ -1460,6 +1461,11 @@ public override SLE.Expression MakeExpression (BuilderContext ctx)
#endif
}

public override Reachability MarkReachable (Reachability rc)
{
return child.MarkReachable (rc);
}

protected override void CloneTo (CloneContext clonectx, Expression t)
{
// Nothing to clone
Expand Down Expand Up @@ -2408,6 +2414,11 @@ public override SLE.Expression MakeExpression (BuilderContext ctx)
{
return orig_expr.MakeExpression (ctx);
}

public override Reachability MarkReachable (Reachability rc)
{
return expr.MarkReachable (rc);
}
}

//
Expand Down
Loading

0 comments on commit 51b93b6

Please sign in to comment.