Skip to content

There's currently no way to specify bounds requiring constants in types to be well-formed #68436

Closed
@varkor

Description

@varkor

When we have a const expression with a generic parameter in it, we cannot be sure that the const expression is valid for all the values that the generic parameter might take.

fn foo<const N: usize>() {
	let _: [u8; { /* some expression involving N */ }] = ...;
	// ^ this type may not be well-formed, because the length may result in an error
}

If we do not force the caller of the function to specify that the expression must be well-formed, we may get post-monomorphisation errors, as we can only determine that the expression is well-formed after substituting the concrete values for the generic parameters. We therefore need some way to specify such a bound. This is currently not possible.

This was discussed in #66962 (comment), #66962 (comment), #68388 (comment).

@eddyb suggested the syntax where [u8; { /* some expression involving N */ }]:, though it is not clear whether this would be appropriate. This needs some discussion and careful consideration of the WF implications.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-trait-systemArea: Trait systemF-const_generics`#![feature(const_generics)]`T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions