Skip to content

Allow filtering what proof trees are dumped to stdout via a rustc attr #113694

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

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_doc_primitive, Normal, template!(NameValueStr: "primitive name"), ErrorFollowing,
r#"`rustc_doc_primitive` is a rustc internal attribute"#,
),
rustc_attr!(
rustc_filter_proof_tree_dump, Normal, template!(List: "filter1, filter2, ..."), DuplicatesOk,
"the `#[rustc_filter_proof_tree_dump(...)]` attribute is used to filter out proof trees \
from `-Zdump-solver-proof-tree` output.",
),

// ==========================================================================
// Internal attributes, Testing:
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ pub struct LocalDefId {
impl !Ord for LocalDefId {}
impl !PartialOrd for LocalDefId {}

/// The [`DefId`] for the local crate root.
pub const CRATE_DEF_ID: LocalDefId = LocalDefId { local_def_index: CRATE_DEF_INDEX };

impl Idx for LocalDefId {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,7 @@ symbols! {
rustc_error,
rustc_evaluate_where_clauses,
rustc_expected_cgu_reuse,
rustc_filter_proof_tree_dump,
rustc_has_incoherent_inherent_impls,
rustc_host,
rustc_if_this_changed,
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_trait_selection/src/solve/eval_ctxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ impl<'tcx> InferCtxtEvalExt<'tcx> for InferCtxt<'tcx> {
Result<(bool, Certainty, Vec<Goal<'tcx, ty::Predicate<'tcx>>>), NoSolution>,
Option<inspect::GoalEvaluation<'tcx>>,
) {
EvalCtxt::enter_root(self, generate_proof_tree, |ecx| {
ecx.evaluate_goal(IsNormalizesToHack::No, goal)
})
EvalCtxt::enter_root(
self,
generate_proof_tree,
|| format!("{:?}", goal.predicate),
|ecx| ecx.evaluate_goal(IsNormalizesToHack::No, goal),
)
}
}

Expand All @@ -176,6 +179,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
fn enter_root<R>(
infcx: &InferCtxt<'tcx>,
generate_proof_tree: GenerateProofTree,
filter: impl FnOnce() -> String,
f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> R,
) -> (R, Option<inspect::GoalEvaluation<'tcx>>) {
let mode = if infcx.intercrate { SolverMode::Coherence } else { SolverMode::Normal };
Expand All @@ -194,7 +198,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
var_values: CanonicalVarValues::dummy(),
nested_goals: NestedGoals::new(),
tainted: Ok(()),
inspect: ProofTreeBuilder::new_maybe_root(infcx.tcx, generate_proof_tree),
inspect: ProofTreeBuilder::new_maybe_root(infcx.tcx, generate_proof_tree, filter),
};
let result = f(&mut ecx);

Expand Down
75 changes: 40 additions & 35 deletions compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,51 @@ impl<'tcx> InferCtxtSelectExt<'tcx> for InferCtxt<'tcx> {
self.instantiate_binder_with_placeholders(obligation.predicate),
);

