Skip to content

Method grammar incomplete/wrong #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gomain opened this issue Dec 10, 2020 · 2 comments
Closed

Method grammar incomplete/wrong #910

gomain opened this issue Dec 10, 2020 · 2 comments

Comments

@gomain
Copy link

gomain commented Dec 10, 2020

The section on methods states this grammar:

Method :
   [FunctionQualifiers] fn [IDENTIFIER] [Generics]?
      ( SelfParam (, [FunctionParam])* ,? )
      [FunctionReturnType]? [WhereClause]?
      [BlockExpression]

SelfParam :
   [OuterAttribute]* ( ShorthandSelf | TypedSelf )

ShorthandSelf :
   (& | & [Lifetime])? mut? self

TypedSelf :
   mut? self : [Type]

and then elaborates that [Type] is limited to the following..

P = &'lt S | &'lt mut S | Box<S> | Rc<S> | Arc<S> | Pin<P>
S = Self | P

My thoughts... Why use, the nonterminal, Type in the rule of TypedSelf? It is clear that only a small subset of Type is allowed here. Suggestion...

TypedSelf :
   mut? self : [SelfType]
SelfType :
   (& | & [Lifetime])? mut? [Self] | Box<[Self]> | Rc<[Self]> | Arc<[Self]> | Pin<[SelfType]>
Self :
   Self | [SelfType]

then in the example there is this

impl Example {
    fn via_projection(self: <Example as Trait>::Output) {}
}

which was not mentioned in the grammar nor is accepted syntax. Should be removed?

@ehuss
Copy link
Contributor

ehuss commented Dec 10, 2020

The grammar describes the syntax of the language, which corresponds to how it is parsed into an AST. This is significant because macros work with the AST. Semantic validation is done later, such as validating the self type. In this example, a macro could accept any type in that position, and that would be valid Rust, such as:

#[cfg(FALSE)]
fn foo(self: ArbitraryThing) {}

@ehuss
Copy link
Contributor

ehuss commented Jan 1, 2021

I'm going to close this, as the grammar is defined as intended. The reference could definitely make this a little clearer, and I opened #916 to track that.

@ehuss ehuss closed this as completed Jan 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants