Skip to content

Don't remove const generic when using #[feature(generic_const_items)]#5996

Open
ytmimi wants to merge 1 commit into
rust-lang:mainfrom
ytmimi:issue_5995
Open

Don't remove const generic when using #[feature(generic_const_items)]#5996
ytmimi wants to merge 1 commit into
rust-lang:mainfrom
ytmimi:issue_5995

Conversation

@ytmimi

@ytmimi ytmimi commented Dec 29, 2023

Copy link
Copy Markdown
Contributor

Fixes #5995

Added support for rewriting generics on const items.

Fixes 5995

Added support for rewriting generics on const items.
@fmease

fmease commented Feb 8, 2024

Copy link
Copy Markdown
Member

Could you add a test for where-clauses, too? That would be great! Thanks in advance :)

@ytmimi

ytmimi commented Feb 8, 2024

Copy link
Copy Markdown
Contributor Author

@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.

@fmease

fmease commented Feb 8, 2024

Copy link
Copy Markdown
Member

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;
}

@fmease

fmease commented Feb 8, 2024

Copy link
Copy Markdown
Member

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

  1. Proposal: Change syntax of where clauses on type aliases rust#89122

@ytmimi

ytmimi commented Feb 9, 2024

Copy link
Copy Markdown
Contributor Author

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;
}

@fmease

fmease commented Feb 9, 2024

Copy link
Copy Markdown
Member

Yeah, seems to need rewrite_where_clause somewhere.

@ytmimi

ytmimi commented Feb 9, 2024

Copy link
Copy Markdown
Contributor Author

Just double checking, but where clauses are new to const items now that they can be generic with the #[feature(generic_const_items)] , right?

@fmease

fmease commented Feb 9, 2024

Copy link
Copy Markdown
Member

Yes, they are part of generic_const_items and don't exist outside of the feature.

@ytmimi

ytmimi commented Feb 17, 2024

Copy link
Copy Markdown
Contributor Author

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>);
}
error: where clauses are not allowed before const item bodies
 --> example.rs:5:1
  |
4 |   pub const K<T>: Option<T>
  |             - while parsing this const item
5 | / where
6 | |     String: From<T>,
7 | |     T: std::fmt::Debug,
  | |_______________________^ unexpected where clause
8 |   = None;
  |     ---- the item body
  |
help: move the body before the where clause
  |
5 ~ = None where
6 |     String: From<T>,
7 ~     T: std::fmt::Debug,;
  |

error: aborting due to 1 previous error

@zesterer

zesterer commented Sep 5, 2024

Copy link
Copy Markdown

Is there a plan for this to be merged? I've just hit this case myself.

@ytmimi

ytmimi commented Sep 5, 2024

Copy link
Copy Markdown
Contributor Author

@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 #[rustfmt::skip] annotation to the generic const.

@jieyouxu jieyouxu added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed pr-not-reviewed labels Feb 23, 2026
@rustbot

rustbot commented Mar 13, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #6823) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

Status: Abandoned

Development

Successfully merging this pull request may close these issues.

generic_const_items syntax gets deleted

5 participants