Skip to content

Commit b39ef8b

Browse files
Use a proper probe for shadowing impl
1 parent 14081a2 commit b39ef8b

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

compiler/rustc_middle/src/traits/solve/inspect.rs

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ pub enum ProbeKind<'tcx> {
153153
/// do a probe to find out what projection type(s) may be used to prove that
154154
/// the source type upholds all of the target type's object bounds.
155155
UpcastProjectionCompatibility,
156+
/// Looking for param-env candidates that satisfy the trait ref for a projection.
157+
ShadowedEnvProbing,
156158
/// Try to unify an opaque type with an existing key in the storage.
157159
OpaqueTypeStorageLookup { result: QueryResult<'tcx> },
158160
}

compiler/rustc_middle/src/traits/solve/inspect/format.rs

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
118118
ProbeKind::TraitCandidate { source, result } => {
119119
write!(self.f, "CANDIDATE {source:?}: {result:?}")
120120
}
121+
ProbeKind::ShadowedEnvProbing => {
122+
write!(self.f, "PROBING FOR IMPLS SHADOWED BY PARAM-ENV CANDIDATE:")
123+
}
121124
}?;
122125

123126
self.nested(|this| {

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

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Code shared by trait and projection goals for candidate assembly.
22
33
use crate::solve::GoalSource;
4-
use crate::solve::{inspect, EvalCtxt, SolverMode};
4+
use crate::solve::{EvalCtxt, SolverMode};
55
use rustc_hir::def_id::DefId;
66
use rustc_infer::traits::query::NoSolution;
77
use rustc_middle::traits::solve::inspect::ProbeKind;
@@ -15,7 +15,6 @@ use rustc_middle::ty::{fast_reject, TypeFoldable};
1515
use rustc_middle::ty::{ToPredicate, TypeVisitableExt};
1616
use rustc_span::{ErrorGuaranteed, DUMMY_SP};
1717
use std::fmt::Debug;
18-
use std::mem;
1918

2019
pub(super) mod structural_traits;
2120

@@ -791,17 +790,16 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
791790
goal: Goal<'tcx, G>,
792791
candidates: &mut Vec<Candidate<'tcx>>,
793792
) {
794-
// HACK: We temporarily remove the `ProofTreeBuilder` to
795-
// avoid adding `Trait` candidates to the candidates used
796-
// to prove the current goal.
797-
let inspect = mem::replace(&mut self.inspect, inspect::ProofTreeBuilder::new_noop());
798-
799793
let tcx = self.tcx();
800794
let trait_goal: Goal<'tcx, ty::TraitPredicate<'tcx>> =
801795
goal.with(tcx, goal.predicate.trait_ref(tcx));
802-
let mut trait_candidates_from_env = Vec::new();
803-
self.assemble_param_env_candidates(trait_goal, &mut trait_candidates_from_env);
804-
self.assemble_alias_bound_candidates(trait_goal, &mut trait_candidates_from_env);
796+
797+
let mut trait_candidates_from_env = vec![];
798+
self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
799+
ecx.assemble_param_env_candidates(trait_goal, &mut trait_candidates_from_env);
800+
ecx.assemble_alias_bound_candidates(trait_goal, &mut trait_candidates_from_env);
801+
});
802+
805803
if !trait_candidates_from_env.is_empty() {
806804
let trait_env_result = self.merge_candidates(trait_candidates_from_env);
807805
match trait_env_result.unwrap().value.certainty {
@@ -830,7 +828,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
830828
}
831829
}
832830
}
833-
self.inspect = inspect;
834831
}
835832

836833
/// If there are multiple ways to prove a trait or projection goal, we have

compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ fn to_selection<'tcx>(
175175
| ProbeKind::UnsizeAssembly
176176
| ProbeKind::UpcastProjectionCompatibility
177177
| ProbeKind::OpaqueTypeStorageLookup { result: _ }
178-
| ProbeKind::Root { result: _ } => {
178+
| ProbeKind::Root { result: _ }
179+
| ProbeKind::ShadowedEnvProbing => {
179180
span_bug!(span, "didn't expect to assemble trait candidate from {:#?}", cand.kind())
180181
}
181182
})

compiler/rustc_trait_selection/src/solve/inspect/analyse.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,25 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
277277
match *step {
278278
inspect::ProbeStep::AddGoal(source, goal) => nested_goals.push((source, goal)),
279279
inspect::ProbeStep::NestedProbe(ref probe) => {
280-
// Nested probes have to prove goals added in their parent
281-
// but do not leak them, so we truncate the added goals
282-
// afterwards.
283-
let num_goals = nested_goals.len();
284-
self.candidates_recur(candidates, nested_goals, probe);
285-
nested_goals.truncate(num_goals);
280+
match probe.kind {
281+
// These never assemble candidates for the goal we're trying to solve.
282+
inspect::ProbeKind::UpcastProjectionCompatibility
283+
| inspect::ProbeKind::ShadowedEnvProbing => continue,
284+
285+
inspect::ProbeKind::NormalizedSelfTyAssembly
286+
| inspect::ProbeKind::UnsizeAssembly
287+
| inspect::ProbeKind::Root { .. }
288+
| inspect::ProbeKind::TryNormalizeNonRigid { .. }
289+
| inspect::ProbeKind::TraitCandidate { .. }
290+
| inspect::ProbeKind::OpaqueTypeStorageLookup { .. } => {
291+
// Nested probes have to prove goals added in their parent
292+
// but do not leak them, so we truncate the added goals
293+
// afterwards.
294+
let num_goals = nested_goals.len();
295+
self.candidates_recur(candidates, nested_goals, probe);
296+
nested_goals.truncate(num_goals);
297+
}
298+
}
286299
}
287300
inspect::ProbeStep::MakeCanonicalResponse { shallow_certainty: c } => {
288301
assert_eq!(shallow_certainty.replace(c), None);
@@ -295,9 +308,10 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
295308
}
296309

297310
match probe.kind {
298-
inspect::ProbeKind::NormalizedSelfTyAssembly
299-
| inspect::ProbeKind::UnsizeAssembly
300-
| inspect::ProbeKind::UpcastProjectionCompatibility => (),
311+
inspect::ProbeKind::UpcastProjectionCompatibility
312+
| inspect::ProbeKind::ShadowedEnvProbing => bug!(),
313+
314+
inspect::ProbeKind::NormalizedSelfTyAssembly | inspect::ProbeKind::UnsizeAssembly => {}
301315

302316
// We add a candidate even for the root evaluation if there
303317
// is only one way to prove a given goal, e.g. for `WellFormed`.

0 commit comments

Comments
 (0)