Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 516ed74

Browse files
committedOct 21, 2024·
Implement const conditions for GATs
1 parent a2df5ea commit 516ed74

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed
 

‎compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -884,10 +884,9 @@ pub(super) fn const_conditions<'tcx>(
884884
}
885885
}
886886
DefKind::Impl { .. } => {
887-
if !tcx
888-
.trait_id_of_impl(def_id.to_def_id())
889-
.is_some_and(|def_id| tcx.is_const_trait(def_id))
890-
{
887+
// FIXME(effects): Should be using a dedicated function to
888+
// test if this is a const trait impl.
889+
if tcx.constness(def_id) != hir::Constness::Const {
891890
return Default::default();
892891
}
893892
}
@@ -900,10 +899,9 @@ pub(super) fn const_conditions<'tcx>(
900899
}
901900
}
902901
ty::AssocItemContainer::ImplContainer => {
903-
if !tcx
904-
.trait_id_of_impl(parent_def_id)
905-
.is_some_and(|def_id| tcx.is_const_trait(def_id))
906-
{
902+
// FIXME(effects): Should be using a dedicated function to
903+
// test if this is a const trait impl.
904+
if tcx.constness(parent_def_id) != hir::Constness::Const {
907905
return Default::default();
908906
}
909907
}
@@ -922,12 +920,18 @@ pub(super) fn const_conditions<'tcx>(
922920
}
923921
_ => return Default::default(),
924922
},
923+
// While associated types are not really const, we do allow them to have `~const`
924+
// bounds and where clauses. `const_conditions` is responsible for gathering
925+
// these up so we can check them in `compare_type_predicate_entailment`, and
926+
// in `HostEffect` goal computation.
925927
Node::TraitItem(item) => match item.kind {
926-
hir::TraitItemKind::Fn(_, _) => (item.generics, None, true),
928+
hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
929+
(item.generics, None, true)
930+
}
927931
_ => return Default::default(),
928932
},
929933
Node::ImplItem(item) => match item.kind {
930-
hir::ImplItemKind::Fn(_, _) => (item.generics, None, true),
934+
hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => (item.generics, None, true),
931935
_ => return Default::default(),
932936
},
933937
_ => return Default::default(),

‎tests/ui/rfcs/rfc-2632-const-trait-impl/const_derives/derive-const-with-params.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,14 @@ LL | #[derive_const(PartialEq)]
3030
|
3131
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
3232

33-
error: aborting due to 3 previous errors; 1 warning emitted
33+
error: `~const` can only be applied to `#[const_trait]` traits
34+
--> $DIR/derive-const-with-params.rs:7:16
35+
|
36+
LL | #[derive_const(PartialEq)]
37+
| ^^^^^^^^^
38+
|
39+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
40+
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
41+
42+
error: aborting due to 4 previous errors; 1 warning emitted
3443

‎tests/ui/rfcs/rfc-2632-const-trait-impl/effects/spec-effectvar-ice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ impl<T> const Foo for T {}
1414
impl<T> const Foo for T where T: const Specialize {}
1515
//~^ error: const `impl` for trait `Foo` which is not marked with `#[const_trait]`
1616
//~| error: `const` can only be applied to `#[const_trait]` traits
17+
//~| error: `const` can only be applied to `#[const_trait]` traits
1718
//~| error: specialization impl does not specialize any associated items
1819
//~| error: cannot specialize on trait `Specialize`
1920
//~| ERROR cannot specialize on predicate

‎tests/ui/rfcs/rfc-2632-const-trait-impl/effects/spec-effectvar-ice.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ error: `const` can only be applied to `#[const_trait]` traits
4242
LL | impl<T> const Foo for T where T: const Specialize {}
4343
| ^^^^^^^^^^
4444

45+
error: `const` can only be applied to `#[const_trait]` traits
46+
--> $DIR/spec-effectvar-ice.rs:14:40
47+
|
48+
LL | impl<T> const Foo for T where T: const Specialize {}
49+
| ^^^^^^^^^^
50+
|
51+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
52+
4553
error: specialization impl does not specialize any associated items
4654
--> $DIR/spec-effectvar-ice.rs:14:1
4755
|
@@ -66,5 +74,5 @@ error: cannot specialize on trait `Specialize`
6674
LL | impl<T> const Foo for T where T: const Specialize {}
6775
| ^^^^^^^^^^^^^^^^
6876

69-
error: aborting due to 7 previous errors; 1 warning emitted
77+
error: aborting due to 8 previous errors; 1 warning emitted
7078

0 commit comments

Comments
 (0)
Please sign in to comment.