Skip to content

Commit 7a45059

Browse files
Add ~const bounds trait bounds when using derive_const
1 parent 56bf28d commit 7a45059

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

compiler/rustc_builtin_macros/src/deriving/debug.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
153153
let path_debug = cx.path_global(span, cx.std_path(&[sym::fmt, sym::Debug]));
154154
let ty_dyn_debug = cx.ty(
155155
span,
156-
ast::TyKind::TraitObject(vec![cx.trait_bound(path_debug)], ast::TraitObjectSyntax::Dyn),
156+
ast::TyKind::TraitObject(
157+
vec![cx.trait_bound(path_debug, false)],
158+
ast::TraitObjectSyntax::Dyn,
159+
),
157160
);
158161
let ty_slice = cx.ty(
159162
span,

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -605,18 +605,26 @@ impl<'a> TraitDef<'a> {
605605
let bounds: Vec<_> = self
606606
.additional_bounds
607607
.iter()
608-
.map(|p| cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)))
608+
.map(|p| {
609+
cx.trait_bound(
610+
p.to_path(cx, self.span, type_ident, generics),
611+
self.is_const,
612+
)
613+
})
609614
.chain(
610615
// Add a bound for the current trait.
611616
self.skip_path_as_bound
612617
.not()
613-
.then(|| cx.trait_bound(trait_path.clone())),
618+
.then(|| cx.trait_bound(trait_path.clone(), self.is_const)),
614619
)
615620
.chain({
616621
// Add a `Copy` bound if required.
617622
if is_packed && self.needs_copy_as_bound_if_packed {
618623
let p = deriving::path_std!(marker::Copy);
619-
Some(cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)))
624+
Some(cx.trait_bound(
625+
p.to_path(cx, self.span, type_ident, generics),
626+
self.is_const,
627+
))
620628
} else {
621629
None
622630
}
@@ -694,18 +702,24 @@ impl<'a> TraitDef<'a> {
694702
let mut bounds: Vec<_> = self
695703
.additional_bounds
696704
.iter()
697-
.map(|p| cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)))
705+
.map(|p| {
706+
cx.trait_bound(
707+
p.to_path(cx, self.span, type_ident, generics),
708+
self.is_const,
709+
)
710+
})
698711
.collect();
699712

700713
// Require the current trait.
701-
bounds.push(cx.trait_bound(trait_path.clone()));
714+
bounds.push(cx.trait_bound(trait_path.clone(), self.is_const));
702715

703716
// Add a `Copy` bound if required.
704717
if is_packed && self.needs_copy_as_bound_if_packed {
705718
let p = deriving::path_std!(marker::Copy);
706-
bounds.push(
707-
cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)),
708-
);
719+
bounds.push(cx.trait_bound(
720+
p.to_path(cx, self.span, type_ident, generics),
721+
self.is_const,
722+
));
709723
}
710724

711725
let predicate = ast::WhereBoundPredicate {

compiler/rustc_builtin_macros/src/deriving/generic/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn mk_ty_param(
154154
.iter()
155155
.map(|b| {
156156
let path = b.to_path(cx, span, self_ident, self_generics);
157-
cx.trait_bound(path)
157+
cx.trait_bound(path, false)
158158
})
159159
.collect();
160160
cx.typaram(span, Ident::new(name, span), bounds, None)

compiler/rustc_expand/src/build.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,14 @@ impl<'a> ExtCtxt<'a> {
131131
}
132132
}
133133

134-
pub fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
134+
pub fn trait_bound(&self, path: ast::Path, is_const: bool) -> ast::GenericBound {
135135
ast::GenericBound::Trait(
136136
self.poly_trait_ref(path.span, path),
137-
ast::TraitBoundModifier::None,
137+
if is_const {
138+
ast::TraitBoundModifier::MaybeConst
139+
} else {
140+
ast::TraitBoundModifier::None
141+
},
138142
)
139143
}
140144

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// check-pass
2+
3+
#![feature(derive_const)]
4+
#![feature(const_trait_impl)]
5+
6+
#[derive_const(PartialEq)]
7+
pub struct Reverse<T>(T);
8+
9+
const fn foo(a: Reverse<i32>, b: Reverse<i32>) -> bool {
10+
a == b
11+
}
12+
13+
fn main() {}

0 commit comments

Comments
 (0)