swiftlang/swift-syntax

Any word inside a class, struct or enum without the keyword func is considered as FunctionDecl

Closed this issue · 8 comments

Why when we type any word inside a class, struct or enum without the keyword func is considered as FunctionDecl?

///Bug
class MyClass {
  word1 //FunctionDecl
}

enum MyEnum {
  word2 //FunctionDecl
}

struct MyStruct {
  word3 //FunctionDecl
}

///Normal:
class MyClass {
  func word1 //FunctionDecl
}

enum MyEnum {
  func word2 //FunctionDecl
}

struct MyStruct {
  func word3 //FunctionDecl
}

I transferred this to swift-syntax, since it's a parser question.

Tracked in Apple’s issue tracker as rdar://114559579

This is the parser recovery kicking in. We want to represent the code as something and we judge that a function declaration is the most likely, since functions are the most comment members of a type. What would you have expected?

This is the parser recovery kicking in. We want to represent the code as something and we judge that a function declaration is the most likely, since functions are the most comment members of a type. What would you have expected?

I think that just writing the word should not give priority to any possibility, until there are some conditions that support this possibility, such as the presence of one parenthese after the word or the presence of the func keyword before it. Until these conditions exist, it can be considered as an UnknownExpr.

From here let me ask the same question to the word we write outside of class, structure or enumeration. Why is it considered an IdentifierExpr and not a FunctionDecl?

I think that just writing the word should not give priority to any possibility, until there are some conditions that support this possibility, such as the presence of one parenthese after the word or the presence of the func keyword before it. Until these conditions exist, it can be considered as an UnknownExpr.

OK, I see a point that we should parse this as MissingDeclSyntax, making word1 an unexpected token within that node.

From here let me ask the same question to the word we write outside of class, structure or enumeration. Why is it considered an IdentifierExpr and not a FunctionDecl?

That’s because a word at the top level of a function is actually syntactically valid if you file allows top-level code. For example

let someWord = 1
someWord

is a valid Swift program.

@ahoppen
I thought this was interesting and tried to resolve some of the concerns in this issue here: #2149

It basically eliminates the problem in this issue, and only parses a declaration with function-related tokens as FuncDecl.
Would changing the current parser behavior and the diagnostics produced be an okay-direction to go?

I think the change in the PR would be more acceptable if it comes with better diagnostics that suggest an addition of func or var, but what do you think?

Thanks for opening the PR @mininny. I just commented on it and the changes look good to me as-is 👍🏽

Done in #2149.