Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Param in desugaring method ~const bounds as effects #128781

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ language_item_table! {
EffectsRuntime, sym::EffectsRuntime, effects_runtime, Target::Struct, GenericRequirement::None;
EffectsNoRuntime, sym::EffectsNoRuntime, effects_no_runtime, Target::Struct, GenericRequirement::None;
EffectsMaybe, sym::EffectsMaybe, effects_maybe, Target::Struct, GenericRequirement::None;
EffectsParam, sym::EffectsParam, effects_param, Target::Struct, GenericRequirement::Exact(1);
EffectsIntersection, sym::EffectsIntersection, effects_intersection, Target::Trait, GenericRequirement::None;
EffectsIntersectionOutput, sym::EffectsIntersectionOutput, effects_intersection_output, Target::AssocTy, GenericRequirement::None;
EffectsCompat, sym::EffectsCompat, effects_compat, Target::Trait, GenericRequirement::Exact(1);
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_hir_analysis/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,15 @@ impl<'tcx> Bounds<'tcx> {
);
return;
};
let self_ty = Ty::new_projection(tcx, assoc, bound_trait_ref.skip_binder().args);
// make `<T as Tr>::Effects: Compat<runtime>`

// make `Param<runtime>: TyCompat<<T as Tr>::Effects>`
let proj_fx = Ty::new_projection(tcx, assoc, bound_trait_ref.skip_binder().args);
let param_def = tcx.require_lang_item(LangItem::EffectsParam, Some(span));
let param_ty = Ty::new_adt(tcx, tcx.adt_def(param_def), tcx.mk_args(&[compat_val.into()]));
let new_trait_ref = ty::TraitRef::new(
tcx,
tcx.require_lang_item(LangItem::EffectsCompat, Some(span)),
[ty::GenericArg::from(self_ty), compat_val.into()],
tcx.require_lang_item(LangItem::EffectsTyCompat, Some(span)),
[param_ty, proj_fx],
);
self.clauses.push((bound_trait_ref.rebind(new_trait_ref).upcast(tcx), span));
}
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
debug!(?predicates);
}

