Skip to content

Commit 0b1521e

Browse files
committed
Make rustc::traits::error_reporting::{recursive_type_with_infinite_size_error, report_object_safety_error} free functions.
1 parent d53bf7a commit 0b1521e

File tree

5 files changed

+62
-59
lines changed

5 files changed

+62
-59
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use crate::hir::map;
5353
use crate::infer::opaque_types;
5454
use crate::infer::{self, SuppressRegionErrors};
5555
use crate::middle::region;
56+
use crate::traits::error_reporting::report_object_safety_error;
5657
use crate::traits::{
5758
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
5859
};
@@ -1487,7 +1488,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14871488
let mut diag = match failure_code {
14881489
FailureCode::Error0038(did) => {
14891490
let violations = self.tcx.object_safety_violations(did);
1490-
self.tcx.report_object_safety_error(span, did, violations)
1491+
report_object_safety_error(self.tcx, span, did, violations)
14911492
}
14921493
FailureCode::Error0317(failure_str) => {
14931494
struct_span_err!(self.tcx.sess, span, E0317, "{}", failure_str)

src/librustc/traits/error_reporting.rs

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
916916

917917
ty::Predicate::ObjectSafe(trait_def_id) => {
918918
let violations = self.tcx.object_safety_violations(trait_def_id);
919-
self.tcx.report_object_safety_error(span, trait_def_id, violations)
919+
report_object_safety_error(self.tcx, span, trait_def_id, violations)
920920
}
921921

922922
ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
@@ -1080,7 +1080,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10801080

10811081
TraitNotObjectSafe(did) => {
10821082
let violations = self.tcx.object_safety_violations(did);
1083-
self.tcx.report_object_safety_error(span, did, violations)
1083+
report_object_safety_error(self.tcx, span, did, violations)
10841084
}
10851085

10861086
// already reported in the query
@@ -1945,64 +1945,62 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
19451945
}
19461946
}
19471947

1948-
impl<'tcx> TyCtxt<'tcx> {
1949-
pub fn recursive_type_with_infinite_size_error(
1950-
self,
1951-
type_def_id: DefId,
1952-
) -> DiagnosticBuilder<'tcx> {
1953-
assert!(type_def_id.is_local());
1954-
let span = self.hir().span_if_local(type_def_id).unwrap();
1955-
let span = self.sess.source_map().def_span(span);
1956-
let mut err = struct_span_err!(
1957-
self.sess,
1958-
span,
1959-
E0072,
1960-
"recursive type `{}` has infinite size",
1961-
self.def_path_str(type_def_id)
1962-
);
1963-
err.span_label(span, "recursive type has infinite size");
1964-
err.help(&format!(
1965-
"insert indirection (e.g., a `Box`, `Rc`, or `&`) \
1948+
pub fn recursive_type_with_infinite_size_error(
1949+
tcx: TyCtxt<'tcx>,
1950+
type_def_id: DefId,
1951+
) -> DiagnosticBuilder<'tcx> {
1952+
assert!(type_def_id.is_local());
1953+
let span = tcx.hir().span_if_local(type_def_id).unwrap();
1954+
let span = tcx.sess.source_map().def_span(span);
1955+
let mut err = struct_span_err!(
1956+
tcx.sess,
1957+
span,
1958+
E0072,
1959+
"recursive type `{}` has infinite size",
1960+
tcx.def_path_str(type_def_id)
1961+
);
1962+
err.span_label(span, "recursive type has infinite size");
1963+
err.help(&format!(
1964+
"insert indirection (e.g., a `Box`, `Rc`, or `&`) \
19661965
at some point to make `{}` representable",
1967-
self.def_path_str(type_def_id)
1968-
));
1969-
err
1970-
}
1971-
1972-
pub fn report_object_safety_error(
1973-
self,
1974-
span: Span,
1975-
trait_def_id: DefId,
1976-
violations: Vec<ObjectSafetyViolation>,
1977-
) -> DiagnosticBuilder<'tcx> {
1978-
let trait_str = self.def_path_str(trait_def_id);
1979-
let span = self.sess.source_map().def_span(span);
1980-
let mut err = struct_span_err!(
1981-
self.sess,
1982-
span,
1983-
E0038,
1984-
"the trait `{}` cannot be made into an object",
1985-
trait_str
1986-
);
1987-
err.span_label(span, format!("the trait `{}` cannot be made into an object", trait_str));
1988-
1989-
let mut reported_violations = FxHashSet::default();
1990-
for violation in violations {
1991-
if reported_violations.insert(violation.clone()) {
1992-
match violation.span() {
1993-
Some(span) => err.span_label(span, violation.error_msg()),
1994-
None => err.note(&violation.error_msg()),
1995-
};
1996-
}
1997-
}
1966+
tcx.def_path_str(type_def_id)
1967+
));
1968+
err
1969+
}
19981970

