Skip to content

Commit

Permalink
feat: trait bounds const opt out (#4056)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright authored Feb 17, 2020
1 parent 06e00e0 commit ec3a26e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
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)
}
}

0 comments on commit ec3a26e

Please sign in to comment.