-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
No way to get compile-time info from the type of local. #88531
Comments
The more 'obvious' alternative is const fn bla<T>() -> usize { type_name::<T>().len() }
fn z(_: &'static [usize]) {}
fn f() {
let x = read_integer_or_whatever();
z(&[bla::<typeof(x)>()]);
} But I've been told that |
You can kind of emulate #![feature(const_type_name)]
#![feature(type_alias_impl_trait)]
use core::any::type_name;
fn read_integer_or_whatever() -> i32 { todo!() }
const fn bla<T>() -> usize { type_name::<T>().len() }
fn z(_: &'static [usize]) {}
fn f() {
let x = read_integer_or_whatever();
// Here's where the magic happens:
type TypeOfX = impl Sized;
if false {
let _def_use = move || -> TypeOfX { x };
loop {}
}
// Note that `z(&[bla::<i32>()])` also doesn't work because
// calls to const fns are not static promoted
const B: usize = bla::<TypeOfX>();
z(&[B]);
} |
As far as I know, there's no equivalent of this with TAIT: let foo = 0i32;
let bar: typeof foo = 100;
[foo, bar]; |
So apparently with trait solver next globally #107374 #![feature(type_alias_impl_trait)]
#![allow(unused)]
struct Foo {
x: u32,
y: u32,
}
fn main() {
let x = Foo { x: 1, y: 2 };
type TypeOfX = impl Sized;
if false {
let _def_use = move || -> TypeOfX { x };
loop {}
}
let c = TypeOfX { x: 1, y: 2 };
}
And I think this is a feature, not a bug. Found this based on this doc: https://hackmd.io/@impl-trait-everywhere/S1lO4jy86 |
This doesn't work. There's no way to express here that the value of
x
is entirely irrelevant, and thatbla
only cares about the type of the argument.If we had a magic
PhantomData::of(x)
that would work in const contexts regardless of whetherx
itself is const, we could do things like this:The text was updated successfully, but these errors were encountered: