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

Duplicate ~const bounds with a non-const one in effects desugaring #116756

Merged
merged 1 commit into from
Oct 17, 2023
Merged
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
34 changes: 33 additions & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.trait_defines_associated_item_named(r.def_id(), ty::AssocKind::Const, assoc_name)
});

let (bound, next_cand) = match (matching_candidates.next(), const_candidates.next()) {
let (mut bound, mut next_cand) = match (matching_candidates.next(), const_candidates.next())
{
(Some(bound), _) => (bound, matching_candidates.next()),
(None, Some(bound)) => (bound, const_candidates.next()),
(None, None) => {
Expand All @@ -1103,6 +1104,37 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
};
debug!(?bound);

// look for a candidate that is not the same as our first bound, disregarding
// whether the bound is const.
while let Some(mut bound2) = next_cand {
debug!(?bound2);
let tcx = self.tcx();
if bound2.bound_vars() != bound.bound_vars() {
break;
}

let generics = tcx.generics_of(bound.def_id());
let Some(host_index) = generics.host_effect_index else { break };

// always return the bound that contains the host param.
if let ty::ConstKind::Param(_) = bound2.skip_binder().args.const_at(host_index).kind() {
(bound, bound2) = (bound2, bound);
}

let unconsted_args = bound
.skip_binder()
.args
.iter()
.enumerate()
.map(|(n, arg)| if host_index == n { tcx.consts.true_.into() } else { arg });

if unconsted_args.eq(bound2.skip_binder().args.iter()) {
next_cand = matching_candidates.next().or_else(|| const_candidates.next());
} else {
break;
}
}

if let Some(bound2) = next_cand {
debug!(?bound2);

Expand Down
27 changes: 27 additions & 0 deletions compiler/rustc_hir_analysis/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ impl<'tcx> Bounds<'tcx> {
trait_ref: ty::PolyTraitRef<'tcx>,
span: Span,
polarity: ty::ImplPolarity,
) {
self.push_trait_bound_inner(tcx, trait_ref, span, polarity);

// if we have a host param, we push an unconst trait bound in addition
// to the const one.
// FIXME(effects) we should find a better way than name matching
if tcx.features().effects && trait_ref.skip_binder().args.host_effect_param().is_some() {
let generics = tcx.generics_of(trait_ref.def_id());
let Some(host_index) = generics.host_effect_index else { return };
let trait_ref = trait_ref.map_bound(|mut trait_ref| {
trait_ref.args =
tcx.mk_args_from_iter(trait_ref.args.iter().enumerate().map(|(n, arg)| {
if host_index == n { tcx.consts.true_.into() } else { arg }
}));
trait_ref
});

self.push_trait_bound_inner(tcx, trait_ref, span, polarity);
}
}

fn push_trait_bound_inner(
&mut self,
tcx: TyCtxt<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
span: Span,
polarity: ty::ImplPolarity,
) {
self.clauses.push((
trait_ref
Expand Down
37 changes: 32 additions & 5 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir::intravisit::{self, Visitor};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{GenericPredicates, ImplTraitInTraitData, ToPredicate};
use rustc_span::symbol::Ident;
use rustc_span::{Span, DUMMY_SP};
use rustc_span::{sym, Span, DUMMY_SP};

/// Returns a list of all type predicates (explicit and implicit) for the definition with
/// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
Expand All @@ -38,11 +38,38 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic
// an obligation and instead be skipped. Otherwise we'd use
// `tcx.def_span(def_id);`
let span = rustc_span::DUMMY_SP;
result.predicates =
tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(std::iter::once((
ty::TraitRef::identity(tcx, def_id).to_predicate(tcx),
let non_const_bound = if tcx.features().effects && tcx.has_attr(def_id, sym::const_trait) {
// when `Self` is a const trait, also add `Self: Trait<.., true>` as implied bound,
// because only implementing `Self: Trait<.., false>` is currently not possible.
Some((
ty::TraitRef::new(
tcx,
def_id,
ty::GenericArgs::for_item(tcx, def_id, |param, _| {
if param.is_host_effect() {
tcx.consts.true_.into()
} else {
tcx.mk_param_from_def(param)
}
}),
)
.to_predicate(tcx),
span,
))));
))
} else {
None
};
result.predicates = tcx.arena.alloc_from_iter(
result
.predicates
.iter()
.copied()
.chain(std::iter::once((
ty::TraitRef::identity(tcx, def_id).to_predicate(tcx),
span,
)))
.chain(non_const_bound),
);
}
debug!("predicates_of(def_id={:?}) = {:?}", def_id, result);
result
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
let item_def_id = tcx.hir().ty_param_owner(def_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
// HACK(eddyb) should get the original `Span`.
let span = tcx.def_span(def_id);
ty::GenericPredicates {
parent: None,
predicates: tcx.arena.alloc_from_iter(
self.param_env.caller_bounds().iter().filter_map(|predicate| {
match predicate.kind().skip_binder() {
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
// HACK(eddyb) should get the original `Span`.
let span = tcx.def_span(def_id);
Some((predicate, span))
}
_ => None,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl GenericParamDef {
}
}

pub fn is_host_effect(&self) -> bool {
matches!(self.kind, GenericParamDefKind::Const { is_host_effect: true, .. })
}

pub fn default_value<'tcx>(
&self,
tcx: TyCtxt<'tcx>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
error[E0277]: the trait bound `T: Foo` is not satisfied
--> $DIR/assoc-type-const-bound-usage.rs:12:6
error[E0308]: mismatched types
--> $DIR/assoc-type-const-bound-usage.rs:12:5
|
LL | <T as Foo>::Assoc::foo();
| ^ the trait `Foo` is not implemented for `T`
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `host`, found `true`
|
help: consider further restricting this bound
|
LL | const fn foo<T: ~const Foo + Foo>() {
| +++++
= note: expected constant `host`
found constant `true`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0308`.
Loading
Loading