From 695a02e0d8a532cd4ec44fe438a0ba5da59b76fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 3 Jan 2024 09:24:42 +0100 Subject: [PATCH] Don't synthesize host effect args inside trait object types --- compiler/rustc_ast_lowering/src/lib.rs | 28 ++++++++++--------- .../const-trait-bounds-trait-objects.rs | 10 ++++++- .../const-trait-bounds-trait-objects.stderr | 24 +++++++++++++++- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 92fd29c47af..f7648785e36 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1454,19 +1454,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| { let bounds = this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound { - GenericBound::Trait( - ty, - TraitBoundModifiers { - polarity: BoundPolarity::Positive | BoundPolarity::Negative(_), - constness, - }, - ) => Some(this.lower_poly_trait_ref(ty, itctx, *constness)), - // We can safely ignore constness here, since AST validation - // will take care of invalid modifier combinations. - GenericBound::Trait( - _, - TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. }, - ) => None, + // We can safely ignore constness here since AST validation + // takes care of rejecting invalid modifier combinations and + // const trait bounds in trait object types. + GenericBound::Trait(ty, modifiers) => match modifiers.polarity { + BoundPolarity::Positive | BoundPolarity::Negative(_) => { + Some(this.lower_poly_trait_ref( + ty, + itctx, + // Still, don't pass along the constness here; we don't want to + // synthesize any host effect args, it'd only cause problems. + ast::BoundConstness::Never, + )) + } + BoundPolarity::Maybe(_) => None, + }, GenericBound::Outlives(lifetime) => { if lifetime_bound.is_none() { lifetime_bound = Some(this.lower_lifetime(lifetime)); diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.rs index 86706671316..a00a6d48105 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.rs +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.rs @@ -1,4 +1,4 @@ -#![feature(const_trait_impl)] +#![feature(const_trait_impl, effects)] // edition: 2021 #[const_trait] @@ -6,4 +6,12 @@ trait Trait {} fn main() { let _: &dyn const Trait; //~ ERROR const trait bounds are not allowed in trait object types + let _: &dyn ~const Trait; //~ ERROR `~const` is not allowed here } + +// Regression test for issue #119525. +trait NonConst {} +const fn handle(_: &dyn const NonConst) {} +//~^ ERROR const trait bounds are not allowed in trait object types +const fn take(_: &dyn ~const NonConst) {} +//~^ ERROR `~const` is not allowed here diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.stderr index 8b9ba94d099..04c2dc2e2e0 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.stderr @@ -4,5 +4,27 @@ error: const trait bounds are not allowed in trait object types LL | let _: &dyn const Trait; | ^^^^^^^^^^^ -error: aborting due to 1 previous error +error: `~const` is not allowed here + --> $DIR/const-trait-bounds-trait-objects.rs:9:17 + | +LL | let _: &dyn ~const Trait; + | ^^^^^^ + | + = note: trait objects cannot have `~const` trait bounds + +error: const trait bounds are not allowed in trait object types + --> $DIR/const-trait-bounds-trait-objects.rs:14:25 + | +LL | const fn handle(_: &dyn const NonConst) {} + | ^^^^^^^^^^^^^^ + +error: `~const` is not allowed here + --> $DIR/const-trait-bounds-trait-objects.rs:16:23 + | +LL | const fn take(_: &dyn ~const NonConst) {} + | ^^^^^^ + | + = note: trait objects cannot have `~const` trait bounds + +error: aborting due to 4 previous errors