Skip to main content

rustc_borrowck/diagnostics/
opaque_types.rs

1use std::ops::ControlFlow;
2
3use either::Either;
4use itertools::Itertools as _;
5use rustc_data_structures::fx::FxIndexSet;
6use rustc_errors::{Diag, Subdiagnostic};
7use rustc_hir as hir;
8use rustc_hir::def_id::DefId;
9use rustc_middle::mir::{self, ConstraintCategory, Location};
10use rustc_middle::ty::{
11    self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
12    Unnormalized,
13};
14use rustc_span::Span;
15use rustc_trait_selection::error_reporting::infer::region::unexpected_hidden_region_diagnostic;
16use rustc_trait_selection::errors::impl_trait_overcapture_suggestion;
17
18use crate::MirBorrowckCtxt;
19use crate::borrow_set::BorrowData;
20use crate::consumers::RegionInferenceContext;
21use crate::region_infer::opaque_types::DeferredOpaqueTypeError;
22use crate::type_check::Locations;
23
24impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
25    pub(crate) fn report_opaque_type_errors(&mut self, errors: Vec<DeferredOpaqueTypeError<'tcx>>) {
26        if errors.is_empty() {
27            return;
28        }
29
30        let infcx = self.infcx;
31        let mut guar = None;
32        let mut last_unexpected_hidden_region: Option<(Span, Ty<'_>, ty::OpaqueTypeKey<'tcx>)> =
33            None;
34        for error in errors {
35            guar = Some(match error {
36                DeferredOpaqueTypeError::InvalidOpaqueTypeArgs(err) => err.report(infcx),
37                DeferredOpaqueTypeError::LifetimeMismatchOpaqueParam(err) => {
38                    infcx.dcx().emit_err(err)
39                }
40                DeferredOpaqueTypeError::UnexpectedHiddenRegion {
41                    opaque_type_key,
42                    hidden_type,
43                    member_region,
44                } => {
45                    let named_ty =
46                        self.regioncx.name_regions_for_member_constraint(infcx.tcx, hidden_type.ty);
47                    let named_key = self
48                        .regioncx
49                        .name_regions_for_member_constraint(infcx.tcx, opaque_type_key);
50                    let named_region =
51                        self.regioncx.name_regions_for_member_constraint(infcx.tcx, member_region);
52                    let diag = unexpected_hidden_region_diagnostic(
53                        infcx,
54                        self.mir_def_id(),
55                        hidden_type.span,
56                        named_ty,
57                        named_region,
58                        named_key,
59                    );
60                    if last_unexpected_hidden_region
61                        != Some((hidden_type.span, named_ty, named_key))
62                    {
63                        last_unexpected_hidden_region =
64                            Some((hidden_type.span, named_ty, named_key));
65                        diag.emit()
66                    } else {
67                        diag.delay_as_bug()
68                    }
69                }
70                DeferredOpaqueTypeError::NonDefiningUseInDefiningScope {
71                    span,
72                    opaque_type_key,
73                } => infcx.dcx().span_err(
74                    span,
75                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("non-defining use of `{0}` in the defining scope",
                Ty::new_opaque(infcx.tcx, opaque_type_key.def_id.to_def_id(),
                    opaque_type_key.args)))
    })format!(
76                        "non-defining use of `{}` in the defining scope",
77                        Ty::new_opaque(
78                            infcx.tcx,
79                            opaque_type_key.def_id.to_def_id(),
80                            opaque_type_key.args
81                        )
82                    ),
83                ),
84            });
85        }
86        let guar = guar.unwrap();
87        self.root_cx.set_tainted_by_errors(guar);
88        self.infcx.set_tainted_by_errors(guar);
89    }
90
91    /// Try to note when an opaque is involved in a borrowck error and that
92    /// opaque captures lifetimes due to edition 2024.
93    // FIXME: This code is otherwise somewhat general, and could easily be adapted
94    // to explain why other things overcapture... like async fn and RPITITs.
95    pub(crate) fn note_due_to_edition_2024_opaque_capture_rules(
96        &self,
97        borrow: &BorrowData<'tcx>,
98        diag: &mut Diag<'_>,
99    ) {
100        // We look at all the locals. Why locals? Because it's the best thing
101        // I could think of that's correlated with the *instantiated* higher-ranked
102        // binder for calls, since we don't really store those anywhere else.
103        for ty in self.body.local_decls.iter().map(|local| local.ty) {
104            if !ty.has_opaque_types() {
105                continue;
106            }
107
108            let tcx = self.infcx.tcx;
109            let ControlFlow::Break((opaque_def_id, offending_region_idx, location)) = ty
110                .visit_with(&mut FindOpaqueRegion {
111                    regioncx: &self.regioncx,
112                    tcx,
113                    borrow_region: borrow.region,
114                })
115            else {
116                continue;
117            };
118
119            // If an opaque explicitly captures a lifetime, then no need to point it out.
120            // FIXME: We should be using a better heuristic for `use<>`.
121            if tcx.rendered_precise_capturing_args(opaque_def_id).is_some() {
122                continue;
123            }
124
125            // If one of the opaque's bounds mentions the region, then no need to
126            // point it out, since it would've been captured on edition 2021 as well.
127            //
128            // Also, while we're at it, collect all the lifetimes that the opaque
129            // *does* mention. We'll use that for the `+ use<'a>` suggestion below.
130            let mut visitor = CheckExplicitRegionMentionAndCollectGenerics {
131                tcx,
132                generics: tcx.generics_of(opaque_def_id),
133                offending_region_idx,
134                seen_opaques: [opaque_def_id].into_iter().collect(),
135                seen_lifetimes: Default::default(),
136            };
137            if tcx
138                .explicit_item_bounds(opaque_def_id)
139                .skip_binder()
140                .visit_with(&mut visitor)
141                .is_break()
142            {
143                continue;
144            }
145
146            // If we successfully located a terminator, then point it out
147            // and provide a suggestion if it's local.
148            match self.body.stmt_at(location) {
149                Either::Right(mir::Terminator { source_info, .. }) => {
150                    diag.span_note(
151                        source_info.span,
152                        "this call may capture more lifetimes than intended, \
153                        because Rust 2024 has adjusted the `impl Trait` lifetime capture rules",
154                    );
155                    let mut captured_args = visitor.seen_lifetimes;
156                    // Add in all of the type and const params, too.
157                    // Ordering here is kinda strange b/c we're walking backwards,
158                    // but we're trying to provide *a* suggestion, not a nice one.
159                    let mut next_generics = Some(visitor.generics);
160                    let mut any_synthetic = false;
161                    while let Some(generics) = next_generics {
162                        for param in &generics.own_params {
163                            if param.kind.is_ty_or_const() {
164                                captured_args.insert(param.def_id);
165                            }
166                            if param.kind.is_synthetic() {
167                                any_synthetic = true;
168                            }
169                        }
170                        next_generics = generics.parent.map(|def_id| tcx.generics_of(def_id));
171                    }
172
173                    if let Some(opaque_def_id) = opaque_def_id.as_local()
174                        && let hir::OpaqueTyOrigin::FnReturn { parent, .. } =
175                            tcx.hir_expect_opaque_ty(opaque_def_id).origin
176                    {
177                        if let Some(sugg) = impl_trait_overcapture_suggestion(
178                            tcx,
179                            opaque_def_id,
180                            parent,
181                            captured_args,
182                        ) {
183                            sugg.add_to_diag(diag);
184                        }
185                    } else {
186                        diag.span_help(
187                            tcx.def_span(opaque_def_id),
188                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if you can modify this crate, add a precise capturing bound to avoid overcapturing: `+ use<{0}>`",
                if any_synthetic {
                    "/* Args */".to_string()
                } else {
                    captured_args.into_iter().map(|def_id|
                                tcx.item_name(def_id)).join(", ")
                }))
    })format!(
189                                "if you can modify this crate, add a precise \
190                                capturing bound to avoid overcapturing: `+ use<{}>`",
191                                if any_synthetic {
192                                    "/* Args */".to_string()
193                                } else {
194                                    captured_args
195                                        .into_iter()
196                                        .map(|def_id| tcx.item_name(def_id))
197                                        .join(", ")
198                                }
199                            ),
200                        );
201                    }
202                    return;
203                }
204                Either::Left(_) => {}
205            }
206        }
207    }
208}
209
210/// This visitor contains the bulk of the logic for this lint.
211struct FindOpaqueRegion<'a, 'tcx> {
212    tcx: TyCtxt<'tcx>,
213    regioncx: &'a RegionInferenceContext<'tcx>,
214    borrow_region: ty::RegionVid,
215}
216
217impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for FindOpaqueRegion<'_, 'tcx> {
218    type Result = ControlFlow<(DefId, usize, Location), ()>;
219
220    fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
221        // If we find an opaque in a local ty, then for each of its captured regions,
222        // try to find a path between that captured regions and our borrow region...
223        if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) = *ty.kind()
224            && let hir::OpaqueTyOrigin::FnReturn { parent, in_trait_or_impl: None } =
225                self.tcx.opaque_ty_origin(def_id)
226        {
227            let variances = self.tcx.variances_of(def_id);
228            for (idx, (arg, variance)) in std::iter::zip(args, variances).enumerate() {
229                // Skip uncaptured args.
230                if *variance == ty::Bivariant {
231                    continue;
232                }
233                // We only care about regions.
234                let Some(opaque_region) = arg.as_region() else {
235                    continue;
236                };
237                // Don't try to convert a late-bound region, which shouldn't exist anyways (yet).
238                if opaque_region.is_bound() {
239                    continue;
240                }
241                let opaque_region_vid = self.regioncx.to_region_vid(opaque_region);
242
243                // Find a path between the borrow region and our opaque capture.
244                if let Some(path) = self
245                    .regioncx
246                    .constraint_path_between_regions(self.borrow_region, opaque_region_vid)
247                {
248                    for constraint in path {
249                        // If we find a call in this path, then check if it defines the opaque.
250                        if let ConstraintCategory::CallArgument(Some(call_ty)) = constraint.category
251                            && let ty::FnDef(call_def_id, _) = *call_ty.kind()
252                            // This function defines the opaque :D
253                            && call_def_id == parent
254                            && let Locations::Single(location) = constraint.locations
255                        {
256                            return ControlFlow::Break((def_id, idx, location));
257                        }
258                    }
259                }
260            }
261        }
262
263        ty.super_visit_with(self)
264    }
265}
266
267struct CheckExplicitRegionMentionAndCollectGenerics<'tcx> {
268    tcx: TyCtxt<'tcx>,
269    generics: &'tcx ty::Generics,
270    offending_region_idx: usize,
271    seen_opaques: FxIndexSet<DefId>,
272    seen_lifetimes: FxIndexSet<DefId>,
273}
274
275impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CheckExplicitRegionMentionAndCollectGenerics<'tcx> {
276    type Result = ControlFlow<(), ()>;
277
278    fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
279        match *ty.kind() {
280            ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
281                if self.seen_opaques.insert(def_id) {
282                    for (bound, _) in self
283                        .tcx
284                        .explicit_item_bounds(def_id)
285                        .iter_instantiated_copied(self.tcx, args)
286                        .map(Unnormalized::skip_norm_wip)
287                    {
288                        bound.visit_with(self)?;
289                    }
290                }
291                ControlFlow::Continue(())
292            }
293            _ => ty.super_visit_with(self),
294        }
295    }
296
297    fn visit_region(&mut self, r: ty::Region<'tcx>) -> Self::Result {
298        match r.kind() {
299            ty::ReEarlyParam(param) => {
300                if param.index as usize == self.offending_region_idx {
301                    ControlFlow::Break(())
302                } else {
303                    self.seen_lifetimes.insert(self.generics.region_param(param, self.tcx).def_id);
304                    ControlFlow::Continue(())
305                }
306            }
307            _ => ControlFlow::Continue(()),
308        }
309    }
310}