Skip to content

Document inferred const args (feature(generic_arg_infer)) #1835

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/items/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,28 @@ fn generic<const B: bool>() {
}
```

r[items.generics.const.inferred]

r[items.generics.const.inferred.syntax]
```grammar,const
InferredConst ->
`_`
| `(` InferredConst `)`
```
Comment on lines +233 to +237
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I fully understand this grammar since it isn't referred to anywhere.

Am I understanding correctly that this is added for GenericArgsConst? Or is it somewhere else? That is, should it be:

GenericArgsConst ->
      BlockExpression
    | LiteralExpression
    | `-` LiteralExpression
    | SimplePathSegment
    | InferredConst

InferredConst ->
      `_`
    | `(` InferredConst `)`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep exactly 👍 It's a little bit funny because it means that InferredType and InferredConst share the same grammar but I'm not sure that matters too much for this?


r[items.generics.const.inferred.intro]
The inferred const asks the compiler to infer the const argument if possible
based on the surrounding information available.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a semi-realistic looking example here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah sure


```rust
fn make_buf() -> [u8; 1024] {
[0x1; _]
}
```

r[items.generics.const.inferred.constraint]
It cannot be used in item signatures.
Comment on lines +249 to +250
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite following this. If we are defining this as a new form allowed in generic args, why would there be a rule that it is not allowed in item signatures?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't allow type inference in signatures in general, not just as a restriction on const arguments. For example this is an error on stable:

struct Foo<T>(T);
fn bar(param: Foo<_>) {}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this already stated in type.inferred.constraint (inferred.md)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we state that for _ type arguments but I felt like like we should note that the restriction applies for _ const arguments too when discussing them as it might be confusing otherwise; people might get the idea that you can use _ const arguments in signatures.


r[items.generics.where]
## Where clauses

Expand Down
1 change: 1 addition & 0 deletions src/paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ GenericArgsConst ->
| LiteralExpression
| `-` LiteralExpression
| SimplePathSegment
| InferredConst

GenericArgsBinding ->
IDENTIFIER GenericArgs? `=` Type
Expand Down
Loading