Skip to main content

rustc_lint/
impl_trait_overcaptures.rs

1use std::cell::LazyCell;
2use std::debug_assert_matches;
3
4use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
5use rustc_data_structures::unord::UnordSet;
6use rustc_errors::{Diagnostic, Subdiagnostic, msg};
7use rustc_hir as hir;
8use rustc_hir::def::DefKind;
9use rustc_hir::def_id::{DefId, LocalDefId};
10use rustc_infer::infer::TyCtxtInferExt;
11use rustc_infer::infer::outlives::env::OutlivesEnvironment;
12use rustc_macros::Diagnostic;
13use rustc_middle::middle::resolve_bound_vars::ResolvedArg;
14use rustc_middle::ty::relate::{
15    Relate, RelateResult, TypeRelation, relate_args_with_variances, structurally_relate_consts,
16    structurally_relate_tys,
17};
18use rustc_middle::ty::{
19    self, RegionExt, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
20    Unnormalized,
21};
22use rustc_middle::{bug, span_bug};
23use rustc_session::lint::fcw;
24use rustc_session::{declare_lint, declare_lint_pass};
25use rustc_span::{Span, Symbol};
26use rustc_trait_selection::diagnostics::{
27    AddPreciseCapturingForOvercapture, impl_trait_overcapture_suggestion,
28};
29use rustc_trait_selection::regions::OutlivesEnvironmentBuildExt;
30use rustc_trait_selection::traits::ObligationCtxt;
31
32use crate::{LateContext, LateLintPass};
33
34#[doc =
r" The `impl_trait_overcaptures` lint warns against cases where lifetime"]
#[doc = r" capture behavior will differ in edition 2024."]
#[doc = r""]
#[doc =
r" In the 2024 edition, `impl Trait`s will capture all lifetimes in scope,"]
#[doc =
r" rather than just the lifetimes that are mentioned in the bounds of the type."]
#[doc =
r" Often these sets are equal, but if not, it means that the `impl Trait` may"]
#[doc = r" cause erroneous borrow-checker errors."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust,compile_fail,edition2021"]
#[doc = r" # #![deny(impl_trait_overcaptures)]"]
#[doc = r" # use std::fmt::Display;"]
#[doc = r" let mut x = vec![];"]
#[doc = r" x.push(1);"]
#[doc = r""]
#[doc = r" fn test(x: &Vec<i32>) -> impl Display {"]
#[doc = r"     x[0]"]
#[doc = r" }"]
#[doc = r""]
#[doc = r" let element = test(&x);"]
#[doc = r" x.push(2);"]
#[doc = r#" println!("{element}");"#]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" In edition < 2024, the returned `impl Display` doesn't capture the"]
#[doc =
r" lifetime from the `&Vec<i32>`, so the vector can be mutably borrowed"]
#[doc = r" while the `impl Display` is live."]
#[doc = r""]
#[doc =
r" To fix this, we can explicitly state that the `impl Display` doesn't"]
#[doc = r" capture any lifetimes, using `impl Display + use<>`."]
pub static IMPL_TRAIT_OVERCAPTURES: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "IMPL_TRAIT_OVERCAPTURES",
            default_level: ::rustc_lint_defs::Allow,
            desc: "`impl Trait` will capture more lifetimes than possibly intended in edition 2024",
            is_externally_loaded: false,
            future_incompatible: Some(::rustc_lint_defs::FutureIncompatibleInfo {
                    reason: ::rustc_lint_defs::FutureIncompatibilityReason::EditionSemanticsChange(::rustc_lint_defs::EditionFcw {
                            edition: rustc_span::edition::Edition::Edition2024,
                            page_slug: "rpit-lifetime-capture",
                        }),
                    ..::rustc_lint_defs::FutureIncompatibleInfo::default_fields_for_macro()
                }),
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
35    /// The `impl_trait_overcaptures` lint warns against cases where lifetime
36    /// capture behavior will differ in edition 2024.
37    ///
38    /// In the 2024 edition, `impl Trait`s will capture all lifetimes in scope,
39    /// rather than just the lifetimes that are mentioned in the bounds of the type.
40    /// Often these sets are equal, but if not, it means that the `impl Trait` may
41    /// cause erroneous borrow-checker errors.
42    ///
43    /// ### Example
44    ///
45    /// ```rust,compile_fail,edition2021
46    /// # #![deny(impl_trait_overcaptures)]
47    /// # use std::fmt::Display;
48    /// let mut x = vec![];
49    /// x.push(1);
50    ///
51    /// fn test(x: &Vec<i32>) -> impl Display {
52    ///     x[0]
53    /// }
54    ///
55    /// let element = test(&x);
56    /// x.push(2);
57    /// println!("{element}");
58    /// ```
59    ///
60    /// {{produces}}
61    ///
62    /// ### Explanation
63    ///
64    /// In edition < 2024, the returned `impl Display` doesn't capture the
65    /// lifetime from the `&Vec<i32>`, so the vector can be mutably borrowed
66    /// while the `impl Display` is live.
67    ///
68    /// To fix this, we can explicitly state that the `impl Display` doesn't
69    /// capture any lifetimes, using `impl Display + use<>`.
70    pub IMPL_TRAIT_OVERCAPTURES,
71    Allow,
72    "`impl Trait` will capture more lifetimes than possibly intended in edition 2024",
73    @future_incompatible = FutureIncompatibleInfo {
74        reason: fcw!(EditionSemanticsChange 2024 "rpit-lifetime-capture"),
75    };
76}
77
78#[doc =
r" The `impl_trait_redundant_captures` lint warns against cases where use of the"]
#[doc = r" precise capturing `use<...>` syntax is not needed."]
#[doc = r""]
#[doc =
r" In the 2024 edition, `impl Trait`s will capture all lifetimes in scope."]
#[doc =
r" If precise-capturing `use<...>` syntax is used, and the set of parameters"]
#[doc =
r" that are captures are *equal* to the set of parameters in scope, then"]
#[doc = r" the syntax is redundant, and can be removed."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust,edition2024,compile_fail"]
#[doc = r" # #![deny(impl_trait_redundant_captures)]"]
#[doc = r" fn test<'a>(x: &'a i32) -> impl Sized + use<'a> { x }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" To fix this, remove the `use<'a>`, since the lifetime is already captured"]
#[doc = r" since it is in scope."]
pub static IMPL_TRAIT_REDUNDANT_CAPTURES: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "IMPL_TRAIT_REDUNDANT_CAPTURES",
            default_level: ::rustc_lint_defs::Allow,
            desc: "redundant precise-capturing `use<...>` syntax on an `impl Trait`",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
