From 56bf28d4f4236f19f962868f181b15b174d041c4 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 7 Feb 2023 20:22:42 +0000 Subject: [PATCH 1/2] Expand const-if-const trait bounds correctly --- compiler/rustc_ast_pretty/src/pprust/state.rs | 14 ++++++++++++-- tests/ui/macros/stringify.rs | 6 +++--- tests/ui/unpretty/ast-const-trait-bound.rs | 4 ++++ tests/ui/unpretty/ast-const-trait-bound.stdout | 4 ++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/ui/unpretty/ast-const-trait-bound.rs create mode 100644 tests/ui/unpretty/ast-const-trait-bound.stdout diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index d7767efa9841b..cd621bc67a1de 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -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); } diff --git a/tests/ui/macros/stringify.rs b/tests/ui/macros/stringify.rs index 5cd217df6fc2f..fdc2a7666d69b 100644 --- a/tests/ui/macros/stringify.rs +++ b/tests/ui/macros/stringify.rs @@ -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 diff --git a/tests/ui/unpretty/ast-const-trait-bound.rs b/tests/ui/unpretty/ast-const-trait-bound.rs new file mode 100644 index 0000000000000..dbcba7871ec96 --- /dev/null +++ b/tests/ui/unpretty/ast-const-trait-bound.rs @@ -0,0 +1,4 @@ +// compile-flags: -Zunpretty=normal +// check-pass + +fn foo() where T: ~const Bar {} diff --git a/tests/ui/unpretty/ast-const-trait-bound.stdout b/tests/ui/unpretty/ast-const-trait-bound.stdout new file mode 100644 index 0000000000000..dbcba7871ec96 --- /dev/null +++ b/tests/ui/unpretty/ast-const-trait-bound.stdout @@ -0,0 +1,4 @@ +// compile-flags: -Zunpretty=normal +// check-pass + +fn foo() where T: ~const Bar {} From 7a4505900db08696071655018e72bad896fb9cb0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 7 Feb 2023 20:59:48 +0000 Subject: [PATCH 2/2] Add ~const bounds trait bounds when using derive_const --- .../src/deriving/debug.rs | 5 +++- .../src/deriving/generic/mod.rs | 30 ++++++++++++++----- .../src/deriving/generic/ty.rs | 2 +- compiler/rustc_expand/src/build.rs | 8 +++-- .../const_derives/derive-const-with-params.rs | 13 ++++++++ 5 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 tests/ui/rfc-2632-const-trait-impl/const_derives/derive-const-with-params.rs diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs index 74e2597830e7e..e8a353b1c8fcc 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -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, diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 97de40bac3466..a8c0aeb787078 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -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 { diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs index c6f5f5d080706..aabd5b1f773f7 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs @@ -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) diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index 6cd56852f9d68..b4c12651e7a2d 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -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 + }, ) } diff --git a/tests/ui/rfc-2632-const-trait-impl/const_derives/derive-const-with-params.rs b/tests/ui/rfc-2632-const-trait-impl/const_derives/derive-const-with-params.rs new file mode 100644 index 0000000000000..72edfbc97e48f --- /dev/null +++ b/tests/ui/rfc-2632-const-trait-impl/const_derives/derive-const-with-params.rs @@ -0,0 +1,13 @@ +// check-pass + +#![feature(derive_const)] +#![feature(const_trait_impl)] + +#[derive_const(PartialEq)] +pub struct Reverse(T); + +const fn foo(a: Reverse, b: Reverse) -> bool { + a == b +} + +fn main() {}