1
Fork 0

make outlives constraints from generic arguments less boring

This commit is contained in:
dianne 2024-12-20 17:08:27 -08:00
parent 6421d4cf80
commit 10061b3a4f
5 changed files with 35 additions and 19 deletions

View file

@ -13,7 +13,7 @@ use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate};
use rustc_infer::infer::{NllRegionVariableOrigin, RelateParamBound}; use rustc_infer::infer::{NllRegionVariableOrigin, RelateParamBound};
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::hir::place::PlaceBase; 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_middle::ty::{self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeVisitor};
use rustc_span::{Ident, Span, kw}; use rustc_span::{Ident, Span, kw};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
@ -49,7 +49,8 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
ConstraintCategory::Cast { is_implicit_coercion: false, .. } => "cast ", ConstraintCategory::Cast { is_implicit_coercion: false, .. } => "cast ",
ConstraintCategory::Cast { is_implicit_coercion: true, .. } => "coercion ", ConstraintCategory::Cast { is_implicit_coercion: true, .. } => "coercion ",
ConstraintCategory::CallArgument(_) => "argument ", 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::SizedBound => "proving this value is `Sized` ",
ConstraintCategory::CopyBound => "copying this value ", ConstraintCategory::CopyBound => "copying this value ",
ConstraintCategory::OpaqueType => "opaque type ", ConstraintCategory::OpaqueType => "opaque type ",

View file

@ -13,9 +13,9 @@ use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound,
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin}; use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin};
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::mir::{ use rustc_middle::mir::{
BasicBlock, Body, ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureOutlivesSubjectTy, AnnotationSource, BasicBlock, Body, ClosureOutlivesRequirement, ClosureOutlivesSubject,
ClosureRegionRequirements, ConstraintCategory, Local, Location, ReturnConstraint, ClosureOutlivesSubjectTy, ClosureRegionRequirements, ConstraintCategory, Local, Location,
TerminatorKind, ReturnConstraint, TerminatorKind,
}; };
use rustc_middle::traits::{ObligationCause, ObligationCauseCode}; use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
use rustc_middle::ty::fold::fold_regions; use rustc_middle::ty::fold::fold_regions;
@ -2063,7 +2063,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// Mimic old logic for this, to minimize false positives in tests. // Mimic old logic for this, to minimize false positives in tests.
&& !path && !path
.iter() .iter()
.any(|c| matches!(c.category, ConstraintCategory::TypeAnnotation)) => .any(|c| matches!(c.category, ConstraintCategory::TypeAnnotation(_))) =>
{ {
1 1
} }
@ -2071,7 +2071,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
ConstraintCategory::Yield ConstraintCategory::Yield
| ConstraintCategory::UseAsConst | ConstraintCategory::UseAsConst
| ConstraintCategory::UseAsStatic | ConstraintCategory::UseAsStatic
| ConstraintCategory::TypeAnnotation | ConstraintCategory::TypeAnnotation(
AnnotationSource::Ascription
| AnnotationSource::Declaration
| AnnotationSource::OpaqueCast,
)
| ConstraintCategory::Cast { .. } | ConstraintCategory::Cast { .. }
| ConstraintCategory::CallArgument(_) | ConstraintCategory::CallArgument(_)
| ConstraintCategory::CopyBound | ConstraintCategory::CopyBound
@ -2082,17 +2086,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// Give assignments a lower priority when flagged as less likely to be interesting. // Give assignments a lower priority when flagged as less likely to be interesting.
// In particular, de-prioritize MIR assignments lowered from argument patterns. // In particular, de-prioritize MIR assignments lowered from argument patterns.
ConstraintCategory::Assignment { has_interesting_ty: false } => 3, 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. // 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, // `Boring` constraints can correspond to user-written code and have useful spans,
// but don't provide any other useful information for diagnostics. // 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 // `BoringNoLocation` constraints can point to user-written code, but are less
// specific, and are not used for relations that would make sense to blame. // specific, and are not used for relations that would make sense to blame.
ConstraintCategory::BoringNoLocation => 6, ConstraintCategory::BoringNoLocation => 7,
// Do not blame internal constraints. // Do not blame internal constraints.
ConstraintCategory::Internal => 7, ConstraintCategory::Internal => 8,
ConstraintCategory::IllegalUniverse => 8, ConstraintCategory::IllegalUniverse => 9,
} }
}; };

View file

@ -298,7 +298,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
context.ambient_variance(), context.ambient_variance(),
base_ty.ty, base_ty.ty,
location.to_locations(), location.to_locations(),
ConstraintCategory::TypeAnnotation, ConstraintCategory::TypeAnnotation(AnnotationSource::OpaqueCast),
) )
.unwrap(); .unwrap();
} }
@ -333,7 +333,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
ty::Invariant, ty::Invariant,
&UserTypeProjection { base: annotation_index, projs: vec![] }, &UserTypeProjection { base: annotation_index, projs: vec![] },
locations, locations,
ConstraintCategory::Boring, ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
) { ) {
let annotation = &self.typeck.user_type_annotations[annotation_index]; let annotation = &self.typeck.user_type_annotations[annotation_index];
span_mirbug!( span_mirbug!(
@ -455,7 +455,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
ty::Invariant, ty::Invariant,
user_ty, user_ty,
Locations::All(*span), Locations::All(*span),
ConstraintCategory::TypeAnnotation, ConstraintCategory::TypeAnnotation(AnnotationSource::Declaration),
) { ) {
span_mirbug!( span_mirbug!(
self, self,
@ -938,7 +938,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
ty::Invariant, ty::Invariant,
&UserTypeProjection { base: annotation_index, projs: vec![] }, &UserTypeProjection { base: annotation_index, projs: vec![] },
location.to_locations(), location.to_locations(),
ConstraintCategory::Boring, ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg),
) { ) {
let annotation = &self.user_type_annotations[annotation_index]; let annotation = &self.user_type_annotations[annotation_index];
span_mirbug!( span_mirbug!(
@ -973,7 +973,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
*variance, *variance,
projection, projection,
Locations::All(stmt.source_info.span), Locations::All(stmt.source_info.span),
ConstraintCategory::TypeAnnotation, ConstraintCategory::TypeAnnotation(AnnotationSource::Ascription),
) { ) {
let annotation = &self.user_type_annotations[projection.base]; let annotation = &self.user_type_annotations[projection.base];
span_mirbug!( span_mirbug!(

View file

@ -229,7 +229,7 @@ pub enum ConstraintCategory<'tcx> {
Yield, Yield,
UseAsConst, UseAsConst,
UseAsStatic, UseAsStatic,
TypeAnnotation, TypeAnnotation(AnnotationSource),
Cast { Cast {
/// Whether this cast is a coercion that was automatically inserted by the compiler. /// Whether this cast is a coercion that was automatically inserted by the compiler.
is_implicit_coercion: bool, is_implicit_coercion: bool,
@ -280,6 +280,15 @@ pub enum ReturnConstraint {
ClosureUpvar(FieldIdx), 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 /// The subject of a `ClosureOutlivesRequirement` -- that is, the thing
/// that must outlive some region. /// that must outlive some region.
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)] #[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]

View file

@ -6,7 +6,7 @@ LL | fn g<'a, 'b>() {
| | | |
| lifetime `'a` defined here | lifetime `'a` defined here
LL | f::<'a, 'b>(()); 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` = help: consider adding the following bound: `'b: 'a`
= note: requirement occurs because of a function pointer to `f` = note: requirement occurs because of a function pointer to `f`