-
Notifications
You must be signed in to change notification settings - Fork 889
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
Don't remove const generic when using #[feature(generic_const_items)]
#5996
base: master
Are you sure you want to change the base?
Conversation
Fixes 5995 Added support for rewriting generics on const items.
Could you add a test for where-clauses, too? That would be great! Thanks in advance :) |
@fmease happy to add the test case, but would you mind providing the code snippet. I'm not as familiar with this feature as you are. |
Sure, for example: pub const K<T>: Option<T> = None
where
String: From<T>; And: pub trait Trait<T: ?Sized> {
const C<'a>: &'a T
where
T: 'a + Eq;
} |
Note that generic const items don't have an entry inside the Style Guide yet as you might or might not have noticed. However, the formatting should basically follow the new1 formatting of type aliases and associated types (except that they don't need to deal with legacy-style leading where clauses, cc #5887). Footnotes |
Looks like there's still a little work to be done. Formatting with this branch on the snippets you provided removes the where clauses: input: pub const K<T>: Option<T> = None
where
String: From<T>;
pub trait Trait<T: ?Sized> {
const C<'a>: &'a T
where
T: 'a + Eq;
} output: pub const K<T>: Option<T> = None;
pub trait Trait<T: ?Sized> {
const C<'a>: &'a T;
} |
Yeah, seems to need |
Just double checking, but where clauses are new to const items now that they can be generic with the |
Yes, they are part of |
Note to self, unlike the deprecated type alias syntax, it's not possible to place the where clause before the assignment. I put together this small snippet that compiles: #![feature(generic_const_items)]
pub const K<T>: Option<T> = None
where
String: From<T>,
T: std::fmt::Debug;
#[derive(Debug)]
struct ToString;
impl From<ToString> for String {
fn from(_value: ToString) -> Self {
String::new()
}
}
fn main() {
println!("{:?}", K::<ToString>);
} But when I switch the order of the where clause and the assignment I get the following compilation errors: #![feature(generic_const_items)]
pub const K<T>: Option<T>
where
String: From<T>,
T: std::fmt::Debug,
= None;
#[derive(Debug)]
struct ToString;
impl From<ToString> for String {
fn from(_value: ToString) -> Self {
String::new()
}
}
fn main() {
println!("{:?}", K::<ToString>);
}
|
Is there a plan for this to be merged? I've just hit this case myself. |
@zesterer I haven't had a chance to revisit this and add the code needed to retaining the where clause. In the meantime you might work around the issue by adding a |
Fixes #5995
Added support for rewriting generics on const items.