-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Limitations #1
Comments
If used outside of a function, a unique label is required. This is a partial solution to issue #1.
This is a partial solution to issue #1.
See rust-lang/rust#54912 for the fix to this issue. With this, we can declare macros like #![feature(underscore_const_names)]
macro_rules! const_assert {
($($xs:expr),+ $(,)*) => {
const _: [(); 0 - !($($xs)&&+) as usize] = [];
};
} |
Another limitation is that the size of a generic type cannot be determined. For example, the following does not work: #[test]
fn test_generic_types() {
fn foo<T, U>(_t: T, _u: U) {
assert_eq_size!(T, U);
}
}
I don't know if it's possible to remove this limitation, but it would be extremely useful to do so. |
@asomers Unfortunately, if we try taking the Say you have the following (test in playground): use std::mem::size_of;
fn test<T, U>() {
// Essentially const_assert!()'s internals
const ASSERT: [
();
0 - !(size_of::<T>() == size_of::<U>()) as usize
] = [];
} This generates the following errors: error[E0401]: can't use type parameters from outer function
--> src/main.rs:7:25
|
3 | fn test<T, U>() {
| ---- - type variable from outer function
| |
| try adding a local type parameter in this method instead
...
7 | 0 - !(size_of::<T>() == size_of::<U>()) as usize
| ^ use of type variable from outer function
error[E0401]: can't use type parameters from outer function
--> src/main.rs:7:43
|
3 | fn test<T, U>() {
| ---- - type variable from outer function
| |
| try adding a local type parameter in this method instead
...
7 | 0 - !(size_of::<T>() == size_of::<U>()) as usize
| ^ use of type variable from outer function I'm not sure if anyone's working to fix this issue. I agree that it would be quite nice to be able to have these assurances in monomorphized functions. |
Interesting. The reference to an "outer function" is what you would expect if What you're doing looks basically like creating a variable-sized array, which of course isn't supported in Rust. I don't have any better ideas about how to do it, though. I suppose you could wrap the generic function in a macro, but that's ugly. |
Not just in an array context; doing this won't work either: const X: usize = size_of::<T>(); |
I found that I can create unique identifiers for macros on stable using procedual macros. Maybe this could help remove some of your limitations? Take a look at this crate for the implementation. |
Is a separate crate still necessary for procedural macros or can they now be written within the same crate in which they're used? Also, I consider bumping the minimum Eventually I want to go down the |
AFAIK you still need a seperate crate, at least for crates with both #[macro_export] and #[proc_macro]. After testing multi-crates it seems like it works, however users of the #[macro_export] library have to explicitly depend on both crates. If not then you get a |
It's not. A |
Why is it still not possible to use this outside of a function even though underscore_const_names has stabilized? |
1.0 is now available! So yes @carbotaniuman now you can use most things outside of the context of a function. |
Currently the following macros are only available from within the context of a function:
assert_eq_size
(commit f3da7e4)forget
,transmute
, anduninitialized
as provided bycore::mem
.transmute
anduninitialized
are marked asunsafe
, which makes it unlikely for them to becomeconst
.const_assert
ifmem::size_of
were aconst fn
. To resolve the limitation,const_assert
would need to be resolved first.const_assert
(commit b7ca44e)const_assert_eq
(commit b7ca44e)const_assert
, so resolving that macro would resolve this one.Commits b7ca44e and f3da7e4 have somewhat resolved this.
The following macros now require labels (identifiers) unique to their namespace to be used in a non-function context:
assert_eq_size
assert_obj_safe
assert_impl
const_assert
const_assert_eq
This, along with having more assertions available (#2), would allow for a 1.0.0 release.
The text was updated successfully, but these errors were encountered: