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```
This commit is contained in:
Matthias Krüger 2023-02-08 18:32:43 +01:00 committed by GitHub
commit 5b8403c463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 17 deletions

View File

@ -1567,8 +1567,18 @@ impl<'a> State<'a> {
match bound {
GenericBound::Trait(tref, modifier) => {
if modifier == &TraitBoundModifier::Maybe {
self.word("?");
match modifier {
TraitBoundModifier::None => {}
TraitBoundModifier::Maybe => {
self.word("?");
}
TraitBoundModifier::MaybeConst => {
self.word_space("~const");
}
TraitBoundModifier::MaybeConstMaybe => {
self.word_space("~const");
self.word("?");
}
}
self.print_poly_trait_ref(tref);
}

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 ty_dyn_debug = cx.ty(
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(
span,

View File

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

View File

@ -154,7 +154,7 @@ fn mk_ty_param(
.iter()
.map(|b| {
let path = b.to_path(cx, span, self_ident, self_generics);
cx.trait_bound(path)
cx.trait_bound(path, false)
})
.collect();
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(
self.poly_trait_ref(path.span, path),
ast::TraitBoundModifier::None,
if is_const {
ast::TraitBoundModifier::MaybeConst
} else {
ast::TraitBoundModifier::None
},
)
}

View File

@ -626,7 +626,7 @@ fn test_item() {
stringify_item!(
impl ~const Struct {}
),
"impl Struct {}", // FIXME
"impl ~const Struct {}",
);
// ItemKind::MacCall
@ -838,7 +838,7 @@ fn test_ty() {
assert_eq!(stringify_ty!(dyn Send + 'a), "dyn Send + 'a");
assert_eq!(stringify_ty!(dyn 'a + Send), "dyn 'a + Send");
assert_eq!(stringify_ty!(dyn ?Sized), "dyn ?Sized");
assert_eq!(stringify_ty!(dyn ~const Clone), "dyn Clone"); // FIXME
assert_eq!(stringify_ty!(dyn ~const Clone), "dyn ~const Clone");
assert_eq!(stringify_ty!(dyn for<'a> Send), "dyn for<'a> Send");
// TyKind::ImplTrait
@ -846,7 +846,7 @@ fn test_ty() {
assert_eq!(stringify_ty!(impl Send + 'a), "impl Send + 'a");
assert_eq!(stringify_ty!(impl 'a + Send), "impl 'a + Send");
assert_eq!(stringify_ty!(impl ?Sized), "impl ?Sized");
assert_eq!(stringify_ty!(impl ~const Clone), "impl Clone"); // FIXME
assert_eq!(stringify_ty!(impl ~const Clone), "impl ~const Clone");
assert_eq!(stringify_ty!(impl for<'a> Send), "impl for<'a> Send");
// TyKind::Paren

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() {}

View File

@ -0,0 +1,4 @@
// compile-flags: -Zunpretty=normal
// check-pass
fn foo() where T: ~const Bar {}

View File

@ -0,0 +1,4 @@
// compile-flags: -Zunpretty=normal
// check-pass
fn foo() where T: ~const Bar {}