Skip to content

Commit 0458d8a

Browse files
Rollup merge of #124844 - compiler-errors:shadow-probe, r=lcnr
Use a proper probe for shadowing impl r? lcnr
2 parents 3628783 + dbd2ca6 commit 0458d8a

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
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::bug;
@@ -16,7 +16,6 @@ use rustc_middle::ty::{fast_reject, TypeFoldable};
1616
use rustc_middle::ty::{ToPredicate, TypeVisitableExt};
1717
use rustc_span::{ErrorGuaranteed, DUMMY_SP};
1818
use std::fmt::Debug;
19-
use std::mem;
2019

2120
pub(super) mod structural_traits;
2221

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

837834
/// 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
@@ -176,7 +176,8 @@ fn to_selection<'tcx>(
176176
| ProbeKind::UnsizeAssembly
177177
| ProbeKind::UpcastProjectionCompatibility
178178
| ProbeKind::OpaqueTypeStorageLookup { result: _ }
179-
| ProbeKind::Root { result: _ } => {
179+
| ProbeKind::Root { result: _ }
180+
| ProbeKind::ShadowedEnvProbing => {
180181
span_bug!(span, "didn't expect to assemble trait candidate from {:#?}", cand.kind())
181182
}
182183
})

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

+24-10
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_middle::traits::query::NoSolution;
1818
use rustc_middle::traits::solve::{inspect, QueryResult};
1919
use rustc_middle::traits::solve::{Certainty, Goal};
2020
use rustc_middle::traits::ObligationCause;
21-
use rustc_middle::ty;
2221
use rustc_middle::ty::TypeFoldable;
22+
use rustc_middle::{bug, ty};
2323
use rustc_span::{Span, DUMMY_SP};
2424

2525
use crate::solve::eval_ctxt::canonical;
@@ -290,12 +290,25 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
290290
match *step {
291291
inspect::ProbeStep::AddGoal(source, goal) => nested_goals.push((source, goal)),
292292
inspect::ProbeStep::NestedProbe(ref probe) => {
293-
// Nested probes have to prove goals added in their parent
294-
// but do not leak them, so we truncate the added goals
295-
// afterwards.
296-
let num_goals = nested_goals.len();
297-
self.candidates_recur(candidates, nested_goals, probe);
298-
nested_goals.truncate(num_goals);
293+
match probe.kind {
294+
// These never assemble candidates for the goal we're trying to solve.
295+
inspect::ProbeKind::UpcastProjectionCompatibility
296+
| inspect::ProbeKind::ShadowedEnvProbing => continue,
297+
298+
inspect::ProbeKind::NormalizedSelfTyAssembly
299+
| inspect::ProbeKind::UnsizeAssembly
300+
| inspect::ProbeKind::Root { .. }
301+
| inspect::ProbeKind::TryNormalizeNonRigid { .. }
302+
| inspect::ProbeKind::TraitCandidate { .. }
303+
| inspect::ProbeKind::OpaqueTypeStorageLookup { .. } => {
304+
// Nested probes have to prove goals added in their parent
305+
// but do not leak them, so we truncate the added goals
306+
// afterwards.
307+
let num_goals = nested_goals.len();
308+
self.candidates_recur(candidates, nested_goals, probe);
309+
nested_goals.truncate(num_goals);
310+
}
311+
}
299312
}
300313
inspect::ProbeStep::MakeCanonicalResponse { shallow_certainty: c } => {
301314
assert_eq!(shallow_certainty.replace(c), None);
@@ -308,9 +321,10 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
308321
}
309322

310323
match probe.kind {
311-
inspect::ProbeKind::NormalizedSelfTyAssembly
312-
| inspect::ProbeKind::UnsizeAssembly
313-
| inspect::ProbeKind::UpcastProjectionCompatibility => (),
324+
inspect::ProbeKind::UpcastProjectionCompatibility
325+
| inspect::ProbeKind::ShadowedEnvProbing => bug!(),
326+
327+
inspect::ProbeKind::NormalizedSelfTyAssembly | inspect::ProbeKind::UnsizeAssembly => {}
314328

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

0 commit comments

Comments
 (0)