Skip to content

Commit 98444ca

Browse files
committed
Move opaque_types::unexpected_hidden_region_diagnostic to error_reporting.
1 parent 3e5259d commit 98444ca

File tree

3 files changed

+85
-87
lines changed

3 files changed

+85
-87
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use super::lexical_region_resolve::RegionResolutionError;
4949
use super::region_constraints::GenericKind;
5050
use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs};
5151

52-
use crate::infer::opaque_types;
5352
use crate::infer::{self, SuppressRegionErrors};
5453
use crate::traits::error_reporting::report_object_safety_error;
5554
use 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+
291370
impl<'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,

src/librustc_infer/infer/opaque_types/mod.rs

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use crate::infer::error_reporting::{note_and_explain_free_region, note_and_explain_region};
1+
use crate::infer::error_reporting::unexpected_hidden_region_diagnostic;
22
use crate::infer::{self, InferCtxt, InferOk, TypeVariableOrigin, TypeVariableOriginKind};
33
use crate::traits::{self, PredicateObligation};
4-
use rustc::middle::region;
54
use rustc::session::config::nightly_options;
65
use rustc::ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder, TypeVisitor};
76
use rustc::ty::free_region_map::FreeRegionRelations;
87
use rustc::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
98
use rustc::ty::{self, GenericParamDefKind, Ty, TyCtxt};
109
use rustc_data_structures::fx::FxHashMap;
1110
use rustc_data_structures::sync::Lrc;
12-
use rustc_errors::{struct_span_err, DiagnosticBuilder};
1311
use rustc_hir as hir;
1412
use rustc_hir::def_id::{DefId, DefIdMap};
1513
use rustc_hir::Node;
@@ -618,86 +616,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
618616
}
619617
}
620618

621-
pub fn unexpected_hidden_region_diagnostic(
622-
tcx: TyCtxt<'tcx>,
623-
region_scope_tree: Option<&region::ScopeTree>,
624-
span: Span,
625-
hidden_ty: Ty<'tcx>,
626-
hidden_region: ty::Region<'tcx>,
627-
) -> DiagnosticBuilder<'tcx> {
628-
let mut err = struct_span_err!(
629-
tcx.sess,
630-
span,
631-
E0700,
632-
"hidden type for `impl Trait` captures lifetime that does not appear in bounds",
633-
);
634-
635-
// Explain the region we are capturing.
636-
if let ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic | ty::ReEmpty(_) = hidden_region {
637-
// Assuming regionck succeeded (*), we ought to always be
638-
// capturing *some* region from the fn header, and hence it
639-
// ought to be free. So under normal circumstances, we will go
640-
// down this path which gives a decent human readable
641-
// explanation.
642-
//
643-
// (*) if not, the `tainted_by_errors` flag would be set to
644-
// true in any case, so we wouldn't be here at all.
645-
note_and_explain_free_region(
646-
tcx,
647-
&mut err,
648-
&format!("hidden type `{}` captures ", hidden_ty),
649-
hidden_region,
650-
"",
651-
);
652-
} else {
653-
// Ugh. This is a painful case: the hidden region is not one
654-
// that we can easily summarize or explain. This can happen
655-
// in a case like
656-
// `src/test/ui/multiple-lifetimes/ordinary-bounds-unsuited.rs`:
657-
//
658-
// ```
659-
// fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b> {
660-
// if condition() { a } else { b }
661-
// }
662-
// ```
663-
//
664-
// Here the captured lifetime is the intersection of `'a` and
665-
// `'b`, which we can't quite express.
666-
667-
if let Some(region_scope_tree) = region_scope_tree {
668-
// If the `region_scope_tree` is available, this is being
669-
// invoked from the "region inferencer error". We can at
670-
// least report a really cryptic error for now.
671-
note_and_explain_region(
672-
tcx,
673-
region_scope_tree,
674-
&mut err,
675-
&format!("hidden type `{}` captures ", hidden_ty),
676-
hidden_region,
677-
"",
678-
);
679-
} else {
680-
// If the `region_scope_tree` is *unavailable*, this is
681-
// being invoked by the code that comes *after* region
682-
// inferencing. This is a bug, as the region inferencer
683-
// ought to have noticed the failed constraint and invoked
684-
// error reporting, which in turn should have prevented us
685-
// from getting trying to infer the hidden type
686-
// completely.
687-
tcx.sess.delay_span_bug(
688-
span,
689-
&format!(
690-
"hidden type captures unexpected lifetime `{:?}` \
691-
but no region inference failure",
692-
hidden_region,
693-
),
694-
);
695-
}
696-
}
697-
698-
err
699-
}
700-
701619
// Visitor that requires that (almost) all regions in the type visited outlive
702620
// `least_region`. We cannot use `push_outlives_components` because regions in
703621
// closure signatures are not included in their outlives components. We need to

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use rustc::mir::ConstraintCategory;
44
use rustc::ty::{self, RegionVid, Ty};
55
use rustc_errors::{Applicability, DiagnosticBuilder};
66
use rustc_infer::infer::{
7-
error_reporting::nice_region_error::NiceRegionError, opaque_types, NLLRegionVariableOrigin,
7+
error_reporting::nice_region_error::NiceRegionError,
8+
error_reporting::unexpected_hidden_region_diagnostic, NLLRegionVariableOrigin,
89
};
910
use rustc_span::symbol::kw;
1011
use rustc_span::Span;
@@ -197,7 +198,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
197198
let region_scope_tree = &self.infcx.tcx.region_scope_tree(self.mir_def_id);
198199
let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
199200
let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
200-
opaque_types::unexpected_hidden_region_diagnostic(
201+
unexpected_hidden_region_diagnostic(
201202
self.infcx.tcx,
202203
Some(region_scope_tree),
203204
span,

0 commit comments

Comments
 (0)