Skip to content

Permit multi-segment paths as const generic arguments without {} #77773

New issue

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

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

Already on GitHub? # to your account

Open
varkor opened this issue Oct 9, 2020 · 6 comments
Open

Permit multi-segment paths as const generic arguments without {} #77773

varkor opened this issue Oct 9, 2020 · 6 comments
Labels
A-const-generics Area: const generics (parameters and arguments) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@varkor
Copy link
Member

varkor commented Oct 9, 2020

mod m {
    pub const N: u32 = 0;
}

struct S<const X: u32>;

fn main() {
    let _ = S::<m::N>; // error[E0573]: expected type, found constant `m::N`
}

(Playground link)

There's no reason this couldn't be supported, but it's nontrivial to implement: we need to wait until type-checking to disambiguate between types and constants due to associated types (see this Zulip conversation).

This is essentially the same issue as #66615, but it's more visible (and affects min_const_generics).

@varkor varkor added 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)]` F-min_const_generics labels Oct 9, 2020
@varkor varkor changed the title Permit multi-segment paths const generic arguments without {} Permit multi-segment paths as const generic arguments without {} Oct 9, 2020
@petrochenkov
Copy link
Contributor

The treatment should be somewhat similar to imports, for every path in a generic argument context resolve should produce a PerNS<Option<Res>> instead of just Option<Res> (i.e. resolve the path in all namespaces).

Then that path is effectively lowered into two hir::Paths, each with its own resolution.
Except that we'll probably won't get away with cloning a path literally (what if it has items inside it? what about node IDs?) and will have to introduce some kind of hir::Path2 with two Resolutions instead.

@varkor
Copy link
Member Author

varkor commented Oct 10, 2020

On one hand, this is an annoying papercut, and essentially a special case that users would have to learn. On the other hand, it's quite a lot of complexity for a small issue. (Though if we end up fixing this issue in the future, after min_const_generics has been stabilised, that could be annoying too, because we'd want to warn about unnecessary {} at some point.)

@leonardo-m
Copy link

Generally special cases in both a compiler and a language should be avoided. Writing S::<{m::N}> isn't too much bad, but the syntax here is already a special case compared to the second part of the [T; N] array syntax...

@RalfJung
Copy link
Member

If it is only the syntax concern that stops this, then why is S::<{m::N}> not part of min_const_generics?

@varkor
Copy link
Member Author

varkor commented Jun 6, 2021

If it is only the syntax concern that stops this, then why is S::<{m::N}> not part of min_const_generics?

{N} is special-cased to be treated as N in min_const_generics, which is not the same as the expression N (you can see this by writing {{N}}, which is not special-cased). This issue is about also special-casing m::N. We'd like to do this, but there are implementation difficulties. In const_generics, {m::N} is just treated as an expression, but we wanted to only allow the special cases in min_const_generics, because the implementation of const generics with arbitrary expressions was still buggy.

@hydra
Copy link

hydra commented Feb 2, 2023

I bumped into this again today after using a constant from another crate via a fully qualified name as an argument to a generic struct, and would suggest that the compiler error message be improved in the interim.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 25, 2024
…compiler-errors

Don't call `walk_` functions directly if there is an equivalent `visit_` method

I was working on rust-lang#77773 and realized in one of my experiments that the `visit_path` method was not always called whereas it should have. This fixes it.

r? `@davidtwco`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 25, 2024
…compiler-errors

Don't call `walk_` functions directly if there is an equivalent `visit_` method

I was working on rust-lang#77773 and realized in one of my experiments that the `visit_path` method was not always called whereas it should have. This fixes it.

r? ``@davidtwco``
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 26, 2024
Rollup merge of rust-lang#120316 - GuillaumeGomez:fix-ast-visitor, r=compiler-errors

Don't call `walk_` functions directly if there is an equivalent `visit_` method

I was working on rust-lang#77773 and realized in one of my experiments that the `visit_path` method was not always called whereas it should have. This fixes it.

r? ``@davidtwco``
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-const-generics Area: const generics (parameters and arguments) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants