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 { match bound {
GenericBound::Trait(tref, modifier) => { GenericBound::Trait(tref, modifier) => {
if modifier == &TraitBoundModifier::Maybe { match modifier {
self.word("?"); 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); 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 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

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