Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ReScope #72362

Merged
merged 8 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/librustc_infer/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
ty::ReStatic
| ty::ReEarlyBound(..)
| ty::ReFree(_)
| ty::ReScope(_)
| ty::ReEmpty(_)
| ty::RePlaceholder(..)
| ty::ReErased => self.canonicalize_region_mode.canonicalize_free_region(self, r),
Expand Down
1 change: 0 additions & 1 deletion src/librustc_infer/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
| ty::ReVar(..)
| ty::ReEmpty(_)
| ty::ReStatic
| ty::ReScope(..)
| ty::ReEarlyBound(..)
| ty::ReFree(..) => {
// see common code below
Expand Down
199 changes: 52 additions & 147 deletions src/librustc_infer/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::Node;
use rustc_middle::middle::region;
use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::{
self,
Expand All @@ -81,58 +80,12 @@ pub mod nice_region_error;

pub(super) fn note_and_explain_region(
tcx: TyCtxt<'tcx>,
region_scope_tree: &region::ScopeTree,
err: &mut DiagnosticBuilder<'_>,
prefix: &str,
region: ty::Region<'tcx>,
suffix: &str,
) {
let (description, span) = match *region {
ty::ReScope(scope) => {
let new_string;
let unknown_scope =
|| format!("{}unknown scope: {:?}{}. Please report a bug.", prefix, scope, suffix);
let span = scope.span(tcx, region_scope_tree);
let hir_id = scope.hir_id(region_scope_tree);
let tag = match hir_id.and_then(|hir_id| tcx.hir().find(hir_id)) {
Some(Node::Block(_)) => "block",
Some(Node::Expr(expr)) => match expr.kind {
hir::ExprKind::Call(..) => "call",
hir::ExprKind::MethodCall(..) => "method call",
hir::ExprKind::Match(.., hir::MatchSource::IfLetDesugar { .. }) => "if let",
hir::ExprKind::Match(.., hir::MatchSource::WhileLetDesugar) => "while let",
hir::ExprKind::Match(.., hir::MatchSource::ForLoopDesugar) => "for",
hir::ExprKind::Match(..) => "match",
_ => "expression",
},
Some(Node::Stmt(_)) => "statement",
Some(Node::Item(it)) => item_scope_tag(&it),
Some(Node::TraitItem(it)) => trait_item_scope_tag(&it),
Some(Node::ImplItem(it)) => impl_item_scope_tag(&it),
Some(_) | None => {
err.span_note(span, &unknown_scope());
return;
}
};
let scope_decorated_tag = match scope.data {
region::ScopeData::Node => tag,
region::ScopeData::CallSite => "scope of call-site for function",
region::ScopeData::Arguments => "scope of function body",
region::ScopeData::Destruction => {
new_string = format!("destruction scope surrounding {}", tag);
&new_string[..]
}
region::ScopeData::Remainder(first_statement_index) => {
new_string = format!(
"block suffix following statement {}",
first_statement_index.index()
);
&new_string[..]
}
};
explain_span(tcx, scope_decorated_tag, span)
}

ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {
msg_span_from_free_region(tcx, region)
}
Expand Down Expand Up @@ -284,7 +237,6 @@ fn explain_span(tcx: TyCtxt<'tcx>, heading: &str, span: Span) -> (String, Option

pub fn unexpected_hidden_region_diagnostic(
tcx: TyCtxt<'tcx>,
region_scope_tree: Option<&region::ScopeTree>,
span: Span,
hidden_ty: Ty<'tcx>,
hidden_region: ty::Region<'tcx>,
Expand All @@ -297,64 +249,56 @@ pub fn unexpected_hidden_region_diagnostic(
);

// Explain the region we are capturing.
if let ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic | ty::ReEmpty(_) = hidden_region {
// Assuming regionck succeeded (*), we ought to always be
// capturing *some* region from the fn header, and hence it
// ought to be free. So under normal circumstances, we will go
// down this path which gives a decent human readable
// explanation.
//
// (*) if not, the `tainted_by_errors` field would be set to
// `Some(ErrorReported)` in any case, so we wouldn't be here at all.
note_and_explain_free_region(
tcx,
&mut err,
&format!("hidden type `{}` captures ", hidden_ty),
hidden_region,
"",
);
} else {
// Ugh. This is a painful case: the hidden region is not one
// that we can easily summarize or explain. This can happen
// in a case like
// `src/test/ui/multiple-lifetimes/ordinary-bounds-unsuited.rs`:
//
// ```
// fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b> {
// if condition() { a } else { b }
// }
// ```
//
// Here the captured lifetime is the intersection of `'a` and
// `'b`, which we can't quite express.

if let Some(region_scope_tree) = region_scope_tree {
// If the `region_scope_tree` is available, this is being
// invoked from the "region inferencer error". We can at
// least report a really cryptic error for now.
note_and_explain_region(
match hidden_region {
ty::ReEmpty(ty::UniverseIndex::ROOT) => {
// All lifetimes shorter than the function body are `empty` in
// lexical region resolution. The default explanation of "an empty
// lifetime" isn't really accurate here.
let message = format!(
"hidden type `{}` captures lifetime smaller than the function body",
hidden_ty
);
err.span_note(span, &message);
}
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic | ty::ReEmpty(_) => {
// Assuming regionck succeeded (*), we ought to always be
// capturing *some* region from the fn header, and hence it
// ought to be free. So under normal circumstances, we will go
// down this path which gives a decent human readable
// explanation.
//
// (*) if not, the `tainted_by_errors` field would be set to
// `Some(ErrorReported)` in any case, so we wouldn't be here at all.
note_and_explain_free_region(
tcx,
region_scope_tree,
&mut err,
&format!("hidden type `{}` captures ", hidden_ty),
hidden_region,
"",
);
} else {
// If the `region_scope_tree` is *unavailable*, this is
// being invoked by the code that comes *after* region
// inferencing. This is a bug, as the region inferencer
// ought to have noticed the failed constraint and invoked
// error reporting, which in turn should have prevented us
// from getting trying to infer the hidden type
// completely.
tcx.sess.delay_span_bug(
span,
&format!(
"hidden type captures unexpected lifetime `{:?}` \
but no region inference failure",
hidden_region,
),
}
_ => {
// Ugh. This is a painful case: the hidden region is not one
// that we can easily summarize or explain. This can happen
// in a case like
// `src/test/ui/multiple-lifetimes/ordinary-bounds-unsuited.rs`:
//
// ```
// fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b> {
// if condition() { a } else { b }
// }
// ```
//
// Here the captured lifetime is the intersection of `'a` and
// `'b`, which we can't quite express.

// We can at least report a really cryptic error for now.
note_and_explain_region(
tcx,
&mut err,
&format!("hidden type `{}` captures ", hidden_ty),
hidden_region,
"",
);
}
}
Expand All @@ -363,11 +307,7 @@ pub fn unexpected_hidden_region_diagnostic(
}

impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
pub fn report_region_errors(
&self,
region_scope_tree: &region::ScopeTree,
errors: &Vec<RegionResolutionError<'tcx>>,
) {
pub fn report_region_errors(&self, errors: &Vec<RegionResolutionError<'tcx>>) {
debug!("report_region_errors(): {} errors to start", errors.len());

// try to pre-process the errors, which will group some of them
Expand All @@ -390,17 +330,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// general bit of code that displays the error information
RegionResolutionError::ConcreteFailure(origin, sub, sup) => {
if sub.is_placeholder() || sup.is_placeholder() {
self.report_placeholder_failure(region_scope_tree, origin, sub, sup)
.emit();
self.report_placeholder_failure(origin, sub, sup).emit();
} else {
self.report_concrete_failure(region_scope_tree, origin, sub, sup)
.emit();
self.report_concrete_failure(origin, sub, sup).emit();
}
}

RegionResolutionError::GenericBoundFailure(origin, param_ty, sub) => {
self.report_generic_bound_failure(
region_scope_tree,
origin.span(),
Some(origin),
param_ty,
Expand All @@ -417,29 +354,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
sup_r,
) => {
if sub_r.is_placeholder() {
self.report_placeholder_failure(
region_scope_tree,
sub_origin,
sub_r,
sup_r,
)
.emit();
self.report_placeholder_failure(sub_origin, sub_r, sup_r).emit();
} else if sup_r.is_placeholder() {
self.report_placeholder_failure(
region_scope_tree,
sup_origin,
sub_r,
sup_r,
)
.emit();
self.report_placeholder_failure(sup_origin, sub_r, sup_r).emit();
} else {
self.report_sub_sup_conflict(
region_scope_tree,
var_origin,
sub_origin,
sub_r,
sup_origin,
sup_r,
var_origin, sub_origin, sub_r, sup_origin, sup_r,
);
}
}
Expand All @@ -460,13 +380,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// value.
let sub_r = self.tcx.mk_region(ty::ReEmpty(var_universe));

self.report_placeholder_failure(
region_scope_tree,
sup_origin,
sub_r,
sup_r,
)
.emit();
self.report_placeholder_failure(sup_origin, sub_r, sup_r).emit();
}

RegionResolutionError::MemberConstraintFailure {
Expand All @@ -477,7 +391,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let hidden_ty = self.resolve_vars_if_possible(&hidden_ty);
unexpected_hidden_region_diagnostic(
self.tcx,
Some(region_scope_tree),
span,
hidden_ty,
member_region,
Expand Down Expand Up @@ -1754,19 +1667,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

pub fn report_generic_bound_failure(
&self,
region_scope_tree: &region::ScopeTree,
span: Span,
origin: Option<SubregionOrigin<'tcx>>,
bound_kind: GenericKind<'tcx>,
sub: Region<'tcx>,
) {
self.construct_generic_bound_failure(region_scope_tree, span, origin, bound_kind, sub)
.emit();
self.construct_generic_bound_failure(span, origin, bound_kind, sub).emit();
}

pub fn construct_generic_bound_failure(
&self,
region_scope_tree: &region::ScopeTree,
span: Span,
origin: Option<SubregionOrigin<'tcx>>,
bound_kind: GenericKind<'tcx>,
Expand Down Expand Up @@ -1918,7 +1828,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
));
note_and_explain_region(
self.tcx,
region_scope_tree,
&mut err,
&format!("{} must be valid for ", labeled_user_string),
sub,
Expand All @@ -1936,7 +1845,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

fn report_sub_sup_conflict(
&self,
region_scope_tree: &region::ScopeTree,
var_origin: RegionVariableOrigin,
sub_origin: SubregionOrigin<'tcx>,
sub_region: Region<'tcx>,
Expand All @@ -1947,7 +1855,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

note_and_explain_region(
self.tcx,
region_scope_tree,
&mut err,
"first, the lifetime cannot outlive ",
sup_region,
Expand All @@ -1973,7 +1880,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if sub_expected == sup_expected && sub_found == sup_found {
note_and_explain_region(
self.tcx,
region_scope_tree,
&mut err,
"...but the lifetime must also be valid for ",
sub_region,
Expand All @@ -1995,7 +1901,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

note_and_explain_region(
self.tcx,
region_scope_tree,
&mut err,
"but, the lifetime must be valid for ",
sub_region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rustc_span::source_map::Span;
mod different_lifetimes;
mod find_anon_type;
mod named_anon_conflict;
mod outlives_closure;
mod placeholder_error;
mod static_impl_trait;
mod trait_impl_difference;
Expand Down Expand Up @@ -57,7 +56,6 @@ impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
ErrorReported
})
.or_else(|| self.try_report_anon_anon_conflict())
.or_else(|| self.try_report_outlives_closure())
.or_else(|| self.try_report_static_impl_trait())
.or_else(|| self.try_report_impl_not_conforming_to_trait())
}
Expand Down
Loading