Skip to content
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

Cannot use _ for const arguments #70754

Closed
jonhoo opened this issue Apr 4, 2020 · 6 comments
Closed

Cannot use _ for const arguments #70754

jonhoo opened this issue Apr 4, 2020 · 6 comments
Labels
A-const-generics Area: const generics (parameters and arguments) A-inference Area: Type inference A-parser Area: The parsing of Rust source code to an AST A-typesystem Area: The type system C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Apr 4, 2020

I tried this code:

#![feature(const_generics)]

struct Foo<const N: usize>([(); N]);

fn main() {
    let x: Foo<_> = Foo([]);
}

I only half-expected the code to compile, given where const generics is at (is inference of const generic arguments even intended to eventually work?), but the resulting error was pretty jarring:

error[E0107]: wrong number of const arguments: expected 1, found 0
 --> src/main.rs:6:12
  |
6 |     let x: Foo<_> = Foo([]);
  |            ^^^^^^ expected 1 const argument

error[E0107]: wrong number of type arguments: expected 0, found 1
 --> src/main.rs:6:16
  |
6 |     let x: Foo<_> = Foo([]);
  |                ^ unexpected type argument

Meta

rustc --version --verbose:

1.44.0-nightly (2020-04-02 537ccdf3ac44c8c7a8d3)
@jonhoo jonhoo added the C-bug Category: This is a bug. label Apr 4, 2020
@Centril Centril added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. A-inference Area: Type inference A-parser Area: The parsing of Rust source code to an AST A-typesystem Area: The type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` and removed C-bug Category: This is a bug. labels Apr 4, 2020
@Centril
Copy link
Contributor

Centril commented Apr 4, 2020

[...] but the resulting error was pretty jarring:

The error is technically correct, but confusing indeed. What happens here is that (as I noted in the other issue you filed, #70753) _ is not interpreted as an expression, so it is interpreted as a type. Since you have a type inference variable, you get an error about wanting a const argument but having a type.

Instead, we would want to interpret _ in this case as a const inference variable. To do so, we would need to delay disambiguation until type checking by adding a variant ast::GenericArg::Infer and handle _ specially in the parser. This seems quite necessary to me.

cc the usual suspects, @varkor @petrochenkov @eddyb

@jonhoo
Copy link
Contributor Author

jonhoo commented Apr 4, 2020

Yeah, when I initially typed the issue up, I wrote "which, while technically accurate, is pretty jarring", but then decided it wasn't necessary :p

@lcnr
Copy link
Contributor

lcnr commented Apr 8, 2020

Handling it in the ast is not actually enough afaict as we require type inference to know what kind of arg is required. So we probably have to update hir::GenericArg as well.

see #66615 (comment)

@varkor varkor changed the title _ in const generic position gives weird errors Cannot use _ for const arguments Dec 5, 2020
@jhpratt
Copy link
Member

jhpratt commented Dec 19, 2020

@Centril Foo<const _> would be unambiguous, correct? I'm not sure I particularly like it, but I think it would work. I ran into this when doing some impls, where it would actually be unambiguous regardless.

@varkor
Copy link
Member

varkor commented Dec 19, 2020

Foo<_> is unambiguous too and doesn't involve new syntax. It's just that it'll take a little bit more implementation work.

@lcnr
Copy link
Contributor

lcnr commented Jun 28, 2022

implemented under feature(generic_arg_infer), tracked in #85077 (comment).

Closing this issue

@lcnr lcnr closed this as completed Jun 28, 2022
@lcnr lcnr removed the F-const_generics `#![feature(const_generics)]` label Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-inference Area: Type inference A-parser Area: The parsing of Rust source code to an AST A-typesystem Area: The type system C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants