-
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
TypeId doesn't match itself in generic function. #73976
Comments
#![feature(const_type_id)]
#![feature(core_intrinsics)]
pub struct GetTypeId<T>(T);
impl<T: 'static> GetTypeId<T> {
pub const VALUE: u64 = std::intrinsics::type_id::<T>();
}
const fn check_type_id<T: 'static>() -> bool {
matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
}
fn main() {
assert!(check_type_id::<usize>());
} Seems something wrong with the intrinsics evaluation. |
I think the issue is that TyKind::Param is in place when we try to hash |
Another example showing this is pre-monomorphization: #![feature(const_type_id)]
#![feature(core_intrinsics)]
struct X([u8; std::intrinsics::type_id::<Self>() as usize]);
fn main() {
let _ = std::mem::MaybeUninit::<X>::uninit();
} rustc happily compiles this, but I would expect something like |
I was trying to do something somewhat similar with const type_id when I filed #72602. There seem to be some inconsistencies in what combinations of generics and patterns are allowed, which might be contributing to the problem. |
I have no idea what is going on, but it certainly looks odd... Cc @rust-lang/wg-const-eval @eddyb @nikomatsakis |
needs_subst would make sense there, too I'd think).
|
@oli-obk I wonder if instead of You said "assert" but I think the type is already monomorphized and what we need to do is return |
oh right, this may be an evaluation happening too early, so |
Doesn't that mean that the code samples above will be forbidden? |
No, just evaluating the consts early will fail. Once everything is monomorphized it will work. |
I have an attempt here: https://github.com/nbdd0121/rust/tree/issue-73976. It prevents OP's snippet from compiling:
|
hmm... maybe const prop is interfering here. Can you try running the same build with |
|
Ah, so this is a different issue. It's the same issue (even if a different symptom) of running struct Foo<T>(T);
impl<T> Foo<T> {
const BAR: usize = std::mem::size_of::<T>();
}
fn muh<T>() {
match 42 {
Foo::<T>::BAR => {}
_ => {}
}
} |
Patterns need to be monomorphic, you can never have a pattern that depends on generic parameters. We may lift this restriction in the future, but it's a separate issue and needs a language RFC afaik |
This gives:
in stable and gives
in nightly. We will need a better diagnostics for the TooGeneric error. |
struct Foo<T>(T);
impl<T> Foo<T> {
const BAR: usize = 42;
}
fn muh<T>() {
match 42 {
Foo::<T>::BAR => {}
_ => {}
}
} works on stable, so the old diagnostic was wrong. It's specifically associated constants that depend on generic parameters. |
@KodrAus The thing I worried is that the error |
Yea, cleaning up the diagnostics can be done after fixing the bug |
Guard against non-monomorphized type_id intrinsic call This PR checks whether the type is sufficient monomorphized when calling type_id or type_name intrinsics. If the type is not sufficiently monomorphized, e.g. used in a pattern, the code will be rejected. Fixes rust-lang#73976
Guard against non-monomorphized type_id intrinsic call This PR checks whether the type is sufficient monomorphized when calling type_id or type_name intrinsics. If the type is not sufficiently monomorphized, e.g. used in a pattern, the code will be rejected. Fixes rust-lang#73976
Guard against non-monomorphized type_id intrinsic call This PR checks whether the type is sufficient monomorphized when calling type_id or type_name intrinsics. If the type is not sufficiently monomorphized, e.g. used in a pattern, the code will be rejected. Fixes rust-lang#73976
Guard against non-monomorphized type_id intrinsic call This PR checks whether the type is sufficient monomorphized when calling type_id or type_name intrinsics. If the type is not sufficiently monomorphized, e.g. used in a pattern, the code will be rejected. Fixes rust-lang#73976
Guard against non-monomorphized type_id intrinsic call This PR checks whether the type is sufficient monomorphized when calling type_id or type_name intrinsics. If the type is not sufficiently monomorphized, e.g. used in a pattern, the code will be rejected. Fixes rust-lang#73976
Guard against non-monomorphized type_id intrinsic call This PR checks whether the type is sufficient monomorphized when calling type_id or type_name intrinsics. If the type is not sufficiently monomorphized, e.g. used in a pattern, the code will be rejected. Fixes rust-lang#73976
Improve diagnostics when constant pattern is too generic This PR is a follow-up to PR rust-lang#74538 and issue rust-lang#73976 When constants queries Layout, TypeId or type_name of a generic parameter, instead of emitting `could not evaluate constant pattern`, we will instead emit a more detailed message `constant pattern depends on a generic parameter`.
I tried this code:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=57bb53139f14a69326e005ea9d9f7cd7
I expected the code to run without errors
Instead, the assertion failed.
Meta
Compiler version: 1.46.0-nightly (2020-07-01 f781bab)
The text was updated successfully, but these errors were encountered: