Skip to content

Commit 702225e

Browse files
authored
Rollup merge of #120598 - compiler-errors:no-rigid-check, r=lcnr
No need to `validate_alias_bound_self_from_param_env` in `assemble_alias_bound_candidates` We already fully normalize the self type before we reach `assemble_alias_bound_candidates`, so there's no reason to double check that a projection is truly rigid by checking param-env bounds. I think this is also blocked on us making sure to always normalize opaques: #120549. r? lcnr
2 parents 933a05b + 6dea155 commit 702225e

File tree

5 files changed

+6
-175
lines changed

5 files changed

+6
-175
lines changed

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

+1-115
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::solve::GoalSource;
55
use crate::traits::coherence;
66
use rustc_hir::def_id::DefId;
77
use rustc_infer::traits::query::NoSolution;
8-
use rustc_infer::traits::Reveal;
98
use rustc_middle::traits::solve::inspect::ProbeKind;
109
use rustc_middle::traits::solve::{
1110
CandidateSource, CanonicalResponse, Certainty, Goal, MaybeCause, QueryResult,
@@ -70,20 +69,6 @@ pub(super) trait GoalKind<'tcx>:
7069
})
7170
}
7271

73-
/// Consider a bound originating from the item bounds of an alias. For this we
74-
/// require that the well-formed requirements of the self type of the goal
75-
/// are "satisfied from the param-env".
76-
/// See [`EvalCtxt::validate_alias_bound_self_from_param_env`].
77-
fn consider_alias_bound_candidate(
78-
ecx: &mut EvalCtxt<'_, 'tcx>,
79-
goal: Goal<'tcx, Self>,
80-
assumption: ty::Clause<'tcx>,
81-
) -> QueryResult<'tcx> {
82-
Self::probe_and_match_goal_against_assumption(ecx, goal, assumption, |ecx| {
83-
ecx.validate_alias_bound_self_from_param_env(goal)
84-
})
85-
}
86-
8772
/// Consider a clause specifically for a `dyn Trait` self type. This requires
8873
/// additionally checking all of the supertraits and object bounds to hold,
8974
/// since they're not implied by the well-formedness of the object type.
@@ -636,7 +621,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
636621
for assumption in
637622
self.tcx().item_bounds(alias_ty.def_id).instantiate(self.tcx(), alias_ty.args)
638623
{
639-
match G::consider_alias_bound_candidate(self, goal, assumption) {
624+
match G::consider_implied_clause(self, goal, assumption, []) {
640625
Ok(result) => {
641626
candidates.push(Candidate { source: CandidateSource::AliasBound, result });
642627
}
@@ -657,105 +642,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
657642
}
658643
}
659644

660-
/// Check that we are allowed to use an alias bound originating from the self
661-
/// type of this goal. This means something different depending on the self type's
662-
/// alias kind.
663-
///
664-
/// * Projection: Given a goal with a self type such as `<Ty as Trait>::Assoc`,
665-
/// we require that the bound `Ty: Trait` can be proven using either a nested alias
666-
/// bound candidate, or a param-env candidate.
667-
///
668-
/// * Opaque: The param-env must be in `Reveal::UserFacing` mode. Otherwise,
669-
/// the goal should be proven by using the hidden type instead.
670-
#[instrument(level = "debug", skip(self), ret)]
671-
pub(super) fn validate_alias_bound_self_from_param_env<G: GoalKind<'tcx>>(
672-
&mut self,
673-
goal: Goal<'tcx, G>,
674-
) -> QueryResult<'tcx> {
675-
match *goal.predicate.self_ty().kind() {
676-
ty::Alias(ty::Projection, projection_ty) => {
677-
let mut param_env_candidates = vec![];
678-
let self_trait_ref = projection_ty.trait_ref(self.tcx());
679-
680-
if self_trait_ref.self_ty().is_ty_var() {
681-
return self
682-
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
683-
}
684-
685-
let trait_goal: Goal<'_, ty::TraitPredicate<'tcx>> = goal.with(
686-
self.tcx(),
687-
ty::TraitPredicate {
688-
trait_ref: self_trait_ref,
689-
polarity: ty::ImplPolarity::Positive,
690-
},
691-
);
692-
693-
self.assemble_param_env_candidates(trait_goal, &mut param_env_candidates);
694-
// FIXME: We probably need some sort of recursion depth check here.
695-
// Can't come up with an example yet, though, and the worst case
696-
// we can have is a compiler stack overflow...
697-
self.assemble_alias_bound_candidates(trait_goal, &mut param_env_candidates);
698-
699-
// FIXME: We must also consider alias-bound candidates for a peculiar
700-
// class of built-in candidates that I'll call "defaulted" built-ins.
701-
//
702-
// For example, we always know that `T: Pointee` is implemented, but
703-
// we do not always know what `<T as Pointee>::Metadata` actually is,
704-
// similar to if we had a user-defined impl with a `default type ...`.
705-
// For these traits, since we're not able to always normalize their
706-
// associated types to a concrete type, we must consider their alias bounds
707-
// instead, so we can prove bounds such as `<T as Pointee>::Metadata: Copy`.
708-
self.assemble_alias_bound_candidates_for_builtin_impl_default_items(
709-
trait_goal,
710-
&mut param_env_candidates,
711-
);
712-
713-
self.merge_candidates(param_env_candidates)
714-
}
715-
ty::Alias(ty::Opaque, _opaque_ty) => match goal.param_env.reveal() {
716-
Reveal::UserFacing => {
717-
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
718-
}
719-
Reveal::All => return Err(NoSolution),
720-
},
721-
_ => bug!("only expected to be called on alias tys"),
722-
}
723-
}
724-
725-
/// Assemble a subset of builtin impl candidates for a class of candidates called
726-
/// "defaulted" built-in traits.
727-
///
728-
/// For example, we always know that `T: Pointee` is implemented, but we do not
729-
/// always know what `<T as Pointee>::Metadata` actually is! See the comment in
730-
/// [`EvalCtxt::validate_alias_bound_self_from_param_env`] for more detail.
731-
#[instrument(level = "debug", skip_all)]
732-
fn assemble_alias_bound_candidates_for_builtin_impl_default_items<G: GoalKind<'tcx>>(
733-
&mut self,
734-
goal: Goal<'tcx, G>,
735-
candidates: &mut Vec<Candidate<'tcx>>,
736-
) {
737-
let lang_items = self.tcx().lang_items();
738-
let trait_def_id = goal.predicate.trait_def_id(self.tcx());
739-
740-
// You probably shouldn't add anything to this list unless you
741-
// know what you're doing.
742-
let result = if lang_items.pointee_trait() == Some(trait_def_id) {
743-
G::consider_builtin_pointee_candidate(self, goal)
744-
} else if lang_items.discriminant_kind_trait() == Some(trait_def_id) {
745-
G::consider_builtin_discriminant_kind_candidate(self, goal)
746-
} else {
747-
Err(NoSolution)
748-
};
749-
750-
match result {
751-
Ok(result) => candidates.push(Candidate {
752-
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
753-
result,
754-
}),
755-
Err(NoSolution) => (),
756-
}
757-
}
758-
759645
#[instrument(level = "debug", skip_all)]
760646
fn assemble_object_bound_candidates<G: GoalKind<'tcx>>(
761647
&mut self,

