Skip to content

Commit c005e76

Browse files
Rework point-at-arg
1 parent fb80d2b commit c005e76

File tree

155 files changed

+1269
-782
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+1269
-782
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,12 +740,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
740740
err.help("...or use `match` instead of `let...else`");
741741
}
742742
_ => {
743-
if let ObligationCauseCode::BindingObligation(_, binding_span) =
744-
cause.code().peel_derives()
743+
if let ObligationCauseCode::BindingObligation(_, span)
744+
| ObligationCauseCode::ExprBindingObligation(_, span, ..)
745+
= cause.code().peel_derives()
746+
&& let TypeError::RegionsPlaceholderMismatch = terr
745747
{
746-
if matches!(terr, TypeError::RegionsPlaceholderMismatch) {
747-
err.span_note(*binding_span, "the lifetime requirement is introduced here");
748-
}
748+
err.span_note(*span, "the lifetime requirement is introduced here");
749749
}
750750
}
751751
}

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3535
let ObligationCauseCode::MatchImpl(parent, impl_def_id) = code else {
3636
return None;
3737
};
38-
let ObligationCauseCode::BindingObligation(_def_id, binding_span) = *parent.code() else {
38+
let (ObligationCauseCode::BindingObligation(_, binding_span) | ObligationCauseCode::ExprBindingObligation(_, binding_span, ..))
39+
= *parent.code() else {
3940
return None;
4041
};
4142
let mut err = self.tcx().sess.struct_span_err(cause.span, "incompatible lifetime on type");

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
211211
);
212212
let mut err = self.tcx().sess.struct_span_err(span, &msg);
213213

214-
let leading_ellipsis = if let ObligationCauseCode::ItemObligation(def_id) = *cause.code() {
214+
let leading_ellipsis = if let ObligationCauseCode::ItemObligation(def_id)
215+
| ObligationCauseCode::ExprItemObligation(def_id, ..) =
216+
*cause.code()
217+
{
215218
err.span_label(span, "doesn't satisfy where-clause");
216219
err.span_label(
217220
self.tcx().def_span(def_id),

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
232232
ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
233233
_ => cause.code(),
234234
}
235-
&& let (&ObligationCauseCode::ItemObligation(item_def_id), None) = (code, override_error_code)
235+
&& let (&ObligationCauseCode::ItemObligation(item_def_id) | &ObligationCauseCode::ExprItemObligation(item_def_id, ..), None) = (code, override_error_code)
236236
{
237237
// Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
238238
// lifetime as above, but called using a fully-qualified path to the method:

compiler/rustc_infer/src/infer/error_reporting/note.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
390390
if matches!(
391391
&trace.cause.code().peel_derives(),
392392
ObligationCauseCode::BindingObligation(..)
393+
| ObligationCauseCode::ExprBindingObligation(..)
393394
) =>
394395
{
395396
// Hack to get around the borrow checker because trace.cause has an `Rc`.
396-
if let ObligationCauseCode::BindingObligation(_, span) =
397+
if let ObligationCauseCode::BindingObligation(_, span)
398+
| ObligationCauseCode::ExprBindingObligation(_, span, ..) =
397399
&trace.cause.code().peel_derives()
398400
{
399401
let span = *span;

compiler/rustc_infer/src/infer/outlives/obligations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
9797
cause.span,
9898
sup_type,
9999
match cause.code().peel_derives() {
100-
ObligationCauseCode::BindingObligation(_, span) => Some(*span),
100+
ObligationCauseCode::BindingObligation(_, span)
101+
| ObligationCauseCode::ExprBindingObligation(_, span, ..) => Some(*span),
101102
_ => None,
102103
},
103104
)

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,13 @@ pub enum ObligationCauseCode<'tcx> {
238238
/// also implement all supertraits of `X`.
239239
ItemObligation(DefId),
240240

241+
ExprItemObligation(DefId, rustc_hir::HirId, usize),
242+
241243
/// Like `ItemObligation`, but with extra detail on the source of the obligation.
242244
BindingObligation(DefId, Span),
243245

246+
ExprBindingObligation(DefId, Span, rustc_hir::HirId, usize),
247+
244248
/// A type like `&'a T` is WF only if `T: 'a`.
245249
ReferenceOutlivesReferent(Ty<'tcx>),
246250

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
15641564
obligation.cause.code().peel_derives(),
15651565
ObligationCauseCode::ItemObligation(_)
15661566
| ObligationCauseCode::BindingObligation(_, _)
1567+
| ObligationCauseCode::ExprItemObligation(..)
1568+
| ObligationCauseCode::ExprBindingObligation(..)
15671569
| ObligationCauseCode::ObjectCastObligation(..)
15681570
| ObligationCauseCode::OpaqueType
15691571
);
@@ -2091,13 +2093,11 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
20912093
}
20922094
}
20932095

2094-
if let ObligationCauseCode::ItemObligation(def_id) = *obligation.cause.code() {
2096+
if let ObligationCauseCode::ItemObligation(def_id) | ObligationCauseCode::ExprItemObligation(def_id, ..) = *obligation.cause.code() {
20952097
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
2096-
} else if let (
2097-
Ok(ref snippet),
2098-
&ObligationCauseCode::BindingObligation(def_id, _),
2099-
) =
2100-
(self.tcx.sess.source_map().span_to_snippet(span), obligation.cause.code())
2098+
} else if let Ok(snippet) = &self.tcx.sess.source_map().span_to_snippet(span)
2099+
&& let ObligationCauseCode::BindingObligation(def_id, _) | ObligationCauseCode::ExprBindingObligation(def_id, ..)
2100+
= *obligation.cause.code()
21012101
{
21022102
let generics = self.tcx.generics_of(def_id);
21032103
if generics.params.iter().any(|p| p.name != kw::SelfUpper)
@@ -2520,15 +2520,10 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
25202520
err: &mut Diagnostic,
25212521
obligation: &PredicateObligation<'tcx>,
25222522
) {
2523-
let (
2524-
ty::PredicateKind::Trait(pred),
2525-
&ObligationCauseCode::BindingObligation(item_def_id, span),
2526-
) = (
2527-
obligation.predicate.kind().skip_binder(),
2528-
obligation.cause.code().peel_derives(),
2529-
) else {
2530-
return;
2531-
};
2523+
let ty::PredicateKind::Trait(pred) = obligation.predicate.kind().skip_binder() else { return; };
2524+
let (ObligationCauseCode::BindingObligation(item_def_id, span)
2525+
| ObligationCauseCode::ExprBindingObligation(item_def_id, span, ..))
2526+
= *obligation.cause.code().peel_derives() else { return; };
25322527
debug!(?pred, ?item_def_id, ?span);
25332528

25342529
let (Some(node), true) = (

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
143143
}
144144

145145
if let ObligationCauseCode::ItemObligation(item)
146-
| ObligationCauseCode::BindingObligation(item, _) = *obligation.cause.code()
146+
| ObligationCauseCode::BindingObligation(item, _)
147+
| ObligationCauseCode::ExprItemObligation(item, ..)
148+
| ObligationCauseCode::ExprBindingObligation(item, ..) = *obligation.cause.code()
147149
{
148150
// FIXME: maybe also have some way of handling methods
149151
// from other traits? That would require name resolution,

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10221022
if let ObligationCauseCode::ImplDerivedObligation(cause) = &*code {
10231023
try_borrowing(cause.derived.parent_trait_pred, &[])
10241024
} else if let ObligationCauseCode::BindingObligation(_, _)
1025-
| ObligationCauseCode::ItemObligation(..) = code
1025+
| ObligationCauseCode::ItemObligation(_)
1026+
| ObligationCauseCode::ExprItemObligation(..)
1027+
| ObligationCauseCode::ExprBindingObligation(..) = code
10261028
{
10271029
try_borrowing(poly_trait_pred, &never_suggest_borrow)
10281030
} else {
@@ -2244,11 +2246,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
22442246
region, object_ty,
22452247
));
22462248
}
2247-
ObligationCauseCode::ItemObligation(_item_def_id) => {
2249+
ObligationCauseCode::ItemObligation(_)
2250+
| ObligationCauseCode::ExprItemObligation(..) => {
22482251
// We hold the `DefId` of the item introducing the obligation, but displaying it
22492252
// doesn't add user usable information. It always point at an associated item.
22502253
}
2251-
ObligationCauseCode::BindingObligation(item_def_id, span) => {
2254+
ObligationCauseCode::BindingObligation(item_def_id, span)
2255+
| ObligationCauseCode::ExprBindingObligation(item_def_id, span, ..) => {
22522256
let item_name = tcx.def_path_str(item_def_id);
22532257
let mut multispan = MultiSpan::from(span);
22542258
if let Some(ident) = tcx.opt_item_ident(item_def_id) {

compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,21 @@ pub enum TraitQueryMode {
117117

118118
/// Creates predicate obligations from the generic bounds.
119119
pub fn predicates_for_generics<'tcx>(
120-
cause: ObligationCause<'tcx>,
120+
cause: impl Fn(usize, Span) -> ObligationCause<'tcx>,
121121
param_env: ty::ParamEnv<'tcx>,
122122
generic_bounds: ty::InstantiatedPredicates<'tcx>,
123123
) -> impl Iterator<Item = PredicateObligation<'tcx>> {
124-
util::predicates_for_generics(cause, 0, param_env, generic_bounds)
124+
let generic_bounds = generic_bounds;
125+
debug!("predicates_for_generics(generic_bounds={:?})", generic_bounds);
126+
127+
std::iter::zip(generic_bounds.predicates, generic_bounds.spans).enumerate().map(
128+
move |(idx, (predicate, span))| Obligation {
129+
cause: cause(idx, span),
130+
recursion_depth: 0,
131+
param_env: param_env,
132+
predicate,
133+
},
134+
)
125135
}
126136

127137
/// Determines whether the type `ty` is known to meet `bound` and

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use rustc_middle::ty::{self, ImplSubject, ToPredicate, Ty, TyCtxt, TypeVisitable
1111
use super::{Normalized, Obligation, ObligationCause, PredicateObligation, SelectionContext};
1212
pub use rustc_infer::traits::{self, util::*};
1313

14-
use std::iter;
15-
1614
///////////////////////////////////////////////////////////////////////////
1715
// `TraitAliasExpander` iterator
1816
///////////////////////////////////////////////////////////////////////////
@@ -210,7 +208,7 @@ pub fn impl_subject_and_oblig<'a, 'tcx>(
210208
let Normalized { value: predicates, obligations: normalization_obligations2 } =
211209
super::normalize(selcx, param_env, ObligationCause::dummy(), predicates);
212210
let impl_obligations =
213-
predicates_for_generics(ObligationCause::dummy(), 0, param_env, predicates);
211+
super::predicates_for_generics(|_, _| ObligationCause::dummy(), param_env, predicates);
214212

215213
let impl_obligations = impl_obligations
216214
.chain(normalization_obligations1.into_iter())
@@ -219,27 +217,6 @@ pub fn impl_subject_and_oblig<'a, 'tcx>(
219217
(subject, impl_obligations)
220218
}
221219

222-
pub fn predicates_for_generics<'tcx>(
223-
cause: ObligationCause<'tcx>,
224-
recursion_depth: usize,
225-
param_env: ty::ParamEnv<'tcx>,
226-
generic_bounds: ty::InstantiatedPredicates<'tcx>,
227-
) -> impl Iterator<Item = PredicateObligation<'tcx>> {
228-
debug!("predicates_for_generics(generic_bounds={:?})", generic_bounds);
229-
230-
iter::zip(generic_bounds.predicates, generic_bounds.spans).map(move |(predicate, span)| {
231-
let cause = match *cause.code() {
232-
traits::ItemObligation(def_id) if !span.is_dummy() => traits::ObligationCause::new(
233-
cause.span,
234-
cause.body_id,
235-
traits::BindingObligation(def_id, span),
236-
),
237-
_ => cause.clone(),
238-
};
239-
Obligation { cause, recursion_depth, param_env, predicate }
240-
})
241-
}
242-
243220
pub fn predicate_for_trait_ref<'tcx>(
244221
tcx: TyCtxt<'tcx>,
245222
cause: ObligationCause<'tcx>,

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl<'tcx> WfPredicates<'tcx> {
711711
iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
712712
.map(|((mut pred, span), origin_def_id)| {
713713
let code = if span.is_dummy() {
714-
traits::MiscObligation
714+
traits::ItemObligation(origin_def_id)
715715
} else {
716716
traits::BindingObligation(origin_def_id, span)
717717
};

compiler/rustc_typeck/src/check/compare_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ pub fn check_type_bounds<'tcx>(
14631463
);
14641464
let mk_cause = |span: Span| {
14651465
let code = if span.is_dummy() {
1466-
traits::MiscObligation
1466+
traits::ItemObligation(trait_ty.def_id)
14671467
} else {
14681468
traits::BindingObligation(trait_ty.def_id, span)
14691469
};

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
607607

608608
#[instrument(skip(self), level = "debug")]
609609
pub(in super::super) fn select_all_obligations_or_error(&self) {
610-
let errors = self.fulfillment_cx.borrow_mut().select_all_or_error(&self);
610+
let mut errors = self.fulfillment_cx.borrow_mut().select_all_or_error(&self);
611611

612612
if !errors.is_empty() {
613+
self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
613614
self.report_fulfillment_errors(&errors, self.inh.body_id, false);
614615
}
615616
}
@@ -623,6 +624,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
623624
let mut result = self.fulfillment_cx.borrow_mut().select_where_possible(self);
624625
if !result.is_empty() {
625626
mutate_fulfillment_errors(&mut result);
627+
self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
626628
self.report_fulfillment_errors(&result, self.inh.body_id, fallback_has_occurred);
627629
}
628630
}
@@ -820,23 +822,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
820822
let ty = item_ty.subst(self.tcx, substs);
821823

822824
self.write_resolution(hir_id, Ok((def_kind, def_id)));
823-
self.add_required_obligations_with_code(
824-
span,
825-
def_id,
826-
&substs,
827-
match lang_item {
828-
hir::LangItem::IntoFutureIntoFuture => {
829-
ObligationCauseCode::AwaitableExpr(expr_hir_id)
830-
}
831-
hir::LangItem::IteratorNext | hir::LangItem::IntoIterIntoIter => {
832-
ObligationCauseCode::ForLoopIterator
833-
}
834-
hir::LangItem::TryTraitFromOutput
835-
| hir::LangItem::TryTraitFromResidual
836-
| hir::LangItem::TryTraitBranch => ObligationCauseCode::QuestionMark,
837-
_ => traits::ItemObligation(def_id),
838-
},
839-
);
825+
826+
let code = match lang_item {
827+
hir::LangItem::IntoFutureIntoFuture => {
828+
Some(ObligationCauseCode::AwaitableExpr(expr_hir_id))
829+
}
830+
hir::LangItem::IteratorNext | hir::LangItem::IntoIterIntoIter => {
831+
Some(ObligationCauseCode::ForLoopIterator)
832+
}
833+
hir::LangItem::TryTraitFromOutput
834+
| hir::LangItem::TryTraitFromResidual
835+
| hir::LangItem::TryTraitBranch => Some(ObligationCauseCode::QuestionMark),
836+
_ => None,
837+
};
838+
if let Some(code) = code {
839+
self.add_required_obligations_with_code(span, def_id, substs, move |_, _| code.clone());
840+
} else {
841+
self.add_required_obligations_for_hir(span, def_id, substs, hir_id);
842+
}
843+
840844
(Res::Def(def_kind, def_id), ty)
841845
}
842846

@@ -1348,7 +1352,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13481352
// First, store the "user substs" for later.
13491353
self.write_user_type_annotation_from_substs(hir_id, def_id, substs, user_self_ty);
13501354

1351-
self.add_required_obligations(span, def_id, &substs);
1355+
self.add_required_obligations_for_hir(span, def_id, &substs, hir_id);
13521356

13531357
// Substitute the values for the type parameters into the type of
13541358
// the referenced item.
@@ -1385,32 +1389,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13851389
}
13861390

13871391
/// Add all the obligations that are required, substituting and normalized appropriately.
1388-
pub(crate) fn add_required_obligations(
1392+
pub(crate) fn add_required_obligations_for_hir(
13891393
&self,
13901394
span: Span,
13911395
def_id: DefId,
1392-
substs: &SubstsRef<'tcx>,
1396+
substs: SubstsRef<'tcx>,
1397+
hir_id: hir::HirId,
13931398
) {
1394-
self.add_required_obligations_with_code(
1395-
span,
1396-
def_id,
1397-
substs,
1398-
traits::ItemObligation(def_id),
1399-
)
1399+
self.add_required_obligations_with_code(span, def_id, substs, |idx, span| {
1400+
if span.is_dummy() {
1401+
ObligationCauseCode::ExprItemObligation(def_id, hir_id, idx)
1402+
} else {
1403+
ObligationCauseCode::ExprBindingObligation(def_id, span, hir_id, idx)
1404+
}
1405+
})
14001406
}
14011407

1402-
#[tracing::instrument(level = "debug", skip(self, span, def_id, substs))]
1408+
#[tracing::instrument(level = "debug", skip(self, code, span, def_id, substs))]
14031409
fn add_required_obligations_with_code(
14041410
&self,
14051411
span: Span,
14061412
def_id: DefId,
1407-
substs: &SubstsRef<'tcx>,
1408-
code: ObligationCauseCode<'tcx>,
1413+
substs: SubstsRef<'tcx>,
1414+
code: impl Fn(usize, Span) -> ObligationCauseCode<'tcx>,
14091415
) {
14101416
let (bounds, _) = self.instantiate_bounds(span, def_id, &substs);
14111417

14121418
for obligation in traits::predicates_for_generics(
1413-
traits::ObligationCause::new(span, self.body_id, code),
1419+
|idx, predicate_span| {
1420+
traits::ObligationCause::new(span, self.body_id, code(idx, predicate_span))
1421+
},
14141422
self.param_env,
14151423
bounds,
14161424
) {

0 commit comments

Comments
 (0)