Skip to content

Commit 695a02e

Browse files
committedJan 3, 2024
Don't synthesize host effect args inside trait object types
1 parent e51e98d commit 695a02e

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
@@ -1454,19 +1454,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14541454
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
14551455
let bounds =
14561456
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
1457-
GenericBound::Trait(
1458-
ty,
1459-
TraitBoundModifiers {
1460-
polarity: BoundPolarity::Positive | BoundPolarity::Negative(_),
1461-
constness,
1462-
},
1463-
) => Some(this.lower_poly_trait_ref(ty, itctx, *constness)),
1464-
// We can safely ignore constness here, since AST validation
1465-
// will take care of invalid modifier combinations.
1466-
GenericBound::Trait(
1467-
_,
1468-
TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. },
1469-
) => None,
1457+
// We can safely ignore constness here since AST validation
1458+
// takes care of rejecting invalid modifier combinations and
1459+
// const trait bounds in trait object types.
1460+
GenericBound::Trait(ty, modifiers) => match modifiers.polarity {
1461+
BoundPolarity::Positive | BoundPolarity::Negative(_) => {
1462+
Some(this.lower_poly_trait_ref(
1463+
ty,
1464+
itctx,
1465+
// Still, don't pass along the constness here; we don't want to
1466+
// synthesize any host effect args, it'd only cause problems.
1467+
ast::BoundConstness::Never,
1468+
))
1469+
}
1470+
BoundPolarity::Maybe(_) => None,
1471+
},
14701472
GenericBound::Outlives(lifetime) => {
14711473
if lifetime_bound.is_none() {
14721474
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)