Skip to content

Commit c4ffd9d

Browse files
committed
Auto merge of #163081 - estebank:not-general-enough, r=<try>
Provide more context on "not general enough" error
2 parents d287eb7 + 9e3a304 commit c4ffd9d

57 files changed

Lines changed: 792 additions & 253 deletions

File tree

Some content is hidden

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

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
415415
let mut preds = path
416416
.iter()
417417
.filter_map(|constraint| match constraint.category {
418-
ConstraintCategory::Predicate(pred) if !pred.is_dummy() => Some(pred),
418+
ConstraintCategory::Predicate(pred, _) if !pred.is_dummy() => Some(pred),
419419
_ => None,
420420
})
421421
.collect::<Vec<Span>>();

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
5757
ConstraintCategory::ClosureUpvar(_) => "closure capture ",
5858
ConstraintCategory::Usage => "this usage ",
5959
ConstraintCategory::SolverRegionConstraint(_)
60-
| ConstraintCategory::Predicate(_)
60+
| ConstraintCategory::Predicate(_, _)
6161
| ConstraintCategory::Boring
6262
| ConstraintCategory::BoringNoLocation
6363
| ConstraintCategory::Internal

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub struct ClosureOutlivesRequirement<'tcx> {
232232

233233
// Make sure this enum doesn't unintentionally grow
234234
#[cfg(target_pointer_width = "64")]
235-
rustc_data_structures::static_assert_size!(ConstraintCategory<'_>, 16);
235+
rustc_data_structures::static_assert_size!(ConstraintCategory<'_>, 24);
236236

237237
/// The subject of a `ClosureOutlivesRequirement` -- that is, the thing
238238
/// that must outlive some region.

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17901790
// Generic arguments are unlikely to be what relates regions together
17911791
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => 3,
17921792
// We handle predicates and opaque types specially; don't prioritize them here.
1793-
ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
1793+
ConstraintCategory::Predicate(_, _) | ConstraintCategory::OpaqueType => 4,
17941794
// `Boring` constraints can correspond to user-written code and have useful spans,
17951795
// but don't provide any other useful information for diagnostics.
17961796
ConstraintCategory::Boring => 5,
@@ -1928,11 +1928,11 @@ impl<'tcx> BestBlame<'tcx> {
19281928
.path
19291929
.iter()
19301930
.find_map(|constraint| {
1931-
if let ConstraintCategory::Predicate(predicate_span) = constraint.category {
1931+
if let ConstraintCategory::Predicate(predicate_span, def_id) = constraint.category {
19321932
// We currently do not store the `DefId` in the `ConstraintCategory`
19331933
// for performances reasons. The error reporting code used by NLL only
19341934
// uses the span, so this doesn't cause any problems at the moment.
1935-
Some(ObligationCauseCode::WhereClause(CRATE_DEF_ID.to_def_id(), predicate_span))
1935+
Some(ObligationCauseCode::WhereClause(def_id, predicate_span))
19361936
} else {
19371937
None
19381938
}

compiler/rustc_borrowck/src/type_check/canonical.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
142142
#[instrument(level = "debug", skip(self))]
143143
pub(super) fn normalize_and_prove_instantiated_clauses(
144144
&mut self,
145-
// Keep this parameter for now, in case we start using
146-
// it in `ConstraintCategory` at some point.
147-
_def_id: DefId,
145+
def_id: DefId,
148146
instantiated_clauses: ty::InstantiatedClauses<'tcx>,
149147
locations: Locations,
150148
) {
151149
for (clause, span) in instantiated_clauses {
152150
debug!(?span, ?clause);
153-
let category = ConstraintCategory::Predicate(span);
151+
let category = ConstraintCategory::Predicate(span, def_id);
154152
let clause = self.normalize_with_category(clause, locations, category);
155153
self.prove_clause(clause, locations, category);
156154
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ pub enum SubregionOrigin<'tcx> {
465465
trait_item_def_id: DefId,
466466
},
467467

468-
AscribeUserTypeProvePredicate(Span),
468+
AscribeUserTypeProvePredicate(Span, DefId),
469469

470470
// FIXME(-Zassumptions-on-binders): this is a temporary hack until we support
471471
// proper diagnostics for solver region constraints.
@@ -480,7 +480,9 @@ impl<'tcx> SubregionOrigin<'tcx> {
480480
pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
481481
match self {
482482
Self::Subtype(type_trace) => type_trace.cause.to_constraint_category(),
483-
Self::AscribeUserTypeProvePredicate(span) => ConstraintCategory::Predicate(*span),
483+
Self::AscribeUserTypeProvePredicate(span, def_id) => {
484+
ConstraintCategory::Predicate(*span, *def_id)
485+
}
484486
Self::SolverRegionConstraint(span) => ConstraintCategory::SolverRegionConstraint(*span),
485487
_ => ConstraintCategory::BoringNoLocation,
486488
}
@@ -1836,7 +1838,7 @@ impl<'tcx> SubregionOrigin<'tcx> {
18361838
SubregionOrigin::Reborrow(a) => a,
18371839
SubregionOrigin::ReferenceOutlivesReferent(_, a) => a,
18381840
SubregionOrigin::CompareImplItemObligation { span, .. } => span,
1839-
SubregionOrigin::AscribeUserTypeProvePredicate(span) => span,
1841+
SubregionOrigin::AscribeUserTypeProvePredicate(span, _) => span,
18401842
SubregionOrigin::CheckAssociatedTypeBounds { ref parent, .. } => parent.span(),
18411843
SubregionOrigin::SolverRegionConstraint(a) => a,
18421844
}
@@ -1870,8 +1872,8 @@ impl<'tcx> SubregionOrigin<'tcx> {
18701872
parent: Box::new(default()),
18711873
},
18721874

1873-
traits::ObligationCauseCode::AscribeUserTypeProvePredicate(span) => {
1874-
SubregionOrigin::AscribeUserTypeProvePredicate(span)
1875+
traits::ObligationCauseCode::AscribeUserTypeProvePredicate(span, def_id) => {
1876+
SubregionOrigin::AscribeUserTypeProvePredicate(span, def_id)
18751877
}
18761878

18771879
traits::ObligationCauseCode::ObjectTypeBound(ty, _reg) => {

compiler/rustc_middle/src/mir/query.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::ErrorGuaranteed;
77
use rustc_index::IndexVec;
88
use rustc_index::bit_set::BitMatrix;
99
use rustc_macros::{StableHash, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
10+
use rustc_span::def_id::DefId;
1011
use rustc_span::{Span, Symbol};
1112

1213
use super::{ConstValue, SourceInfo};
@@ -129,7 +130,7 @@ pub enum ConstraintCategory<'tcx> {
129130
/// A constraint from a user-written predicate
130131
/// with the provided span, written on the item
131132
/// with the given `DefId`
132-
Predicate(Span),
133+
Predicate(Span, DefId),
133134

134135
/// A "boring" constraint (caused by the given location) is one that
135136
/// the user probably doesn't want to see described in diagnostics,

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ impl<'tcx> ObligationCause<'tcx> {
137137
pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
138138
match self.code() {
139139
ObligationCauseCode::MatchImpl(cause, _) => cause.to_constraint_category(),
140-
ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span) => {
141-
ConstraintCategory::Predicate(*predicate_span)
140+
ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span, def_id) => {
141+
ConstraintCategory::Predicate(*predicate_span, *def_id)
142142
}
143143
_ => ConstraintCategory::BoringNoLocation,
144144
}
@@ -402,7 +402,7 @@ pub enum ObligationCauseCode<'tcx> {
402402
output_ty: Option<Ty<'tcx>>,
403403
},
404404

405-
AscribeUserTypeProvePredicate(Span),
405+
AscribeUserTypeProvePredicate(Span, DefId),
406406

407407
RustCall,
408408

compiler/rustc_trait_selection/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ impl<'tcx> ActualImplExplNotes<'tcx> {
11661166
pub(crate) struct TraitPlaceholderMismatch<'tcx> {
11671167
#[primary_span]
11681168
pub span: Span,
1169-
#[label("doesn't satisfy where-clause")]
1169+
#[label("unsatisfied where-clause on `{$def_id}`")]
11701170
pub satisfy_span: Option<Span>,
11711171
#[label("due to a where-clause on `{$def_id}`...")]
11721172
pub where_span: Option<Span>,

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,43 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
255255
) -> Diag<'tcx> {
256256
let span = cause.span;
257257

258+
let mut code = cause.code();
259+
loop {
260+
match code {
261+
ObligationCauseCode::MatchImpl(inner_cause, _) => {
262+
code = inner_cause.code();
263+
}
264+
ObligationCauseCode::ImplDerived(derived) => {
265+
code = &derived.derived.parent_code;
266+
}
267+
ObligationCauseCode::BuiltinDerived(derived) => {
268+
code = &derived.parent_code;
269+
}
270+
ObligationCauseCode::WellFormedDerived(derived) => {
271+
code = &derived.parent_code;
272+
}
273+
ObligationCauseCode::ImplDerivedHost(derived) => {
274+
code = &derived.derived.parent_code;
275+
}
276+
ObligationCauseCode::BuiltinDerivedHost(derived) => {
277+
code = &derived.parent_code;
278+
}
279+
_ => break,
280+
}
281+
}
258282
let (leading_ellipsis, satisfy_span, where_span, dup_span, def_id) =
259283
if let ObligationCauseCode::WhereClause(def_id, span)
260-
| ObligationCauseCode::WhereClauseInExpr(def_id, span, ..) = *cause.code()
284+
| ObligationCauseCode::WhereClauseInExpr(def_id, span, ..) = *code
261285
&& def_id != CRATE_DEF_ID.to_def_id()
262286
{
263287
(
264288
true,
265289
Some(span),
266-
Some(self.tcx().def_span(def_id)),
290+
Some(
291+
self.tcx()
292+
.opt_item_ident(def_id)
293+
.map_or_else(|| self.tcx().def_span(def_id), |n| n.span),
294+
),
267295
None,
268296
self.tcx().def_path_str(def_id),
269297
)
@@ -356,6 +384,17 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
356384

357385
let mut current_code = cause.code();
358386
let mut coroutine_def_id = None;
387+
if cause.body_def_id != CRATE_DEF_ID {
388+
self.cx.note_obligation_cause_code(
389+
cause.body_def_id,
390+
&mut err,
391+
actual_trait_ref,
392+
self.tcx().param_env(cause.body_def_id),
393+
cause.code(),
394+
&mut vec![],
395+
&mut Default::default(),
396+
);
397+
}
359398

360399
loop {
361400
match current_code {

0 commit comments

Comments
 (0)