From: Michael Goulet Date: Tue, 7 Feb 2023 20:59:48 +0000 (+0000) Subject: Add ~const bounds trait bounds when using derive_const X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=7a4505900db08696071655018e72bad896fb9cb0;p=rust.git Add ~const bounds trait bounds when using derive_const --- diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs index 74e2597830e..e8a353b1c8f 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -153,7 +153,10 @@ fn expr_for_field( 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 97de40bac34..a8c0aeb7870 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 @@ fn create_derived_impl( 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 @@ fn create_derived_impl( 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 c6f5f5d0807..aabd5b1f773 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 6cd56852f9d..b4c12651e7a 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -131,10 +131,14 @@ pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef { } } - 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 00000000000..72edfbc97e4 --- /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() {}