Rollup merge of #119540 - fmease:no-effect-args-inside-dyn-trait, r=compiler-errors
Don't synthesize host effect args inside trait object types While we were indeed emitting an error for `~const` & `const` trait bounds in trait object types, we were still synthesizing host effect args for them. Since we don't record the original trait bound modifiers for dyn-Trait in `hir::TyKind::TraitObject` (unlike we do for let's say impl-Trait, `hir::TyKind::OpaqueTy`), AstConv just assumes `ty::BoundConstness::NotConst` in `conv_object_ty_poly_trait_ref` which given `<host> dyn ~const NonConstTrait` resulted in us not realizing that `~const` was used on a non-const trait which lead to a failed assertion in the end. Instead of updating `hir::TyKind::TraitObject` to track this kind of information, just strip the user-provided constness (similar to #119505). Fixes #119524.
This commit is contained in:
commit
f4335a419f
3 changed files with 47 additions and 15 deletions
|
@ -1434,19 +1434,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||||
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
|
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
|
||||||
let bounds =
|
let bounds =
|
||||||
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
|
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
|
||||||
GenericBound::Trait(
|
// We can safely ignore constness here since AST validation
|
||||||
ty,
|
// takes care of rejecting invalid modifier combinations and
|
||||||
TraitBoundModifiers {
|
// const trait bounds in trait object types.
|
||||||
polarity: BoundPolarity::Positive | BoundPolarity::Negative(_),
|
GenericBound::Trait(ty, modifiers) => match modifiers.polarity {
|
||||||
constness,
|
BoundPolarity::Positive | BoundPolarity::Negative(_) => {
|
||||||
},
|
Some(this.lower_poly_trait_ref(
|
||||||
) => Some(this.lower_poly_trait_ref(ty, itctx, *constness)),
|
ty,
|
||||||
// We can safely ignore constness here, since AST validation
|
itctx,
|
||||||
// will take care of invalid modifier combinations.
|
// Still, don't pass along the constness here; we don't want to
|
||||||
GenericBound::Trait(
|
// synthesize any host effect args, it'd only cause problems.
|
||||||
_,
|
ast::BoundConstness::Never,
|
||||||
TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. },
|
))
|
||||||
) => None,
|
}
|
||||||
|
BoundPolarity::Maybe(_) => None,
|
||||||
|
},
|
||||||
GenericBound::Outlives(lifetime) => {
|
GenericBound::Outlives(lifetime) => {
|
||||||
if lifetime_bound.is_none() {
|
if lifetime_bound.is_none() {
|
||||||
lifetime_bound = Some(this.lower_lifetime(lifetime));
|
lifetime_bound = Some(this.lower_lifetime(lifetime));
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#![feature(const_trait_impl)]
|
#![feature(const_trait_impl, effects)]
|
||||||
// edition: 2021
|
// edition: 2021
|
||||||
|
|
||||||
#[const_trait]
|
#[const_trait]
|
||||||
|
@ -6,4 +6,12 @@ trait Trait {}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _: &dyn const Trait; //~ ERROR const trait bounds are not allowed in trait object types
|
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
|
||||||
|
|
|
@ -4,5 +4,27 @@ error: const trait bounds are not allowed in trait object types
|
||||||
LL | let _: &dyn const Trait;
|
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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue