Merge pull request #4616 from tamasvajk/feature/csharp9-attribute-local-function

C#: Allow attributes on local functions
This commit is contained in:
Tamás Vajk
2020-11-26 16:04:00 +01:00
committed by GitHub
11 changed files with 3839 additions and 6 deletions

View File

@@ -0,0 +1,2 @@
lgtm,codescanning
* The `LocalFunction` class now extends `Attributable`. This is a C# 9 change.

View File

@@ -10,7 +10,8 @@ private import TypeRef
* An element that can have attributes. Either an assembly (`Assembly`), a field (`Field`),
* a parameter (`Parameter`), an operator (`Operator`), a method (`Method`), a constructor (`Constructor`),
* a destructor (`Destructor`), a callable accessor (`CallableAccessor`), a value or reference type
* (`ValueOrRefType`), or a declaration with accessors (`DeclarationWithAccessors`).
* (`ValueOrRefType`), a declaration with accessors (`DeclarationWithAccessors`), or a local function
* (`LocalFunction`).
*/
class Attributable extends @attributable {
/** Gets an attribute attached to this element, if any. */

View File

@@ -972,7 +972,7 @@ class ExplicitConversionOperator extends ConversionOperator {
* }
* ```
*/
class LocalFunction extends Callable, Modifiable, @local_function {
class LocalFunction extends Callable, Modifiable, Attributable, @local_function {
override string getName() { local_functions(this, result, _, _) }
override LocalFunction getUnboundDeclaration() { local_functions(this, _, _, result) }
@@ -996,4 +996,6 @@ class LocalFunction extends Callable, Modifiable, @local_function {
override Parameter getRawParameter(int i) { result = getParameter(i) }
override string getAPrimaryQlClass() { result = "LocalFunction" }
override string toString() { result = Callable.super.toString() }
}

View File

@@ -227,7 +227,8 @@ tokens(
@external_element = @externalMetric | @externalDefect | @externalDataElement;
@attributable = @assembly | @field | @parameter | @operator | @method | @constructor
| @destructor | @callable_accessor | @value_or_ref_type | @declaration_with_accessors;
| @destructor | @callable_accessor | @value_or_ref_type | @declaration_with_accessors
| @local_function;
/** LOCATIONS, ASEMMBLIES, MODULES, FILES and FOLDERS **/
@@ -986,7 +987,7 @@ case @expr.kind of
| 109 = @local_function_invocation_expr
| 110 = @ref_expr
| 111 = @discard_expr
/* C# 8.0 */
/* C# 8.0 */
| 112 = @range_expr
| 113 = @index_expr
| 114 = @switch_expr

View File

@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
public class Class1
public class LocalFunction
{
public async Task M1()
{
@@ -15,4 +15,15 @@ public class Class1
static extern void localExtern();
}
public void M2()
{
[Obsolete]
int? dup([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] bool b, int? i)
{
return 2 * i;
}
dup(true, 42);
}
}

View File

@@ -6,3 +6,6 @@ localFunctionModifier
| LocalFunction.cs:16:9:16:41 | localExtern | extern |
| LocalFunction.cs:16:9:16:41 | localExtern | private |
| LocalFunction.cs:16:9:16:41 | localExtern | static |
| LocalFunction.cs:21:9:25:9 | dup | private |
localFunctionAttribute
| LocalFunction.cs:21:9:25:9 | dup | LocalFunction.cs:21:10:21:17 | [Obsolete(...)] |

View File

@@ -5,3 +5,5 @@ query predicate noBody(LocalFunction lf) { not lf.hasBody() }
query predicate localFunctionModifier(LocalFunction lf, string modifier) {
lf.hasModifier(modifier)
}
query predicate localFunctionAttribute(LocalFunction lf, Attribute a) { a = lf.getAnAttribute() }

View File

@@ -48,7 +48,7 @@ Discard.cs:
# 10| 0: [ReturnStmt] return ...;
# 10| 0: [IntLiteral] 0
LocalFunction.cs:
# 4| [Class] Class1
# 4| [Class] LocalFunction
# 6| 5: [Method] M1
# 6| -1: [TypeMention] Task
# 7| 4: [BlockStmt] {...}
@@ -77,6 +77,36 @@ LocalFunction.cs:
# 14| 0: [IntLiteral] 2
# 16| 3: [LocalFunctionStmt] localExtern(...)
# 16| 0: [LocalFunction] localExtern
# 19| 6: [Method] M2
# 19| -1: [TypeMention] Void
# 20| 4: [BlockStmt] {...}
# 21| 0: [LocalFunctionStmt] dup(...)
# 21| 0: [LocalFunction] dup
#-----| 0: (Attributes)
# 21| 1: [Attribute] [Obsolete(...)]
# 21| 0: [TypeMention] ObsoleteAttribute
#-----| 2: (Parameters)
# 22| 0: [Parameter] b
# 22| -1: [TypeMention] bool
#-----| 0: (Attributes)
# 22| 1: [Attribute] [NotNullWhen(...)]
# 22| -1: [TypeMention] NotNullWhenAttribute
# 22| 0: [BoolLiteral] true
# 22| 1: [Parameter] i
# 22| -1: [TypeMention] int?
# 22| 1: [TypeMention] int
# 23| 4: [BlockStmt] {...}
# 24| 0: [ReturnStmt] return ...;
# 24| 0: [MulExpr] ... * ...
# 24| 0: [CastExpr] (...) ...
# 24| 1: [IntLiteral] 2
# 24| 1: [ParameterAccess] access to parameter i
# 27| 1: [ExprStmt] ...;
# 27| 0: [LocalFunctionCall] call to local function dup
# 27| -1: [LocalFunctionAccess] access to local function dup
# 27| 0: [BoolLiteral] true
# 27| 1: [CastExpr] (...) ...
# 27| 1: [IntLiteral] 42
NativeInt.cs:
# 3| [Class] NativeInt
# 5| 5: [Method] M1

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,2 @@
description: Added 'local_function' to 'attributable'.
compatibility: backwards