Skip to content

Commit

Permalink
Implement const conditions for GATs
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 21, 2024
1 parent a2df5ea commit 516ed74
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
24 changes: 14 additions & 10 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,9 @@ pub(super) fn const_conditions<'tcx>(
}
}
DefKind::Impl { .. } => {
if !tcx
.trait_id_of_impl(def_id.to_def_id())
.is_some_and(|def_id| tcx.is_const_trait(def_id))
{
// FIXME(effects): Should be using a dedicated function to
// test if this is a const trait impl.
if tcx.constness(def_id) != hir::Constness::Const {
return Default::default();
}
}
Expand All @@ -900,10 +899,9 @@ pub(super) fn const_conditions<'tcx>(
}
}
ty::AssocItemContainer::ImplContainer => {
if !tcx
.trait_id_of_impl(parent_def_id)
.is_some_and(|def_id| tcx.is_const_trait(def_id))
{
// FIXME(effects): Should be using a dedicated function to
// test if this is a const trait impl.
if tcx.constness(parent_def_id) != hir::Constness::Const {
return Default::default();
}
}
Expand All @@ -922,12 +920,18 @@ pub(super) fn const_conditions<'tcx>(
}
_ => return Default::default(),
},
// While associated types are not really const, we do allow them to have `~const`
// bounds and where clauses. `const_conditions` is responsible for gathering
// these up so we can check them in `compare_type_predicate_entailment`, and
// in `HostEffect` goal computation.
Node::TraitItem(item) => match item.kind {
hir::TraitItemKind::Fn(_, _) => (item.generics, None, true),
hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
(item.generics, None, true)
}
_ => return Default::default(),
},
Node::ImplItem(item) => match item.kind {
hir::ImplItemKind::Fn(_, _) => (item.generics, None, true),
hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => (item.generics, None, true),
_ => return Default::default(),
},
_ => return Default::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ LL | #[derive_const(PartialEq)]
|
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

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

error: aborting due to 4 previous errors; 1 warning emitted

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl<T> const Foo for T {}
impl<T> const Foo for T where T: const Specialize {}
//~^ error: const `impl` for trait `Foo` which is not marked with `#[const_trait]`
//~| error: `const` can only be applied to `#[const_trait]` traits
//~| error: `const` can only be applied to `#[const_trait]` traits
//~| error: specialization impl does not specialize any associated items
//~| error: cannot specialize on trait `Specialize`
//~| ERROR cannot specialize on predicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ error: `const` can only be applied to `#[const_trait]` traits
LL | impl<T> const Foo for T where T: const Specialize {}
| ^^^^^^^^^^

error: `const` can only be applied to `#[const_trait]` traits
--> $DIR/spec-effectvar-ice.rs:14:40
|
LL | impl<T> const Foo for T where T: const Specialize {}
| ^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: specialization impl does not specialize any associated items
--> $DIR/spec-effectvar-ice.rs:14:1
|
Expand All @@ -66,5 +74,5 @@ error: cannot specialize on trait `Specialize`
LL | impl<T> const Foo for T where T: const Specialize {}
| ^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors; 1 warning emitted
error: aborting due to 8 previous errors; 1 warning emitted

0 comments on commit 516ed74

Please sign in to comment.