rust-lang / reference

The Rust Reference
https://doc.rust-lang.org/nightly/reference/
Apache License 2.0
1.22k stars 477 forks source link

Type Alias grammar is confusing #1389

Open harudagondi opened 1 year ago

harudagondi commented 1 year ago

At the top, the grammar is written as:

Syntax TypeAlias : type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ;

I assumed this applies to every instance of a type alias, but it's not. For example, type aliases are not allowed to have no type specified when declared as a top level item, but it is when as an associated type in a trait.

This is mentioned in the later paragraphs, however:

A type alias, when not used as an associated type, must include a Type and may not include TypeParamBounds.

A type alias, when used as an associated type in a trait, must not include a Type specification but may include TypeParamBounds.

A type alias, when used as an associated type in a trait impl, must include a Type specification and may not include TypeParamBounds.

This is confusing for me. I think it would be better if there was a syntax box like the one mentioned above for each case where a type alias can be found. So for example, these would be the syntax boxes for each:

Syntax TypeAlias : type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? = Type WhereClause? ;

Syntax TypeAlias : type IDENTIFIER GenericParams? : TypeParamBounds WhereClause? ;

Syntax TypeAlias : type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? = Type WhereClause? ;

gdennie commented 11 months ago

Perhaps they should be called different names: "type alias" and "associated type alias"