Skip to content

Commit 0740aa1

Browse files
committed
Generate const predicates for const trait aliases
1 parent 66e880b commit 0740aa1

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
@@ -1197,6 +1197,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11971197
walk_list!(this, visit_assoc_item, items, AssocCtxt::Trait);
11981198
});
11991199
}
1200+
ItemKind::TraitAlias(box TraitAlias { constness, generics, bounds, .. }) => {
1201+
let disallowed = matches!(constness, ast::Const::No)
1202+
.then(|| TildeConstReason::Trait { span: item.span });
1203+
self.with_tilde_const(disallowed, |this| {
1204+
this.visit_generics(generics);
1205+
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits)
1206+
});
1207+
}
12001208
ItemKind::Mod(safety, ident, mod_kind) => {
12011209
if let &Safety::Unsafe(span) = safety {
12021210
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
@@ -1013,7 +1013,8 @@ pub(super) fn const_conditions<'tcx>(
10131013
Node::Item(item) => match item.kind {
10141014
hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
10151015
hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1016-
hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
1016+
hir::ItemKind::TraitAlias(_, _, generics, supertraits)
1017+
| hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => {
10171018
(generics, Some((item.owner_id.def_id, supertraits)), false)
10181019
}
10191020
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
@@ -1123,13 +1124,14 @@ pub(super) fn explicit_implied_const_bounds<'tcx>(
11231124
span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
11241125
}
11251126
None => match tcx.hir_node_by_def_id(def_id) {
1126-
Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
1127-
implied_predicates_with_filter(
1128-
tcx,
1129-
def_id.to_def_id(),
1130-
PredicateFilter::SelfConstIfConst,
1131-
)
1132-
}
1127+
Node::Item(hir::Item {
1128+
kind: hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..),
1129+
..
1130+
}) => implied_predicates_with_filter(
1131+
tcx,
1132+
def_id.to_def_id(),
1133+
PredicateFilter::SelfConstIfConst,
1134+
),
11331135
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
11341136
| Node::OpaqueTy(_) => {
11351137
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
@@ -2068,7 +2068,7 @@ impl<'tcx> TyCtxt<'tcx> {
20682068
DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) => {
20692069
self.constness(def_id) == hir::Constness::Const
20702070
}
2071-
DefKind::Trait => self.is_const_trait(def_id),
2071+
DefKind::TraitAlias | DefKind::Trait => self.is_const_trait(def_id),
20722072
DefKind::AssocTy => {
20732073
let parent_def_id = self.parent(def_id);
20742074
match self.def_kind(parent_def_id) {
@@ -2111,7 +2111,6 @@ impl<'tcx> TyCtxt<'tcx> {
21112111
| DefKind::Variant
21122112
| DefKind::TyAlias
21132113
| DefKind::ForeignTy
2114-
| DefKind::TraitAlias
21152114
| DefKind::TyParam
21162115
| DefKind::Const
21172116
| 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)