diff --git a/rustfmt-core/rustfmt-lib/src/types.rs b/rustfmt-core/rustfmt-lib/src/types.rs index c05a76bd2b3..bf821455fec 100644 --- a/rustfmt-core/rustfmt-lib/src/types.rs +++ b/rustfmt-core/rustfmt-lib/src/types.rs @@ -523,7 +523,12 @@ impl Rewrite for ast::GenericBound { ast::TraitBoundModifier::Maybe => poly_trait_ref .rewrite(context, shape.offset_left(1)?) .map(|s| format!("?{}", s)), - _ => unimplemented!(), + ast::TraitBoundModifier::MaybeConst => poly_trait_ref + .rewrite(context, shape.offset_left(7)?) + .map(|s| format!("?const {}", s)), + ast::TraitBoundModifier::MaybeConstMaybe => poly_trait_ref + .rewrite(context, shape.offset_left(8)?) + .map(|s| format!("?const ?{}", s)), }; rewrite.map(|s| if has_paren { format!("({})", s) } else { s }) } diff --git a/rustfmt-core/rustfmt-lib/tests/source/type.rs b/rustfmt-core/rustfmt-lib/tests/source/type.rs index 27387c5bdca..eb4600e5bce 100644 --- a/rustfmt-core/rustfmt-lib/tests/source/type.rs +++ b/rustfmt-core/rustfmt-lib/tests/source/type.rs @@ -139,3 +139,30 @@ fn foo(a: SomeLongComplexType, b: SomeOtherLongComplexType) -> Box Box>; + +// Const opt-out + +trait T: ? const Super {} + +const fn maybe_const() -> i32 { ::CONST } + +struct S(std::marker::PhantomData); + +impl ? const T {} + +fn trait_object() -> &'static dyn ? const T { &S } + +fn i(_: impl IntoIterator>) {} + +fn apit(_: impl ?const T) {} + +fn rpit() -> impl ? const T { S } + +pub struct Foo(T); +impl Foo { + fn new(t: T) -> Self { + // not calling methods on `t`, so we opt out of requiring + // `` to have const methods via `?const` + Self(t) + } +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/type.rs b/rustfmt-core/rustfmt-lib/tests/target/type.rs index 575bd3573bc..20e97440e7c 100644 --- a/rustfmt-core/rustfmt-lib/tests/target/type.rs +++ b/rustfmt-core/rustfmt-lib/tests/target/type.rs @@ -144,3 +144,36 @@ type MyFn = fn( a: SomeLongComplexType, b: SomeOtherLongComplexType, ) -> Box>; + +// Const opt-out + +trait T: ?const Super {} + +const fn maybe_const() -> i32 { + ::CONST +} + +struct S(std::marker::PhantomData); + +impl ?const T {} + +fn trait_object() -> &'static dyn ?const T { + &S +} + +fn i(_: impl IntoIterator>) {} + +fn apit(_: impl ?const T) {} + +fn rpit() -> impl ?const T { + S +} + +pub struct Foo(T); +impl Foo { + fn new(t: T) -> Self { + // not calling methods on `t`, so we opt out of requiring + // `` to have const methods via `?const` + Self(t) + } +}