@@ -49,7 +49,6 @@ use super::lexical_region_resolve::RegionResolutionError;
4949use super :: region_constraints:: GenericKind ;
5050use super :: { InferCtxt , RegionVariableOrigin , SubregionOrigin , TypeTrace , ValuePairs } ;
5151
52- use crate :: infer:: opaque_types;
5352use crate :: infer:: { self , SuppressRegionErrors } ;
5453use crate :: traits:: error_reporting:: report_object_safety_error;
5554use crate :: traits:: {
@@ -288,6 +287,86 @@ fn explain_span(tcx: TyCtxt<'tcx>, heading: &str, span: Span) -> (String, Option
288287 ( format ! ( "the {} at {}:{}" , heading, lo. line, lo. col. to_usize( ) + 1 ) , Some ( span) )
289288}
290289
290+ pub fn unexpected_hidden_region_diagnostic (
291+ tcx : TyCtxt < ' tcx > ,
292+ region_scope_tree : Option < & region:: ScopeTree > ,
293+ span : Span ,
294+ hidden_ty : Ty < ' tcx > ,
295+ hidden_region : ty:: Region < ' tcx > ,
296+ ) -> DiagnosticBuilder < ' tcx > {
297+ let mut err = struct_span_err ! (
298+ tcx. sess,
299+ span,
300+ E0700 ,
301+ "hidden type for `impl Trait` captures lifetime that does not appear in bounds" ,
302+ ) ;
303+
304+ // Explain the region we are capturing.
305+ if let ty:: ReEarlyBound ( _) | ty:: ReFree ( _) | ty:: ReStatic | ty:: ReEmpty ( _) = hidden_region {
306+ // Assuming regionck succeeded (*), we ought to always be
307+ // capturing *some* region from the fn header, and hence it
308+ // ought to be free. So under normal circumstances, we will go
309+ // down this path which gives a decent human readable
310+ // explanation.
311+ //
312+ // (*) if not, the `tainted_by_errors` flag would be set to
313+ // true in any case, so we wouldn't be here at all.
314+ note_and_explain_free_region (
315+ tcx,
316+ & mut err,
317+ & format ! ( "hidden type `{}` captures " , hidden_ty) ,
318+ hidden_region,
319+ "" ,
320+ ) ;
321+ } else {
322+ // Ugh. This is a painful case: the hidden region is not one
323+ // that we can easily summarize or explain. This can happen
324+ // in a case like
325+ // `src/test/ui/multiple-lifetimes/ordinary-bounds-unsuited.rs`:
326+ //
327+ // ```
328+ // fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b> {
329+ // if condition() { a } else { b }
330+ // }
331+ // ```
332+ //
333+ // Here the captured lifetime is the intersection of `'a` and
334+ // `'b`, which we can't quite express.
335+
336+ if let Some ( region_scope_tree) = region_scope_tree {
337+ // If the `region_scope_tree` is available, this is being
338+ // invoked from the "region inferencer error". We can at
339+ // least report a really cryptic error for now.
340+ note_and_explain_region (
341+ tcx,
342+ region_scope_tree,
343+ & mut err,
344+ & format ! ( "hidden type `{}` captures " , hidden_ty) ,
345+ hidden_region,
346+ "" ,
347+ ) ;
348+ } else {
349+ // If the `region_scope_tree` is *unavailable*, this is
350+ // being invoked by the code that comes *after* region
351+ // inferencing. This is a bug, as the region inferencer
352+ // ought to have noticed the failed constraint and invoked
353+ // error reporting, which in turn should have prevented us
354+ // from getting trying to infer the hidden type
355+ // completely.
356+ tcx. sess . delay_span_bug (
357+ span,
358+ & format ! (
359+ "hidden type captures unexpected lifetime `{:?}` \
360+ but no region inference failure",
361+ hidden_region,
362+ ) ,
363+ ) ;
364+ }
365+ }
366+
367+ err
368+ }
369+
291370impl < ' a , ' tcx > InferCtxt < ' a , ' tcx > {
292371 pub fn report_region_errors (
293372 & self ,
@@ -410,7 +489,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
410489 span,
411490 } => {
412491 let hidden_ty = self . resolve_vars_if_possible ( & hidden_ty) ;
413- opaque_types :: unexpected_hidden_region_diagnostic (
492+ unexpected_hidden_region_diagnostic (
414493 self . tcx ,
415494 Some ( region_scope_tree) ,
416495 span,
@@ -2077,7 +2156,7 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
20772156/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
20782157/// extra information about each type, but we only care about the category.
20792158#[ derive( Clone , Copy , PartialEq , Eq , Hash ) ]
2080- crate enum TyCategory {
2159+ pub enum TyCategory {
20812160 Closure ,
20822161 Opaque ,
20832162 Generator ,
0 commit comments