Skip to content

Suboptimal diagnostic with a missing fn const #89007

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
leonardo-m opened this issue Sep 16, 2021 · 1 comment
Open

Suboptimal diagnostic with a missing fn const #89007

leonardo-m opened this issue Sep 16, 2021 · 1 comment
Labels
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.

Comments

@leonardo-m
Copy link

(This is a spinoff of Issue 88424).
Here the signature of new_unchecked lacks a "const" and the compiler doesn't give me enough help to see/fix the problem:

#![feature(
    const_fn_trait_bound,
    const_panic,
    const_trait_impl,
    generic_const_exprs,
)]

#![allow(incomplete_features)]

pub trait ToFromUsize {
    fn to_usize(self) -> usize;
    fn from_usize(x: usize) -> Self;
    const MAX: Self;
}
impl const ToFromUsize for usize {
    fn to_usize(self) -> usize { self }
    fn from_usize(x: usize) -> Self { x }
    const MAX: Self = Self::MAX;
}

pub const fn assert_nonzero(n: usize) -> usize {
    assert!(n > 0);
    n
}

pub const fn is_contained(n: usize, m: usize) -> usize {
    assert!(n <= m);
    n
}

pub const fn is_representable<Ti: ~const ToFromUsize>(n: usize) -> usize {
    assert!(n <= Ti::MAX.to_usize());
    n
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
#[repr(transparent)]
pub struct Foo<Ti: ToFromUsize + Copy, const N: usize>(Ti)
where [(); assert_nonzero(N)]:,
      [(); is_representable::<Ti>(N - 1)]:;

impl<Ti: ToFromUsize + Copy, const N: usize> Foo<Ti, N>
where [(); assert_nonzero(N)]:,
      [(); is_representable::<Ti>(N - 1)]: {

    pub unsafe fn new_unchecked(i: Ti) -> Self where Ti: ~const ToFromUsize {
        Self(i)
    }
}

fn main() {}

It gives:

error: `~const` is not allowed here
  --> ...\test2.rs:46:58
   |
46 |     pub unsafe fn new_unchecked(i: Ti) -> Self where Ti: ~const ToFromUsize {
   |                                                          ^^^^^^^^^^^^^^^^^^
   |
   = note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
@leonardo-m leonardo-m 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 Sep 16, 2021
@leonardo-m
Copy link
Author

I think this is a related diagnostic issue (there is no mention that Add should be const):

#![feature(
    const_fn_trait_bound,
    const_trait_impl,
    generic_const_exprs,
)]
#![allow(incomplete_features)]

use std::ops::Add;

const fn foo<T>(a: T, b: T) -> T where T: ~const Add<Output=T> {
    a + b
}

fn main() {
    const X: u32 = foo(1_u32, 1_u32);
}

Gives:

error[E0277]: cannot add `u32` to `u32`
  --> ...\test3.rs:15:20
   |
15 |     const X: u32 = foo(1_u32, 1_u32);
   |                    ^^^ no implementation for `u32 + u32`
   |
   = help: the trait `Add` is not implemented for `u32`
note: required by a bound in `foo`
  --> ...\test3.rs:10:43
   |
10 | const fn foo<T>(a: T, b: T) -> T where T: ~const Add<Output=T> {
   |                                           ^^^^^^^^^^^^^^^^^^^^ required by this bound in `foo`
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
14 | fn main() where u32: Add {
   |           ++++++++++++++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

No branches or pull requests

1 participant