Skip to main content

rustc_trait_selection/error_reporting/infer/nice_region_error/
trait_impl_difference.rs

1//! Error Reporting for `impl` items that do not match the obligations from their `trait`.
2
3use rustc_errors::ErrorGuaranteed;
4use rustc_hir::def::{Namespace, Res};
5use rustc_hir::def_id::DefId;
6use rustc_hir::intravisit::{Visitor, walk_ty};
7use rustc_hir::{self as hir, AmbigArg};
8use rustc_infer::infer::SubregionOrigin;
9use rustc_middle::hir::nested_filter;
10use rustc_middle::traits::ObligationCauseCode;
11use rustc_middle::ty::error::ExpectedFound;
12use rustc_middle::ty::print::RegionHighlightMode;
13use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
14use rustc_span::{Ident, Span};
15use tracing::debug;
16
17use crate::error_reporting::infer::nice_region_error::NiceRegionError;
18use crate::error_reporting::infer::nice_region_error::placeholder_error::Highlighted;
19use crate::errors::{ConsiderBorrowingParamHelp, TraitImplDiff};
20use crate::infer::{RegionResolutionError, ValuePairs};
21
22impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
23    /// Print the error message for lifetime errors when the `impl` doesn't conform to the `trait`.
24    pub(super) fn try_report_impl_not_conforming_to_trait(&self) -> Option<ErrorGuaranteed> {
25        let error = self.error.as_ref()?;
26        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs:26",
                        "rustc_trait_selection::error_reporting::infer::nice_region_error::trait_impl_difference",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs"),
                        ::tracing_core::__macro_support::Option::Some(26u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::trait_impl_difference"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("try_report_impl_not_conforming_to_trait {0:?}",
                                                    error) as &dyn Value))])
            });
    } else { ; }
};debug!("try_report_impl_not_conforming_to_trait {:?}", error);
27        if let RegionResolutionError::SubSupConflict(
28            _,
29            var_origin,
30            sub_origin,
31            _sub,
32            sup_origin,
33            _sup,
34            _,
35        ) = error.clone()
36            && let (SubregionOrigin::Subtype(sup_trace), SubregionOrigin::Subtype(sub_trace)) =
37                (&sup_origin, &sub_origin)
38            && let &ObligationCauseCode::CompareImplItem { trait_item_def_id, .. } =
39                sub_trace.cause.code()
40            && sub_trace.values == sup_trace.values
41            && let ValuePairs::PolySigs(ExpectedFound { expected, found }) = sub_trace.values
42        {
43            // FIXME(compiler-errors): Don't like that this needs `Ty`s, but
44            // all of the region highlighting machinery only deals with those.
45            let guar = self.emit_err(var_origin.span(), expected, found, trait_item_def_id);
46            return Some(guar);
47        }
48        None
49    }
50
51    fn emit_err(
52        &self,
53        sp: Span,
54        expected: ty::PolyFnSig<'tcx>,
55        found: ty::PolyFnSig<'tcx>,
56        trait_item_def_id: DefId,
57    ) -> ErrorGuaranteed {
58        let trait_sp = self.tcx().def_span(trait_item_def_id);
59
60        // Mark all unnamed regions in the type with a number.
61        // This diagnostic is called in response to lifetime errors, so be informative.
62        struct HighlightBuilder<'tcx> {
63            tcx: TyCtxt<'tcx>,
64            highlight: RegionHighlightMode<'tcx>,
65            counter: usize,
66        }
67
68        impl<'tcx> HighlightBuilder<'tcx> {
69            fn build(tcx: TyCtxt<'tcx>, sig: ty::PolyFnSig<'tcx>) -> RegionHighlightMode<'tcx> {
70                let mut builder =
71                    HighlightBuilder { tcx, highlight: RegionHighlightMode::default(), counter: 1 };
72                sig.visit_with(&mut builder);
73                builder.highlight
74            }
75        }
76
77        impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for HighlightBuilder<'tcx> {
78            fn visit_region(&mut self, r: ty::Region<'tcx>) {
79                if !r.is_named(self.tcx) && self.counter <= 3 {
80                    self.highlight.highlighting_region(r, self.counter);
81                    self.counter += 1;
82                }
83            }
84        }
85
86        let tcx = self.cx.tcx;
87        let expected_highlight = HighlightBuilder::build(tcx, expected);
88        let expected = Highlighted {
89            highlight: expected_highlight,
90            ns: Namespace::TypeNS,
91            tcx,
92            value: expected,
93        }
94        .to_string();
95        let found_highlight = HighlightBuilder::build(tcx, found);
96        let found =
97            Highlighted { highlight: found_highlight, ns: Namespace::TypeNS, tcx, value: found }
98                .to_string();
99
100        // Get the span of all the used type parameters in the method.
101        let assoc_item = self.tcx().associated_item(trait_item_def_id);
102        let mut visitor =
103            TypeParamSpanVisitor { tcx: self.tcx(), types: ::alloc::vec::Vec::new()vec![], elided_lifetime_paths: ::alloc::vec::Vec::new()vec![] };
104        match assoc_item.kind {
105            ty::AssocKind::Fn { .. } => {
106                if let Some(hir_id) =
107                    assoc_item.def_id.as_local().map(|id| self.tcx().local_def_id_to_hir_id(id))
108                    && let Some(decl) = self.tcx().hir_fn_decl_by_hir_id(hir_id)
109                {
110                    visitor.visit_fn_decl(decl);
111                }
112            }
113            _ => {}
114        }
115
116        let diag = TraitImplDiff {
117            sp,
118            trait_sp,
119            note: (),
120            param_help: ConsiderBorrowingParamHelp { spans: visitor.types.to_vec() },
121            rel_help: visitor.types.is_empty(),
122            expected,
123            found,
124        };
125
126        let mut diag = self.tcx().dcx().create_err(diag);
127        // A limit not to make diag verbose.
128        const ELIDED_LIFETIME_NOTE_LIMIT: usize = 5;
129        let elided_lifetime_paths = visitor.elided_lifetime_paths;
130        let total_elided_lifetime_paths = elided_lifetime_paths.len();
131        let shown_elided_lifetime_paths = if tcx.sess.opts.verbose {
132            total_elided_lifetime_paths
133        } else {
134            ELIDED_LIFETIME_NOTE_LIMIT
135        };
136
137        for elided in elided_lifetime_paths.into_iter().take(shown_elided_lifetime_paths) {
138            diag.span_note(
139                elided.span,
140                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` here is elided as `{1}`",
                elided.ident, elided.shorthand))
    })format!("`{}` here is elided as `{}`", elided.ident, elided.shorthand),
141            );
142        }
143        if total_elided_lifetime_paths > shown_elided_lifetime_paths {
144            diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("and {0} more elided lifetime{1} in type paths",
                total_elided_lifetime_paths - shown_elided_lifetime_paths,
                if total_elided_lifetime_paths - shown_elided_lifetime_paths
                        == 1 {
                    ""
                } else { "s" }))
    })format!(
145                "and {} more elided lifetime{} in type paths",
146                total_elided_lifetime_paths - shown_elided_lifetime_paths,
147                if total_elided_lifetime_paths - shown_elided_lifetime_paths == 1 {
148                    ""
149                } else {
150                    "s"
151                },
152            ));
153        }
154        diag.emit()
155    }
156}
157
158#[derive(#[automatically_derived]
impl ::core::clone::Clone for ElidedLifetimeInPath {
    #[inline]
    fn clone(&self) -> ElidedLifetimeInPath {
        ElidedLifetimeInPath {
            span: ::core::clone::Clone::clone(&self.span),
            ident: ::core::clone::Clone::clone(&self.ident),
            shorthand: ::core::clone::Clone::clone(&self.shorthand),
        }
    }
}Clone)]
159struct ElidedLifetimeInPath {
160    span: Span,
161    ident: Ident,
162    shorthand: String,
163}
164
165struct TypeParamSpanVisitor<'tcx> {
166    tcx: TyCtxt<'tcx>,
167    types: Vec<Span>,
168    elided_lifetime_paths: Vec<ElidedLifetimeInPath>,
169}
170
171impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
172    type NestedFilter = nested_filter::OnlyBodies;
173
174    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
175        self.tcx
176    }
177
178    fn visit_qpath(&mut self, qpath: &'tcx hir::QPath<'tcx>, id: hir::HirId, _span: Span) {
179        fn record_elided_lifetimes(
180            tcx: TyCtxt<'_>,
181            elided_lifetime_paths: &mut Vec<ElidedLifetimeInPath>,
182            segment: &hir::PathSegment<'_>,
183        ) {
184            let Some(args) = segment.args else { return };
185            if args.parenthesized != hir::GenericArgsParentheses::No {
186                // Our diagnostic rendering below uses `<...>` syntax; skip cases like `Fn(..) -> ..`.
187                return;
188            }
189            let elided_count = args
190                .args
191                .iter()
192                .filter(|arg| {
193                    let hir::GenericArg::Lifetime(l) = arg else { return false };
194                    l.syntax == hir::LifetimeSyntax::Implicit
195                        && #[allow(non_exhaustive_omitted_patterns)] match l.source {
    hir::LifetimeSource::Path { .. } => true,
    _ => false,
}matches!(l.source, hir::LifetimeSource::Path { .. })
196                })
197                .count();
198            if elided_count == 0
199                || elided_lifetime_paths.iter().any(|p| p.span == segment.ident.span)
200            {
201                return;
202            }
203
204            let sm = tcx.sess.source_map();
205            let mut parts = args
206                .args
207                .iter()
208                .map(|arg| match arg {
209                    hir::GenericArg::Lifetime(l) => {
210                        if l.syntax == hir::LifetimeSyntax::Implicit
211                            && #[allow(non_exhaustive_omitted_patterns)] match l.source {
    hir::LifetimeSource::Path { .. } => true,
    _ => false,
}matches!(l.source, hir::LifetimeSource::Path { .. })
212                        {
213                            "'_".to_string()
214                        } else {
215                            sm.span_to_snippet(l.ident.span)
216                                .unwrap_or_else(|_| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\'{0}", l.ident.name))
    })format!("'{}", l.ident.name))
217                        }
218                    }
219                    hir::GenericArg::Type(ty) => {
220                        sm.span_to_snippet(ty.span).unwrap_or_else(|_| "..".to_string())
221                    }
222                    hir::GenericArg::Const(ct) => {
223                        sm.span_to_snippet(ct.span).unwrap_or_else(|_| "..".to_string())
224                    }
225                    hir::GenericArg::Infer(_) => "_".to_string(),
226                })
227                .collect::<Vec<_>>();
228            parts.extend(args.constraints.iter().map(|constraint| {
229                sm.span_to_snippet(constraint.span)
230                    .unwrap_or_else(|_| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} = ..", constraint.ident))
    })format!("{} = ..", constraint.ident))
231            }));
232            let shorthand = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}<{1}>", segment.ident,
                parts.join(", ")))
    })format!("{}<{}>", segment.ident, parts.join(", "));
233
234            elided_lifetime_paths.push(ElidedLifetimeInPath {
235                span: segment.ident.span,
236                ident: segment.ident,
237                shorthand,
238            });
239        }
240
241        match qpath {
242            hir::QPath::Resolved(_, path) => {
243                for segment in path.segments {
244                    record_elided_lifetimes(self.tcx, &mut self.elided_lifetime_paths, segment);
245                }
246            }
247            hir::QPath::TypeRelative(_, segment) => {
248                record_elided_lifetimes(self.tcx, &mut self.elided_lifetime_paths, segment);
249            }
250        }
251
252        hir::intravisit::walk_qpath(self, qpath, id);
253    }
254
255    fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx, AmbigArg>) {
256        match arg.kind {
257            hir::TyKind::Ref(_, ref mut_ty) => {
258                // We don't want to suggest looking into borrowing `&T` or `&Self`.
259                if let Some(ambig_ty) = mut_ty.ty.try_as_ambig_ty() {
260                    walk_ty(self, ambig_ty);
261                }
262                return;
263            }
264            hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
265                [segment]
266                    if #[allow(non_exhaustive_omitted_patterns)] match segment.res {
    Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } |
        Res::Def(hir::def::DefKind::TyParam, _) => true,
    _ => false,
}matches!(
267                        segment.res,
268                        Res::SelfTyParam { .. }
269                            | Res::SelfTyAlias { .. }
270                            | Res::Def(hir::def::DefKind::TyParam, _)
271                    ) =>
272                {
273                    self.types.push(path.span);
274                }
275                _ => {}
276            },
277            _ => {}
278        }
279        hir::intravisit::walk_ty(self, arg);
280    }
281}