Skip to content

Commit

Permalink
make outlives constraints from generic arguments less boring
Browse files Browse the repository at this point in the history
  • Loading branch information
dianne committed Jan 7, 2025
1 parent 6421d4c commit 10061b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate};
use rustc_infer::infer::{NllRegionVariableOrigin, RelateParamBound};
use rustc_middle::bug;
use rustc_middle::hir::place::PlaceBase;
use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint};
use rustc_middle::ty::{self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeVisitor};
use rustc_span::{Ident, Span, kw};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
Expand Down Expand Up @@ -49,7 +49,8 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
ConstraintCategory::Cast { is_implicit_coercion: false, .. } => "cast ",
ConstraintCategory::Cast { is_implicit_coercion: true, .. } => "coercion ",
ConstraintCategory::CallArgument(_) => "argument ",
ConstraintCategory::TypeAnnotation => "type annotation ",
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => "generic argument ",
ConstraintCategory::TypeAnnotation(_) => "type annotation ",
ConstraintCategory::SizedBound => "proving this value is `Sized` ",
ConstraintCategory::CopyBound => "copying this value ",
ConstraintCategory::OpaqueType => "opaque type ",
Expand Down
26 changes: 16 additions & 10 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound,
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin};
use rustc_middle::bug;
use rustc_middle::mir::{
BasicBlock, Body, ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureOutlivesSubjectTy,
ClosureRegionRequirements, ConstraintCategory, Local, Location, ReturnConstraint,
TerminatorKind,
AnnotationSource, BasicBlock, Body, ClosureOutlivesRequirement, ClosureOutlivesSubject,
ClosureOutlivesSubjectTy, ClosureRegionRequirements, ConstraintCategory, Local, Location,
ReturnConstraint, TerminatorKind,
};
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
use rustc_middle::ty::fold::fold_regions;
Expand Down Expand Up @@ -2063,15 +2063,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// Mimic old logic for this, to minimize false positives in tests.
&& !path
.iter()
.any(|c| matches!(c.category, ConstraintCategory::TypeAnnotation)) =>
.any(|c| matches!(c.category, ConstraintCategory::TypeAnnotation(_))) =>
{
1
}
// Between other interesting constraints, order by their position on the `path`.
ConstraintCategory::Yield
| ConstraintCategory::UseAsConst
| ConstraintCategory::UseAsStatic
| ConstraintCategory::TypeAnnotation
| ConstraintCategory::TypeAnnotation(
AnnotationSource::Ascription
| AnnotationSource::Declaration
| AnnotationSource::OpaqueCast,
)
| ConstraintCategory::Cast { .. }
| ConstraintCategory::CallArgument(_)
| ConstraintCategory::CopyBound
Expand All @@ -2082,17 +2086,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// Give assignments a lower priority when flagged as less likely to be interesting.
// In particular, de-prioritize MIR assignments lowered from argument patterns.
ConstraintCategory::Assignment { has_interesting_ty: false } => 3,
// Generic arguments are unlikely to be what relates regions together
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => 4,
// We handle predicates and opaque types specially; don't prioritize them here.
ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 5,
// `Boring` constraints can correspond to user-written code and have useful spans,
// but don't provide any other useful information for diagnostics.
ConstraintCategory::Boring => 5,
ConstraintCategory::Boring => 6,
// `BoringNoLocation` constraints can point to user-written code, but are less
// specific, and are not used for relations that would make sense to blame.
ConstraintCategory::BoringNoLocation => 6,
ConstraintCategory::BoringNoLocation => 7,
// Do not blame internal constraints.
ConstraintCategory::Internal => 7,
ConstraintCategory::IllegalUniverse => 8,
ConstraintCategory::Internal => 8,
ConstraintCategory::IllegalUniverse => 9,
}
};

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
context.ambient_variance(),
base_ty.ty,
location.to_locations(),
ConstraintCategory::TypeAnnotation,
ConstraintCategory::TypeAnnotation(AnnotationSource::OpaqueCast),
)
.unwrap();
}
Expand Down Expand Up @@ -333,7 +333,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
ty::Invariant,
&UserTypeProjection { base: annotation_index, projs: vec![] },
locations,
ConstraintCategory::Boring,
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
) {
let annotation = &self.typeck.user_type_annotations[annotation_index];
span_mirbug!(
Expand Down Expand Up @@ -455,7 +455,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
ty::Invariant,
user_ty,
Locations::All(*span),
ConstraintCategory::TypeAnnotation,
ConstraintCategory::TypeAnnotation(AnnotationSource::Declaration),
) {
span_mirbug!(
self,
Expand Down Expand Up @@ -938,7 +938,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
ty::Invariant,
&UserTypeProjection { base: annotation_index, projs: vec![] },
location.to_locations(),
ConstraintCategory::Boring,
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
) {
let annotation = &self.user_type_annotations[annotation_index];
span_mirbug!(
Expand Down Expand Up @@ -973,7 +973,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
*variance,
projection,
Locations::All(stmt.source_info.span),
ConstraintCategory::TypeAnnotation,
ConstraintCategory::TypeAnnotation(AnnotationSource::Ascription),
) {
let annotation = &self.user_type_annotations[projection.base];
span_mirbug!(
Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_middle/src/mir/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub enum ConstraintCategory<'tcx> {
Yield,
UseAsConst,
UseAsStatic,
TypeAnnotation,
TypeAnnotation(AnnotationSource),
Cast {
/// Whether this cast is a coercion that was automatically inserted by the compiler.
is_implicit_coercion: bool,
Expand Down Expand Up @@ -280,6 +280,15 @@ pub enum ReturnConstraint {
ClosureUpvar(FieldIdx),
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)]
pub enum AnnotationSource {
Ascription,
Declaration,
OpaqueCast,
GenericArg,
}

/// The subject of a `ClosureOutlivesRequirement` -- that is, the thing
/// that must outlive some region.
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fn/implied-bounds-unnorm-associated-type-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | fn g<'a, 'b>() {
| |
| lifetime `'a` defined here
LL | f::<'a, 'b>(());
| ^^^^^^^^^^^^^^^ requires that `'b` must outlive `'a`
| ^^^^^^^^^^^^^^^ generic argument requires that `'b` must outlive `'a`
|
= help: consider adding the following bound: `'b: 'a`
= note: requirement occurs because of a function pointer to `f`
Expand Down

0 comments on commit 10061b3

Please sign in to comment.