79    /// The `impl_trait_redundant_captures` lint warns against cases where use of the
80    /// precise capturing `use<...>` syntax is not needed.
81    ///
82    /// In the 2024 edition, `impl Trait`s will capture all lifetimes in scope.
83    /// If precise-capturing `use<...>` syntax is used, and the set of parameters
84    /// that are captures are *equal* to the set of parameters in scope, then
85    /// the syntax is redundant, and can be removed.
86    ///
87    /// ### Example
88    ///
89    /// ```rust,edition2024,compile_fail
90    /// # #![deny(impl_trait_redundant_captures)]
91    /// fn test<'a>(x: &'a i32) -> impl Sized + use<'a> { x }
92    /// ```
93    ///
94    /// {{produces}}
95    ///
96    /// ### Explanation
97    ///
98    /// To fix this, remove the `use<'a>`, since the lifetime is already captured
99    /// since it is in scope.
100    pub IMPL_TRAIT_REDUNDANT_CAPTURES,
101    Allow,
102    "redundant precise-capturing `use<...>` syntax on an `impl Trait`",
103}
104
105#[doc =
r" Lint for opaque types that will begin capturing in-scope but unmentioned lifetimes"]
#[doc = r" in edition 2024."]
pub struct ImplTraitOvercaptures;
#[automatically_derived]
impl ::core::marker::Copy for ImplTraitOvercaptures { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for ImplTraitOvercaptures { }
#[automatically_derived]
impl ::core::clone::Clone for ImplTraitOvercaptures {
    #[inline]
    fn clone(&self) -> ImplTraitOvercaptures { *self }
}
impl ::rustc_lint_defs::LintPass for ImplTraitOvercaptures {
    fn name(&self) -> &'static str { "ImplTraitOvercaptures" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [IMPL_TRAIT_OVERCAPTURES, IMPL_TRAIT_REDUNDANT_CAPTURES]))
    }
}
impl ImplTraitOvercaptures {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [IMPL_TRAIT_OVERCAPTURES, IMPL_TRAIT_REDUNDANT_CAPTURES]))
    }
}declare_lint_pass!(
106    /// Lint for opaque types that will begin capturing in-scope but unmentioned lifetimes
107    /// in edition 2024.
108    ImplTraitOvercaptures => [IMPL_TRAIT_OVERCAPTURES, IMPL_TRAIT_REDUNDANT_CAPTURES]
109);
110
111impl<'tcx> LateLintPass<'tcx> for ImplTraitOvercaptures {
112    fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'tcx>) {
113        match &it.kind {
114            hir::ItemKind::Fn { .. } => check_fn(cx.tcx, it.owner_id.def_id),
115            _ => {}
116        }
117    }
118
119    fn check_impl_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::ImplItem<'tcx>) {
120        match &it.kind {
121            hir::ImplItemKind::Fn(_, _) => check_fn(cx.tcx, it.owner_id.def_id),
122            _ => {}
123        }
124    }
125
126    fn check_trait_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::TraitItem<'tcx>) {
127        match &it.kind {
128            hir::TraitItemKind::Fn(_, _) => check_fn(cx.tcx, it.owner_id.def_id),
129            _ => {}
130        }
131    }
132}
133
134#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for ParamKind {
    #[inline]
    fn eq(&self, other: &ParamKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (ParamKind::Early(__self_0, __self_1),
                    ParamKind::Early(__arg1_0, __arg1_1)) =>
                    __self_1 == __arg1_1 && __self_0 == __arg1_0,
                (ParamKind::Free(__self_0), ParamKind::Free(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ParamKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
        let _: ::core::cmp::AssertParamIsEq<u32>;
        let _: ::core::cmp::AssertParamIsEq<DefId>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ParamKind {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            ParamKind::Early(__self_0, __self_1) => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            ParamKind::Free(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            _ => {}
        }
    }
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for ParamKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ParamKind::Early(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f, "Early",
                    __self_0, &__self_1),
            ParamKind::Free(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Free",
                    &__self_0),
            ParamKind::Late => ::core::fmt::Formatter::write_str(f, "Late"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for ParamKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ParamKind {
    #[inline]
    fn clone(&self) -> ParamKind {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _: ::core::clone::AssertParamIsClone<u32>;
        let _: ::core::clone::AssertParamIsClone<DefId>;
        *self
    }
}Clone)]
135enum ParamKind {
136    // Early-bound var.
137    Early(Symbol, u32),
138    // Late-bound var on function, not within a binder. We can capture these.
139    Free(DefId),
140    // Late-bound var in a binder. We can't capture these yet.
141    Late,
142}
143
144fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
145    let sig = tcx.fn_sig(parent_def_id).instantiate_identity().skip_norm_wip();
146
147    let mut in_scope_parameters = FxIndexMap::default();
148    // Populate the in_scope_parameters list first with all of the generics in scope
149    let mut current_def_id = Some(parent_def_id.to_def_id());
150    while let Some(def_id) = current_def_id {
151        let generics = tcx.generics_of(def_id);
152        for param in &generics.own_params {
153            in_scope_parameters.insert(param.def_id, ParamKind::Early(param.name, param.index));
154        }
155        current_def_id = generics.parent;
156    }
157
158    for bound_var in sig.bound_vars() {
159        let ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(def_id)) = bound_var else {
160            ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(parent_def_id),
    format_args!("unexpected non-lifetime binder on fn sig"));span_bug!(tcx.def_span(parent_def_id), "unexpected non-lifetime binder on fn sig");
161        };
162
163        in_scope_parameters.insert(def_id, ParamKind::Free(def_id));
164    }
165
166    let sig = tcx.liberate_late_bound_regions(parent_def_id.to_def_id(), sig);
167
168    // Then visit the signature to walk through all the binders (incl. the late-bound
169    // vars on the function itself, which we need to count too).
170    sig.visit_with(&mut VisitOpaqueTypes {
171        tcx,
172        parent_def_id,
173        in_scope_parameters,
174        seen: Default::default(),
175        // Lazily compute these two, since they're likely a bit expensive.
176        variances: LazyCell::new(|| {
177            let mut functional_variances = FunctionalVariances {
178                tcx,
179                variances: FxHashMap::default(),
180                ambient_variance: ty::Covariant,
181                generics: tcx.generics_of(parent_def_id),
182            };
183            functional_variances.relate(sig, sig).unwrap();
184            functional_variances.variances
185        }),
186        outlives_env: LazyCell::new(|| {
187            let typing_env = ty::TypingEnv::non_body_analysis(tcx, parent_def_id);
188            let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
189            let ocx = ObligationCtxt::new(&infcx);
190            let assumed_wf_tys = ocx.assumed_wf_types(param_env, parent_def_id).unwrap_or_default();
191            OutlivesEnvironment::new(&infcx, parent_def_id, param_env, assumed_wf_tys)
192        }),
193    });
194}
195
196struct VisitOpaqueTypes<'tcx, VarFn, OutlivesFn> {
197    tcx: TyCtxt<'tcx>,
198    parent_def_id: LocalDefId,
199    in_scope_parameters: FxIndexMap<DefId, ParamKind>,
200    variances: LazyCell<FxHashMap<DefId, ty::Variance>, VarFn>,
201    outlives_env: LazyCell<OutlivesEnvironment<'tcx>, OutlivesFn>,
202    seen: FxIndexSet<LocalDefId>,
203}
204
205impl<'tcx, VarFn, OutlivesFn> TypeVisitor<TyCtxt<'tcx>>
206    for VisitOpaqueTypes<'tcx, VarFn, OutlivesFn>
207where
208    VarFn: FnOnce() -> FxHashMap<DefId, ty::Variance>,
209    OutlivesFn: FnOnce() -> OutlivesEnvironment<'tcx>,
210{
211    fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(&mut self, t: &ty::Binder<'tcx, T>) {
212        // When we get into a binder, we need to add its own bound vars to the scope.
213        let mut added = ::alloc::vec::Vec::new()vec![];
214        for arg in t.bound_vars() {
215            let arg: ty::BoundVariableKind<'tcx> = arg;
216            match arg {
217                ty::BoundVariableKind::Region(ty::BoundRegionKind::Named(def_id))
218                | ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id)) => {
219                    // Return type notation introduces a binder containing the referenced
220                    // function's own bound parameters. For self-referential RTN, these may
221                    // already be present as `Free` entries from the enclosing signature.
222                    // Temporarily shadow them as `Late` and restore them when leaving.
223                    let previous = self.in_scope_parameters.insert(def_id, ParamKind::Late);
224                    added.push((def_id, previous));
225                }
226                _ => {
227                    self.tcx.dcx().span_delayed_bug(
228                        self.tcx.def_span(self.parent_def_id),
229                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unsupported bound variable kind: {0:?}",
                arg))
    })format!("unsupported bound variable kind: {arg:?}"),
