Skip to content

Commit f4335a4

Browse files
authored
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.
2 parents 0db26e0 + 695a02e commit f4335a4

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -1434,19 +1434,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14341434
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
14351435
let bounds =
14361436
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
1437-
GenericBound::Trait(
1438-
ty,
1439-
TraitBoundModifiers {
1440-
polarity: BoundPolarity::Positive | BoundPolarity::Negative(_),
1441-
constness,
1442-
},
1443-
) => Some(this.lower_poly_trait_ref(ty, itctx, *constness)),
1444-
// We can safely ignore constness here, since AST validation
1445-
// will take care of invalid modifier combinations.
1446-
GenericBound::Trait(
1447-
_,
1448-
TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. },
1449-
) => None,
1437+
// We can safely ignore constness here since AST validation
1438+
// takes care of rejecting invalid modifier combinations and
1439+
// const trait bounds in trait object types.
1440+
GenericBound::Trait(ty, modifiers) => match modifiers.polarity {
1441+
BoundPolarity::Positive | BoundPolarity::Negative(_) => {
1442+
Some(this.lower_poly_trait_ref(
1443+
ty,
1444+
itctx,
1445+
// Still, don't pass along the constness here; we don't want to
1446+
// synthesize any host effect args, it'd only cause problems.
1447+
ast::BoundConstness::Never,
1448+
))
1449+
}
1450+
BoundPolarity::Maybe(_) => None,
1451+
},
14501452
GenericBound::Outlives(lifetime) => {
14511453
if lifetime_bound.is_none() {
14521454
lifetime_bound = Some(this.lower_lifetime(lifetime));
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
#![feature(const_trait_impl)]
1+
#![feature(const_trait_impl, effects)]
22
// edition: 2021
33

44
#[const_trait]
55
trait Trait {}
66

77
fn main() {
88
let _: &dyn const Trait; //~ ERROR const trait bounds are not allowed in trait object types
9+
let _: &dyn ~const Trait; //~ ERROR `~const` is not allowed here
910
}
11+
12+
// Regression test for issue #119525.
13+
trait NonConst {}
14+
const fn handle(_: &dyn const NonConst) {}
15+
//~^ ERROR const trait bounds are not allowed in trait object types
16+
const fn take(_: &dyn ~const NonConst) {}
17+
//~^ ERROR `~const` is not allowed here

tests/ui/rfcs/rfc-2632-const-trait-impl/const-trait-bounds-trait-objects.stderr

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,27 @@ error: const trait bounds are not allowed in trait object types
44
LL | let _: &dyn const Trait;
55
| ^^^^^^^^^^^
66

7-
error: aborting due to 1 previous error
7+
error: `~const` is not allowed here
8+
--> $DIR/const-trait-bounds-trait-objects.rs:9:17
9+
|
10+
LL | let _: &dyn ~const Trait;
11+
| ^^^^^^
12+
|
13+
= note: trait objects cannot have `~const` trait bounds
14+
15+
error: const trait bounds are not allowed in trait object types
16+
--> $DIR/const-trait-bounds-trait-objects.rs:14:25
17+
|
18+
LL | const fn handle(_: &dyn const NonConst) {}
19+
| ^^^^^^^^^^^^^^
20+
21+
error: `~const` is not allowed here
22+
--> $DIR/const-trait-bounds-trait-objects.rs:16:23
23+
|
24+
LL | const fn take(_: &dyn ~const NonConst) {}
25+
| ^^^^^^
26+
|
27+
= note: trait objects cannot have `~const` trait bounds
28+
29+
error: aborting due to 4 previous errors
830

0 commit comments

Comments
 (0)