tests/ui/for/issue-20605.next.stderr

+2-40
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,12 @@ error[E0277]: the trait bound `dyn Iterator<Item = &'a mut u8>: IntoIterator` is
44
LL | for item in *things { *item = 0 }
55
| ^^^^^^^ the trait `IntoIterator` is not implemented for `dyn Iterator<Item = &'a mut u8>`
66

7-
error[E0277]: the size for values of type `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` cannot be known at compilation time
8-
--> $DIR/issue-20605.rs:5:17
9-
|
10-
LL | for item in *things { *item = 0 }
11-
| ^^^^^^^ doesn't have a size known at compile-time
12-
|
13-
= help: the trait `Sized` is not implemented for `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter`
14-
= note: all local variables must have a statically known size
15-
= help: unsized locals are gated as an unstable feature
16-
177
error: the type `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` is not well-formed
188
--> $DIR/issue-20605.rs:5:17
199
|
2010
LL | for item in *things { *item = 0 }
2111
| ^^^^^^^
2212

23-
error[E0277]: `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` is not an iterator
24-
--> $DIR/issue-20605.rs:5:17
25-
|
26-
LL | for item in *things { *item = 0 }
27-
| ^^^^^^^ `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` is not an iterator
28-
|
29-
= help: the trait `Iterator` is not implemented for `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter`
30-
3113
error: the type `&mut <dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` is not well-formed
3214
--> $DIR/issue-20605.rs:5:17
3315
|
@@ -40,33 +22,13 @@ error: the type `Option<<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::Into
4022
LL | for item in *things { *item = 0 }
4123
| ^^^^^^^
4224

43-
error[E0277]: the size for values of type `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item` cannot be known at compilation time
44-
--> $DIR/issue-20605.rs:5:5
45-
|
46-
LL | for item in *things { *item = 0 }
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
48-
|
49-
= help: the trait `Sized` is not implemented for `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item`
50-
note: required by a bound in `None`
51-
--> $SRC_DIR/core/src/option.rs:LL:COL
52-
53-
error[E0277]: the size for values of type `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item` cannot be known at compilation time
54-
--> $DIR/issue-20605.rs:5:9
55-
|
56-
LL | for item in *things { *item = 0 }
57-
| ^^^^ doesn't have a size known at compile-time
58-
|
59-
= help: the trait `Sized` is not implemented for `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item`
60-
= note: all local variables must have a statically known size
61-
= help: unsized locals are gated as an unstable feature
62-
63-
error[E0614]: type `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item` cannot be dereferenced
25+
error[E0614]: type `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::Item` cannot be dereferenced
6426
--> $DIR/issue-20605.rs:5:27
6527
|
6628
LL | for item in *things { *item = 0 }
6729
| ^^^^^
6830

69-
error: aborting due to 9 previous errors
31+
error: aborting due to 5 previous errors
7032

7133
Some errors have detailed explanations: E0277, E0614.
7234
For more information about an error, try `rustc --explain E0277`.

tests/ui/for/issue-20605.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ fn changer<'a>(mut things: Box<dyn Iterator<Item=&'a mut u8>>) {
55
for item in *things { *item = 0 }
66
//[current]~^ ERROR the size for values of type
77
//[next]~^^ ERROR the trait bound `dyn Iterator<Item = &'a mut u8>: IntoIterator` is not satisfied
8-
//[next]~| ERROR the size for values of type `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` cannot be known at compilation time
98
//[next]~| ERROR the type `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` is not well-formed
10-
//[next]~| ERROR `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` is not an iterator
119
//[next]~| ERROR the type `&mut <dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter` is not well-formed
12-
//[next]~| ERROR the size for values of type `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item` cannot be known at compilation time
1310
//[next]~| ERROR the type `Option<<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item>` is not well-formed
14-
//[next]~| ERROR the size for values of type `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item` cannot be known at compilation time
15-
//[next]~| ERROR type `<<dyn Iterator<Item = &'a mut u8> as IntoIterator>::IntoIter as Iterator>::Item` cannot be dereferenced
11+
//[next]~| ERROR type `<dyn Iterator<Item = &'a mut u8> as IntoIterator>::Item` cannot be dereferenced
12+
1613
// FIXME(-Znext-solver): these error messages are horrible and have to be
1714
// improved before we stabilize the new solver.
1815
}

