rustc_trait_selection/error_reporting/infer/nice_region_error/
static_impl_trait.rs1use rustc_data_structures::fx::FxIndexSet;
4use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
5use rustc_hir::def_id::DefId;
6use rustc_hir::intravisit::{Visitor, VisitorExt, walk_ty};
7use rustc_hir::{
8 self as hir, AmbigArg, GenericBound, GenericParam, GenericParamKind, Item, ItemKind, Lifetime,
9 LifetimeKind, LifetimeParamKind, MissingLifetimeKind, Node, TyKind,
10};
11use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
12use rustc_span::def_id::LocalDefId;
13use rustc_span::{Ident, Span};
14use tracing::debug;
15
16use crate::error_reporting::infer::nice_region_error::NiceRegionError;
17use crate::errors::ButNeedsToSatisfy;
18use crate::infer::{RegionResolutionError, SubregionOrigin};
19
20impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
21 pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorGuaranteed> {
24 debug!("try_report_static_impl_trait(error={:?})", self.error);
25 let tcx = self.tcx();
26 let (var_origin, sub_origin, sub_r, sup_origin, sup_r, spans) = match self.error.as_ref()? {
27 RegionResolutionError::SubSupConflict(
28 _,
29 var_origin,
30 sub_origin,
31 sub_r,
32 sup_origin,
33 sup_r,
34 spans,
35 ) if sub_r.is_static() => (var_origin, sub_origin, sub_r, sup_origin, sup_r, spans),
36 _ => return None,
37 };
38 debug!(
39 "try_report_static_impl_trait(var={:?}, sub={:?} {:?} sup={:?} {:?})",
40 var_origin, sub_origin, sub_r, sup_origin, sup_r
41 );
42 let anon_reg_sup = tcx.is_suitable_region(self.generic_param_scope, *sup_r)?;
43 debug!("try_report_static_impl_trait: anon_reg_sup={:?}", anon_reg_sup);
44 let sp = var_origin.span();
45 let return_sp = sub_origin.span();
46 let param = self.find_param_with_region(*sup_r, *sub_r)?;
47 let simple_ident = param.param.pat.simple_ident();
48 let lifetime_name =
49 if sup_r.is_named(self.tcx()) { sup_r.to_string() } else { "'_".to_owned() };
50
51 let (mention_influencer, influencer_point) =
52 if sup_origin.span().overlaps(param.param_ty_span) {
53 (false, sup_origin.span())
65 } else {
66 (!sup_origin.span().overlaps(return_sp), param.param_ty_span)
67 };
68
69 debug!("try_report_static_impl_trait: param_info={:?}", param);
70
71 let mut spans = spans.clone();
72
73 if mention_influencer {
74 spans.push(sup_origin.span());
75 }
76 spans.sort();
78 spans.dedup_by_key(|span| (span.lo(), span.hi()));
79
80 let require_span =
82 if sup_origin.span().overlaps(return_sp) { sup_origin.span() } else { return_sp };
83
84 let spans_empty = spans.is_empty();
85 let require_as_note = spans.iter().any(|sp| sp.overlaps(return_sp) || *sp > return_sp);
86 let bound = if let SubregionOrigin::RelateParamBound(_, _, Some(bound)) = sub_origin {
87 Some(*bound)
88 } else {
89 None
90 };
91
92 let diag = ButNeedsToSatisfy {
93 sp,
94 influencer_point,
95 spans: spans.clone(),
96 require_span_as_note: require_as_note.then_some(require_span),
100 require_span_as_label: (!require_as_note).then_some(require_span),
102
103 has_lifetime: sup_r.is_named(self.tcx()),
104 lifetime: lifetime_name.clone(),
105 has_param_name: simple_ident.is_some(),
106 param_name: simple_ident.map(|x| x.to_string()).unwrap_or_default(),
107 spans_empty,
108 bound,
109 };
110
111 let mut err = self.tcx().dcx().create_err(diag);
112
113 let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
114
115 let arg = match param.param.pat.simple_ident() {
116 Some(simple_ident) => format!("argument `{simple_ident}`"),
117 None => "the argument".to_string(),
118 };
119 let captures = format!("captures data from {arg}");
120 suggest_new_region_bound(
121 tcx,
122 &mut err,
123 fn_returns,
124 lifetime_name,
125 Some(arg),
126 captures,
127 Some((param.param_ty_span, param.param_ty.to_string())),
128 Some(anon_reg_sup.scope),
129 );
130
131 let reported = err.emit();
132 Some(reported)
133 }
134}
135
136pub fn suggest_new_region_bound(
137 tcx: TyCtxt<'_>,
138 err: &mut Diag<'_>,
139 fn_returns: Vec<&rustc_hir::Ty<'_>>,
140 lifetime_name: String,
141 arg: Option<String>,
142 captures: String,
143 param: Option<(Span, String)>,
144 scope_def_id: Option<LocalDefId>,
145) {
146 debug!("try_report_static_impl_trait: fn_return={:?}", fn_returns);
147 let consider = "consider changing";
149 let declare = "to declare that";
150 let explicit = format!("you can add an explicit `{lifetime_name}` lifetime bound");
151 let explicit_static =
152 arg.map(|arg| format!("explicit `'static` bound to the lifetime of {arg}"));
153 let add_static_bound = "alternatively, add an explicit `'static` bound to this reference";
154 let plus_lt = format!(" + {lifetime_name}");
155 for fn_return in fn_returns {
156 if fn_return.span.desugaring_kind().is_some() {
157 continue;
159 }
160 match fn_return.kind {
161 TyKind::OpaqueDef(opaque) => {
163 let did = opaque.def_id.to_def_id();
165 let ty = Ty::new_opaque(tcx, did, ty::GenericArgs::identity_for_item(tcx, did));
166
167 if let Some(span) = opaque.bounds.iter().find_map(|arg| match arg {
168 GenericBound::Outlives(Lifetime {
169 kind: LifetimeKind::Static, ident, ..
170 }) => Some(ident.span),
171 _ => None,
172 }) {
173 if let Some(explicit_static) = &explicit_static {
174 err.span_suggestion_verbose(
175 span,
176 format!("{consider} `{ty}`'s {explicit_static}"),
177 &lifetime_name,
178 Applicability::MaybeIncorrect,
179 );
180 }
181 if let Some((param_span, ref param_ty)) = param {
182 err.span_suggestion_verbose(
183 param_span,
184 add_static_bound,
185 param_ty,
186 Applicability::MaybeIncorrect,
187 );
188 }
189 } else if opaque.bounds.iter().any(|arg| {
190 matches!(arg,
191 GenericBound::Outlives(Lifetime { ident, .. })
192 if ident.name.to_string() == lifetime_name )
193 }) {
194 } else {
195 let existing_lt_name = if let Some(id) = scope_def_id
197 && let Some(generics) = tcx.hir_get_generics(id)
198 && let named_lifetimes = generics
199 .params
200 .iter()
201 .filter(|p| {
202 matches!(
203 p.kind,
204 GenericParamKind::Lifetime {
205 kind: hir::LifetimeParamKind::Explicit
206 }
207 )
208 })
209 .map(|p| {
210 if let hir::ParamName::Plain(name) = p.name {
211 Some(name.to_string())
212 } else {
213 None
214 }
215 })
216 .filter(|n| !matches!(n, None))
217 .collect::<Vec<_>>()
218 && named_lifetimes.len() > 0
219 {
220 named_lifetimes[0].clone()
221 } else {
222 None
223 };
224 let name = if let Some(name) = &existing_lt_name { name } else { "'a" };
225 if let Some(id) = scope_def_id
228 && let Some(generics) = tcx.hir_get_generics(id)
229 && let mut spans_suggs =
230 make_elided_region_spans_suggs(name, generics.params.iter())
231 && spans_suggs.len() > 1
232 {
233 let use_lt = if existing_lt_name == None {
234 spans_suggs.push((generics.span.shrink_to_hi(), format!("<{name}>")));
235 format!("you can introduce a named lifetime parameter `{name}`")
236 } else {
237 format!("you can use the named lifetime parameter `{name}`")
239 };
240 spans_suggs.push((fn_return.span.shrink_to_hi(), format!(" + {name} ")));
241 err.multipart_suggestion_verbose(
242 format!("{declare} `{ty}` {captures}, {use_lt}",),
243 spans_suggs,
244 Applicability::MaybeIncorrect,
245 );
246 } else {
247 err.span_suggestion_verbose(
248 fn_return.span.shrink_to_hi(),
249 format!("{declare} `{ty}` {captures}, {explicit}",),
250 &plus_lt,
251 Applicability::MaybeIncorrect,
252 );
253 }
254 }
255 }
256 TyKind::TraitObject(_, lt) => {
257 if let LifetimeKind::ImplicitObjectLifetimeDefault = lt.kind {
258 err.span_suggestion_verbose(
259 fn_return.span.shrink_to_hi(),
260 format!("{declare} the trait object {captures}, {explicit}",),
261 &plus_lt,
262 Applicability::MaybeIncorrect,
263 );
264 } else if lt.ident.name.to_string() != lifetime_name {
265 if let Some(explicit_static) = &explicit_static {
270 err.span_suggestion_verbose(
271 lt.ident.span,
272 format!("{consider} the trait object's {explicit_static}"),
273 &lifetime_name,
274 Applicability::MaybeIncorrect,
275 );
276 }
277 if let Some((param_span, param_ty)) = param.clone() {
278 err.span_suggestion_verbose(
279 param_span,
280 add_static_bound,
281 param_ty,
282 Applicability::MaybeIncorrect,
283 );
284 }
285 }
286 }
287 _ => {}
288 }
289 }
290}
291
292fn make_elided_region_spans_suggs<'a>(
293 name: &str,
294 generic_params: impl Iterator<Item = &'a GenericParam<'a>>,
295) -> Vec<(Span, String)> {
296 let mut spans_suggs = Vec::new();
297 let mut bracket_span = None;
298 let mut consecutive_brackets = 0;
299
300 let mut process_consecutive_brackets =
301 |span: Option<Span>, spans_suggs: &mut Vec<(Span, String)>| {
302 if let Some(span) = span
303 && bracket_span.is_none_or(|bracket_span| span == bracket_span)
304 {
305 consecutive_brackets += 1;
306 } else if let Some(bracket_span) = bracket_span.take() {
307 let sugg = std::iter::once("<")
308 .chain(std::iter::repeat_n(name, consecutive_brackets).intersperse(", "))
309 .chain([">"])
310 .collect();
311 spans_suggs.push((bracket_span.shrink_to_hi(), sugg));
312 consecutive_brackets = 0;
313 }
314 bracket_span = span;
315 };
316
317 for p in generic_params {
318 if let GenericParamKind::Lifetime { kind: LifetimeParamKind::Elided(kind) } = p.kind {
319 match kind {
320 MissingLifetimeKind::Underscore => {
321 process_consecutive_brackets(None, &mut spans_suggs);
322 spans_suggs.push((p.span, name.to_string()))
323 }
324 MissingLifetimeKind::Ampersand => {
325 process_consecutive_brackets(None, &mut spans_suggs);
326 spans_suggs.push((p.span.shrink_to_hi(), format!("{name} ")));
327 }
328 MissingLifetimeKind::Comma => {
329 process_consecutive_brackets(None, &mut spans_suggs);
330 spans_suggs.push((p.span.shrink_to_hi(), format!("{name}, ")));
331 }
332 MissingLifetimeKind::Brackets => {
333 process_consecutive_brackets(Some(p.span), &mut spans_suggs);
334 }
335 }
336 }
337 }
338 process_consecutive_brackets(None, &mut spans_suggs);
339
340 spans_suggs
341}
342
343impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
344 pub fn get_impl_ident_and_self_ty_from_trait(
345 tcx: TyCtxt<'tcx>,
346 def_id: DefId,
347 trait_objects: &FxIndexSet<DefId>,
348 ) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
349 match tcx.hir_get_if_local(def_id)? {
350 Node::ImplItem(impl_item) => {
351 let impl_did = tcx.hir_get_parent_item(impl_item.hir_id());
352 if let hir::OwnerNode::Item(Item {
353 kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
354 ..
355 }) = tcx.hir_owner_node(impl_did)
356 {
357 Some((impl_item.ident, self_ty))
358 } else {
359 None
360 }
361 }
362 Node::TraitItem(trait_item) => {
363 let trait_id = tcx.hir_get_parent_item(trait_item.hir_id());
364 debug_assert_eq!(tcx.def_kind(trait_id.def_id), hir::def::DefKind::Trait);
365 let trait_did = trait_id.to_def_id();
369 tcx.local_trait_impls(trait_did).iter().find_map(|&impl_did| {
370 if let Node::Item(Item {
371 kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
372 }) = tcx.hir_node_by_def_id(impl_did)
373 && trait_objects.iter().all(|did| {
374 let mut traits = vec![];
380 let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
381 hir_v.visit_ty_unambig(self_ty);
382 !traits.is_empty()
383 })
384 {
385 Some((trait_item.ident, *self_ty))
386 } else {
387 None
388 }
389 })
390 }
391 _ => None,
392 }
393 }
394}
395
396pub struct TraitObjectVisitor(pub FxIndexSet<DefId>);
398
399impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for TraitObjectVisitor {
400 fn visit_ty(&mut self, t: Ty<'tcx>) {
401 match t.kind() {
402 ty::Dynamic(preds, re) if re.is_static() => {
403 if let Some(def_id) = preds.principal_def_id() {
404 self.0.insert(def_id);
405 }
406 }
407 _ => t.super_visit_with(self),
408 }
409 }
410}
411
412pub struct HirTraitObjectVisitor<'a>(pub &'a mut Vec<Span>, pub DefId);
414
415impl<'a, 'tcx> Visitor<'tcx> for HirTraitObjectVisitor<'a> {
416 fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx, AmbigArg>) {
417 if let TyKind::TraitObject(poly_trait_refs, lifetime_ptr) = t.kind
418 && let Lifetime { kind: LifetimeKind::ImplicitObjectLifetimeDefault, .. } =
419 lifetime_ptr.pointer()
420 {
421 for ptr in poly_trait_refs {
422 if Some(self.1) == ptr.trait_ref.trait_def_id() {
423 self.0.push(ptr.span);
424 }
425 }
426 }
427 walk_ty(self, t);
428 }
429}