Skip to content

Commit 8b122f1

Browse files
committed
Generate const predicates for const trait aliases
1 parent 5f6772c commit 8b122f1

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
@@ -1192,6 +1192,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11921192
walk_list!(this, visit_assoc_item, items, AssocCtxt::Trait);
11931193
});
11941194
}
1195+
ItemKind::TraitAlias(box TraitAlias { constness, generics, bounds, .. }) => {
1196+
let disallowed = matches!(constness, ast::Const::No)
1197+
.then(|| TildeConstReason::Trait { span: item.span });
1198+
self.with_tilde_const(disallowed, |this| {
1199+
this.visit_generics(generics);
1200+
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits)
1201+
});
1202+
}
11951203
ItemKind::Mod(safety, ident, mod_kind) => {
11961204
if let &Safety::Unsafe(span) = safety {
11971205
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
@@ -847,7 +847,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
847847
hir::ItemKind::Trait(constness, is_auto, safety, ..) => {
848848
(constness, false, is_auto == hir::IsAuto::Yes, safety)
849849
}
850-
hir::ItemKind::TraitAlias(..) => (hir::Constness::NotConst, true, false, hir::Safety::Safe),
850+
hir::ItemKind::TraitAlias(constness, ..) => (constness, true, false, hir::Safety::Safe),
851851
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
852852
};
853853

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,8 @@ pub(super) fn const_conditions<'tcx>(
10311031
Node::Item(item) => match item.kind {
10321032
hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
10331033
hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1034-
hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
1034+
hir::ItemKind::TraitAlias(_, _, generics, supertraits)
1035+
| hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
10351036
(generics, Some((item.owner_id.def_id, supertraits)), false)
10361037
}
10371038
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
@@ -1143,13 +1144,14 @@ pub(super) fn explicit_implied_const_bounds<'tcx>(
11431144
span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
11441145
}
11451146
None => match tcx.hir_node_by_def_id(def_id) {
1146-
Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
1147-
implied_predicates_with_filter(
1148-
tcx,
1149-
def_id.to_def_id(),
1150-
PredicateFilter::SelfConstIfConst,
1151-
)
1152-
}
1147+
Node::Item(hir::Item {
1148+
kind: hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..),
1149+
..
1150+
}) => implied_predicates_with_filter(
1151+
tcx,
1152+
def_id.to_def_id(),
1153+
PredicateFilter::SelfConstIfConst,
1154+
),
11531155
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
11541156
| Node::OpaqueTy(_) => {
11551157
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
@@ -2101,7 +2101,7 @@ impl<'tcx> TyCtxt<'tcx> {
21012101
DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) => {
21022102
self.constness(def_id) == hir::Constness::Const
21032103
}
2104-
DefKind::Trait => self.is_const_trait(def_id),
2104+
DefKind::TraitAlias | DefKind::Trait => self.is_const_trait(def_id),
21052105
DefKind::AssocTy => {
21062106
let parent_def_id = self.parent(def_id);
21072107
match self.def_kind(parent_def_id) {
@@ -2144,7 +2144,6 @@ impl<'tcx> TyCtxt<'tcx> {
21442144
| DefKind::Variant
21452145
| DefKind::TyAlias
21462146
| DefKind::ForeignTy
2147-
| DefKind::TraitAlias
21482147
| DefKind::TyParam
21492148
| DefKind::Const
21502149
| 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)