tests/ui/traits/next-solver/object-unsafety.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub fn copy_any<T>(t: &T) -> T {
1414
//~| ERROR the trait bound `dyn Setup<From = T>: Setup` is not satisfied
1515
//~| ERROR mismatched types
1616
//~| ERROR the type `<dyn Setup<From = T> as Setup>::From` is not well-formed
17-
//~| ERROR the size for values of type `<dyn Setup<From = T> as Setup>::From` cannot be known at compilation time
1817

1918
// FIXME(-Znext-solver): These error messages are horrible and some of them
2019
// are even simple fallout from previous error.

tests/ui/traits/next-solver/object-unsafety.stderr

+1-14
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,7 @@ error: the type `<dyn Setup<From = T> as Setup>::From` is not well-formed
4242
LL | copy::<dyn Setup<From=T>>(t)
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

45-
error[E0277]: the size for values of type `<dyn Setup<From = T> as Setup>::From` cannot be known at compilation time
46-
--> $DIR/object-unsafety.rs:12:5
47-
|
48-
LL | copy::<dyn Setup<From=T>>(t)
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
50-
|
51-
= help: the trait `Sized` is not implemented for `<dyn Setup<From = T> as Setup>::From`
52-
= note: the return type of a function must have a statically known size
53-
help: consider further restricting the associated type
54-
|
55-
LL | pub fn copy_any<T>(t: &T) -> T where <dyn Setup<From = T> as Setup>::From: Sized {
56-
| +++++++++++++++++++++++++++++++++++++++++++++++++
57-
58-
error: aborting due to 5 previous errors
45+
error: aborting due to 4 previous errors
5946

6047
Some errors have detailed explanations: E0277, E0308.
6148
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)