@@ -9,7 +9,7 @@ use rustc::mir::{
99use rustc:: ty:: adjustment:: PointerCast ;
1010use rustc:: ty:: { self , TyCtxt } ;
1111use rustc_data_structures:: fx:: FxHashSet ;
12- use rustc_errors:: DiagnosticBuilder ;
12+ use rustc_errors:: { Applicability , DiagnosticBuilder } ;
1313use rustc_index:: vec:: IndexVec ;
1414use syntax_pos:: symbol:: Symbol ;
1515use syntax_pos:: Span ;
@@ -206,6 +206,47 @@ impl BorrowExplanation {
206206 ) ,
207207 ) ;
208208 } ;
209+
210+ self . add_lifetime_bound_suggestion_to_diagnostic (
211+ tcx,
212+ err,
213+ & category,
214+ span,
215+ region_name,
216+ ) ;
217+ }
218+ _ => { }
219+ }
220+ }
221+ pub ( in crate :: borrow_check) fn add_lifetime_bound_suggestion_to_diagnostic < ' tcx > (
222+ & self ,
223+ tcx : TyCtxt < ' tcx > ,
224+ err : & mut DiagnosticBuilder < ' _ > ,
225+ category : & ConstraintCategory ,
226+ span : Span ,
227+ region_name : & RegionName ,
228+ ) {
229+ match category {
230+ ConstraintCategory :: OpaqueType => {
231+ if let Ok ( snippet) = tcx. sess . source_map ( ) . span_to_snippet ( span) {
232+ let suggestable_name = if region_name. was_named ( ) {
233+ region_name. to_string ( )
234+ } else {
235+ "'_" . to_string ( )
236+ } ;
237+
238+ err. span_suggestion (
239+ span,
240+ & format ! (
241+ "you can add a constraint to the {}to make it last less than \
242+ `'static` and match `{}`",
243+ category. description( ) ,
244+ region_name,
245+ ) ,
246+ format ! ( "{} + {}" , snippet, suggestable_name) ,
247+ Applicability :: Unspecified ,
248+ ) ;
249+ }
209250 }
210251 _ => { }
211252 }
0 commit comments