|
1 | | -use crate::errors::RegionOriginNote; |
| 1 | +use crate::errors::{ |
| 2 | + note_and_explain, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent, |
| 3 | + RegionOriginNote, |
| 4 | +}; |
2 | 5 | use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt}; |
3 | 6 | use crate::infer::{self, SubregionOrigin}; |
4 | 7 | use rustc_errors::{ |
5 | 8 | fluent, struct_span_err, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, |
6 | | - ErrorGuaranteed, |
| 9 | + ErrorGuaranteed, IntoDiagnostic, |
7 | 10 | }; |
8 | 11 | use rustc_hir::def_id::{DefId, LocalDefId}; |
9 | 12 | use rustc_middle::traits::ObligationCauseCode; |
@@ -119,104 +122,83 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { |
119 | 122 | err |
120 | 123 | } |
121 | 124 | infer::Reborrow(span) => { |
122 | | - let mut err = struct_span_err!( |
123 | | - self.tcx.sess, |
124 | | - span, |
125 | | - E0312, |
126 | | - "lifetime of reference outlives lifetime of borrowed content..." |
127 | | - ); |
128 | | - note_and_explain_region( |
| 125 | + let reference_valid = note_and_explain::RegionExplanation::new( |
129 | 126 | self.tcx, |
130 | | - &mut err, |
131 | | - "...the reference is valid for ", |
132 | 127 | sub, |
133 | | - "...", |
134 | 128 | None, |
| 129 | + note_and_explain::PrefixKind::RefValidFor, |
| 130 | + note_and_explain::SuffixKind::Continues, |
135 | 131 | ); |
136 | | - note_and_explain_region( |
| 132 | + let content_valid = note_and_explain::RegionExplanation::new( |
137 | 133 | self.tcx, |
138 | | - &mut err, |
139 | | - "...but the borrowed content is only valid for ", |
140 | 134 | sup, |
141 | | - "", |
142 | 135 | None, |
| 136 | + note_and_explain::PrefixKind::ContentValidFor, |
| 137 | + note_and_explain::SuffixKind::Empty, |
143 | 138 | ); |
144 | | - err |
| 139 | + OutlivesContent { |
| 140 | + span, |
| 141 | + notes: reference_valid.into_iter().chain(content_valid).collect(), |
| 142 | + } |
| 143 | + .into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic) |
145 | 144 | } |
146 | 145 | infer::RelateObjectBound(span) => { |
147 | | - let mut err = struct_span_err!( |
148 | | - self.tcx.sess, |
149 | | - span, |
150 | | - E0476, |
151 | | - "lifetime of the source pointer does not outlive lifetime bound of the \ |
152 | | - object type" |
153 | | - ); |
154 | | - note_and_explain_region( |
| 146 | + let object_valid = note_and_explain::RegionExplanation::new( |
155 | 147 | self.tcx, |
156 | | - &mut err, |
157 | | - "object type is valid for ", |
158 | 148 | sub, |
159 | | - "", |
160 | 149 | None, |
| 150 | + note_and_explain::PrefixKind::TypeValidFor, |
| 151 | + note_and_explain::SuffixKind::Empty, |
161 | 152 | ); |
162 | | - note_and_explain_region( |
| 153 | + let pointer_valid = note_and_explain::RegionExplanation::new( |
163 | 154 | self.tcx, |
164 | | - &mut err, |
165 | | - "source pointer is only valid for ", |
166 | 155 | sup, |
167 | | - "", |
168 | 156 | None, |
| 157 | + note_and_explain::PrefixKind::SourcePointerValidFor, |
| 158 | + note_and_explain::SuffixKind::Empty, |
169 | 159 | ); |
170 | | - err |
| 160 | + OutlivesBound { |
| 161 | + span, |
| 162 | + notes: object_valid.into_iter().chain(pointer_valid).collect(), |
| 163 | + } |
| 164 | + .into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic) |
171 | 165 | } |
172 | 166 | infer::RelateParamBound(span, ty, opt_span) => { |
173 | | - let mut err = struct_span_err!( |
174 | | - self.tcx.sess, |
175 | | - span, |
176 | | - E0477, |
177 | | - "the type `{}` does not fulfill the required lifetime", |
178 | | - self.ty_to_string(ty) |
| 167 | + let prefix = match *sub { |
| 168 | + ty::ReStatic => note_and_explain::PrefixKind::TypeSatisfy, |
| 169 | + _ => note_and_explain::PrefixKind::TypeOutlive, |
| 170 | + }; |
| 171 | + let suffix = if opt_span.is_some() { |
| 172 | + note_and_explain::SuffixKind::ReqByBinding |
| 173 | + } else { |
| 174 | + note_and_explain::SuffixKind::Empty |
| 175 | + }; |
| 176 | + let note = note_and_explain::RegionExplanation::new( |
| 177 | + self.tcx, sub, opt_span, prefix, suffix, |
179 | 178 | ); |
180 | | - match *sub { |
181 | | - ty::ReStatic => note_and_explain_region( |
182 | | - self.tcx, |
183 | | - &mut err, |
184 | | - "type must satisfy ", |
185 | | - sub, |
186 | | - if opt_span.is_some() { " as required by this binding" } else { "" }, |
187 | | - opt_span, |
188 | | - ), |
189 | | - _ => note_and_explain_region( |
190 | | - self.tcx, |
191 | | - &mut err, |
192 | | - "type must outlive ", |
193 | | - sub, |
194 | | - if opt_span.is_some() { " as required by this binding" } else { "" }, |
195 | | - opt_span, |
196 | | - ), |
197 | | - } |
198 | | - err |
| 179 | + FullfillReqLifetime { span, ty: self.resolve_vars_if_possible(ty), note } |
| 180 | + .into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic) |
199 | 181 | } |
200 | 182 | infer::RelateRegionParamBound(span) => { |
201 | | - let mut err = |
202 | | - struct_span_err!(self.tcx.sess, span, E0478, "lifetime bound not satisfied"); |
203 | | - note_and_explain_region( |
| 183 | + let param_instantiated = note_and_explain::RegionExplanation::new( |
204 | 184 | self.tcx, |
205 | | - &mut err, |
206 | | - "lifetime parameter instantiated with ", |
207 | 185 | sup, |
208 | | - "", |
209 | 186 | None, |
| 187 | + note_and_explain::PrefixKind::LfInstantiatedWith, |
| 188 | + note_and_explain::SuffixKind::Empty, |
210 | 189 | ); |
211 | | - note_and_explain_region( |
| 190 | + let param_must_outlive = note_and_explain::RegionExplanation::new( |
212 | 191 | self.tcx, |
213 | | - &mut err, |
214 | | - "but lifetime parameter must outlive ", |
215 | 192 | sub, |
216 | | - "", |
217 | 193 | None, |
| 194 | + note_and_explain::PrefixKind::LfMustOutlive, |
| 195 | + note_and_explain::SuffixKind::Empty, |
218 | 196 | ); |
219 | | - err |
| 197 | + LfBoundNotSatisfied { |
| 198 | + span, |
| 199 | + notes: param_instantiated.into_iter().chain(param_must_outlive).collect(), |
| 200 | + } |
| 201 | + .into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic) |
220 | 202 | } |
221 | 203 | infer::ReferenceOutlivesReferent(ty, span) => { |
222 | 204 | let mut err = struct_span_err!( |
|
0 commit comments