1
Fork 0

Add ~const bounds trait bounds when using derive_const

This commit is contained in:
Michael Goulet 2023-02-07 20:59:48 +00:00
parent 56bf28d4f4
commit 7a4505900d
5 changed files with 46 additions and 12 deletions

View file

@ -153,7 +153,10 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
let path_debug = cx.path_global(span, cx.std_path(&[sym::fmt, sym::Debug])); let path_debug = cx.path_global(span, cx.std_path(&[sym::fmt, sym::Debug]));
let ty_dyn_debug = cx.ty( let ty_dyn_debug = cx.ty(
span, span,
ast::TyKind::TraitObject(vec![cx.trait_bound(path_debug)], ast::TraitObjectSyntax::Dyn), ast::TyKind::TraitObject(
vec![cx.trait_bound(path_debug, false)],
ast::TraitObjectSyntax::Dyn,
),
); );
let ty_slice = cx.ty( let ty_slice = cx.ty(
span, span,

View file

@ -605,18 +605,26 @@ impl<'a> TraitDef<'a> {
let bounds: Vec<_> = self let bounds: Vec<_> = self
.additional_bounds .additional_bounds
.iter() .iter()
.map(|p| cx.trait_bound(p.to_path(cx, self.span, type_ident, generics))) .map(|p| {
cx.trait_bound(
p.to_path(cx, self.span, type_ident, generics),
self.is_const,
)
})
.chain( .chain(
// Add a bound for the current trait. // Add a bound for the current trait.
self.skip_path_as_bound self.skip_path_as_bound
.not() .not()
.then(|| cx.trait_bound(trait_path.clone())), .then(|| cx.trait_bound(trait_path.clone(), self.is_const)),
) )
.chain({ .chain({
// Add a `Copy` bound if required. // Add a `Copy` bound if required.
if is_packed && self.needs_copy_as_bound_if_packed { if is_packed && self.needs_copy_as_bound_if_packed {
let p = deriving::path_std!(marker::Copy); let p = deriving::path_std!(marker::Copy);
Some(cx.trait_bound(p.to_path(cx, self.span, type_ident, generics))) Some(cx.trait_bound(
p.to_path(cx, self.span, type_ident, generics),
self.is_const,
))
} else { } else {
None None
} }
@ -694,18 +702,24 @@ impl<'a> TraitDef<'a> {
let mut bounds: Vec<_> = self let mut bounds: Vec<_> = self
.additional_bounds .additional_bounds
.iter() .iter()
.map(|p| cx.trait_bound(p.to_path(cx, self.span, type_ident, generics))) .map(|p| {
cx.trait_bound(
p.to_path(cx, self.span, type_ident, generics),
self.is_const,
)
})
.collect(); .collect();
// Require the current trait. // Require the current trait.
bounds.push(cx.trait_bound(trait_path.clone())); bounds.push(cx.trait_bound(trait_path.clone(), self.is_const));
// Add a `Copy` bound if required. // Add a `Copy` bound if required.
if is_packed && self.needs_copy_as_bound_if_packed { if is_packed && self.needs_copy_as_bound_if_packed {
let p = deriving::path_std!(marker::Copy); let p = deriving::path_std!(marker::Copy);
bounds.push( bounds.push(cx.trait_bound(
cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)), p.to_path(cx, self.span, type_ident, generics),
); self.is_const,
));
} }
let predicate = ast::WhereBoundPredicate { let predicate = ast::WhereBoundPredicate {

View file

@ -154,7 +154,7 @@ fn mk_ty_param(
.iter() .iter()
.map(|b| { .map(|b| {
let path = b.to_path(cx, span, self_ident, self_generics); let path = b.to_path(cx, span, self_ident, self_generics);
cx.trait_bound(path) cx.trait_bound(path, false)
}) })
.collect(); .collect();
cx.typaram(span, Ident::new(name, span), bounds, None) cx.typaram(span, Ident::new(name, span), bounds, None)

View file

@ -131,10 +131,14 @@ impl<'a> ExtCtxt<'a> {
} }
} }
pub fn trait_bound(&self, path: ast::Path) -> ast::GenericBound { pub fn trait_bound(&self, path: ast::Path, is_const: bool) -> ast::GenericBound {
ast::GenericBound::Trait( ast::GenericBound::Trait(
self.poly_trait_ref(path.span, path), self.poly_trait_ref(path.span, path),
ast::TraitBoundModifier::None, if is_const {
ast::TraitBoundModifier::MaybeConst
} else {
ast::TraitBoundModifier::None
},
) )
} }

View file

@ -0,0 +1,13 @@
// check-pass
#![feature(derive_const)]
#![feature(const_trait_impl)]
#[derive_const(PartialEq)]
pub struct Reverse<T>(T);
const fn foo(a: Reverse<i32>, b: Reverse<i32>) -> bool {
a == b
}
fn main() {}