1999-
if self.sess.trait_methods_not_found.borrow().contains(&span) {
2000-
// Avoid emitting error caused by non-existing method (#58734)
2001-
err.cancel();
1971+
pub fn report_object_safety_error(
1972+
tcx: TyCtxt<'tcx>,
1973+
span: Span,
1974+
trait_def_id: DefId,
1975+
violations: Vec<ObjectSafetyViolation>,
1976+
) -> DiagnosticBuilder<'tcx> {
1977+
let trait_str = tcx.def_path_str(trait_def_id);
1978+
let span = tcx.sess.source_map().def_span(span);
1979+
let mut err = struct_span_err!(
1980+
tcx.sess,
1981+
span,
1982+
E0038,
1983+
"the trait `{}` cannot be made into an object",
1984+
trait_str
1985+
);
1986+
err.span_label(span, format!("the trait `{}` cannot be made into an object", trait_str));
1987+
1988+
let mut reported_violations = FxHashSet::default();
1989+
for violation in violations {
1990+
if reported_violations.insert(violation.clone()) {
1991+
match violation.span() {
1992+
Some(span) => err.span_label(span, violation.error_msg()),
1993+
None => err.note(&violation.error_msg()),
1994+
};
20021995
}
1996+
}
20031997

2004-
err
1998+
if tcx.sess.trait_methods_not_found.borrow().contains(&span) {
1999+
// Avoid emitting error caused by non-existing method (#58734)
2000+
err.cancel();
20052001
}
2002+
2003+
err
20062004
}
20072005

20082006
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

src/librustc_typeck/astconv.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use errors::{Applicability, DiagnosticId};
1313
use rustc::hir::intravisit::Visitor;
1414
use rustc::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
1515
use rustc::traits;
16+
use rustc::traits::error_reporting::report_object_safety_error;
1617
use rustc::ty::subst::{self, InternalSubsts, Subst, SubstsRef};
1718
use rustc::ty::wf::object_region_bounds;
1819
use rustc::ty::{self, Const, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable};
@@ -1454,7 +1455,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14541455
let object_safety_violations =
14551456
tcx.astconv_object_safety_violations(item.trait_ref().def_id());
14561457
if !object_safety_violations.is_empty() {
1457-
tcx.report_object_safety_error(
1458+
report_object_safety_error(
1459+
tcx,
14581460
span,
14591461
item.trait_ref().def_id(),
14601462
object_safety_violations,

src/librustc_typeck/check/cast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use errors::{Applicability, DiagnosticBuilder};
3737
use rustc::middle::lang_items;
3838
use rustc::session::Session;
3939
use rustc::traits;
40+
use rustc::traits::error_reporting::report_object_safety_error;
4041
use rustc::ty::adjustment::AllowTwoPhase;
4142
use rustc::ty::cast::{CastKind, CastTy};
4243
use rustc::ty::error::TypeError;
@@ -519,7 +520,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
519520

520521
fn report_object_unsafe_cast(&self, fcx: &FnCtxt<'a, 'tcx>, did: DefId) {
521522
let violations = fcx.tcx.object_safety_violations(did);
522-
let mut err = fcx.tcx.report_object_safety_error(self.cast_span, did, violations);
523+
let mut err = report_object_safety_error(fcx.tcx, self.cast_span, did, violations);
523524
err.note(&format!("required by cast to type '{}'", fcx.ty_to_string(self.cast_ty)));
524525
err.emit();
525526
}

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ use rustc::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
100100
use rustc::infer::{self, InferCtxt, InferOk, InferResult};
101101
use rustc::middle::region;
102102
use rustc::mir::interpret::ConstValue;
103+
use rustc::traits::error_reporting::recursive_type_with_infinite_size_error;
103104
use rustc::traits::{self, ObligationCause, ObligationCauseCode, TraitEngine};
104105
use rustc::ty::adjustment::{
105106
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast,
@@ -2222,7 +2223,7 @@ fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: DefId) -> bool {
22222223
// caught by case 1.
22232224
match rty.is_representable(tcx, sp) {
22242225
Representability::SelfRecursive(spans) => {
2225-
let mut err = tcx.recursive_type_with_infinite_size_error(item_def_id);
2226+
let mut err = recursive_type_with_infinite_size_error(tcx, item_def_id);
22262227
for span in spans {
22272228
err.span_label(span, "recursive without indirection");
22282229
}

0 commit comments

Comments
 (0)