From aadd58ef7a78c3eabc75c73db6b166debdd7f1d2 Mon Sep 17 00:00:00 2001 From: yanchen4791 Date: Wed, 11 Jan 2023 00:19:40 -0800 Subject: [PATCH] Add 'static lifetime suggestion when GAT implied 'static requirement from HRTB --- .../src/diagnostics/region_errors.rs | 117 +++++++++++++++++- .../rustc_borrowck/src/region_infer/mod.rs | 8 ++ compiler/rustc_middle/src/ty/sty.rs | 7 ++ .../collectivity-regression.stderr | 10 ++ tests/ui/lifetimes/issue-105507.fixed | 43 +++++++ tests/ui/lifetimes/issue-105507.rs | 43 +++++++ tests/ui/lifetimes/issue-105507.stderr | 34 +++++ 7 files changed, 258 insertions(+), 4 deletions(-) create mode 100644 tests/ui/lifetimes/issue-105507.fixed create mode 100644 tests/ui/lifetimes/issue-105507.rs create mode 100644 tests/ui/lifetimes/issue-105507.stderr diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index f3050a6ef3f..187861ba127 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -5,8 +5,13 @@ use rustc_data_structures::fx::FxIndexSet; use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan}; use rustc_hir as hir; +use rustc_hir::def::Res::Def; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::Visitor; +use rustc_hir::GenericBound::Trait; +use rustc_hir::QPath::Resolved; +use rustc_hir::WherePredicate::BoundPredicate; +use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate}; use rustc_infer::infer::{ error_reporting::nice_region_error::{ self, find_anon_type, find_param_with_region, suggest_adding_lifetime_params, @@ -186,6 +191,101 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { false } + // For generic associated types (GATs) which implied 'static requirement + // from higher-ranked trait bounds (HRTB). Try to locate span of the trait + // and the span which bounded to the trait for adding 'static lifetime suggestion + fn suggest_static_lifetime_for_gat_from_hrtb( + &self, + diag: &mut DiagnosticBuilder<'_, ErrorGuaranteed>, + lower_bound: RegionVid, + ) { + let mut suggestions = vec![]; + let hir = self.infcx.tcx.hir(); + + // find generic associated types in the given region 'lower_bound' + let gat_id_and_generics = self + .regioncx + .placeholders_contained_in(lower_bound) + .map(|placeholder| { + if let Some(id) = placeholder.name.get_id() + && let Some(placeholder_id) = id.as_local() + && let gat_hir_id = hir.local_def_id_to_hir_id(placeholder_id) + && let Some(generics_impl) = hir.get_parent(gat_hir_id).generics() + { + Some((gat_hir_id, generics_impl)) + } else { + None + } + }) + .collect::>(); + debug!(?gat_id_and_generics); + + // find higher-ranked trait bounds bounded to the generic associated types + let mut hrtb_bounds = vec![]; + gat_id_and_generics.iter().flatten().for_each(|(gat_hir_id, generics)| { + for pred in generics.predicates { + let BoundPredicate( + WhereBoundPredicate { + bound_generic_params, + bounds, + .. + }) = pred else { continue; }; + if bound_generic_params + .iter() + .rfind(|bgp| hir.local_def_id_to_hir_id(bgp.def_id) == *gat_hir_id) + .is_some() + { + for bound in *bounds { + hrtb_bounds.push(bound); + } + } + } + }); + debug!(?hrtb_bounds); + + hrtb_bounds.iter().for_each(|bound| { + let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }, _) = bound else { return; }; + diag.span_note( + *trait_span, + format!("due to current limitations in the borrow checker, this implies a `'static` lifetime") + ); + let Some(generics_fn) = hir.get_generics(self.body.source.def_id().expect_local()) else { return; }; + let Def(_, trait_res_defid) = trait_ref.path.res else { return; }; + debug!(?generics_fn); + generics_fn.predicates.iter().for_each(|predicate| { + let BoundPredicate( + WhereBoundPredicate { + span: bounded_span, + bounded_ty, + bounds, + .. + } + ) = predicate else { return; }; + bounds.iter().for_each(|bd| { + if let Trait(PolyTraitRef { trait_ref: tr_ref, .. }, _) = bd + && let Def(_, res_defid) = tr_ref.path.res + && res_defid == trait_res_defid // trait id matches + && let TyKind::Path(Resolved(_, path)) = bounded_ty.kind + && let Def(_, defid) = path.res + && generics_fn.params + .iter() + .rfind(|param| param.def_id.to_def_id() == defid) + .is_some() { + suggestions.push((bounded_span.shrink_to_hi(), format!(" + 'static"))); + } + }); + }); + }); + if suggestions.len() > 0 { + suggestions.dedup(); + diag.multipart_suggestion_verbose( + format!("consider restricting the type parameter to the `'static` lifetime"), + suggestions, + Applicability::MaybeIncorrect, + ); + } + } + /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`. pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) { // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are @@ -223,12 +323,21 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { // to report it; we could probably handle it by // iterating over the universal regions and reporting // an error that multiple bounds are required. - self.buffer_error(self.infcx.tcx.sess.create_err( - GenericDoesNotLiveLongEnough { + let mut diag = + self.infcx.tcx.sess.create_err(GenericDoesNotLiveLongEnough { kind: type_test.generic_kind.to_string(), span: type_test_span, - }, - )); + }); + + // Add notes and suggestions for the case of 'static lifetime + // implied but not specified when a generic associated types + // are from higher-ranked trait bounds + self.suggest_static_lifetime_for_gat_from_hrtb( + &mut diag, + type_test.lower_bound, + ); + + self.buffer_error(diag); } } diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 308f6e19a73..7eae52bca58 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -527,6 +527,14 @@ impl<'tcx> RegionInferenceContext<'tcx> { self.scc_values.region_value_str(scc) } + pub(crate) fn placeholders_contained_in<'a>( + &'a self, + r: RegionVid, + ) -> impl Iterator + 'a { + let scc = self.constraint_sccs.scc(r.to_region_vid()); + self.scc_values.placeholders_contained_in(scc) + } + /// Returns access to the value of `r` for debugging purposes. pub(crate) fn region_universe(&self, r: RegionVid) -> ty::UniverseIndex { let scc = self.constraint_sccs.scc(r.to_region_vid()); diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index e49e7e86da0..ce2f03679e0 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -100,6 +100,13 @@ impl BoundRegionKind { None } + + pub fn get_id(&self) -> Option { + match *self { + BoundRegionKind::BrNamed(id, _) => return Some(id), + _ => None, + } + } } pub trait Article { diff --git a/tests/ui/generic-associated-types/collectivity-regression.stderr b/tests/ui/generic-associated-types/collectivity-regression.stderr index 1dbe1e2cb22..a085096e1f8 100644 --- a/tests/ui/generic-associated-types/collectivity-regression.stderr +++ b/tests/ui/generic-associated-types/collectivity-regression.stderr @@ -9,6 +9,16 @@ LL | | // probably should work. LL | | let _x = x; LL | | }; | |_____^ + | +note: due to current limitations in the borrow checker, this implies a `'static` lifetime + --> $DIR/collectivity-regression.rs:11:16 + | +LL | for<'a> T: Get = ()>, + | ^^^^^^^^^^^^^^^^^^^ +help: consider restricting the type parameter to the `'static` lifetime + | +LL | for<'a> T: Get = ()> + 'static, + | +++++++++ error: aborting due to previous error diff --git a/tests/ui/lifetimes/issue-105507.fixed b/tests/ui/lifetimes/issue-105507.fixed new file mode 100644 index 00000000000..277ce8a77e9 --- /dev/null +++ b/tests/ui/lifetimes/issue-105507.fixed @@ -0,0 +1,43 @@ +// run-rustfix +// +#![allow(warnings)] +struct Wrapper<'a, T: ?Sized>(&'a T); + +trait Project { + type Projected<'a> where Self: 'a; + fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_>; +} +trait MyTrait {} +trait ProjectedMyTrait {} + +impl Project for Option { + type Projected<'a> = Option> where T: 'a; + fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_> { + this.0.as_ref().map(Wrapper) + } +} + +impl MyTrait for Option> {} + +impl MyTrait for Wrapper<'_, T> {} + +impl ProjectedMyTrait for T + where + T: Project, + for<'a> T::Projected<'a>: MyTrait, + //~^ NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime + //~| NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime +{} + +fn require_trait(_: T) {} + +fn foo(wrap: Wrapper<'_, Option>, wrap1: Wrapper<'_, Option>) { + //~^ HELP consider restricting the type parameter to the `'static` lifetime + //~| HELP consider restricting the type parameter to the `'static` lifetime + require_trait(wrap); + //~^ ERROR `T` does not live long enough + require_trait(wrap1); + //~^ ERROR `U` does not live long enough +} + +fn main() {} diff --git a/tests/ui/lifetimes/issue-105507.rs b/tests/ui/lifetimes/issue-105507.rs new file mode 100644 index 00000000000..f46c6b6f21e --- /dev/null +++ b/tests/ui/lifetimes/issue-105507.rs @@ -0,0 +1,43 @@ +// run-rustfix +// +#![allow(warnings)] +struct Wrapper<'a, T: ?Sized>(&'a T); + +trait Project { + type Projected<'a> where Self: 'a; + fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_>; +} +trait MyTrait {} +trait ProjectedMyTrait {} + +impl Project for Option { + type Projected<'a> = Option> where T: 'a; + fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_> { + this.0.as_ref().map(Wrapper) + } +} + +impl MyTrait for Option> {} + +impl MyTrait for Wrapper<'_, T> {} + +impl ProjectedMyTrait for T + where + T: Project, + for<'a> T::Projected<'a>: MyTrait, + //~^ NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime + //~| NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime +{} + +fn require_trait(_: T) {} + +fn foo(wrap: Wrapper<'_, Option>, wrap1: Wrapper<'_, Option>) { + //~^ HELP consider restricting the type parameter to the `'static` lifetime + //~| HELP consider restricting the type parameter to the `'static` lifetime + require_trait(wrap); + //~^ ERROR `T` does not live long enough + require_trait(wrap1); + //~^ ERROR `U` does not live long enough +} + +fn main() {} diff --git a/tests/ui/lifetimes/issue-105507.stderr b/tests/ui/lifetimes/issue-105507.stderr new file mode 100644 index 00000000000..44d3a7eb9a4 --- /dev/null +++ b/tests/ui/lifetimes/issue-105507.stderr @@ -0,0 +1,34 @@ +error: `T` does not live long enough + --> $DIR/issue-105507.rs:37:5 + | +LL | require_trait(wrap); + | ^^^^^^^^^^^^^^^^^^^ + | +note: due to current limitations in the borrow checker, this implies a `'static` lifetime + --> $DIR/issue-105507.rs:27:35 + | +LL | for<'a> T::Projected<'a>: MyTrait, + | ^^^^^^^ +help: consider restricting the type parameter to the `'static` lifetime + | +LL | fn foo(wrap: Wrapper<'_, Option>, wrap1: Wrapper<'_, Option>) { + | +++++++++ +++++++++ + +error: `U` does not live long enough + --> $DIR/issue-105507.rs:39:5 + | +LL | require_trait(wrap1); + | ^^^^^^^^^^^^^^^^^^^^ + | +note: due to current limitations in the borrow checker, this implies a `'static` lifetime + --> $DIR/issue-105507.rs:27:35 + | +LL | for<'a> T::Projected<'a>: MyTrait, + | ^^^^^^^ +help: consider restricting the type parameter to the `'static` lifetime + | +LL | fn foo(wrap: Wrapper<'_, Option>, wrap1: Wrapper<'_, Option>) { + | +++++++++ +++++++++ + +error: aborting due to 2 previous errors +