rustc_borrowck/diagnostics/outlives_suggestion.rs
1//! Contains utilities for generating suggestions for borrowck errors related to unsatisfied
2//! outlives constraints.
3
4#![allow(rustc::diagnostic_outside_of_impl)]
5#![allow(rustc::untranslatable_diagnostic)]
6
7use std::collections::BTreeMap;
8
9use rustc_data_structures::fx::FxIndexSet;
10use rustc_errors::Diag;
11use rustc_middle::ty::RegionVid;
12use smallvec::SmallVec;
13use tracing::debug;
14
15use super::{ErrorConstraintInfo, RegionName, RegionNameSource};
16use crate::MirBorrowckCtxt;
17
18/// The different things we could suggest.
19enum SuggestedConstraint {
20 /// Outlives(a, [b, c, d, ...]) => 'a: 'b + 'c + 'd + ...
21 Outlives(RegionName, SmallVec<[RegionName; 2]>),
22
23 /// 'a = 'b
24 Equal(RegionName, RegionName),
25
26 /// 'a: 'static i.e. 'a = 'static and the user should just use 'static
27 Static(RegionName),
28}
29
30/// Collects information about outlives constraints that needed to be added for a given MIR node
31/// corresponding to a function definition.
32///
33/// Adds a help note suggesting adding a where clause with the needed constraints.
34#[derive(Default)]
35pub(crate) struct OutlivesSuggestionBuilder {
36 /// The list of outlives constraints that need to be added. Specifically, we map each free
37 /// region to all other regions that it must outlive. I will use the shorthand `fr:
38 /// outlived_frs`. Not all of these regions will already have names necessarily. Some could be
39 /// implicit free regions that we inferred. These will need to be given names in the final
40 /// suggestion message.
41 constraints_to_add: BTreeMap<RegionVid, Vec<RegionVid>>,
42}
43
44impl OutlivesSuggestionBuilder {
45 /// Returns `true` iff the `RegionNameSource` is a valid source for an outlives
46 /// suggestion.
47 //
48 // FIXME: Currently, we only report suggestions if the `RegionNameSource` is an early-bound
49 // region or a named region, avoiding using regions with synthetic names altogether. This
50 // allows us to avoid giving impossible suggestions (e.g. adding bounds to closure args).
51 // We can probably be less conservative, since some inferred free regions are namable (e.g.
52 // the user can explicitly name them. To do this, we would allow some regions whose names
53 // come from `MatchedAdtAndSegment`, being careful to filter out bad suggestions, such as
54 // naming the `'self` lifetime in methods, etc.
55 fn region_name_is_suggestable(name: &RegionName) -> bool {
56 match name.source {
57 RegionNameSource::NamedEarlyParamRegion(..)
58 | RegionNameSource::NamedLateParamRegion(..)
59 | RegionNameSource::Static => true,
60
61 // Don't give suggestions for upvars, closure return types, or other unnameable
62 // regions.
63 RegionNameSource::SynthesizedFreeEnvRegion(..)
64 | RegionNameSource::AnonRegionFromArgument(..)
65 | RegionNameSource::AnonRegionFromUpvar(..)
66 | RegionNameSource::AnonRegionFromOutput(..)
67 | RegionNameSource::AnonRegionFromYieldTy(..)
68 | RegionNameSource::AnonRegionFromAsyncFn(..)
69 | RegionNameSource::AnonRegionFromImplSignature(..) => {
70 debug!("Region {:?} is NOT suggestable", name);
71 false
72 }
73 }
74 }
75
76 /// Returns a name for the region if it is suggestable. See `region_name_is_suggestable`.
77 fn region_vid_to_name(
78 &self,
79 mbcx: &MirBorrowckCtxt<'_, '_, '_>,
80 region: RegionVid,
81 ) -> Option<RegionName> {
82 mbcx.give_region_a_name(region).filter(Self::region_name_is_suggestable)
83 }
84
85 /// Compiles a list of all suggestions to be printed in the final big suggestion.
86 fn compile_all_suggestions(
87 &self,
88 mbcx: &MirBorrowckCtxt<'_, '_, '_>,
89 ) -> SmallVec<[SuggestedConstraint; 2]> {
90 let mut suggested = SmallVec::new();
91
92 // Keep track of variables that we have already suggested unifying so that we don't print
93 // out silly duplicate messages.
94 let mut unified_already = FxIndexSet::default();
95
96 for (fr, outlived) in &self.constraints_to_add {
97 let Some(fr_name) = self.region_vid_to_name(mbcx, *fr) else {
98 continue;
99 };
100
101 let outlived = outlived
102 .iter()
103 // if there is a `None`, we will just omit that constraint
104 .filter_map(|fr| self.region_vid_to_name(mbcx, *fr).map(|rname| (fr, rname)))
105 .collect::<Vec<_>>();
106
107 // No suggestable outlived lifetimes.
108 if outlived.is_empty() {
109 continue;
110 }
111
112 // There are three types of suggestions we can make:
113 // 1) Suggest a bound: 'a: 'b
114 // 2) Suggest replacing 'a with 'static. If any of `outlived` is `'static`, then we
115 // should just replace 'a with 'static.
116 // 3) Suggest unifying 'a with 'b if we have both 'a: 'b and 'b: 'a
117
118 if outlived
119 .iter()
120 .any(|(_, outlived_name)| matches!(outlived_name.source, RegionNameSource::Static))
121 {
122 suggested.push(SuggestedConstraint::Static(fr_name));
123 } else {
124 // We want to isolate out all lifetimes that should be unified and print out
125 // separate messages for them.
126
127 let (unified, other): (Vec<_>, Vec<_>) = outlived.into_iter().partition(
128 // Do we have both 'fr: 'r and 'r: 'fr?
129 |(r, _)| {
130 self.constraints_to_add
131 .get(r)
132 .is_some_and(|r_outlived| r_outlived.as_slice().contains(fr))
133 },
134 );
135
136 for (r, bound) in unified.into_iter() {
137 if !unified_already.contains(fr) {
138 suggested.push(SuggestedConstraint::Equal(fr_name, bound));
139 unified_already.insert(r);
140 }
141 }
142
143 if !other.is_empty() {
144 let other = other.iter().map(|(_, rname)| *rname).collect::<SmallVec<_>>();
145 suggested.push(SuggestedConstraint::Outlives(fr_name, other))
146 }
147 }
148 }
149
150 suggested
151 }
152
153 /// Add the outlives constraint `fr: outlived_fr` to the set of constraints we need to suggest.
154 pub(crate) fn collect_constraint(&mut self, fr: RegionVid, outlived_fr: RegionVid) {
155 debug!("Collected {:?}: {:?}", fr, outlived_fr);
156
157 // Add to set of constraints for final help note.
158 self.constraints_to_add.entry(fr).or_default().push(outlived_fr);
159 }
160
161 /// Emit an intermediate note on the given `Diag` if the involved regions are suggestable.
162 pub(crate) fn intermediate_suggestion(
163 &mut self,
164 mbcx: &MirBorrowckCtxt<'_, '_, '_>,
165 errci: &ErrorConstraintInfo<'_>,
166 diag: &mut Diag<'_>,
167 ) {
168 // Emit an intermediate note.
169 let fr_name = self.region_vid_to_name(mbcx, errci.fr);
170 let outlived_fr_name = self.region_vid_to_name(mbcx, errci.outlived_fr);
171
172 if let (Some(fr_name), Some(outlived_fr_name)) = (fr_name, outlived_fr_name)
173 && !matches!(outlived_fr_name.source, RegionNameSource::Static)
174 {
175 diag.help(format!(
176 "consider adding the following bound: `{fr_name}: {outlived_fr_name}`",
177 ));
178 }
179 }
180
181 /// If there is a suggestion to emit, add a diagnostic to the buffer. This is the final
182 /// suggestion including all collected constraints.
183 pub(crate) fn add_suggestion(&self, mbcx: &mut MirBorrowckCtxt<'_, '_, '_>) {
184 // No constraints to add? Done.
185 if self.constraints_to_add.is_empty() {
186 debug!("No constraints to suggest.");
187 return;
188 }
189
190 // If there is only one constraint to suggest, then we already suggested it in the
191 // intermediate suggestion above.
192 if self.constraints_to_add.len() == 1
193 && self.constraints_to_add.values().next().unwrap().len() == 1
194 {
195 debug!("Only 1 suggestion. Skipping.");
196 return;
197 }
198
199 // Get all suggestable constraints.
200 let suggested = self.compile_all_suggestions(mbcx);
201
202 // If there are no suggestable constraints...
203 if suggested.is_empty() {
204 debug!("Only 1 suggestable constraint. Skipping.");
205 return;
206 }
207
208 // If there is exactly one suggestable constraints, then just suggest it. Otherwise, emit a
209 // list of diagnostics.
210 let mut diag = if let [constraint] = suggested.as_slice() {
211 mbcx.dcx().struct_help(match constraint {
212 SuggestedConstraint::Outlives(a, bs) => {
213 let bs: SmallVec<[String; 2]> = bs.iter().map(|r| r.to_string()).collect();
214 format!("add bound `{a}: {}`", bs.join(" + "))
215 }
216
217 SuggestedConstraint::Equal(a, b) => {
218 format!("`{a}` and `{b}` must be the same: replace one with the other")
219 }
220 SuggestedConstraint::Static(a) => format!("replace `{a}` with `'static`"),
221 })
222 } else {
223 // Create a new diagnostic.
224 let mut diag = mbcx
225 .infcx
226 .tcx
227 .dcx()
228 .struct_help("the following changes may resolve your lifetime errors");
229
230 // Add suggestions.
231 for constraint in suggested {
232 match constraint {
233 SuggestedConstraint::Outlives(a, bs) => {
234 let bs: SmallVec<[String; 2]> = bs.iter().map(|r| r.to_string()).collect();
235 diag.help(format!("add bound `{a}: {}`", bs.join(" + ")));
236 }
237 SuggestedConstraint::Equal(a, b) => {
238 diag.help(format!(
239 "`{a}` and `{b}` must be the same: replace one with the other",
240 ));
241 }
242 SuggestedConstraint::Static(a) => {
243 diag.help(format!("replace `{a}` with `'static`"));
244 }
245 }
246 }
247
248 diag
249 };
250
251 // We want this message to appear after other messages on the mir def.
252 let mir_span = mbcx.body.span;
253 diag.sort_span = mir_span.shrink_to_hi();
254
255 // Buffer the diagnostic
256 mbcx.buffer_non_error(diag);
257 }
258}