let (result, _) = EvalCtxt::enter_root(self, GenerateProofTree::Never, |ecx| {
let goal = Goal::new(ecx.tcx(), trait_goal.param_env, trait_goal.predicate);
let (orig_values, canonical_goal) = ecx.canonicalize_goal(goal);
let mut candidates = ecx.compute_canonical_trait_candidates(canonical_goal);

// pseudo-winnow
if candidates.len() == 0 {
return Err(SelectionError::Unimplemented);
} else if candidates.len() > 1 {
let mut i = 0;
while i < candidates.len() {
let should_drop_i = (0..candidates.len()).filter(|&j| i != j).any(|j| {
candidate_should_be_dropped_in_favor_of(
ecx.tcx(),
&candidates[i],
&candidates[j],
)
});
if should_drop_i {
candidates.swap_remove(i);
} else {
i += 1;
if i > 1 {
return Ok(None);
let (result, _) = EvalCtxt::enter_root(
self,
GenerateProofTree::Never,
|| unreachable!("proof trees cannot be generated for selection"),
|ecx| {
let goal = Goal::new(ecx.tcx(), trait_goal.param_env, trait_goal.predicate);
let (orig_values, canonical_goal) = ecx.canonicalize_goal(goal);
let mut candidates = ecx.compute_canonical_trait_candidates(canonical_goal);

// pseudo-winnow
if candidates.len() == 0 {
return Err(SelectionError::Unimplemented);
} else if candidates.len() > 1 {
let mut i = 0;
while i < candidates.len() {
let should_drop_i = (0..candidates.len()).filter(|&j| i != j).any(|j| {
candidate_should_be_dropped_in_favor_of(
ecx.tcx(),
&candidates[i],
&candidates[j],
)
});
if should_drop_i {
candidates.swap_remove(i);
} else {
i += 1;
if i > 1 {
return Ok(None);
}
}
}
}
}

let candidate = candidates.pop().unwrap();
let (certainty, nested_goals) = ecx
.instantiate_and_apply_query_response(
trait_goal.param_env,
orig_values,
candidate.result,
)
.map_err(|_| SelectionError::Unimplemented)?;
let candidate = candidates.pop().unwrap();
let (certainty, nested_goals) = ecx
.instantiate_and_apply_query_response(
trait_goal.param_env,
orig_values,
candidate.result,
)
.map_err(|_| SelectionError::Unimplemented)?;

Ok(Some((candidate, certainty, nested_goals)))
});
Ok(Some((candidate, certainty, nested_goals)))
},
);

let (candidate, certainty, nested_goals) = match result {
Ok(Some((candidate, certainty, nested_goals))) => (candidate, certainty, nested_goals),
Expand Down
43 changes: 41 additions & 2 deletions compiler/rustc_trait_selection/src/solve/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_middle::traits::solve::{
};
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::config::DumpSolverProofTree;
use rustc_span::def_id::CRATE_DEF_ID;

use super::eval_ctxt::UseGlobalCache;
use super::GenerateProofTree;
Expand Down Expand Up @@ -199,15 +200,53 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
pub fn new_maybe_root(
tcx: TyCtxt<'tcx>,
generate_proof_tree: GenerateProofTree,
filter: impl FnOnce() -> String,
) -> ProofTreeBuilder<'tcx> {
match generate_proof_tree {
GenerateProofTree::Never => ProofTreeBuilder::new_noop(),
GenerateProofTree::IfEnabled => {
let opts = &tcx.sess.opts.unstable_opts;
match opts.dump_solver_proof_tree {
DumpSolverProofTree::Always => {
let use_cache = opts.dump_solver_proof_tree_use_cache.unwrap_or(true);
ProofTreeBuilder::new_root(UseGlobalCache::from_bool(use_cache))
let should_generate = if tcx
.get_attrs(CRATE_DEF_ID, rustc_span::sym::rustc_filter_proof_tree_dump)
.next()
.is_some()
{
// dont do the filtering behaviour if there are no attributes anywhere
let goal = filter();
tcx
.get_attrs(CRATE_DEF_ID, rustc_span::sym::rustc_filter_proof_tree_dump)
.flat_map(|attr| {
let meta = attr.meta_kind().unwrap();
match meta {
rustc_ast::MetaItemKind::Word
| rustc_ast::MetaItemKind::NameValue(_) => {
bug!("wrong attribute kind for `rustc_filter_proof_tree_dump`")
}
rustc_ast::MetaItemKind::List(nested_meta) => {
nested_meta.into_iter().map(|nested_meta| {
match nested_meta {
rustc_ast::NestedMetaItem::MetaItem(_) => unreachable!("only string literals are supported in `rustc_filter_proof_tree_dump`"),
rustc_ast::NestedMetaItem::Lit(lit) => match lit.kind {
rustc_ast::LitKind::Str(sym, _) => sym,
_ => unreachable!("only string literals are supported in `rustc_filter_proof_tree_dump`"),
},
}
})
}
}
}).any(|sym| goal.as_str() == sym.as_str())
} else {
true
};

if should_generate {
let use_cache = opts.dump_solver_proof_tree_use_cache.unwrap_or(true);
ProofTreeBuilder::new_root(UseGlobalCache::from_bool(use_cache))
} else {
ProofTreeBuilder::new_noop()
}
}
// `OnError` is handled by reevaluating goals in error
// reporting with `GenerateProofTree::Yes`.
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/traits/new-solver/alias_eq_simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// check-pass
// compile-flags: -Ztrait-solver=next
// compile-flags: -Zdump-solver-proof-tree=always
// compile-flags: -Zdump-solver-proof-tree-use-cache=no

#![feature(rustc_attrs)]
#![rustc_filter_proof_tree_dump(
"Binder { value: TraitPredicate(<(i32, u32) as TraitA>, polarity:Positive), bound_vars: [] }"
)]

// test that the new solver can handle `alias-eq(<i32 as TraitB>::Assoc, u32)`

Expand Down
Loading