230                    );
231                }
232            }
233        }
234
235        t.super_visit_with(self);
236
237        // Restore the previous scope entries, removing newly added parameters.
238        for (arg, previous) in added.into_iter().rev() {
239            if let Some(previous) = previous {
240                self.in_scope_parameters.insert(arg, previous);
241            } else {
242                self.in_scope_parameters.shift_remove(&arg);
243            }
244        }
245    }
246
247    fn visit_ty(&mut self, t: Ty<'tcx>) {
248        if !t.has_aliases() {
249            return;
250        }
251
252        if let ty::Alias(_, ty::AliasTy { kind: ty::Projection { def_id }, args, .. }) = *t.kind()
253            && self.tcx.is_impl_trait_in_trait(def_id)
254        {
255            // visit the opaque of the RPITIT
256            self.tcx.type_of(def_id).instantiate(self.tcx, args).skip_norm_wip().visit_with(self)
257        } else if let ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args: opaque_ty_args, .. }) = *t.kind()
258            && let Some(opaque_def_id) = def_id.as_local()
259            // Don't recurse infinitely on an opaque
260            && self.seen.insert(opaque_def_id)
261            // If it's owned by this function
262            && let opaque =
263                self.tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty()
264            // We want to recurse into RPITs and async fns, even though the latter
265            // doesn't overcapture on its own, it may mention additional RPITs
266            // in its bounds.
267            && let hir::OpaqueTyOrigin::FnReturn { parent, .. }
268                | hir::OpaqueTyOrigin::AsyncFn { parent, .. } = opaque.origin
269            && parent == self.parent_def_id
270        {
271            let opaque_span = self.tcx.def_span(opaque_def_id);
272            let new_capture_rules = opaque_span.at_least_rust_2024();
273            if !new_capture_rules
274                && !opaque.bounds.iter().any(|bound| #[allow(non_exhaustive_omitted_patterns)] match bound {
    hir::GenericBound::Use(..) => true,
    _ => false,
}matches!(bound, hir::GenericBound::Use(..)))
275            {
276                // Compute the set of args that are captured by the opaque...
277                let mut captured = FxIndexSet::default();
278                let mut captured_regions = FxIndexSet::default();
279                let variances = self.tcx.variances_of(opaque_def_id);
280                let mut current_def_id = Some(opaque_def_id.to_def_id());
281                while let Some(def_id) = current_def_id {
282                    let generics = self.tcx.generics_of(def_id);
283                    for param in &generics.own_params {
284                        // A param is captured if it's invariant.
285                        if variances[param.index as usize] != ty::Invariant {
286                            continue;
287                        }
288
289                        let arg = opaque_ty_args[param.index as usize];
290                        // We need to turn all `ty::Param`/`ConstKind::Param` and
291                        // `ReEarlyParam`/`ReBound` into def ids.
292                        captured.insert(extract_def_id_from_arg(self.tcx, generics, arg));
293
294                        captured_regions.extend(arg.as_region());
295                    }
296                    current_def_id = generics.parent;
297                }
298
299                // Compute the set of in scope params that are not captured.
300                let mut uncaptured_args: FxIndexSet<_> = self
301                    .in_scope_parameters
302                    .iter()
303                    .filter(|&(def_id, _)| !captured.contains(def_id))
304                    .collect();
305                // Remove the set of lifetimes that are in-scope that outlive some other captured
306                // lifetime and are contravariant (i.e. covariant in argument position).
307                uncaptured_args.retain(|&(def_id, kind)| {
308                    let Some(ty::Bivariant | ty::Contravariant) = self.variances.get(def_id) else {
309                        // Keep all covariant/invariant args. Also if variance is `None`,
310                        // then that means it's either not a lifetime, or it didn't show up
311                        // anywhere in the signature.
312                        return true;
313                    };
314                    // We only computed variance of lifetimes...
315                    if true {
    {
        match self.tcx.def_kind(*def_id) {
            DefKind::LifetimeParam => {}
            ref left_val => {
                ::core::panicking::assert_matches_failed(left_val,
                    "DefKind::LifetimeParam", ::core::option::Option::None);
            }
        }
    };
};debug_assert_matches!(self.tcx.def_kind(*def_id), DefKind::LifetimeParam);
316                    let uncaptured = match *kind {
317                        ParamKind::Early(name, index) => ty::Region::new_early_param(
318                            self.tcx,
319                            ty::EarlyParamRegion { name, index },
320                        ),
321                        ParamKind::Free(def_id) => ty::Region::new_late_param(
322                            self.tcx,
323                            self.parent_def_id.to_def_id(),
324                            ty::LateParamRegionKind::Named(def_id),
325                        ),
326                        // Totally ignore late bound args from binders.
327                        ParamKind::Late => return true,
328                    };
329                    // Does this region outlive any captured region?
330                    !captured_regions.iter().any(|r| {
331                        self.outlives_env
332                            .free_region_map()
333                            .sub_free_regions(self.tcx, *r, uncaptured)
334                    })
335                });
336
337                // If we have uncaptured args, and if the opaque doesn't already have
338                // `use<>` syntax on it, and we're < edition 2024, then warn the user.
339                if !uncaptured_args.is_empty() {
340                    let suggestion = impl_trait_overcapture_suggestion(
341                        self.tcx,
342                        opaque_def_id,
343                        self.parent_def_id,
344                        captured,
345                    );
346
347                    let uncaptured_spans: Vec<_> = uncaptured_args
348                        .into_iter()
349                        .map(|(&def_id, _)| self.tcx.def_span(def_id))
350                        .collect();
351
352                    self.tcx.emit_node_span_lint(
353                        IMPL_TRAIT_OVERCAPTURES,
354                        self.tcx.local_def_id_to_hir_id(opaque_def_id),
355                        opaque_span,
356                        ImplTraitOvercapturesLint {
357                            self_ty: t,
358                            num_captured: uncaptured_spans.len(),
359                            uncaptured_spans,
360                            suggestion,
361                        },
362                    );
363                }
364            }
365
366            // Otherwise, if we are edition 2024, have `use<>` syntax, and
367            // have no uncaptured args, then we should warn to the user that
368            // it's redundant to capture all args explicitly.
369            if new_capture_rules
370                && let Some((use_idx, captured_args, capturing_span)) =
371                    opaque.bounds.iter().enumerate().find_map(|(i, bound)| match *bound {
372                        hir::GenericBound::Use(a, s) => Some((i, a, s)),
373                        _ => None,
374                    })
375            {
376                let mut explicitly_captured = UnordSet::default();
377                for arg in captured_args {
378                    match self.tcx.named_bound_var(arg.hir_id()) {
379                        Some(
380                            ResolvedArg::EarlyBound(def_id) | ResolvedArg::LateBound(_, _, def_id),
381                        ) => {
382                            if self.tcx.def_kind(self.tcx.local_parent(def_id)) == DefKind::OpaqueTy
383                            {
384                                let def_id = self
385                                    .tcx
386                                    .map_opaque_lifetime_to_parent_lifetime(def_id)
387                                    .opt_param_def_id(self.tcx, self.parent_def_id.to_def_id())
388                                    .expect("variable should have been duplicated from parent");
389
390                                explicitly_captured.insert(def_id);
391                            } else {
392                                explicitly_captured.insert(def_id.to_def_id());
393                            }
394                        }
395                        _ => {
396                            self.tcx.dcx().span_delayed_bug(
397                                self.tcx.hir_span(arg.hir_id()),
398                                "no valid for captured arg",
399                            );
400                        }
401                    }
402                }
403
404                if self
405                    .in_scope_parameters
406                    .iter()
407                    .all(|(def_id, _)| explicitly_captured.contains(def_id))
408                {
409                    // Extend the removal span to include the `+` joiner adjacent
410                    // to `use<...>`, so applying the suggestion does not leave
411                    // behind a stray `+` that fails to parse.
412                    let suggestion_span = if let Some(next) = opaque.bounds.get(use_idx + 1) {
413                        capturing_span.with_hi(next.span().lo())
414                    } else if let Some(prev_idx) = use_idx.checked_sub(1) {
415                        let prev = opaque.bounds[prev_idx];
416                        capturing_span.with_lo(prev.span().hi())
417                    } else {
418                        // `impl use<...>` with no other bound is not valid
419                        // syntax, so this branch is unreachable in practice.
420                        capturing_span
421                    };
422
423                    self.tcx.emit_node_span_lint(
424                        IMPL_TRAIT_REDUNDANT_CAPTURES,
425                        self.tcx.local_def_id_to_hir_id(opaque_def_id),
426                        opaque_span,
427                        ImplTraitRedundantCapturesLint { capturing_span: suggestion_span },
428                    );
429                }
430            }
431
432            // Walk into the bounds of the opaque, too, since we want to get nested opaques
433            // in this lint as well. Interestingly, one place that I expect this lint to fire
434            // is for `impl for<'a> Bound<Out = impl Other>`, since `impl Other` will begin
435            // to capture `'a` in e2024 (even though late-bound vars in opaques are not allowed).
436            for clause in self
437                .tcx
438                .item_bounds(def_id)
439                .iter_instantiated(self.tcx, opaque_ty_args)
440                .map(Unnormalized::skip_norm_wip)
441            {
442                clause.visit_with(self)
443            }
444        }
445
446        t.super_visit_with(self);
447    }
448}
449
450struct ImplTraitOvercapturesLint<'tcx> {
451    uncaptured_spans: Vec<Span>,
452    self_ty: Ty<'tcx>,
453    num_captured: usize,
454    suggestion: Option<AddPreciseCapturingForOvercapture>,
455}
456
457impl<'a> Diagnostic<'a, ()> for ImplTraitOvercapturesLint<'_> {
458    fn into_diag(
459        self,
460        dcx: rustc_errors::DiagCtxtHandle<'a>,
461        level: rustc_errors::Level,
462    ) -> rustc_errors::Diag<'a, ()> {
463        let mut diag = rustc_errors::Diag::new(
464            dcx,
465            level,
466            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`{$self_ty}` will capture more lifetimes than possibly intended in edition 2024"))msg!("`{$self_ty}` will capture more lifetimes than possibly intended in edition 2024"),
467        );
468        diag.arg("self_ty", self.self_ty.to_string())
469            .arg("num_captured", self.num_captured)
470            .span_note(
471                self.uncaptured_spans,
472                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("specifically, {$num_captured ->\n                        [one] this lifetime is\n                        *[other] these lifetimes are\n                    } in scope but not mentioned in the type's bounds"))msg!(
473                    "specifically, {$num_captured ->
474                        [one] this lifetime is
475                        *[other] these lifetimes are
476                    } in scope but not mentioned in the type's bounds"
477                ),
478            )
479            .note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("all lifetimes in scope will be captured by `impl Trait`s in edition 2024"))msg!("all lifetimes in scope will be captured by `impl Trait`s in edition 2024"));
480        if let Some(suggestion) = self.suggestion {
481            suggestion.add_to_diag(&mut diag);
482        }
483        diag
484    }
485}
486
487#[derive(const _: () =
    {
        impl<'_sess, G> rustc_errors::Diagnostic<'_sess, G> for
            ImplTraitRedundantCapturesLint where
            G: rustc_errors::EmissionGuarantee {
            #[track_caller]
            fn into_diag(self, dcx: rustc_errors::DiagCtxtHandle<'_sess>,
                level: rustc_errors::Level) -> rustc_errors::Diag<'_sess, G> {
                match self {
                    ImplTraitRedundantCapturesLint { capturing_span: __binding_0
                        } => {
                        let mut diag =
                            rustc_errors::Diag::new(dcx, level,
                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("all possible in-scope parameters are already captured, so `use<...>` syntax is redundant")));
                        let __code_3 =
                            [::alloc::__export::must_use({
                                                ::alloc::fmt::format(format_args!(""))
                                            })].into_iter();
                        ;
                        diag.span_suggestions_with_style(__binding_0,
                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("remove the `use<...>` syntax")),
                            __code_3, rustc_errors::Applicability::MachineApplicable,
                            rustc_errors::SuggestionStyle::ShowCode);
                        diag
                    }
                }
            }
        }
    };Diagnostic)]
