Skip to content

Commit 5b8403c

Browse files
authored
Rollup merge of #107777 - compiler-errors:derive_const-actually-derive-const, r=fee1-dead
Make `derive_const` derive properly const-if-const impls Fixes #107774 Fixes #107666 Also fixes rendering of const-if-const bounds in pretty printing. r? ```@oli-obk``` or ```@fee1-dead```
2 parents abc2203 + 7a45059 commit 5b8403c

File tree

9 files changed

+69
-17
lines changed

9 files changed

+69
-17
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1567,8 +1567,18 @@ impl<'a> State<'a> {
15671567

15681568
match bound {
15691569
GenericBound::Trait(tref, modifier) => {
1570-
if modifier == &TraitBoundModifier::Maybe {
1571-
self.word("?");
1570+
match modifier {
1571+
TraitBoundModifier::None => {}
1572+
TraitBoundModifier::Maybe => {
1573+
self.word("?");
1574+
}
1575+
TraitBoundModifier::MaybeConst => {
1576+
self.word_space("~const");
1577+
}
1578+
TraitBoundModifier::MaybeConstMaybe => {
1579+
self.word_space("~const");
1580+
self.word("?");
1581+
}
15721582
}
15731583
self.print_poly_trait_ref(tref);
15741584
}

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

tests/ui/macros/stringify.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ fn test_item() {
626626
stringify_item!(
627627
impl ~const Struct {}
628628
),
629-
"impl Struct {}", // FIXME
629+
"impl ~const Struct {}",
630630
);
631631

632632
// ItemKind::MacCall
@@ -838,15 +838,15 @@ fn test_ty() {
838838
assert_eq!(stringify_ty!(dyn Send + 'a), "dyn Send + 'a");
839839
assert_eq!(stringify_ty!(dyn 'a + Send), "dyn 'a + Send");
840840
assert_eq!(stringify_ty!(dyn ?Sized), "dyn ?Sized");
841-
assert_eq!(stringify_ty!(dyn ~const Clone), "dyn Clone"); // FIXME
841+
assert_eq!(stringify_ty!(dyn ~const Clone), "dyn ~const Clone");
842842
assert_eq!(stringify_ty!(dyn for<'a> Send), "dyn for<'a> Send");
843843

844844
// TyKind::ImplTrait
845845
assert_eq!(stringify_ty!(impl Send), "impl Send");
846846
assert_eq!(stringify_ty!(impl Send + 'a), "impl Send + 'a");
847847
assert_eq!(stringify_ty!(impl 'a + Send), "impl 'a + Send");
848848
assert_eq!(stringify_ty!(impl ?Sized), "impl ?Sized");
849-
assert_eq!(stringify_ty!(impl ~const Clone), "impl Clone"); // FIXME
849+
assert_eq!(stringify_ty!(impl ~const Clone), "impl ~const Clone");
850850
assert_eq!(stringify_ty!(impl for<'a> Send), "impl for<'a> Send");
851851

852852
// TyKind::Paren
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() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: -Zunpretty=normal
2+
// check-pass
3+
4+
fn foo() where T: ~const Bar {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: -Zunpretty=normal
2+
// check-pass
3+
4+
fn foo() where T: ~const Bar {}

0 commit comments

Comments
 (0)