Skip to content

Commit 3f8ce7c

Browse files
Do not assume child bound assumptions for rigid alias
1 parent fdd1a3b commit 3f8ce7c

File tree

7 files changed

+105
-18
lines changed

7 files changed

+105
-18
lines changed

compiler/rustc_middle/src/ty/context.rs

+14
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
345345
self.item_bounds(def_id).map_bound(IntoIterator::into_iter)
346346
}
347347

348+
fn item_self_bounds(
349+
self,
350+
def_id: DefId,
351+
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
352+
self.item_super_predicates(def_id).map_bound(IntoIterator::into_iter)
353+
}
354+
355+
fn item_non_self_bounds(
356+
self,
357+
def_id: DefId,
358+
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
359+
self.item_non_self_assumptions(def_id).map_bound(IntoIterator::into_iter)
360+
}
361+
348362
fn predicates_of(
349363
self,
350364
def_id: DefId,

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

+49-14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ use crate::solve::{
1919
MaybeCause, NoSolution, QueryResult,
2020
};
2121

22+
enum AliasBoundKind {
23+
SelfBounds,
24+
NonSelfBounds,
25+
}
26+
2227
/// A candidate is a possible way to prove a goal.
2328
///
2429
/// It consists of both the `source`, which describes how that goal would be proven,
@@ -510,7 +515,12 @@ where
510515
candidates: &mut Vec<Candidate<I>>,
511516
) {
512517
let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
513-
ecx.assemble_alias_bound_candidates_recur(goal.predicate.self_ty(), goal, candidates);
518+
ecx.assemble_alias_bound_candidates_recur(
519+
goal.predicate.self_ty(),
520+
goal,
521+
candidates,
522+
AliasBoundKind::SelfBounds,
523+
);
514524
});
515525
}
516526

@@ -528,6 +538,7 @@ where
528538
self_ty: I::Ty,
529539
goal: Goal<I, G>,
530540
candidates: &mut Vec<Candidate<I>>,
541+
consider_self_bounds: AliasBoundKind,
531542
) {
532543
let (kind, alias_ty) = match self_ty.kind() {
533544
ty::Bool
@@ -580,16 +591,37 @@ where
580591
}
581592
};
582593

583-
for assumption in
584-
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
585-
{
586-
candidates.extend(G::probe_and_consider_implied_clause(
587-
self,
588-
CandidateSource::AliasBound,
589-
goal,
590-
assumption,
591-
[],
592-
));
594+
match consider_self_bounds {
595+
AliasBoundKind::SelfBounds => {
596+
for assumption in self
597+
.cx()
598+
.item_self_bounds(alias_ty.def_id)
599+
.iter_instantiated(self.cx(), alias_ty.args)
600+
{
601+
candidates.extend(G::probe_and_consider_implied_clause(
602+
self,
603+
CandidateSource::AliasBound,
604+
goal,
605+
assumption,
606+
[],
607+
));
608+
}
609+
}
610+
AliasBoundKind::NonSelfBounds => {
611+
for assumption in self
612+
.cx()
613+
.item_non_self_bounds(alias_ty.def_id)
614+
.iter_instantiated(self.cx(), alias_ty.args)
615+
{
616+
candidates.extend(G::probe_and_consider_implied_clause(
617+
self,
618+
CandidateSource::AliasBound,
619+
goal,
620+
assumption,
621+
[],
622+
));
623+
}
624+
}
593625
}
594626

595627
candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
@@ -600,9 +632,12 @@ where
600632

601633
// Recurse on the self type of the projection.
602634
match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
603-
Ok(next_self_ty) => {
604-
self.assemble_alias_bound_candidates_recur(next_self_ty, goal, candidates)
605-
}
635+
Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
636+
next_self_ty,
637+
goal,
638+
candidates,
639+
AliasBoundKind::NonSelfBounds,
640+
),
606641
Err(NoSolution) => {}
607642
}
608643
}

compiler/rustc_type_ir/src/interner.rs

+10
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ pub trait Interner:
203203
def_id: Self::DefId,
204204
) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
205205

206+
fn item_self_bounds(
207+
self,
208+
def_id: Self::DefId,
209+
) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
210+
211+
fn item_non_self_bounds(
212+
self,
213+
def_id: Self::DefId,
214+
) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
215+
206216
fn predicates_of(
207217
self,
208218
def_id: Self::DefId,

tests/ui/associated-type-bounds/cant-see-copy-bound-from-child-rigid.stderr renamed to tests/ui/associated-type-bounds/cant-see-copy-bound-from-child-rigid.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0382]: use of moved value: `x`
2-
--> $DIR/cant-see-copy-bound-from-child-rigid.rs:14:9
2+
--> $DIR/cant-see-copy-bound-from-child-rigid.rs:18:9
33
|
44
LL | fn foo<T: Trait>(x: T::Assoc) -> (T::Assoc, T::Assoc)
55
| - move occurs because `x` has type `<T as Trait>::Assoc`, which does not implement the `Copy` trait
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/cant-see-copy-bound-from-child-rigid.rs:18:9
3+
|
4+
LL | fn foo<T: Trait>(x: T::Assoc) -> (T::Assoc, T::Assoc)
5+
| - move occurs because `x` has type `<T as Trait>::Assoc`, which does not implement the `Copy` trait
6+
...
7+
LL | (x, x)
8+
| - ^ value used here after move
9+
| |
10+
| value moved here
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0382`.

tests/ui/associated-type-bounds/cant-see-copy-bound-from-child-rigid.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
4+
15
trait Id {
26
type This: ?Sized;
37
}

tests/ui/traits/const-traits/const-impl-trait.stderr

+13-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ note: `PartialEq` can't be used with `~const` because it isn't annotated with `#
9999
--> $SRC_DIR/core/src/cmp.rs:LL:COL
100100
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
101101

102+
error: `~const` can only be applied to `#[const_trait]` traits
103+
--> $DIR/const-impl-trait.rs:27:22
104+
|
105+
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy {
106+
| ^^^^^^ can't be applied to `PartialEq`
107+
|
108+
note: `PartialEq` can't be used with `~const` because it isn't annotated with `#[const_trait]`
109+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
110+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
111+
102112
error: `~const` can only be applied to `#[const_trait]` traits
103113
--> $DIR/const-impl-trait.rs:23:22
104114
|
@@ -120,9 +130,9 @@ note: `PartialEq` can't be used with `~const` because it isn't annotated with `#
120130
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
121131

122132
error: `~const` can only be applied to `#[const_trait]` traits
123-
--> $DIR/const-impl-trait.rs:27:22
133+
--> $DIR/const-impl-trait.rs:23:22
124134
|
125-
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy {
135+
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy;
126136
| ^^^^^^ can't be applied to `PartialEq`
127137
|
128138
note: `PartialEq` can't be used with `~const` because it isn't annotated with `#[const_trait]`
@@ -181,7 +191,7 @@ LL | a == a
181191
|
182192
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
183193

184-
error: aborting due to 20 previous errors
194+
error: aborting due to 21 previous errors
185195

186196
Some errors have detailed explanations: E0015, E0635.
187197
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)