Skip to content

Commit 52e3b38

Browse files
committed
Generate const predicates for const trait aliases
1 parent 2f24967 commit 52e3b38

File tree

7 files changed

+45
-85
lines changed

7 files changed

+45
-85
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11631163
walk_list!(this, visit_assoc_item, items, AssocCtxt::Trait);
11641164
});
11651165
}
1166+
ItemKind::TraitAlias(box TraitAlias { constness, generics, bounds, .. }) => {
1167+
let disallowed = matches!(constness, ast::Const::No)
1168+
.then(|| TildeConstReason::Trait { span: item.span });
1169+
self.with_tilde_const(disallowed, |this| {
1170+
this.visit_generics(generics);
1171+
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits)
1172+
});
1173+
}
11661174
ItemKind::Mod(safety, ident, mod_kind) => {
11671175
if let &Safety::Unsafe(span) = safety {
11681176
self.dcx().emit_err(errors::UnsafeItem { span, kind: "module" });

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
848848
hir::ItemKind::Trait(constness, is_auto, safety, ..) => {
849849
(constness, false, is_auto == hir::IsAuto::Yes, safety)
850850
}
851-
hir::ItemKind::TraitAlias(..) => (hir::Constness::NotConst, true, false, hir::Safety::Safe),
851+
hir::ItemKind::TraitAlias(constness, ..) => (constness, true, false, hir::Safety::Safe),
852852
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
853853
};
854854

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,8 @@ pub(super) fn const_conditions<'tcx>(
10181018
Node::Item(item) => match item.kind {
10191019
hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
10201020
hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1021-
hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
1021+
hir::ItemKind::TraitAlias(_, _, generics, supertraits)
1022+
| hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
10221023
(generics, Some((item.owner_id.def_id, supertraits)), false)
10231024
}
10241025
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
@@ -1128,13 +1129,14 @@ pub(super) fn explicit_implied_const_bounds<'tcx>(
11281129
span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
11291130
}
11301131
None => match tcx.hir_node_by_def_id(def_id) {
1131-
Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
1132-
implied_predicates_with_filter(
1133-
tcx,
1134-
def_id.to_def_id(),
1135-
PredicateFilter::SelfConstIfConst,
1136-
)
1137-
}
1132+
Node::Item(hir::Item {
1133+
kind: hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..),
1134+
..
1135+
}) => implied_predicates_with_filter(
1136+
tcx,
1137+
def_id.to_def_id(),
1138+
PredicateFilter::SelfConstIfConst,
1139+
),
11381140
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
11391141
| Node::OpaqueTy(_) => {
11401142
explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ impl<'tcx> TyCtxt<'tcx> {
21092109
DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) => {
21102110
self.constness(def_id) == hir::Constness::Const
21112111
}
2112-
DefKind::Trait => self.is_const_trait(def_id),
2112+
DefKind::TraitAlias | DefKind::Trait => self.is_const_trait(def_id),
21132113
DefKind::AssocTy => {
21142114
let parent_def_id = self.parent(def_id);
21152115
match self.def_kind(parent_def_id) {
@@ -2152,7 +2152,6 @@ impl<'tcx> TyCtxt<'tcx> {
21522152
| DefKind::Variant
21532153
| DefKind::TyAlias
21542154
| DefKind::ForeignTy
2155-
| DefKind::TraitAlias
21562155
| DefKind::TyParam
21572156
| DefKind::Const
21582157
| DefKind::ConstParam
Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
1-
error: `[const]` is not allowed here
2-
--> $DIR/trait_alias.rs:14:19
3-
|
4-
LL | const trait Foo = [const] Bar + Baz;
5-
| ^^^^^^^
1+
error[E0277]: the trait bound `T: [const] Baz` is not satisfied
2+
--> $DIR/trait_alias.rs:20:11
63
|
7-
= note: this item cannot have `[const]` trait bounds
4+
LL | x.baz();
5+
| ^^^
86

9-
error: `[const]` can only be applied to `const` traits
10-
--> $DIR/trait_alias.rs:17:17
7+
error[E0277]: the trait bound `(): const Foo` is not satisfied
8+
--> $DIR/trait_alias.rs:25:19
119
|
12-
LL | const fn foo<T: [const] Foo>(x: &T) {
13-
| ^^^^^^^ can't be applied to `Foo`
14-
|
15-
help: mark `Foo` as `const` to allow it to have `const` implementations
10+
LL | const _: () = foo(&());
11+
| --- ^^^
12+
| |
13+
| required by a bound introduced by this call
1614
|
17-
LL | #[const_trait] const trait Foo = [const] Bar + Baz;
18-
| ++++++++++++++
19-
20-
error: `[const]` can only be applied to `const` traits
21-
--> $DIR/trait_alias.rs:17:17
15+
note: required by a bound in `foo`
16+
--> $DIR/trait_alias.rs:16:17
2217
|
2318
LL | const fn foo<T: [const] Foo>(x: &T) {
24-
| ^^^^^^^ can't be applied to `Foo`
25-
|
26-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
27-
help: mark `Foo` as `const` to allow it to have `const` implementations
28-
|
29-
LL | #[const_trait] const trait Foo = [const] Bar + Baz;
30-
| ++++++++++++++
31-
32-
error[E0277]: the trait bound `T: [const] Bar` is not satisfied
33-
--> $DIR/trait_alias.rs:20:7
34-
|
35-
LL | x.bar();
36-
| ^^^
37-
38-
error[E0277]: the trait bound `T: [const] Baz` is not satisfied
39-
--> $DIR/trait_alias.rs:24:11
40-
|
41-
LL | x.baz();
42-
| ^^^
19+
| ^^^^^^^^^^^ required by this bound in `foo`
4320

44-
error: aborting due to 5 previous errors
21+
error: aborting due to 2 previous errors
4522

4623
For more information about this error, try `rustc --explain E0277`.
Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
1-
error: `[const]` is not allowed here
2-
--> $DIR/trait_alias.rs:14:19
1+
error[E0277]: the trait bound `(): const Foo` is not satisfied
2+
--> $DIR/trait_alias.rs:25:19
33
|
4-
LL | const trait Foo = [const] Bar + Baz;
5-
| ^^^^^^^
4+
LL | const _: () = foo(&());
5+
| --- ^^^
6+
| |
7+
| required by a bound introduced by this call
68
|
7-
= note: this item cannot have `[const]` trait bounds
8-
9-
error: `[const]` can only be applied to `const` traits
10-
--> $DIR/trait_alias.rs:17:17
11-
|
12-
LL | const fn foo<T: [const] Foo>(x: &T) {
13-
| ^^^^^^^ can't be applied to `Foo`
14-
|
15-
help: mark `Foo` as `const` to allow it to have `const` implementations
16-
|
17-
LL | #[const_trait] const trait Foo = [const] Bar + Baz;
18-
| ++++++++++++++
19-
20-
error: `[const]` can only be applied to `const` traits
21-
--> $DIR/trait_alias.rs:17:17
9+
note: required by a bound in `foo`
10+
--> $DIR/trait_alias.rs:16:17
2211
|
2312
LL | const fn foo<T: [const] Foo>(x: &T) {
24-
| ^^^^^^^ can't be applied to `Foo`
25-
|
26-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
27-
help: mark `Foo` as `const` to allow it to have `const` implementations
28-
|
29-
LL | #[const_trait] const trait Foo = [const] Bar + Baz;
30-
| ++++++++++++++
31-
32-
error[E0277]: the trait bound `T: [const] Bar` is not satisfied
33-
--> $DIR/trait_alias.rs:20:7
34-
|
35-
LL | x.bar();
36-
| ^^^
13+
| ^^^^^^^^^^^ required by this bound in `foo`
3714

38-
error: aborting due to 4 previous errors
15+
error: aborting due to 1 previous error
3916

4017
For more information about this error, try `rustc --explain E0277`.

tests/ui/consts/trait_alias.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ impl const Bar for () {}
1212
impl const Baz for () {}
1313

1414
const trait Foo = [const] Bar + Baz;
15-
//~^ ERROR: `[const]` is not allowed here
1615

1716
const fn foo<T: [const] Foo>(x: &T) {
18-
//~^ ERROR: `[const]` can only be applied to `const` traits
19-
//~| ERROR: `[const]` can only be applied to `const` traits
2017
x.bar();
21-
//~^ ERROR: the trait bound `T: [const] Bar` is not satisfied
2218
#[cfg(fail)]
2319
{
2420
x.baz();
@@ -27,5 +23,6 @@ const fn foo<T: [const] Foo>(x: &T) {
2723
}
2824

2925
const _: () = foo(&());
26+
//~^ ERROR: `(): const Foo` is not satisfied
3027

3128
fn main() {}

0 commit comments

Comments
 (0)