Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: do not warn when a lifetime bound infers itself #128908

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1924,14 +1924,13 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
impl ExplicitOutlivesRequirements {
fn lifetimes_outliving_lifetime<'tcx>(
tcx: TyCtxt<'tcx>,
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
inferred_outlives: impl Iterator<Item = &'tcx (ty::Clause<'tcx>, Span)>,
item: DefId,
lifetime: DefId,
) -> Vec<ty::Region<'tcx>> {
let item_generics = tcx.generics_of(item);

inferred_outlives
.iter()
.filter_map(|(clause, _)| match clause.kind().skip_binder() {
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match *a {
ty::ReEarlyParam(ebr)
Expand All @@ -1947,11 +1946,10 @@ impl ExplicitOutlivesRequirements {
}

fn lifetimes_outliving_type<'tcx>(
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
inferred_outlives: impl Iterator<Item = &'tcx (ty::Clause<'tcx>, Span)>,
index: u32,
) -> Vec<ty::Region<'tcx>> {
inferred_outlives
.iter()
.filter_map(|(clause, _)| match clause.kind().skip_binder() {
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
a.is_param(index).then_some(b)
Expand Down Expand Up @@ -2094,7 +2092,11 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
(
Self::lifetimes_outliving_lifetime(
cx.tcx,
inferred_outlives,
// don't warn if the inferred span actually came from the predicate we're looking at
// this happens if the type is recursively defined
inferred_outlives
.iter()
.filter(|(_, span)| !predicate.span.contains(*span)),
item.owner_id.to_def_id(),
region_def_id,
),
Expand All @@ -2116,7 +2118,14 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
};
let index = ty_generics.param_def_id_to_index[&def_id];
(
Self::lifetimes_outliving_type(inferred_outlives, index),
Self::lifetimes_outliving_type(
// don't warn if the inferred span actually came from the predicate we're looking at
// this happens if the type is recursively defined
inferred_outlives.iter().filter(|(_, span)| {
!predicate.span.contains(*span)
}),
index,
),
&predicate.bounds,
predicate.span,
predicate.origin == PredicateOrigin::WhereClause,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//@ run-rustfix
//@ check-pass
#![deny(explicit_outlives_requirements)]

pub trait TypeCx {
type Ty;
}

pub struct Pat<Cx: TypeCx> {
pub ty: Cx::Ty,
}

// Simple recursive case: no warning
pub struct MyTypeContextSimpleRecursive<'thir, 'tcx: 'thir> {
pub pat: Pat<MyTypeContextSimpleRecursive<'thir, 'tcx>>,
}
impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContextSimpleRecursive<'thir, 'tcx> {
type Ty = ();
}

// Non-recursive case: we want a warning
pub struct MyTypeContextNotRecursive<'thir, 'tcx: 'thir> {
pub tcx: &'tcx (),
pub thir: &'thir (),
}
impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContextNotRecursive<'thir, 'tcx> {
type Ty = ();
}


// Mixed-recursive case: we want a warning
pub struct MyTypeContextMixedRecursive<'thir, 'tcx: 'thir> {
pub pat: Pat<MyTypeContextMixedRecursive<'thir, 'tcx>>,
pub tcx: &'tcx (),
pub thir: &'thir (),
}
impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContextMixedRecursive<'thir, 'tcx> {
type Ty = ();
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//@ run-rustfix
//@ check-pass
#![deny(explicit_outlives_requirements)]

pub trait TypeCx {
type Ty;
}

pub struct Pat<Cx: TypeCx> {
pub ty: Cx::Ty,
}

// Simple recursive case: no warning
pub struct MyTypeContextSimpleRecursive<'thir, 'tcx: 'thir> {
pub pat: Pat<MyTypeContextSimpleRecursive<'thir, 'tcx>>,
}
impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContextSimpleRecursive<'thir, 'tcx> {
type Ty = ();
}

// Non-recursive case: we want a warning
pub struct MyTypeContextNotRecursive<'thir, 'tcx: 'thir> {
pub tcx: &'tcx (),
pub thir: &'thir (),
}
impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContextNotRecursive<'thir, 'tcx> {
type Ty = ();
}


// Mixed-recursive case: we want a warning
pub struct MyTypeContextMixedRecursive<'thir, 'tcx: 'thir> {
pub pat: Pat<MyTypeContextMixedRecursive<'thir, 'tcx>>,
pub tcx: &'tcx (),
pub thir: &'thir (),
}
impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContextMixedRecursive<'thir, 'tcx> {
type Ty = ();
}

fn main() {}
Loading