Skip to content

Commit 10061b3

Browse files
committed
make outlives constraints from generic arguments less boring
1 parent 6421d4c commit 10061b3

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate};
1313
use rustc_infer::infer::{NllRegionVariableOrigin, RelateParamBound};
1414
use rustc_middle::bug;
1515
use rustc_middle::hir::place::PlaceBase;
16-
use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
16+
use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint};
1717
use rustc_middle::ty::{self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeVisitor};
1818
use rustc_span::{Ident, Span, kw};
1919
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
@@ -49,7 +49,8 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
4949
ConstraintCategory::Cast { is_implicit_coercion: false, .. } => "cast ",
5050
ConstraintCategory::Cast { is_implicit_coercion: true, .. } => "coercion ",
5151
ConstraintCategory::CallArgument(_) => "argument ",
52-
ConstraintCategory::TypeAnnotation => "type annotation ",
52+
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => "generic argument ",
53+
ConstraintCategory::TypeAnnotation(_) => "type annotation ",
5354
ConstraintCategory::SizedBound => "proving this value is `Sized` ",
5455
ConstraintCategory::CopyBound => "copying this value ",
5556
ConstraintCategory::OpaqueType => "opaque type ",

Diff for: compiler/rustc_borrowck/src/region_infer/mod.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound,
1313
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin};
1414
use rustc_middle::bug;
1515
use rustc_middle::mir::{
16-
BasicBlock, Body, ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureOutlivesSubjectTy,
17-
ClosureRegionRequirements, ConstraintCategory, Local, Location, ReturnConstraint,
18-
TerminatorKind,
16+
AnnotationSource, BasicBlock, Body, ClosureOutlivesRequirement, ClosureOutlivesSubject,
17+
ClosureOutlivesSubjectTy, ClosureRegionRequirements, ConstraintCategory, Local, Location,
18+
ReturnConstraint, TerminatorKind,
1919
};
2020
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
2121
use rustc_middle::ty::fold::fold_regions;
@@ -2063,15 +2063,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20632063
// Mimic old logic for this, to minimize false positives in tests.
20642064
&& !path
20652065
.iter()
2066-
.any(|c| matches!(c.category, ConstraintCategory::TypeAnnotation)) =>
2066+
.any(|c| matches!(c.category, ConstraintCategory::TypeAnnotation(_))) =>
20672067
{
20682068
1
20692069
}
20702070
// Between other interesting constraints, order by their position on the `path`.
20712071
ConstraintCategory::Yield
20722072
| ConstraintCategory::UseAsConst
20732073
| ConstraintCategory::UseAsStatic
2074-
| ConstraintCategory::TypeAnnotation
2074+
| ConstraintCategory::TypeAnnotation(
2075+
AnnotationSource::Ascription
2076+
| AnnotationSource::Declaration
2077+
| AnnotationSource::OpaqueCast,
2078+
)
20752079
| ConstraintCategory::Cast { .. }
20762080
| ConstraintCategory::CallArgument(_)
20772081
| ConstraintCategory::CopyBound
@@ -2082,17 +2086,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20822086
// Give assignments a lower priority when flagged as less likely to be interesting.
20832087
// In particular, de-prioritize MIR assignments lowered from argument patterns.
20842088
ConstraintCategory::Assignment { has_interesting_ty: false } => 3,
2089+
// Generic arguments are unlikely to be what relates regions together
2090+
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => 4,
20852091
// We handle predicates and opaque types specially; don't prioritize them here.
2086-
ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
2092+
ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 5,
20872093
// `Boring` constraints can correspond to user-written code and have useful spans,
20882094
// but don't provide any other useful information for diagnostics.
2089-
ConstraintCategory::Boring => 5,
2095+
ConstraintCategory::Boring => 6,
20902096
// `BoringNoLocation` constraints can point to user-written code, but are less
20912097
// specific, and are not used for relations that would make sense to blame.
2092-
ConstraintCategory::BoringNoLocation => 6,
2098+
ConstraintCategory::BoringNoLocation => 7,
20932099
// Do not blame internal constraints.
2094-
ConstraintCategory::Internal => 7,
2095-
ConstraintCategory::IllegalUniverse => 8,
2100+
ConstraintCategory::Internal => 8,
2101+
ConstraintCategory::IllegalUniverse => 9,
20962102
}
20972103
};
20982104

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
298298
context.ambient_variance(),
299299
base_ty.ty,
300300
location.to_locations(),
301-
ConstraintCategory::TypeAnnotation,
301+
ConstraintCategory::TypeAnnotation(AnnotationSource::OpaqueCast),
302302
)
303303
.unwrap();
304304
}
@@ -333,7 +333,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
333333
ty::Invariant,
334334
&UserTypeProjection { base: annotation_index, projs: vec![] },
335335
locations,
336-
ConstraintCategory::Boring,
336+
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
337337
) {
338338
let annotation = &self.typeck.user_type_annotations[annotation_index];
339339
span_mirbug!(
@@ -455,7 +455,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
455455
ty::Invariant,
456456
user_ty,
457457
Locations::All(*span),
458-
ConstraintCategory::TypeAnnotation,
458+
ConstraintCategory::TypeAnnotation(AnnotationSource::Declaration),
459459
) {
460460
span_mirbug!(
461461
self,
@@ -938,7 +938,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
938938
ty::Invariant,
939939
&UserTypeProjection { base: annotation_index, projs: vec![] },
940940
location.to_locations(),
941-
ConstraintCategory::Boring,
941+
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
942942
) {
943943
let annotation = &self.user_type_annotations[annotation_index];
944944
span_mirbug!(
@@ -973,7 +973,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
973973
*variance,
974974
projection,
975975
Locations::All(stmt.source_info.span),
976-
ConstraintCategory::TypeAnnotation,
976+
ConstraintCategory::TypeAnnotation(AnnotationSource::Ascription),
977977
) {
978978
let annotation = &self.user_type_annotations[projection.base];
979979
span_mirbug!(

Diff for: compiler/rustc_middle/src/mir/query.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub enum ConstraintCategory<'tcx> {
229229
Yield,
230230
UseAsConst,
231231
UseAsStatic,
232-
TypeAnnotation,
232+
TypeAnnotation(AnnotationSource),
233233
Cast {
234234
/// Whether this cast is a coercion that was automatically inserted by the compiler.
235235
is_implicit_coercion: bool,
@@ -280,6 +280,15 @@ pub enum ReturnConstraint {
280280
ClosureUpvar(FieldIdx),
281281
}
282282

283+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
284+
#[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)]
285+
pub enum AnnotationSource {
286+
Ascription,
287+
Declaration,
288+
OpaqueCast,
289+
GenericArg,
290+
}
291+
283292
/// The subject of a `ClosureOutlivesRequirement` -- that is, the thing
284293
/// that must outlive some region.
285294
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]

Diff for: tests/ui/fn/implied-bounds-unnorm-associated-type-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn g<'a, 'b>() {
66
| |
77
| lifetime `'a` defined here
88
LL | f::<'a, 'b>(());
9-
| ^^^^^^^^^^^^^^^ requires that `'b` must outlive `'a`
9+
| ^^^^^^^^^^^^^^^ generic argument requires that `'b` must outlive `'a`
1010
|
1111
= help: consider adding the following bound: `'b: 'a`
1212
= note: requirement occurs because of a function pointer to `f`

0 commit comments

Comments
 (0)