|
| 1 | +use crate::infer::error_reporting::nice_region_error::find_anon_type; |
| 2 | +use rustc_errors::{self, fluent, AddSubdiagnostic}; |
| 3 | +use rustc_middle::ty::{self, TyCtxt}; |
| 4 | +use rustc_span::{symbol::kw, Span}; |
| 5 | + |
| 6 | +#[derive(Default)] |
| 7 | +struct DescriptionCtx<'a> { |
| 8 | + span: Option<Span>, |
| 9 | + kind: &'a str, |
| 10 | + arg: String, |
| 11 | + num_arg: u32, |
| 12 | +} |
| 13 | + |
| 14 | +impl<'a> DescriptionCtx<'a> { |
| 15 | + fn new<'tcx>( |
| 16 | + tcx: TyCtxt<'tcx>, |
| 17 | + region: ty::Region<'tcx>, |
| 18 | + alt_span: Option<Span>, |
| 19 | + ) -> Option<Self> { |
| 20 | + let mut me = DescriptionCtx::default(); |
| 21 | + me.span = alt_span; |
| 22 | + match *region { |
| 23 | + ty::ReEarlyBound(_) | ty::ReFree(_) => { |
| 24 | + return Self::from_early_bound_and_free_regions(tcx, region); |
| 25 | + } |
| 26 | + ty::ReStatic => { |
| 27 | + me.kind = "restatic"; |
| 28 | + } |
| 29 | + |
| 30 | + ty::ReEmpty(ty::UniverseIndex::ROOT) => me.kind = "reempty", |
| 31 | + |
| 32 | + // uh oh, hope no user ever sees THIS |
| 33 | + ty::ReEmpty(ui) => { |
| 34 | + me.kind = "reemptyuni"; |
| 35 | + me.arg = format!("{:?}", ui); |
| 36 | + } |
| 37 | + |
| 38 | + ty::RePlaceholder(_) => return None, |
| 39 | + |
| 40 | + // FIXME(#13998) RePlaceholder should probably print like |
| 41 | + // ReFree rather than dumping Debug output on the user. |
| 42 | + // |
| 43 | + // We shouldn't really be having unification failures with ReVar |
| 44 | + // and ReLateBound though. |
| 45 | + ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => { |
| 46 | + me.kind = "revar"; |
| 47 | + me.arg = format!("{:?}", region); |
| 48 | + } |
| 49 | + }; |
| 50 | + Some(me) |
| 51 | + } |
| 52 | + |
| 53 | + fn from_early_bound_and_free_regions<'tcx>( |
| 54 | + tcx: TyCtxt<'tcx>, |
| 55 | + region: ty::Region<'tcx>, |
| 56 | + ) -> Option<Self> { |
| 57 | + let mut me = DescriptionCtx::default(); |
| 58 | + let scope = region.free_region_binding_scope(tcx).expect_local(); |
| 59 | + match *region { |
| 60 | + ty::ReEarlyBound(ref br) => { |
| 61 | + let mut sp = tcx.def_span(scope); |
| 62 | + if let Some(param) = |
| 63 | + tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name)) |
| 64 | + { |
| 65 | + sp = param.span; |
| 66 | + } |
| 67 | + if br.has_name() { |
| 68 | + me.kind = "as_defined"; |
| 69 | + me.arg = br.name.to_string(); |
| 70 | + } else { |
| 71 | + me.kind = "as_defined_anon"; |
| 72 | + }; |
| 73 | + me.span = Some(sp) |
| 74 | + } |
| 75 | + ty::ReFree(ref fr) => { |
| 76 | + if !fr.bound_region.is_named() |
| 77 | + && let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) |
| 78 | + { |
| 79 | + me.kind = "defined_here"; |
| 80 | + me.span = Some(ty.span); |
| 81 | + } else { |
| 82 | + match fr.bound_region { |
| 83 | + ty::BoundRegionKind::BrNamed(_, name) => { |
| 84 | + let mut sp = tcx.def_span(scope); |
| 85 | + if let Some(param) = |
| 86 | + tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name)) |
| 87 | + { |
| 88 | + sp = param.span; |
| 89 | + } |
| 90 | + if name == kw::UnderscoreLifetime { |
| 91 | + me.kind = "as_defined_anon"; |
| 92 | + } else { |
| 93 | + me.kind = "as_defined"; |
| 94 | + me.arg = name.to_string(); |
| 95 | + }; |
| 96 | + me.span = Some(sp); |
| 97 | + } |
| 98 | + ty::BrAnon(idx) => { |
| 99 | + me.kind = "anon_num_here"; |
| 100 | + me.num_arg = idx+1; |
| 101 | + me.span = Some(tcx.def_span(scope)); |
| 102 | + }, |
| 103 | + _ => { |
| 104 | + me.kind = "defined_here_reg"; |
| 105 | + me.arg = region.to_string(); |
| 106 | + me.span = Some(tcx.def_span(scope)); |
| 107 | + }, |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + _ => bug!(), |
| 112 | + } |
| 113 | + Some(me) |
| 114 | + } |
| 115 | + |
| 116 | + fn add_to(self, diag: &mut rustc_errors::Diagnostic) { |
| 117 | + diag.set_arg("desc_kind", self.kind); |
| 118 | + diag.set_arg("desc_arg", self.arg); |
| 119 | + diag.set_arg("desc_num_arg", self.num_arg); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +pub enum PrefixKind { |
| 124 | + Empty, |
| 125 | +} |
| 126 | + |
| 127 | +pub enum SuffixKind { |
| 128 | + Continues, |
| 129 | +} |
| 130 | + |
| 131 | +impl PrefixKind { |
| 132 | + fn add_to(self, diag: &mut rustc_errors::Diagnostic) { |
| 133 | + match self { |
| 134 | + Self::Empty => diag.set_arg("pref_kind", "empty"), |
| 135 | + }; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl SuffixKind { |
| 140 | + fn add_to(self, diag: &mut rustc_errors::Diagnostic) { |
| 141 | + match self { |
| 142 | + Self::Continues => diag.set_arg("suff_kind", "continues"), |
| 143 | + }; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +pub struct RegionExplanation<'a> { |
| 148 | + desc: DescriptionCtx<'a>, |
| 149 | + prefix: PrefixKind, |
| 150 | + suffix: SuffixKind, |
| 151 | +} |
| 152 | + |
| 153 | +impl RegionExplanation<'_> { |
| 154 | + pub fn new<'tcx>( |
| 155 | + tcx: TyCtxt<'tcx>, |
| 156 | + region: ty::Region<'tcx>, |
| 157 | + alt_span: Option<Span>, |
| 158 | + prefix: PrefixKind, |
| 159 | + suffix: SuffixKind, |
| 160 | + ) -> Option<Self> { |
| 161 | + Some(Self { desc: DescriptionCtx::new(tcx, region, alt_span)?, prefix, suffix }) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +impl AddSubdiagnostic for RegionExplanation<'_> { |
| 166 | + fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) { |
| 167 | + if let Some(span) = self.desc.span { |
| 168 | + diag.span_note(span, fluent::infer::region_explanation); |
| 169 | + } else { |
| 170 | + diag.note(fluent::infer::region_explanation); |
| 171 | + } |
| 172 | + self.desc.add_to(diag); |
| 173 | + self.prefix.add_to(diag); |
| 174 | + self.suffix.add_to(diag); |
| 175 | + } |
| 176 | +} |
0 commit comments