rustc_trait_selection/error_reporting/infer/nice_region_error/
placeholder_relation.rs

1use rustc_data_structures::intern::Interned;
2use rustc_errors::Diag;
3use rustc_middle::bug;
4use rustc_middle::ty::{self, RePlaceholder, Region};
5
6use crate::error_reporting::infer::nice_region_error::NiceRegionError;
7use crate::errors::PlaceholderRelationLfNotSatisfied;
8use crate::infer::{RegionResolutionError, SubregionOrigin};
9
10impl<'tcx> NiceRegionError<'_, 'tcx> {
11    /// Emitted wwhen given a `ConcreteFailure` when relating two placeholders.
12    pub(super) fn try_report_placeholder_relation(&self) -> Option<Diag<'tcx>> {
13        match &self.error {
14            Some(RegionResolutionError::ConcreteFailure(
15                SubregionOrigin::RelateRegionParamBound(span, _),
16                Region(Interned(
17                    RePlaceholder(ty::Placeholder {
18                        bound: ty::BoundRegion { kind: sub_name, .. },
19                        ..
20                    }),
21                    _,
22                )),
23                Region(Interned(
24                    RePlaceholder(ty::Placeholder {
25                        bound: ty::BoundRegion { kind: sup_name, .. },
26                        ..
27                    }),
28                    _,
29                )),
30            )) => {
31                let span = *span;
32                let (sub_span, sub_symbol) = match *sub_name {
33                    ty::BoundRegionKind::Named(def_id) => {
34                        (Some(self.tcx().def_span(def_id)), Some(self.tcx().item_name(def_id)))
35                    }
36                    ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => (None, None),
37                    ty::BoundRegionKind::NamedAnon(_) => bug!("only used for pretty printing"),
38                };
39                let (sup_span, sup_symbol) = match *sup_name {
40                    ty::BoundRegionKind::Named(def_id) => {
41                        (Some(self.tcx().def_span(def_id)), Some(self.tcx().item_name(def_id)))
42                    }
43                    ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => (None, None),
44                    ty::BoundRegionKind::NamedAnon(_) => bug!("only used for pretty printing"),
45                };
46                let diag = match (sub_span, sup_span, sub_symbol, sup_symbol) {
47                    (Some(sub_span), Some(sup_span), Some(sub_symbol), Some(sup_symbol)) => {
48                        PlaceholderRelationLfNotSatisfied::HasBoth {
49                            span,
50                            sub_span,
51                            sup_span,
52                            sub_symbol,
53                            sup_symbol,
54                            note: (),
55                        }
56                    }
57                    (Some(sub_span), Some(sup_span), _, Some(sup_symbol)) => {
58                        PlaceholderRelationLfNotSatisfied::HasSup {
59                            span,
60                            sub_span,
61                            sup_span,
62                            sup_symbol,
63                            note: (),
64                        }
65                    }
66                    (Some(sub_span), Some(sup_span), Some(sub_symbol), _) => {
67                        PlaceholderRelationLfNotSatisfied::HasSub {
68                            span,
69                            sub_span,
70                            sup_span,
71                            sub_symbol,
72                            note: (),
73                        }
74                    }
75                    (Some(sub_span), Some(sup_span), _, _) => {
76                        PlaceholderRelationLfNotSatisfied::HasNone {
77                            span,
78                            sub_span,
79                            sup_span,
80                            note: (),
81                        }
82                    }
83                    _ => PlaceholderRelationLfNotSatisfied::OnlyPrimarySpan { span, note: () },
84                };
85                Some(self.tcx().dcx().create_err(diag))
86            }
87            _ => None,
88        }
89    }
90}