Skip to content

Commit

Permalink
Merge pull request #40 from alex-pinkus/restrict-local-modifiers
Browse files Browse the repository at this point in the history
Add restrictions on modifiers in local scope
  • Loading branch information
alex-pinkus authored Nov 6, 2021
2 parents 4c672b7 + 64db1fd commit c5e610a
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 102 deletions.
71 changes: 38 additions & 33 deletions corpus/classes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal open class Test {

(source_file
(class_declaration
(modifiers (visibility_modifier) (inheritance_modifier))
(modifiers (visibility_modifier) (visibility_modifier))
(type_identifier)
(class_body
(property_declaration
Expand Down Expand Up @@ -159,7 +159,7 @@ class GenericSubscript {
(type_identifier)
(class_body
(subscript_declaration
(modifiers (inheritance_modifier))
(modifiers (visibility_modifier))
(parameter (simple_identifier) (user_type (type_identifier)))
(optional_type (user_type (type_identifier)))
(computed_getter
Expand Down Expand Up @@ -201,40 +201,45 @@ class GenericSubscript {
Subscript with multiple arguments
===

public subscript<D>(_ value: D, arg2: Arg2) -> D where D: Decodable {
return value
class Subscriptable {
public subscript<D>(_ value: D, arg2: Arg2) -> D where D: Decodable {
return value
}
}

---

(source_file
(subscript_declaration
(modifiers
(visibility_modifier))
(type_parameters
(type_parameter
(type_identifier)))
(external_parameter_name)
(parameter
(simple_identifier)
(user_type
(type_identifier)))
(parameter
(simple_identifier)
(user_type
(type_identifier)))
(user_type
(type_identifier))
(type_constraints
(type_constraint
(inheritance_constraint
(identifier
(simple_identifier))
(user_type
(type_identifier)))))
(statements
(control_transfer_statement
(simple_identifier)))))
(source_file
(class_declaration
(type_identifier)
(class_body
(subscript_declaration
(modifiers
(visibility_modifier))
(type_parameters
(type_parameter
(type_identifier)))
(external_parameter_name)
(parameter
(simple_identifier)
(user_type
(type_identifier)))
(parameter
(simple_identifier)
(user_type
(type_identifier)))
(user_type
(type_identifier))
(type_constraints
(type_constraint
(inheritance_constraint
(identifier
(simple_identifier))
(user_type
(type_identifier)))))
(statements
(control_transfer_statement
(simple_identifier)))))))

==================
Inheritance
Expand Down Expand Up @@ -303,7 +308,7 @@ class SomethingElse: ThingProvider {
(type_annotation
(optional_type (user_type (type_identifier)))))
(property_declaration
(modifiers (property_modifier))
(modifiers (property_behavior_modifier))
(value_binding_pattern (non_binding_pattern (simple_identifier)))
(type_annotation
(user_type (type_identifier)))
Expand Down
53 changes: 37 additions & 16 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,19 @@ someVeryLongFunctionCall()
Multiple expressions on one line using semicolons
==================

print(a); return b
print(a); throw b

---

(source_file
(call_expression
(source_file
(call_expression
(simple_identifier)
(call_suffix (value_arguments (value_argument (simple_identifier)))))
(control_transfer_statement (simple_identifier)))
(call_suffix
(value_arguments
(value_argument
(simple_identifier)))))
(throw_keyword)
(simple_identifier))

==================
Indexing
Expand Down Expand Up @@ -230,20 +234,37 @@ let a: SomeClass = .someInstance
Tuple expressions
==================

let a: (Int, Int) = (1, 2)
return (lhs: 3, rhs: 4)
func math() {
let a: (Int, Int) = (1, 2)
return (lhs: 3, rhs: 4)
}

---

(source_file
(property_declaration
(value_binding_pattern (non_binding_pattern (simple_identifier)))
(type_annotation (tuple_type (user_type (type_identifier)) (user_type (type_identifier))))
(tuple_expression (integer_literal) (integer_literal)))
(control_transfer_statement
(tuple_expression
(simple_identifier) (integer_literal)
(simple_identifier) (integer_literal))))
(source_file
(function_declaration
(simple_identifier)
(function_body
(statements
(property_declaration
(value_binding_pattern
(non_binding_pattern
(simple_identifier)))
(type_annotation
(tuple_type
(user_type
(type_identifier))
(user_type
(type_identifier))))
(tuple_expression
(integer_literal)
(integer_literal)))
(control_transfer_statement
(tuple_expression
(simple_identifier)
(integer_literal)
(simple_identifier)
(integer_literal)))))))

==================
Function calls
Expand Down
4 changes: 2 additions & 2 deletions corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ test { [weak self, otherSelf] (a) in }
(simple_identifier)
(call_suffix
(lambda_literal
(capture_list (simple_identifier) (simple_identifier))
(capture_list (ownership_modifier) (simple_identifier) (simple_identifier))
(lambda_function_type (lambda_function_type_parameters (simple_identifier)))))))

==================
Expand Down Expand Up @@ -469,7 +469,7 @@ private lazy var onCatClosure: (_ cat: Cat) throws -> Void = { _ in

(source_file
(property_declaration
(modifiers (visibility_modifier) (property_modifier))
(modifiers (visibility_modifier) (property_behavior_modifier))
(value_binding_pattern (non_binding_pattern (simple_identifier)))
(type_annotation
(function_type
Expand Down
93 changes: 78 additions & 15 deletions corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -356,25 +356,39 @@ someLabel: if a.isEmpty, let b = getB() {
If let with function call
===============

if let a = try? Foo.getValue("key") {
return a
func doSomething() {
if let a = try? Foo.getValue("key") {
return a
}
return default
}
return default

---

(source_file
(if_statement
(value_binding_pattern (non_binding_pattern (simple_identifier)))
(call_expression
(try_expression
(navigation_expression (simple_identifier)
(navigation_suffix (simple_identifier))))
(call_suffix
(value_arguments
(value_argument (line_string_literal)))))
(statements (control_transfer_statement (simple_identifier))))
(control_transfer_statement (simple_identifier)))
(source_file
(function_declaration
(simple_identifier)
(function_body
(statements
(if_statement
(value_binding_pattern
(non_binding_pattern
(simple_identifier)))
(call_expression
(try_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier))))
(call_suffix
(value_arguments
(value_argument
(line_string_literal)))))
(statements
(control_transfer_statement
(simple_identifier))))
(control_transfer_statement
(simple_identifier))))))

==================
Compound if let
Expand Down Expand Up @@ -531,3 +545,52 @@ let ø = unicode()
(simple_identifier)
(call_suffix
(value_arguments)))))

===
Contextual keywords are usable as identifiers
===

public init() {
// `prefix` doesn't work as a function modifier here, so it's legal as an identifier
prefix = prefixToJSON

// But some modifiers are legal!
weak var weakPrefix = prefix
final class LocalClass { }

// Annotations are legal too!
@someAnnotation
func innerFunc() { }
}

---

(source_file
(function_declaration
(modifiers
(visibility_modifier))
(function_body
(comment)
(statements
(assignment
(directly_assignable_expression
(simple_identifier))
(simple_identifier))
(comment)
(property_declaration
(ownership_modifier)
(value_binding_pattern
(non_binding_pattern
(simple_identifier)))
(simple_identifier))
(class_declaration
(inheritance_modifier)
(type_identifier)
(class_body))
(comment)
(function_declaration
(attribute
(user_type
(type_identifier)))
(simple_identifier)
(function_body))))))
Loading

0 comments on commit c5e610a

Please sign in to comment.