Skip to content

Commit ec3a26e

Browse files
feat: trait bounds const opt out (#4056)
1 parent 06e00e0 commit ec3a26e

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

rustfmt-core/rustfmt-lib/src/types.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,12 @@ impl Rewrite for ast::GenericBound {
523523
ast::TraitBoundModifier::Maybe => poly_trait_ref
524524
.rewrite(context, shape.offset_left(1)?)
525525
.map(|s| format!("?{}", s)),
526-
_ => unimplemented!(),
526+
ast::TraitBoundModifier::MaybeConst => poly_trait_ref
527+
.rewrite(context, shape.offset_left(7)?)
528+
.map(|s| format!("?const {}", s)),
529+
ast::TraitBoundModifier::MaybeConstMaybe => poly_trait_ref
530+
.rewrite(context, shape.offset_left(8)?)
531+
.map(|s| format!("?const ?{}", s)),
527532
};
528533
rewrite.map(|s| if has_paren { format!("({})", s) } else { s })
529534
}

rustfmt-core/rustfmt-lib/tests/source/type.rs

+27
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,30 @@ fn foo(a: SomeLongComplexType, b: SomeOtherLongComplexType) -> Box<Future<Item =
139139
}
140140

141141
type MyFn = fn(a: SomeLongComplexType, b: SomeOtherLongComplexType,) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;
142+
143+
// Const opt-out
144+
145+
trait T: ? const Super {}
146+
147+
const fn maybe_const<S: ? const T>() -> i32 { <S as T>::CONST }
148+
149+
struct S<T:? const ? Sized>(std::marker::PhantomData<T>);
150+
151+
impl ? const T {}
152+
153+
fn trait_object() -> &'static dyn ? const T { &S }
154+
155+
fn i(_: impl IntoIterator<Item = Box<dyn ? const T>>) {}
156+
157+
fn apit(_: impl ?const T) {}
158+
159+
fn rpit() -> impl ? const T { S }
160+
161+
pub struct Foo<T: Trait>(T);
162+
impl<T: ? const Trait> Foo<T> {
163+
fn new(t: T) -> Self {
164+
// not calling methods on `t`, so we opt out of requiring
165+
// `<T as Trait>` to have const methods via `?const`
166+
Self(t)
167+
}
168+
}

rustfmt-core/rustfmt-lib/tests/target/type.rs

+33
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,36 @@ type MyFn = fn(
144144
a: SomeLongComplexType,
145145
b: SomeOtherLongComplexType,
146146
) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;
147+
148+
// Const opt-out
149+
150+
trait T: ?const Super {}
151+
152+
const fn maybe_const<S: ?const T>() -> i32 {
153+
<S as T>::CONST
154+
}
155+
156+
struct S<T: ?const ?Sized>(std::marker::PhantomData<T>);
157+
158+
impl ?const T {}
159+
160+
fn trait_object() -> &'static dyn ?const T {
161+
&S
162+
}
163+
164+
fn i(_: impl IntoIterator<Item = Box<dyn ?const T>>) {}
165+
166+
fn apit(_: impl ?const T) {}
167+
168+
fn rpit() -> impl ?const T {
169+
S
170+
}
171+
172+
pub struct Foo<T: Trait>(T);
173+
impl<T: ?const Trait> Foo<T> {
174+
fn new(t: T) -> Self {
175+
// not calling methods on `t`, so we opt out of requiring
176+
// `<T as Trait>` to have const methods via `?const`
177+
Self(t)
178+
}
179+
}

0 commit comments

Comments
 (0)