// add `Self::Effects: Compat<HOST>` to ensure non-const impls don't get called
// add `Param<RUNTIME>: TyCompat<Self::Effects>` to ensure non-const impls don't get called
// in const contexts.
if let Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(..), .. }) = node
&& let Some(host_effect_index) = generics.host_effect_index
Expand All @@ -334,14 +334,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
let Some(assoc_def_id) = tcx.associated_type_for_effects(parent) else {
bug!("associated_type_for_effects returned None when there is host effect in generics");
};
let effects =
let proj_effects =
Ty::new_projection(tcx, assoc_def_id, ty::GenericArgs::identity_for_item(tcx, parent));
let param = generics.param_at(host_effect_index, tcx);
let span = tcx.def_span(param.def_id);
let host = ty::Const::new_param(tcx, ty::ParamConst::for_def(param));
let compat = tcx.require_lang_item(LangItem::EffectsCompat, Some(span));
let trait_ref =
ty::TraitRef::new(tcx, compat, [ty::GenericArg::from(effects), host.into()]);
let param = tcx.require_lang_item(LangItem::EffectsParam, Some(span));
let args = tcx.mk_args(&[host.into()]);
let param_ty = Ty::new_adt(tcx, tcx.adt_def(param), args);
let compat = tcx.require_lang_item(LangItem::EffectsTyCompat, Some(span));
let trait_ref = ty::TraitRef::new(tcx, compat, [param_ty, proj_effects]);
predicates.push((ty::Binder::dummy(trait_ref).upcast(tcx), span));
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ symbols! {
EffectsIntersectionOutput,
EffectsMaybe,
EffectsNoRuntime,
EffectsParam,
EffectsRuntime,
EffectsTyCompat,
Effects__,
Expand Down
8 changes: 7 additions & 1 deletion library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,9 @@ pub mod effects {
#[lang = "EffectsRuntime"]
pub struct Runtime;

#[cfg_attr(not(bootstrap), lang = "EffectsParam")]
pub struct Param<const RUNTIME: bool>;

#[lang = "EffectsCompat"]
pub trait Compat<#[rustc_runtime] const RUNTIME: bool> {}

Expand All @@ -1097,8 +1100,11 @@ pub mod effects {
pub trait TyCompat<T: ?Sized> {}

impl<T: ?Sized> TyCompat<T> for T {}
impl<T: ?Sized> TyCompat<T> for Maybe {}
impl<T: ?Sized> TyCompat<Maybe> for T {}
impl TyCompat<Param<true>> for Runtime {}
impl TyCompat<Runtime> for Param<true> {}
impl TyCompat<Param<false>> for NoRuntime {}
impl TyCompat<NoRuntime> for Param<false> {}

#[lang = "EffectsIntersection"]
pub trait Intersection {
Expand Down
1 change: 1 addition & 0 deletions tests/ui/delegation/not-supported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ mod effects {

reuse Trait::foo;
//~^ ERROR delegation to a function with effect parameter is not supported yet
//~| ERROR mismatched types
}

fn main() {}
13 changes: 11 additions & 2 deletions tests/ui/delegation/not-supported.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,16 @@ LL | pub reuse to_reuse2::foo;
LL | reuse to_reuse1::foo;
| ^^^

error: aborting due to 17 previous errors; 2 warnings emitted
error[E0308]: mismatched types
--> $DIR/not-supported.rs:112:18
|
LL | reuse Trait::foo;
| ^^^ expected `true`, found `host`
|
= note: expected constant `true`
found constant `host`

error: aborting due to 18 previous errors; 2 warnings emitted

Some errors have detailed explanations: E0049, E0195, E0391.
Some errors have detailed explanations: E0049, E0195, E0308, E0391.
For more information about an error, try `rustc --explain E0049`.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ error: using `#![feature(effects)]` without enabling next trait solver globally
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable

error[E0277]: the trait bound `Trait::{synthetic#0}: Compat` is not satisfied
error[E0277]: the trait bound `Param<_>: TyCompat<Trait::{synthetic#0}>` is not satisfied
--> $DIR/assoc-type-const-bound-usage-0.rs:13:5
|
LL | T::Assoc::func()
| ^^^^^^^^ the trait `Compat` is not implemented for `Trait::{synthetic#0}`
| ^^^^^^^^ the trait `TyCompat<Trait::{synthetic#0}>` is not implemented for `Param<_>`
|
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `Trait::func`
--> $DIR/assoc-type-const-bound-usage-0.rs:6:1
|
Expand All @@ -18,12 +21,15 @@ LL | #[const_trait]
LL | fn func() -> i32;
| ---- required by a bound in this associated function

error[E0277]: the trait bound `Trait::{synthetic#0}: Compat` is not satisfied
error[E0277]: the trait bound `Param<_>: TyCompat<Trait::{synthetic#0}>` is not satisfied
--> $DIR/assoc-type-const-bound-usage-0.rs:17:5
|
LL | <T as Trait>::Assoc::func()
| ^^^^^^^^^^^^^^^^^^^ the trait `Compat` is not implemented for `Trait::{synthetic#0}`
| ^^^^^^^^^^^^^^^^^^^ the trait `TyCompat<Trait::{synthetic#0}>` is not implemented for `Param<_>`
|
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `Trait::func`
--> $DIR/assoc-type-const-bound-usage-0.rs:6:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ error: using `#![feature(effects)]` without enabling next trait solver globally
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable

error[E0277]: the trait bound `Trait::{synthetic#0}: Compat` is not satisfied
error[E0277]: the trait bound `Param<_>: TyCompat<Trait::{synthetic#0}>` is not satisfied
--> $DIR/assoc-type-const-bound-usage-1.rs:15:44
|
LL | fn unqualified<T: const Trait>() -> Type<{ T::Assoc::func() }> {
| ^^^^^^^^ the trait `Compat` is not implemented for `Trait::{synthetic#0}`
| ^^^^^^^^ the trait `TyCompat<Trait::{synthetic#0}>` is not implemented for `Param<_>`
|
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `Trait::func`
--> $DIR/assoc-type-const-bound-usage-1.rs:7:1
|
Expand All @@ -18,12 +21,15 @@ LL | #[const_trait]
LL | fn func() -> i32;
| ---- required by a bound in this associated function

error[E0277]: the trait bound `Trait::{synthetic#0}: Compat` is not satisfied
error[E0277]: the trait bound `Param<_>: TyCompat<Trait::{synthetic#0}>` is not satisfied
--> $DIR/assoc-type-const-bound-usage-1.rs:19:42
|
LL | fn qualified<T: const Trait>() -> Type<{ <T as Trait>::Assoc::func() }> {
| ^^^^^^^^^^^^^^^^^^^ the trait `Compat` is not implemented for `Trait::{synthetic#0}`
| ^^^^^^^^^^^^^^^^^^^ the trait `TyCompat<Trait::{synthetic#0}>` is not implemented for `Param<_>`
|
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `Trait::func`
--> $DIR/assoc-type-const-bound-usage-1.rs:7:1
|
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ error: using `#![feature(effects)]` without enabling next trait solver globally
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable

error[E0277]: the trait bound `Add::{synthetic#0}: Compat` is not satisfied
error[E0277]: the trait bound `Param<true>: TyCompat<Add::{synthetic#0}>` is not satisfied
--> $DIR/assoc-type.rs:41:15
|
LL | type Qux: Add;
| ^^^ the trait `Compat` is not implemented for `Add::{synthetic#0}`
| ^^^ the trait `TyCompat<Add::{synthetic#0}>` is not implemented for `Param<true>`
|
help: consider further restricting the associated type
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
LL | trait Baz where Add::{synthetic#0}: Compat {
| ++++++++++++++++++++++++++++++++
LL | trait Baz where Param<true>: TyCompat<Add::{synthetic#0}> {
| +++++++++++++++++++++++++++++++++++++++++++++++

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ error: using `#![feature(effects)]` without enabling next trait solver globally
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable

error[E0277]: the trait bound `Runtime: ~const Compat` is not satisfied
error[E0277]: the trait bound `Param<host>: TyCompat<Runtime>` is not satisfied
--> $DIR/call-const-trait-method-fail.rs:25:5
|
LL | a.plus(b)
| ^ the trait `~const Compat` is not implemented for `Runtime`
| ^ the trait `TyCompat<Runtime>` is not implemented for `Param<host>`
|
= help: the trait `Compat` is implemented for `Runtime`
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `Plus::plus`
--> $DIR/call-const-trait-method-fail.rs:3:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const fn equals_self<T: ~const Foo>(t: &T) -> bool {
// it not using the impl.

pub const EQ: bool = equals_self(&S);
//~^ ERROR: the trait bound `Runtime: const Compat` is not satisfied
//~^ ERROR: the trait bound
// FIXME(effects) diagnostic

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ error: using `#![feature(effects)]` without enabling next trait solver globally
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable

error[E0277]: the trait bound `Runtime: const Compat` is not satisfied
--> $DIR/call-generic-method-nonconst.rs:23:34
error[E0277]: the trait bound `Param<false>: TyCompat<Runtime>` is not satisfied
--> $DIR/call-generic-method-nonconst.rs:23:22
|
LL | pub const EQ: bool = equals_self(&S);
| ----------- ^^ the trait `const Compat` is not implemented for `Runtime`
| |
| required by a bound introduced by this call
| ^^^^^^^^^^^^^^^ the trait `TyCompat<Runtime>` is not implemented for `Param<false>`
|
= help: the trait `Compat` is implemented for `Runtime`
= help: the trait `TyCompat<NoRuntime>` is implemented for `Param<false>`
= help: for that trait implementation, expected `NoRuntime`, found `Runtime`
note: required by a bound in `equals_self`
--> $DIR/call-generic-method-nonconst.rs:16:25
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ error: using `#![feature(effects)]` without enabling next trait solver globally
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable

error[E0277]: the trait bound `Runtime: ~const Compat` is not satisfied
error[E0277]: the trait bound `Param<host>: TyCompat<Runtime>` is not satisfied
--> $DIR/const-default-method-bodies.rs:24:18
|
LL | NonConstImpl.a();
| ^ the trait `~const Compat` is not implemented for `Runtime`
| ^ the trait `TyCompat<Runtime>` is not implemented for `Param<host>`
|
= help: the trait `Compat` is implemented for `Runtime`
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `ConstDefaultFn::a`
--> $DIR/const-default-method-bodies.rs:3:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub mod effects {
#[lang = "EffectsRuntime"]
#[stable(feature = "minicore", since = "1.0.0")]
pub struct Runtime;
#[lang = "EffectsParam"]
#[stable(feature = "minicore", since = "1.0.0")]
pub struct Param<const RUNTIME: bool>;

#[lang = "EffectsCompat"]
#[stable(feature = "minicore", since = "1.0.0")]
Expand All @@ -122,9 +125,15 @@ pub mod effects {
#[stable(feature = "minicore", since = "1.0.0")]
impl<T: ?Sized> TyCompat<T> for T {}
#[stable(feature = "minicore", since = "1.0.0")]
impl<T: ?Sized> TyCompat<T> for Maybe {}
#[stable(feature = "minicore", since = "1.0.0")]
impl<T: ?Sized> TyCompat<Maybe> for T {}
#[stable(feature = "minicore", since = "1.0.0")]
impl TyCompat<Param<true>> for Runtime {}
#[stable(feature = "minicore", since = "1.0.0")]
impl TyCompat<Runtime> for Param<true> {}
#[stable(feature = "minicore", since = "1.0.0")]
impl TyCompat<Param<false>> for NoRuntime {}
#[stable(feature = "minicore", since = "1.0.0")]
impl TyCompat<NoRuntime> for Param<false> {}

#[lang = "EffectsIntersection"]
#[stable(feature = "minicore", since = "1.0.0")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
error[E0277]: the trait bound `FnOnce<()>::{synthetic#0}: const Compat` is not satisfied
error[E0277]: the trait bound `Param<false>: TyCompat<FnOnce<()>::{synthetic#0}>` is not satisfied
--> $DIR/const-fns-are-early-bound.rs:31:17
|
LL | is_const_fn(foo);
| ----------- ^^^ the trait `const Compat` is not implemented for `FnOnce<()>::{synthetic#0}`
| ----------- ^^^ the trait `TyCompat<FnOnce<()>::{synthetic#0}>` is not implemented for `Param<false>`
| |
| required by a bound introduced by this call
|
= help: the trait `TyCompat<NoRuntime>` is implemented for `Param<false>`
note: required by a bound in `is_const_fn`
--> $DIR/const-fns-are-early-bound.rs:25:12
|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
error[E0277]: the trait bound `cross_crate::MyTrait::{synthetic#0}: ~const Compat` is not satisfied
error[E0277]: the trait bound `Param<host>: TyCompat<cross_crate::MyTrait::{synthetic#0}>` is not satisfied
--> $DIR/cross-crate.rs:19:14
|
LL | NonConst.func();
| ^^^^ the trait `~const Compat` is not implemented for `cross_crate::MyTrait::{synthetic#0}`
| ^^^^ the trait `TyCompat<cross_crate::MyTrait::{synthetic#0}>` is not implemented for `Param<host>`
|
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `func`
--> $DIR/auxiliary/cross-crate.rs:5:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ error: using `#![feature(effects)]` without enabling next trait solver globally
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable

error[E0277]: the trait bound `Runtime: ~const Compat` is not satisfied
error[E0277]: the trait bound `Param<host>: TyCompat<Runtime>` is not satisfied
--> $DIR/default-method-body-is-const-same-trait-ck.rs:8:12
|
LL | ().a()
| ^ the trait `~const Compat` is not implemented for `Runtime`
| ^ the trait `TyCompat<Runtime>` is not implemented for `Param<host>`
|
= help: the trait `Compat` is implemented for `Runtime`
= help: the following other types implement trait `TyCompat<T>`:
`Param<false>` implements `TyCompat<NoRuntime>`
`Param<true>` implements `TyCompat<Runtime>`
note: required by a bound in `Tr::a`
--> $DIR/default-method-body-is-const-same-trait-ck.rs:3:1
|
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ mod effects {
pub struct Maybe;
#[lang = "EffectsRuntime"]
pub struct Runtime;
#[lang = "EffectsParam"]
pub struct Param<const RUNTIME: bool>;

#[lang = "EffectsCompat"]
pub trait Compat<#[rustc_runtime] const RUNTIME: bool> {}
Expand All @@ -559,8 +561,12 @@ mod effects {
pub trait TyCompat<T: ?Sized> {}

impl<T: ?Sized> TyCompat<T> for T {}
impl<T: ?Sized> TyCompat<T> for Maybe {}
impl<T: ?Sized> TyCompat<Maybe> for T {}
impl TyCompat<Param<true>> for Runtime {}
impl TyCompat<Runtime> for Param<true> {}
impl TyCompat<Param<false>> for NoRuntime {}
impl TyCompat<NoRuntime> for Param<false> {}


#[lang = "EffectsIntersection"]
pub trait Intersection {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ error: cannot specialize on const impl with non-const impl
LL | impl<T: Spec + Sup> A for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: cannot specialize on trait `Compat`
error: cannot specialize on trait `TyCompat`
--> $DIR/specializing-constness.rs:23:16
|
LL | impl<T: Spec + Sup> A for T {
| ^^^

error: cannot specialize on trait `Compat`
error: cannot specialize on trait `TyCompat`
--> $DIR/specializing-constness.rs:23:9
|
LL | impl<T: Spec + Sup> A for T {
Expand Down
Loading
Loading