Auto merge of #143879 - fee1-dead-contrib:push-lrlpoouyqqry, r=fmease

parse `const trait Trait`

r? oli-obk or anyone from project-const-traits

cc `@rust-lang/project-const-traits`
This commit is contained in:
bors 2025-07-17 15:54:33 +00:00
commit 9cd918bcbb
94 changed files with 365 additions and 299 deletions

View file

@ -306,7 +306,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
cur_f = Some(field);
}
},
ItemKind::Trait(is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
ItemKind::Trait(_constness, is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
if self.enable_ordering_for_trait && *is_auto == IsAuto::No =>
{
let mut cur_t: Option<(TraitItemId, Ident)> = None;

View file

@ -740,7 +740,7 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
);
}
},
ItemKind::Trait(_, unsafety, ..) => match (headers.safety, unsafety) {
ItemKind::Trait(_, _, unsafety, ..) => match (headers.safety, unsafety) {
(false, Safety::Unsafe) => span_lint(
cx,
MISSING_SAFETY_DOC,

View file

@ -125,7 +125,7 @@ declare_lint_pass!(LenZero => [LEN_ZERO, LEN_WITHOUT_IS_EMPTY, COMPARISON_TO_EMP
impl<'tcx> LateLintPass<'tcx> for LenZero {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
if let ItemKind::Trait(_, _, ident, _, _, trait_items) = item.kind
if let ItemKind::Trait(_, _, _, ident, _, _, trait_items) = item.kind
&& !item.span.from_expansion()
{
check_trait_items(cx, item, ident, trait_items);

View file

@ -101,7 +101,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
let attrs = cx.tcx.hir_attrs(it.hir_id());
check_missing_inline_attrs(cx, attrs, it.span, desc);
},
hir::ItemKind::Trait(ref _is_auto, ref _unsafe, _ident, _generics, _bounds, trait_items) => {
hir::ItemKind::Trait(ref _constness, ref _is_auto, ref _unsafe, _ident, _generics, _bounds, trait_items) => {
// note: we need to check if the trait is exported so we can't use
// `LateLintPass::check_trait_item` here.
for &tit in trait_items {

View file

@ -112,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
// special handling for self trait bounds as these are not considered generics
// ie. trait Foo: Display {}
if let Item {
kind: ItemKind::Trait(_, _, _, _, bounds, ..),
kind: ItemKind::Trait(_, _, _, _, _, bounds, ..),
..
} = item
{
@ -133,7 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
..
}) = segments.first()
&& let Some(Node::Item(Item {
kind: ItemKind::Trait(_, _, _, _, self_bounds, _),
kind: ItemKind::Trait(_, _, _, _, _, self_bounds, _),
..
})) = cx.tcx.hir_get_if_local(*def_id)
{

View file

@ -131,7 +131,7 @@ impl LateLintPass<'_> for UpperCaseAcronyms {
return;
}
match it.kind {
ItemKind::TyAlias(ident, ..) | ItemKind::Struct(ident, ..) | ItemKind::Trait(_, _, ident, ..) => {
ItemKind::TyAlias(ident, ..) | ItemKind::Struct(ident, ..) | ItemKind::Trait(_, _, _, ident, ..) => {
check_ident(cx, &ident, it.hir_id(), self.upper_case_acronyms_aggressive);
},
ItemKind::Enum(ident, _, ref enumdef) => {

View file

@ -444,6 +444,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
},
(
Trait(box ast::Trait {
constness: lc,
is_auto: la,
safety: lu,
ident: li,
@ -452,6 +453,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
items: lis,
}),
Trait(box ast::Trait {
constness: rc,
is_auto: ra,
safety: ru,
ident: ri,
@ -460,7 +462,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
items: ris,
}),
) => {
la == ra
matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
&& la == ra
&& matches!(lu, Safety::Default) == matches!(ru, Safety::Default)
&& eq_id(*li, *ri)
&& eq_generics(lg, rg)

View file

@ -252,11 +252,11 @@ fn item_search_pat(item: &Item<'_>) -> (Pat, Pat) {
ItemKind::Struct(_, _, VariantData::Struct { .. }) => (Pat::Str("struct"), Pat::Str("}")),
ItemKind::Struct(..) => (Pat::Str("struct"), Pat::Str(";")),
ItemKind::Union(..) => (Pat::Str("union"), Pat::Str("}")),
ItemKind::Trait(_, Safety::Unsafe, ..)
ItemKind::Trait(_, _, Safety::Unsafe, ..)
| ItemKind::Impl(Impl {
safety: Safety::Unsafe, ..
}) => (Pat::Str("unsafe"), Pat::Str("}")),
ItemKind::Trait(IsAuto::Yes, ..) => (Pat::Str("auto"), Pat::Str("}")),
ItemKind::Trait(_, IsAuto::Yes, ..) => (Pat::Str("auto"), Pat::Str("}")),
ItemKind::Trait(..) => (Pat::Str("trait"), Pat::Str("}")),
ItemKind::Impl(_) => (Pat::Str("impl"), Pat::Str("}")),
_ => return (Pat::Str(""), Pat::Str("")),

View file

@ -84,8 +84,7 @@ mod issue14871 {
const ONE: Self;
}
#[const_trait]
pub trait NumberConstants {
pub const trait NumberConstants {
fn constant(value: usize) -> Self;
}

View file

@ -84,8 +84,7 @@ mod issue14871 {
const ONE: Self;
}
#[const_trait]
pub trait NumberConstants {
pub const trait NumberConstants {
fn constant(value: usize) -> Self;
}

View file

@ -3,8 +3,7 @@
// Reduced test case from https://github.com/rust-lang/rust-clippy/issues/14658
#[const_trait]
trait ConstTrait {
const trait ConstTrait {
fn method(self);
}

View file

@ -3,8 +3,7 @@
// Reduced test case from https://github.com/rust-lang/rust-clippy/issues/14658
#[const_trait]
trait ConstTrait {
const trait ConstTrait {
fn method(self);
}

View file

@ -1,5 +1,5 @@
error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/const_trait.rs:24:1
--> tests/ui/missing_const_for_fn/const_trait.rs:23:1
|
LL | / fn can_be_const() {
LL | | 0u64.method();

View file

@ -167,8 +167,7 @@ where
}
// #13476
#[const_trait]
trait ConstTrait {}
const trait ConstTrait {}
const fn const_trait_bounds_good<T: ConstTrait + [const] ConstTrait>() {}
const fn const_trait_bounds_bad<T: [const] ConstTrait>() {}

View file

@ -167,8 +167,7 @@ where
}
// #13476
#[const_trait]
trait ConstTrait {}
const trait ConstTrait {}
const fn const_trait_bounds_good<T: ConstTrait + [const] ConstTrait>() {}
const fn const_trait_bounds_bad<T: [const] ConstTrait + [const] ConstTrait>() {}

View file

@ -59,19 +59,19 @@ LL | fn bad_trait_object(arg0: &(dyn Any + Send + Send)) {
| ^^^^^^^^^^^^^^^^^ help: try: `Any + Send`
error: these bounds contain repeated elements
--> tests/ui/trait_duplication_in_bounds.rs:174:36
--> tests/ui/trait_duplication_in_bounds.rs:173:36
|
LL | const fn const_trait_bounds_bad<T: [const] ConstTrait + [const] ConstTrait>() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[const] ConstTrait`
error: these where clauses contain repeated elements
--> tests/ui/trait_duplication_in_bounds.rs:181:8
--> tests/ui/trait_duplication_in_bounds.rs:180:8
|
LL | T: IntoIterator<Item = U::Owned> + IntoIterator<Item = U::Owned>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `IntoIterator<Item = U::Owned>`
error: these where clauses contain repeated elements
--> tests/ui/trait_duplication_in_bounds.rs:203:8
--> tests/ui/trait_duplication_in_bounds.rs:202:8
|
LL | T: AssocConstTrait<ASSOC = 0> + AssocConstTrait<ASSOC = 0>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `AssocConstTrait<ASSOC = 0>`

View file

@ -1011,8 +1011,7 @@ pub mod ops {
}
#[lang = "add_assign"]
#[const_trait]
pub trait AddAssign<Rhs = Self> {
pub const trait AddAssign<Rhs = Self> {
fn add_assign(&mut self, rhs: Rhs);
}

View file

@ -1172,6 +1172,7 @@ pub(crate) fn format_trait(
unreachable!();
};
let ast::Trait {
constness,
is_auto,
safety,
ident,
@ -1182,7 +1183,8 @@ pub(crate) fn format_trait(
let mut result = String::with_capacity(128);
let header = format!(
"{}{}{}trait ",
"{}{}{}{}trait ",
format_constness(constness),
format_visibility(context, &item.vis),
format_safety(safety),
format_auto(is_auto),