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

Wrong suggestion for const well-formedness of bool #122395

Closed
slanterns opened this issue Mar 12, 2024 · 3 comments · Fixed by #122802
Closed

Wrong suggestion for const well-formedness of bool #122395

slanterns opened this issue Mar 12, 2024 · 3 comments · Fixed by #122802
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. F-generic_const_exprs `#![feature(generic_const_exprs)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@slanterns
Copy link
Contributor

slanterns commented Mar 12, 2024

Code

#![feature(generic_const_exprs)]

fn bb<const N: bool>() {}

fn b<const N: bool>() {
    bb::<{!N}>();
}

fn main() {}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=6c33a1495d719fbc2d8efe30177c3ca4

Current output

help: try adding a `where` bound using this expression: `where [(); {!N}]:`

Desired output

`where [(); !N as usize]:`

Rationale and extra context

The well-formedness shouldn't be written as where [(); {!N}]: when !N is a bool, which doesn't compile. Should cast it to an integral type.

Other cases

No response

Rust Version

1.78.0-nightly (2024-03-11 4a0cc881dcc4d800f106)

Anything else?

No response

@slanterns slanterns added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 12, 2024
@slanterns

This comment was marked as resolved.

@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2024

Error: Label const-generics-bad-diagnostics can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@slanterns

This comment was marked as resolved.

@rustbot rustbot added A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Mar 12, 2024
@compiler-errors compiler-errors added the const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. label Mar 12, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 25, 2024
…st, r=Nadrieril

Provide structured suggestion for unconstrained generic constant

```
error: unconstrained generic constant
  --> $DIR/const-argument-if-length.rs:18:10
   |
LL |     pad: [u8; is_zst::<T>()],
   |          ^^^^^^^^^^^^^^^^^^^
   |
help: try adding a `where` bound
   |
LL | pub struct AtLeastByte<T: ?Sized> where [(); is_zst::<T>()]: {
   |                                   ++++++++++++++++++++++++++
```

Detect when the constant expression isn't `usize` and suggest casting:

```
error: unconstrained generic constant
 --> f300.rs:6:10
  |
6 |     bb::<{!N}>();
  |          ^^^^
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs:3539:36
  |
help: try adding a `where` bound
  |
5 | fn b<const N: bool>() where [(); {!N} as usize]: {
  |                       ++++++++++++++++++++++++++
```

Fix rust-lang#122395.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 25, 2024
…st, r=Nadrieril

Provide structured suggestion for unconstrained generic constant

```
error: unconstrained generic constant
  --> $DIR/const-argument-if-length.rs:18:10
   |
LL |     pad: [u8; is_zst::<T>()],
   |          ^^^^^^^^^^^^^^^^^^^
   |
help: try adding a `where` bound
   |
LL | pub struct AtLeastByte<T: ?Sized> where [(); is_zst::<T>()]: {
   |                                   ++++++++++++++++++++++++++
```

Detect when the constant expression isn't `usize` and suggest casting:

```
error: unconstrained generic constant
 --> f300.rs:6:10
  |
6 |     bb::<{!N}>();
  |          ^^^^
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs:3539:36
  |
help: try adding a `where` bound
  |
5 | fn b<const N: bool>() where [(); {!N} as usize]: {
  |                       ++++++++++++++++++++++++++
```

Fix rust-lang#122395.
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 25, 2024
…, r=Nadrieril

Provide structured suggestion for unconstrained generic constant

```
error: unconstrained generic constant
  --> $DIR/const-argument-if-length.rs:18:10
   |
LL |     pad: [u8; is_zst::<T>()],
   |          ^^^^^^^^^^^^^^^^^^^
   |
help: try adding a `where` bound
   |
LL | pub struct AtLeastByte<T: ?Sized> where [(); is_zst::<T>()]: {
   |                                   ++++++++++++++++++++++++++
```

Detect when the constant expression isn't `usize` and suggest casting:

```
error: unconstrained generic constant
 --> f300.rs:6:10
  |
6 |     bb::<{!N}>();
  |          ^^^^
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs:3539:36
  |
help: try adding a `where` bound
  |
5 | fn b<const N: bool>() where [(); {!N} as usize]: {
  |                       ++++++++++++++++++++++++++
```

Fix rust-lang#122395.
@bors bors closed this as completed in 6b24fdf Mar 25, 2024
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-diagnostics Area: Messages for errors, warnings, and lints const-generics-bad-diagnostics An error is correctly emitted, but is confusing, for `min_const_generics`. F-generic_const_exprs `#![feature(generic_const_exprs)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants