Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion rustfmt-core/rustfmt-lib/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down
27 changes: 27 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,30 @@ fn foo(a: SomeLongComplexType, b: SomeOtherLongComplexType) -> Box<Future<Item =
}

type MyFn = fn(a: SomeLongComplexType, b: SomeOtherLongComplexType,) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;

// Const opt-out

trait T: ? const Super {}

const fn maybe_const<S: ? const T>() -> i32 { <S as T>::CONST }

struct S<T:? const ? Sized>(std::marker::PhantomData<T>);

impl ? const T {}

fn trait_object() -> &'static dyn ? const T { &S }

fn i(_: impl IntoIterator<Item = Box<dyn ? const T>>) {}

fn apit(_: impl ?const T) {}

fn rpit() -> impl ? const T { S }

pub struct Foo<T: Trait>(T);
impl<T: ? const Trait> Foo<T> {
fn new(t: T) -> Self {
// not calling methods on `t`, so we opt out of requiring
// `<T as Trait>` to have const methods via `?const`
Self(t)
}
}
33 changes: 33 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,36 @@ type MyFn = fn(
a: SomeLongComplexType,
b: SomeOtherLongComplexType,
) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;

// Const opt-out

trait T: ?const Super {}

const fn maybe_const<S: ?const T>() -> i32 {
<S as T>::CONST
}

struct S<T: ?const ?Sized>(std::marker::PhantomData<T>);

impl ?const T {}

fn trait_object() -> &'static dyn ?const T {
&S
}

fn i(_: impl IntoIterator<Item = Box<dyn ?const T>>) {}

fn apit(_: impl ?const T) {}

fn rpit() -> impl ?const T {
S
}

pub struct Foo<T: Trait>(T);
impl<T: ?const Trait> Foo<T> {
fn new(t: T) -> Self {
// not calling methods on `t`, so we opt out of requiring
// `<T as Trait>` to have const methods via `?const`
Self(t)
}
}