Skip to main content

rustc_hir_analysis/check/compare_impl_item/
refine.rs

1use itertools::Itertools as _;
2use rustc_data_structures::fx::FxIndexSet;
3use rustc_hir as hir;
4use rustc_hir::def_id::{DefId, LocalDefId};
5use rustc_infer::infer::TyCtxtInferExt;
6use rustc_lint_defs::builtin::{REFINING_IMPL_TRAIT_INTERNAL, REFINING_IMPL_TRAIT_REACHABLE};
7use rustc_middle::span_bug;
8use rustc_middle::traits::ObligationCause;
9use rustc_middle::ty::print::{with_no_trimmed_paths, with_types_for_signature};
10use rustc_middle::ty::{
11    self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable,
12    TypeVisitableExt, TypeVisitor, TypingMode,
13};
14use rustc_span::Span;
15use rustc_trait_selection::regions::InferCtxtRegionExt;
16use rustc_trait_selection::traits::{ObligationCtxt, elaborate, normalize_param_env_or_error};
17
18/// Check that an implementation does not refine an RPITIT from a trait method signature.
19pub(crate) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
20    tcx: TyCtxt<'tcx>,
21    impl_m: ty::AssocItem,
22    trait_m: ty::AssocItem,
23    impl_trait_ref: ty::TraitRef<'tcx>,
24) {
25    if !tcx.impl_method_has_trait_impl_trait_tys(impl_m.def_id) {
26        return;
27    }
28
29    // unreachable traits don't have any library guarantees, there's no need to do this check.
30    let is_internal = trait_m
31        .container_id(tcx)
32        .as_local()
33        .is_some_and(|trait_def_id| !tcx.effective_visibilities(()).is_reachable(trait_def_id))
34        // If a type in the trait ref is private, then there's also no reason to do this check.
35        || impl_trait_ref.args.iter().any(|arg| {
36            if let Some(ty) = arg.as_type()
37                && let Some(self_visibility) = type_visibility(tcx, ty)
38            {
39                return !self_visibility.is_public();
40            }
41            false
42        });
43
44    let impl_def_id = impl_m.container_id(tcx);
45    let impl_m_args = ty::GenericArgs::identity_for_item(tcx, impl_m.def_id);
46    let trait_m_to_impl_m_args = impl_m_args.rebase_onto(tcx, impl_def_id, impl_trait_ref.args);
47    let bound_trait_m_sig = tcx.fn_sig(trait_m.def_id).instantiate(tcx, trait_m_to_impl_m_args);
48    let trait_m_sig = tcx.liberate_late_bound_regions(impl_m.def_id, bound_trait_m_sig);
49    // replace the self type of the trait ref with `Self` so that diagnostics render better.
50    let trait_m_sig_with_self_for_diag = tcx.liberate_late_bound_regions(
51        impl_m.def_id,
52        tcx.fn_sig(trait_m.def_id).instantiate(
53            tcx,
54            tcx.mk_args_from_iter(
55                [tcx.types.self_param.into()]
56                    .into_iter()
57                    .chain(trait_m_to_impl_m_args.iter().skip(1)),
58            ),
59        ),
60    );
61
62    let Ok(hidden_tys) = tcx.collect_return_position_impl_trait_in_trait_tys(impl_m.def_id) else {
63        // Error already emitted, no need to delay another.
64        return;
65    };
66
67    if hidden_tys.items().any(|(_, &ty)| ty.skip_binder().references_error()) {
68        return;
69    }
70
71    let mut collector = ImplTraitInTraitCollector { tcx, types: FxIndexSet::default() };
72    trait_m_sig.visit_with(&mut collector);
73
74    // Bound that we find on RPITITs in the trait signature.
75    let mut trait_bounds = ::alloc::vec::Vec::new()vec![];
76    // Bounds that we find on the RPITITs in the impl signature.
77    let mut impl_bounds = ::alloc::vec::Vec::new()vec![];
78    // Pairs of trait and impl opaques.
79    let mut pairs = ::alloc::vec::Vec::new()vec![];
80
81    for trait_projection in collector.types.into_iter().rev() {
82        let impl_opaque_args = trait_projection.args.rebase_onto(tcx, trait_m.def_id, impl_m_args);
83        let hidden_ty =
84            hidden_tys[&trait_projection.kind.def_id()].instantiate(tcx, impl_opaque_args);
85
86        // If the hidden type is not an opaque, then we have "refined" the trait signature.
87        let ty::Alias(
88            impl_opaque @ ty::AliasTy { kind: ty::Opaque { def_id: impl_opaque_def_id }, .. },
89        ) = *hidden_ty.kind()
90        else {
91            report_mismatched_rpitit_signature(
92                tcx,
93                trait_m_sig_with_self_for_diag,
94                trait_m.def_id,
95                impl_m.def_id,
96                None,
97                is_internal,
98            );
99            return;
100        };
101
102        // This opaque also needs to be from the impl method -- otherwise,
103        // it's a refinement to a TAIT.
104        if !tcx.hir_get_if_local(impl_opaque_def_id).is_some_and(|node| {
105            #[allow(non_exhaustive_omitted_patterns)] match node.expect_opaque_ty().origin
    {
    hir::OpaqueTyOrigin::AsyncFn { parent, .. } |
        hir::OpaqueTyOrigin::FnReturn { parent, .. } if
        parent == impl_m.def_id.expect_local() => true,
    _ => false,
}matches!(
106                node.expect_opaque_ty().origin,
107                hir::OpaqueTyOrigin::AsyncFn { parent, .. }  | hir::OpaqueTyOrigin::FnReturn { parent, .. }
108                    if parent == impl_m.def_id.expect_local()
109            )
110        }) {
111            report_mismatched_rpitit_signature(
112                tcx,
113                trait_m_sig_with_self_for_diag,
114                trait_m.def_id,
115                impl_m.def_id,
116                None,
117                is_internal,
118            );
119            return;
120        }
121
122        trait_bounds.extend(
123            tcx.item_bounds(trait_projection.kind.def_id())
124                .iter_instantiated(tcx, trait_projection.args),
125        );
126        impl_bounds.extend(elaborate(
127            tcx,
128            tcx.explicit_item_bounds(impl_opaque_def_id)
129                .iter_instantiated_copied(tcx, impl_opaque.args),
130        ));
131
132        pairs.push((trait_projection, impl_opaque));
133    }
134
135    let hybrid_preds = tcx
136        .predicates_of(impl_def_id)
137        .instantiate_identity(tcx)
138        .into_iter()
139        .chain(tcx.predicates_of(trait_m.def_id).instantiate_own(tcx, trait_m_to_impl_m_args))
140        .map(|(clause, _)| clause);
141    let param_env = ty::ParamEnv::new(tcx.mk_clauses_from_iter(hybrid_preds));
142    let param_env = normalize_param_env_or_error(tcx, param_env, ObligationCause::dummy());
143
144    let ref infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
145    let ocx = ObligationCtxt::new(infcx);
146
147    // Normalize the bounds. This has two purposes:
148    //
149    // 1. Project the RPITIT projections from the trait to the opaques on the impl,
150    //    which means that they don't need to be mapped manually.
151    //
152    // 2. Deeply normalize any other projections that show up in the bound. That makes sure
153    //    that we don't consider `tests/ui/async-await/in-trait/async-associated-types.rs`
154    //    or `tests/ui/impl-trait/in-trait/refine-normalize.rs` to be refining.
155    let Ok((trait_bounds, impl_bounds)) =
156        ocx.deeply_normalize(&ObligationCause::dummy(), param_env, (trait_bounds, impl_bounds))
157    else {
158        tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (selection)");
159        return;
160    };
161
162    // Since we've normalized things, we need to resolve regions, since we'll
163    // possibly have introduced region vars during projection. We don't expect
164    // this resolution to have incurred any region errors -- but if we do, then
165    // just delay a bug.
166    let mut implied_wf_types = FxIndexSet::default();
167    implied_wf_types.extend(trait_m_sig.inputs_and_output);
168    implied_wf_types.extend(ocx.normalize(
169        &ObligationCause::dummy(),
170        param_env,
171        trait_m_sig.inputs_and_output,
172    ));
173    if !ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
174        tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (selection)");
175        return;
176    }
177    let errors = infcx.resolve_regions(impl_m.def_id.expect_local(), param_env, implied_wf_types);
178    if !errors.is_empty() {
179        tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (regions)");
180        return;
181    }
182    // Resolve any lifetime variables that may have been introduced during normalization.
183    let Ok((trait_bounds, impl_bounds)) = infcx.fully_resolve((trait_bounds, impl_bounds)) else {
184        // If resolution didn't fully complete, we cannot continue checking RPITIT refinement, and
185        // delay a bug as the original code contains load-bearing errors.
186        tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (resolution)");
187        return;
188    };
189
190    if trait_bounds.references_error() || impl_bounds.references_error() {
191        return;
192    }
193
194    // For quicker lookup, use an `IndexSet` (we don't use one earlier because
195    // it's not foldable..).
196    // Also, We have to anonymize binders in these types because they may contain
197    // `BrNamed` bound vars, which contain unique `DefId`s which correspond to syntax
198    // locations that we don't care about when checking bound equality.
199    let trait_bounds = FxIndexSet::from_iter(trait_bounds.fold_with(&mut Anonymize { tcx }));
200    let impl_bounds = impl_bounds.fold_with(&mut Anonymize { tcx });
201
202    // Find any clauses that are present in the impl's RPITITs that are not
203    // present in the trait's RPITITs. This will trigger on trivial predicates,
204    // too, since we *do not* use the trait solver to prove that the RPITIT's
205    // bounds are not stronger -- we're doing a simple, syntactic compatibility
206    // check between bounds. This is strictly forwards compatible, though.
207    for (clause, span) in impl_bounds {
208        if !trait_bounds.contains(&clause) {
209            report_mismatched_rpitit_signature(
210                tcx,
211                trait_m_sig_with_self_for_diag,
212                trait_m.def_id,
213                impl_m.def_id,
214                Some(span),
215                is_internal,
216            );
217            return;
218        }
219    }
220
221    // Make sure that the RPITIT doesn't capture fewer regions than
222    // the trait definition. We hard-error if it captures *more*, since that
223    // is literally unrepresentable in the type system; however, we may be
224    // promising stronger outlives guarantees if we capture *fewer* regions.
225    for (trait_projection, impl_opaque) in pairs {
226        let impl_variances = tcx.variances_of(impl_opaque.kind.def_id());
227        let impl_captures: FxIndexSet<_> = impl_opaque
228            .args
229            .iter()
230            .zip_eq(impl_variances)
231            .filter(|(_, v)| **v == ty::Invariant)
232            .map(|(arg, _)| arg)
233            .collect();
234
235        let trait_variances = tcx.variances_of(trait_projection.kind.def_id());
236        let mut trait_captures = FxIndexSet::default();
237        for (arg, variance) in trait_projection.args.iter().zip_eq(trait_variances) {
238            if *variance != ty::Invariant {
239                continue;
240            }
241            arg.visit_with(&mut CollectParams { params: &mut trait_captures });
242        }
243
244        if !trait_captures.iter().all(|arg| impl_captures.contains(arg)) {
245            report_mismatched_rpitit_captures(
246                tcx,
247                impl_opaque.kind.def_id().expect_local(),
248                trait_captures,
249                is_internal,
250            );
251        }
252    }
253}
254
255struct ImplTraitInTraitCollector<'tcx> {
256    tcx: TyCtxt<'tcx>,
257    types: FxIndexSet<ty::AliasTy<'tcx>>,
258}
259
260impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitCollector<'tcx> {
261    fn visit_ty(&mut self, ty: Ty<'tcx>) {
262        if let ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. }) = *ty.kind()
263            && self.tcx.is_impl_trait_in_trait(def_id)
264        {
265            if self.types.insert(proj) {
266                for (pred, _) in self
267                    .tcx
268                    .explicit_item_bounds(def_id)
269                    .iter_instantiated_copied(self.tcx, proj.args)
270                {
271                    pred.visit_with(self);
272                }
273            }
274        } else {
275            ty.super_visit_with(self);
276        }
277    }
278}
279
280fn report_mismatched_rpitit_signature<'tcx>(
281    tcx: TyCtxt<'tcx>,
282    trait_m_sig: ty::FnSig<'tcx>,
283    trait_m_def_id: DefId,
284    impl_m_def_id: DefId,
285    unmatched_bound: Option<Span>,
286    is_internal: bool,
287) {
288    let mapping = std::iter::zip(
289        tcx.fn_sig(trait_m_def_id).skip_binder().bound_vars(),
290        tcx.fn_sig(impl_m_def_id).skip_binder().bound_vars(),
291    )
292    .enumerate()
293    .filter_map(|(idx, (impl_bv, trait_bv))| {
294        if let ty::BoundVariableKind::Region(impl_bv) = impl_bv
295            && let ty::BoundVariableKind::Region(trait_bv) = trait_bv
296        {
297            let var = ty::BoundVar::from_usize(idx);
298            Some((
299                ty::LateParamRegionKind::from_bound(var, impl_bv),
300                ty::LateParamRegionKind::from_bound(var, trait_bv),
301            ))
302        } else {
303            None
304        }
305    })
306    .collect();
307
308    let mut return_ty = trait_m_sig.output().fold_with(&mut super::RemapLateParam { tcx, mapping });
309
310    if tcx.asyncness(impl_m_def_id).is_async() && tcx.asyncness(trait_m_def_id).is_async() {
311        let &ty::Alias(
312            future_ty @ ty::AliasTy { kind: ty::Projection { def_id: future_ty_def_id }, .. },
313        ) = return_ty.kind()
314        else {
315            ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(trait_m_def_id),
    format_args!("expected return type of async fn in trait to be a AFIT projection"));span_bug!(
316                tcx.def_span(trait_m_def_id),
317                "expected return type of async fn in trait to be a AFIT projection"
318            );
319        };
320        let Some(future_output_ty) = tcx
321            .explicit_item_bounds(future_ty_def_id)
322            .iter_instantiated_copied(tcx, future_ty.args)
323            .find_map(|(clause, _)| match clause.kind().no_bound_vars()? {
324                ty::ClauseKind::Projection(proj) => proj.term.as_type(),
325                _ => None,
326            })
327        else {
328            ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(trait_m_def_id),
    format_args!("expected `Future` projection bound in AFIT"));span_bug!(tcx.def_span(trait_m_def_id), "expected `Future` projection bound in AFIT");
329        };
330        return_ty = future_output_ty;
331    }
332
333    let (span, impl_return_span, pre, post) =
334        match tcx.hir_node_by_def_id(impl_m_def_id.expect_local()).fn_decl().unwrap().output {
335            hir::FnRetTy::DefaultReturn(span) => (tcx.def_span(impl_m_def_id), span, "-> ", " "),
336            hir::FnRetTy::Return(ty) => (ty.span, ty.span, "", ""),
337        };
338    let trait_return_span =
339        tcx.hir_get_if_local(trait_m_def_id).map(|node| match node.fn_decl().unwrap().output {
340            hir::FnRetTy::DefaultReturn(_) => tcx.def_span(trait_m_def_id),
341            hir::FnRetTy::Return(ty) => ty.span,
342        });
343
344    // Use ForSignature mode to ensure RPITITs are printed as `impl Trait` rather than
345    // `impl Trait { T::method(..) }` when RTN is enabled.
346    //
347    // We use `with_no_trimmed_paths!` to avoid triggering the `trimmed_def_paths` query,
348    // which requires diagnostic context (via `must_produce_diag`). Since we're formatting
349    // the type before creating the diagnostic, we need to avoid this query. This is the
350    // standard approach used elsewhere in the compiler for formatting types in suggestions
351    // (e.g., see `rustc_hir_typeck/src/demand.rs`).
352    let return_ty_suggestion =
353        {
    let _guard = NoTrimmedGuard::new();
    {
        let _guard =
            ::rustc_middle::ty::print::pretty::RtnModeHelper::with(RtnMode::ForSignature);
        ::alloc::__export::must_use({
                ::alloc::fmt::format(format_args!("{0}", return_ty))
            })
    }
}with_no_trimmed_paths!(with_types_for_signature!(format!("{return_ty}")));
354
355    let span = unmatched_bound.unwrap_or(span);
356    tcx.emit_node_span_lint(
357        if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
358        tcx.local_def_id_to_hir_id(impl_m_def_id.expect_local()),
359        span,
360        crate::errors::ReturnPositionImplTraitInTraitRefined {
361            impl_return_span,
362            trait_return_span,
363            pre,
364            post,
365            return_ty: return_ty_suggestion,
366            unmatched_bound,
367        },
368    );
369}
370
371fn type_visibility<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<ty::Visibility<DefId>> {
372    match *ty.kind() {
373        ty::Ref(_, ty, _) => type_visibility(tcx, ty),
374        ty::Adt(def, args) => {
375            if def.is_fundamental() {
376                type_visibility(tcx, args.type_at(0))
377            } else {
378                Some(tcx.visibility(def.did()))
379            }
380        }
381        _ => None,
382    }
383}
384
385struct Anonymize<'tcx> {
386    tcx: TyCtxt<'tcx>,
387}
388
389impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Anonymize<'tcx> {
390    fn cx(&self) -> TyCtxt<'tcx> {
391        self.tcx
392    }
393
394    fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
395    where
396        T: TypeFoldable<TyCtxt<'tcx>>,
397    {
398        self.tcx.anonymize_bound_vars(t)
399    }
400}
401
402struct CollectParams<'a, 'tcx> {
403    params: &'a mut FxIndexSet<ty::GenericArg<'tcx>>,
404}
405impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CollectParams<'_, 'tcx> {
406    fn visit_ty(&mut self, ty: Ty<'tcx>) {
407        if let ty::Param(_) = ty.kind() {
408            self.params.insert(ty.into());
409        } else {
410            ty.super_visit_with(self);
411        }
412    }
413    fn visit_region(&mut self, r: ty::Region<'tcx>) {
414        match r.kind() {
415            ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
416                self.params.insert(r.into());
417            }
418            _ => {}
419        }
420    }
421    fn visit_const(&mut self, ct: ty::Const<'tcx>) {
422        if let ty::ConstKind::Param(_) = ct.kind() {
423            self.params.insert(ct.into());
424        } else {
425            ct.super_visit_with(self);
426        }
427    }
428}
429
430fn report_mismatched_rpitit_captures<'tcx>(
431    tcx: TyCtxt<'tcx>,
432    impl_opaque_def_id: LocalDefId,
433    mut trait_captured_args: FxIndexSet<ty::GenericArg<'tcx>>,
434    is_internal: bool,
435) {
436    let Some(use_bound_span) =
437        tcx.hir_node_by_def_id(impl_opaque_def_id).expect_opaque_ty().bounds.iter().find_map(
438            |bound| match *bound {
439                rustc_hir::GenericBound::Use(_, span) => Some(span),
440                hir::GenericBound::Trait(_) | hir::GenericBound::Outlives(_) => None,
441            },
442        )
443    else {
444        // I have no idea when you would ever undercapture without a `use<..>`.
445        tcx.dcx().delayed_bug("expected use<..> to undercapture in an impl opaque");
446        return;
447    };
448
449    trait_captured_args
450        .sort_by_cached_key(|arg| !#[allow(non_exhaustive_omitted_patterns)] match arg.kind() {
    ty::GenericArgKind::Lifetime(_) => true,
    _ => false,
}matches!(arg.kind(), ty::GenericArgKind::Lifetime(_)));
451    let suggestion = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("use<{0}>",
                trait_captured_args.iter().join(", ")))
    })format!("use<{}>", trait_captured_args.iter().join(", "));
452
453    tcx.emit_node_span_lint(
454        if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
455        tcx.local_def_id_to_hir_id(impl_opaque_def_id),
456        use_bound_span,
457        crate::errors::ReturnPositionImplTraitInTraitRefinedLifetimes {
458            suggestion_span: use_bound_span,
459            suggestion,
460        },
461    );
462}