Skip to main content

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::NamedForPrinting(_) => {
38                        ::rustc_middle::util::bug::bug_fmt(format_args!("only used for pretty printing"))bug!("only used for pretty printing")
39                    }
40                };
41                let (sup_span, sup_symbol) = match *sup_name {
42                    ty::BoundRegionKind::Named(def_id) => {
43                        (Some(self.tcx().def_span(def_id)), Some(self.tcx().item_name(def_id)))
44                    }
45                    ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => (None, None),
46                    ty::BoundRegionKind::NamedForPrinting(_) => {
47                        ::rustc_middle::util::bug::bug_fmt(format_args!("only used for pretty printing"))bug!("only used for pretty printing")
48                    }
49                };
50                let diag = match (sub_span, sup_span, sub_symbol, sup_symbol) {
51                    (Some(sub_span), Some(sup_span), Some(sub_symbol), Some(sup_symbol)) => {
52                        PlaceholderRelationLfNotSatisfied::HasBoth {
53                            span,
54                            sub_span,
55                            sup_span,
56                            sub_symbol,
57                            sup_symbol,
58                            note: (),
59                        }
60                    }
61                    (Some(sub_span), Some(sup_span), _, Some(sup_symbol)) => {
62                        PlaceholderRelationLfNotSatisfied::HasSup {
63                            span,
64                            sub_span,
65                            sup_span,
66                            sup_symbol,
67                            note: (),
68                        }
69                    }
70                    (Some(sub_span), Some(sup_span), Some(sub_symbol), _) => {
71                        PlaceholderRelationLfNotSatisfied::HasSub {
72                            span,
73                            sub_span,
74                            sup_span,
75                            sub_symbol,
76                            note: (),
77                        }
78                    }
79                    (Some(sub_span), Some(sup_span), _, _) => {
80                        PlaceholderRelationLfNotSatisfied::HasNone {
81                            span,
82                            sub_span,
83                            sup_span,
84                            note: (),
85                        }
86                    }
87                    _ => PlaceholderRelationLfNotSatisfied::OnlyPrimarySpan { span, note: () },
88                };
89                Some(self.tcx().dcx().create_err(diag))
90            }
91            _ => None,
92        }
93    }
94}