Skip to content

Commit 2cf4b87

Browse files
committed
De-dupe NLL HRTB diagnostics' use of type_op_prove_predicate
1 parent a69c7cc commit 2cf4b87

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4055,6 +4055,7 @@ dependencies = [
40554055
"rustc_span",
40564056
"rustc_target",
40574057
"rustc_trait_selection",
4058+
"rustc_traits",
40584059
"smallvec",
40594060
"tracing",
40604061
]

Diff for: compiler/rustc_mir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rustc_serialize = { path = "../rustc_serialize" }
2727
rustc_session = { path = "../rustc_session" }
2828
rustc_target = { path = "../rustc_target" }
2929
rustc_trait_selection = { path = "../rustc_trait_selection" }
30+
rustc_traits = { path = "../rustc_traits" }
3031
rustc_ast = { path = "../rustc_ast" }
3132
rustc_span = { path = "../rustc_span" }
3233
rustc_apfloat = { path = "../rustc_apfloat" }

Diff for: compiler/rustc_mir/src/borrow_check/diagnostics/bound_region_errors.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use rustc_infer::infer::canonical::Canonical;
33
use rustc_infer::infer::error_reporting::nice_region_error::NiceRegionError;
44
use rustc_infer::infer::region_constraints::Constraint;
55
use rustc_infer::infer::{InferCtxt, RegionResolutionError, SubregionOrigin, TyCtxtInferExt as _};
6-
use rustc_infer::traits::{Normalized, Obligation, ObligationCause, TraitEngine, TraitEngineExt};
6+
use rustc_infer::traits::{Normalized, ObligationCause, TraitEngine, TraitEngineExt};
77
use rustc_middle::ty::error::TypeError;
88
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
99
use rustc_span::Span;
1010
use rustc_trait_selection::traits::query::type_op;
1111
use rustc_trait_selection::traits::{SelectionContext, TraitEngineExt as _};
12+
use rustc_traits::type_op_prove_predicate_with_span;
1213

1314
use std::fmt;
1415
use std::rc::Rc;
@@ -209,17 +210,7 @@ impl TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
209210
) -> Option<DiagnosticBuilder<'tcx>> {
210211
tcx.infer_ctxt().enter_with_canonical(span, &self.canonical_query, |ref infcx, key, _| {
211212
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(tcx);
212-
213-
let (param_env, prove_predicate) = key.into_parts();
214-
fulfill_cx.register_predicate_obligation(
215-
infcx,
216-
Obligation::new(
217-
ObligationCause::dummy_with_span(span),
218-
param_env,
219-
prove_predicate.predicate,
220-
),
221-
);
222-
213+
type_op_prove_predicate_with_span(infcx, &mut *fulfill_cx, key, Some(span));
223214
try_extract_error_from_fulfill_cx(fulfill_cx, infcx, placeholder_region, error_region)
224215
})
225216
}

Diff for: compiler/rustc_traits/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ mod normalize_erasing_regions;
1919
mod normalize_projection_ty;
2020
mod type_op;
2121

22+
pub use type_op::type_op_prove_predicate_with_span;
23+
2224
use rustc_middle::ty::query::Providers;
2325

2426
pub fn provide(p: &mut Providers) {

Diff for: compiler/rustc_traits/src/type_op.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::ty::{
1010
self, FnSig, Lift, PolyFnSig, PredicateKind, Ty, TyCtxt, TypeFoldable, Variance,
1111
};
1212
use rustc_middle::ty::{ParamEnv, ParamEnvAnd, Predicate, ToPredicate};
13-
use rustc_span::DUMMY_SP;
13+
use rustc_span::{Span, DUMMY_SP};
1414
use rustc_trait_selection::infer::InferCtxtBuilderExt;
1515
use rustc_trait_selection::infer::InferCtxtExt;
1616
use rustc_trait_selection::traits::query::normalize::AtExt;
@@ -247,11 +247,25 @@ fn type_op_prove_predicate<'tcx>(
247247
canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, ProvePredicate<'tcx>>>,
248248
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
249249
tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| {
250-
let (param_env, ProvePredicate { predicate }) = key.into_parts();
251-
fulfill_cx.register_predicate_obligation(
252-
infcx,
253-
Obligation::new(ObligationCause::dummy(), param_env, predicate),
254-
);
250+
type_op_prove_predicate_with_span(infcx, fulfill_cx, key, None);
255251
Ok(())
256252
})
257253
}
254+
255+
/// The core of the `type_op_prove_predicate` query: for diagnostics purposes in NLL HRTB errors,
256+
/// this query can be re-run to better track the span of the obligation cause, and improve the error
257+
/// message. Do not call directly unless you're in that very specific context.
258+
pub fn type_op_prove_predicate_with_span<'a, 'tcx: 'a>(
259+
infcx: &'a InferCtxt<'a, 'tcx>,
260+
fulfill_cx: &'a mut dyn TraitEngine<'tcx>,
261+
key: ParamEnvAnd<'tcx, ProvePredicate<'tcx>>,
262+
span: Option<Span>,
263+
) {
264+
let cause = if let Some(span) = span {
265+
ObligationCause::dummy_with_span(span)
266+
} else {
267+
ObligationCause::dummy()
268+
};
269+
let (param_env, ProvePredicate { predicate }) = key.into_parts();
270+
fulfill_cx.register_predicate_obligation(infcx, Obligation::new(cause, param_env, predicate));
271+
}

0 commit comments

Comments
 (0)