488#[diag("all possible in-scope parameters are already captured, so `use<...>` syntax is redundant")]
489struct ImplTraitRedundantCapturesLint {
490    #[suggestion("remove the `use<...>` syntax", code = "", applicability = "machine-applicable")]
491    capturing_span: Span,
492}
493
494fn extract_def_id_from_arg<'tcx>(
495    tcx: TyCtxt<'tcx>,
496    generics: &'tcx ty::Generics,
497    arg: ty::GenericArg<'tcx>,
498) -> DefId {
499    match arg.kind() {
500        ty::GenericArgKind::Lifetime(re) => match re.kind() {
501            ty::ReEarlyParam(ebr) => generics.region_param(ebr, tcx).def_id,
502            ty::ReBound(_, ty::BoundRegion { kind: ty::BoundRegionKind::Named(def_id), .. })
503            | ty::ReLateParam(ty::LateParamRegion {
504                scope: _,
505                kind: ty::LateParamRegionKind::Named(def_id),
506            }) => def_id,
507            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
508        },
509        ty::GenericArgKind::Type(ty) => {
510            let ty::Param(param_ty) = *ty.kind() else {
511                ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
512            };
513            generics.type_param(param_ty, tcx).def_id
514        }
515        ty::GenericArgKind::Const(ct) => {
516            let ty::ConstKind::Param(param_ct) = ct.kind() else {
517                ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
518            };
519            generics.const_param(param_ct, tcx).def_id
520        }
521    }
522}
523
524/// Computes the variances of regions that appear in the type, but considering
525/// late-bound regions too, which don't have their variance computed usually.
526///
527/// Like generalization, this is a unary operation implemented on top of the binary
528/// relation infrastructure, mostly because it's much easier to have the relation
529/// track the variance for you, rather than having to do it yourself.
530struct FunctionalVariances<'tcx> {
531    tcx: TyCtxt<'tcx>,
532    variances: FxHashMap<DefId, ty::Variance>,
533    ambient_variance: ty::Variance,
534    generics: &'tcx ty::Generics,
535}
536
537impl<'tcx> TypeRelation<TyCtxt<'tcx>> for FunctionalVariances<'tcx> {
538    fn cx(&self) -> TyCtxt<'tcx> {
539        self.tcx
540    }
541
542    fn relate_ty_args(
543        &mut self,
544        a_ty: Ty<'tcx>,
545        _: Ty<'tcx>,
546        def_id: DefId,
547        a_args: ty::GenericArgsRef<'tcx>,
548        b_args: ty::GenericArgsRef<'tcx>,
549        _: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
550    ) -> RelateResult<'tcx, Ty<'tcx>> {
551        let variances = self.cx().variances_of(def_id);
552        relate_args_with_variances(self, variances, a_args, b_args)?;
553        Ok(a_ty)
554    }
555
556    fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
557        &mut self,
558        variance: ty::Variance,
559        _: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
560        a: T,
561        b: T,
562    ) -> RelateResult<'tcx, T> {
563        let old_variance = self.ambient_variance;
564        self.ambient_variance = self.ambient_variance.xform(variance);
565        self.relate(a, b).unwrap();
566        self.ambient_variance = old_variance;
567        Ok(a)
568    }
569
570    fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
571        structurally_relate_tys(self, a, b).unwrap();
572        Ok(a)
573    }
574
575    fn regions(
576        &mut self,
577        a: ty::Region<'tcx>,
578        _: ty::Region<'tcx>,
579    ) -> RelateResult<'tcx, ty::Region<'tcx>> {
580        let def_id = match a.kind() {
581            ty::ReEarlyParam(ebr) => self.generics.region_param(ebr, self.tcx).def_id,
582            ty::ReBound(_, ty::BoundRegion { kind: ty::BoundRegionKind::Named(def_id), .. })
583            | ty::ReLateParam(ty::LateParamRegion {
584                scope: _,
585                kind: ty::LateParamRegionKind::Named(def_id),
586            }) => def_id,
587            _ => {
588                return Ok(a);
589            }
590        };
591
592        if let Some(variance) = self.variances.get_mut(&def_id) {
593            *variance = unify(*variance, self.ambient_variance);
594        } else {
595            self.variances.insert(def_id, self.ambient_variance);
596        }
597
598        Ok(a)
599    }
600
601    fn consts(
602        &mut self,
603        a: ty::Const<'tcx>,
604        b: ty::Const<'tcx>,
605    ) -> RelateResult<'tcx, ty::Const<'tcx>> {
606        structurally_relate_consts(self, a, b).unwrap();
607        Ok(a)
608    }
609
610    fn binders<T>(
611        &mut self,
612        a: ty::Binder<'tcx, T>,
613        b: ty::Binder<'tcx, T>,
614    ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
615    where
616        T: Relate<TyCtxt<'tcx>>,
617    {
618        self.relate(a.skip_binder(), b.skip_binder()).unwrap();
619        Ok(a)
620    }
621}
622
623/// What is the variance that satisfies the two variances?
624fn unify(a: ty::Variance, b: ty::Variance) -> ty::Variance {
625    match (a, b) {
626        // Bivariance is lattice bottom.
627        (ty::Bivariant, other) | (other, ty::Bivariant) => other,
628        // Invariant is lattice top.
629        (ty::Invariant, _) | (_, ty::Invariant) => ty::Invariant,
630        // If type is required to be covariant and contravariant, then it's invariant.
631        (ty::Contravariant, ty::Covariant) | (ty::Covariant, ty::Contravariant) => ty::Invariant,
632        // Otherwise, co + co = co, contra + contra = contra.
633        (ty::Contravariant, ty::Contravariant) => ty::Contravariant,
634        (ty::Covariant, ty::Covariant) => ty::Covariant,
635    }
636}