1
Fork 0

Remove Session methods that duplicate DiagCtxt methods.

Also add some `dcx` methods to types that wrap `TyCtxt`, for easier
access.
This commit is contained in:
Nicholas Nethercote 2023-12-18 22:21:37 +11:00
parent d51db05d7e
commit 99472c7049
298 changed files with 1806 additions and 2064 deletions

View file

@ -221,7 +221,7 @@ impl CanonicalizeMode for CanonicalizeQueryResponse {
// rust-lang/rust#57464: `impl Trait` can leak local
// scopes (in manner violating typeck). Therefore, use
// `span_delayed_bug` to allow type error over an ICE.
canonicalizer.tcx.sess.span_delayed_bug(
canonicalizer.tcx.dcx().span_delayed_bug(
rustc_span::DUMMY_SP,
format!("unexpected region in query response: `{r:?}`"),
);

View file

@ -59,8 +59,10 @@ use crate::traits::{
};
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, DiagnosticStyledString};
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
use rustc_errors::{
error_code, pluralize, struct_span_err, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder,
DiagnosticStyledString, ErrorGuaranteed, IntoDiagnosticArg,
};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
@ -142,7 +144,11 @@ impl Drop for TypeErrCtxt<'_, '_> {
}
}
impl TypeErrCtxt<'_, '_> {
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub fn dcx(&self) -> &'tcx DiagCtxt {
self.infcx.tcx.dcx()
}
/// This is just to avoid a potential footgun of accidentally
/// dropping `typeck_results` by calling `InferCtxt::err_ctxt`
#[deprecated(note = "you already have a `TypeErrCtxt`")]
@ -307,7 +313,7 @@ pub fn unexpected_hidden_region_diagnostic<'tcx>(
hidden_region: ty::Region<'tcx>,
opaque_ty_key: ty::OpaqueTypeKey<'tcx>,
) -> DiagnosticBuilder<'tcx> {
let mut err = tcx.sess.create_err(errors::OpaqueCapturesLifetime {
let mut err = tcx.dcx().create_err(errors::OpaqueCapturesLifetime {
span,
opaque_ty: Ty::new_opaque(tcx, opaque_ty_key.def_id.to_def_id(), opaque_ty_key.args),
opaque_ty_span: tcx.def_span(opaque_ty_key.def_id),
@ -516,7 +522,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
self.tcx
.sess
.dcx()
.span_delayed_bug(self.tcx.def_span(generic_param_scope), "expected region errors")
}
@ -2180,7 +2186,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
span,
self.type_error_additional_suggestions(&trace, terr),
);
let mut diag = self.tcx.sess.create_err(failure_code);
let mut diag = self.tcx.dcx().create_err(failure_code);
self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr, false, false);
diag
}
@ -2346,7 +2352,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
},
};
let mut err = self.tcx.sess.struct_span_err_with_code(
let mut err = self.tcx.dcx().struct_span_err_with_code(
span,
format!("{labeled_user_string} may not live long enough"),
match sub.kind() {
@ -2771,7 +2777,7 @@ impl<'tcx> InferCtxt<'tcx> {
};
struct_span_err!(
self.tcx.sess,
self.tcx.dcx(),
var_origin.span(),
E0495,
"cannot infer an appropriate lifetime{} due to conflicting requirements",

View file

@ -366,7 +366,7 @@ impl<'tcx> InferCtxt<'tcx> {
let multi_suggestions = Vec::new();
let bad_label = Some(arg_data.make_bad_error(span));
match error_code {
TypeAnnotationNeeded::E0282 => self.tcx.sess.dcx().create_err(AnnotationRequired {
TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
span,
source_kind,
source_name,
@ -375,7 +375,7 @@ impl<'tcx> InferCtxt<'tcx> {
multi_suggestions,
bad_label,
}),
TypeAnnotationNeeded::E0283 => self.tcx.sess.dcx().create_err(AmbiguousImpl {
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
span,
source_kind,
source_name,
@ -384,7 +384,7 @@ impl<'tcx> InferCtxt<'tcx> {
multi_suggestions,
bad_label,
}),
TypeAnnotationNeeded::E0284 => self.tcx.sess.dcx().create_err(AmbiguousReturn {
TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
span,
source_kind,
source_name,
@ -570,7 +570,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
match error_code {
TypeAnnotationNeeded::E0282 => self.tcx.sess.dcx().create_err(AnnotationRequired {
TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
span,
source_kind,
source_name: &name,
@ -579,7 +579,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
multi_suggestions,
bad_label: None,
}),
TypeAnnotationNeeded::E0283 => self.tcx.sess.dcx().create_err(AmbiguousImpl {
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
span,
source_kind,
source_name: &name,
@ -588,7 +588,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
multi_suggestions,
bad_label: None,
}),
TypeAnnotationNeeded::E0284 => self.tcx.sess.dcx().create_err(AmbiguousReturn {
TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
span,
source_kind,
source_name: &name,

View file

@ -130,7 +130,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let suggestion =
AddLifetimeParamsSuggestion { tcx: self.tcx(), sub, ty_sup, ty_sub, add_note: true };
let err = LifetimeMismatch { span, labels, suggestion };
let reported = self.tcx().sess.emit_err(err);
let reported = self.tcx().dcx().emit_err(err);
Some(reported)
}
}

View file

@ -116,7 +116,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
.unwrap_or(DoesNotOutliveStaticFromImpl::Unspanned),
implicit_static_lifetimes,
};
let reported = self.tcx().sess.emit_err(err);
let reported = self.tcx().dcx().emit_err(err);
Some(reported)
}
}

View file

@ -331,7 +331,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
leading_ellipsis,
);
self.tcx().sess.create_err(TraitPlaceholderMismatch {
self.tcx().dcx().create_err(TraitPlaceholderMismatch {
span,
satisfy_span,
where_span,

View file

@ -81,7 +81,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
}
_ => PlaceholderRelationLfNotSatisfied::OnlyPrimarySpan { span, note: () },
};
Some(self.tcx().sess.create_err(diag))
Some(self.tcx().dcx().create_err(diag))
}
_ => None,
}

View file

@ -67,7 +67,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
AssocItemContainer::ImplContainer => (false, String::new()),
};
let mut err = self.tcx().sess.create_err(ButCallingIntroduces {
let mut err = self.tcx().dcx().create_err(ButCallingIntroduces {
param_ty_span: param.param_ty_span,
cause_span: cause.span,
has_param_name: simple_ident.is_some(),
@ -195,7 +195,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
bound,
};
let mut err = self.tcx().sess.create_err(diag);
let mut err = self.tcx().dcx().create_err(diag);
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.def_id);

View file

@ -121,7 +121,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
found,
};
self.tcx().sess.emit_err(diag)
self.tcx().dcx().emit_err(diag)
}
}

View file

@ -134,7 +134,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::ContentValidFor,
note_and_explain::SuffixKind::Empty,
);
self.tcx.sess.dcx().create_err(OutlivesContent {
self.dcx().create_err(OutlivesContent {
span,
notes: reference_valid.into_iter().chain(content_valid).collect(),
})
@ -154,7 +154,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::SourcePointerValidFor,
note_and_explain::SuffixKind::Empty,
);
self.tcx.sess.dcx().create_err(OutlivesBound {
self.dcx().create_err(OutlivesBound {
span,
notes: object_valid.into_iter().chain(pointer_valid).collect(),
})
@ -172,7 +172,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let note = note_and_explain::RegionExplanation::new(
self.tcx, sub, opt_span, prefix, suffix,
);
self.tcx.sess.dcx().create_err(FulfillReqLifetime {
self.dcx().create_err(FulfillReqLifetime {
span,
ty: self.resolve_vars_if_possible(ty),
note,
@ -193,7 +193,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::LfParamMustOutlive,
note_and_explain::SuffixKind::Empty,
);
self.tcx.sess.dcx().create_err(LfBoundNotSatisfied {
self.dcx().create_err(LfBoundNotSatisfied {
span,
notes: param_instantiated.into_iter().chain(param_must_outlive).collect(),
})
@ -213,7 +213,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::DataValidFor,
note_and_explain::SuffixKind::Empty,
);
self.tcx.sess.dcx().create_err(RefLongerThanData {
self.dcx().create_err(RefLongerThanData {
span,
ty: self.resolve_vars_if_possible(ty),
notes: pointer_valid.into_iter().chain(data_valid).collect(),
@ -274,7 +274,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::LfMustOutlive,
note_and_explain::SuffixKind::Empty,
);
self.tcx.sess.dcx().create_err(LfBoundNotSatisfied {
self.dcx().create_err(LfBoundNotSatisfied {
span,
notes: instantiated.into_iter().chain(must_outlive).collect(),
})

View file

@ -800,7 +800,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
// Errors in earlier passes can yield error variables without
// resolution errors here; delay ICE in favor of those errors.
self.tcx().sess.span_delayed_bug(
self.tcx().dcx().span_delayed_bug(
self.var_infos[node_idx].origin.span(),
format!(
"collect_error_for_expanding_node() could not find \

View file

@ -20,7 +20,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::undo_log::Rollback;
use rustc_data_structures::unify as ut;
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_errors::{DiagCtxt, DiagnosticBuilder, ErrorGuaranteed};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVarValue};
@ -739,6 +739,10 @@ pub struct CombinedSnapshot<'tcx> {
}
impl<'tcx> InferCtxt<'tcx> {
pub fn dcx(&self) -> &'tcx DiagCtxt {
self.tcx.dcx()
}
pub fn next_trait_solver(&self) -> bool {
self.next_trait_solver
}
@ -1432,7 +1436,7 @@ impl<'tcx> InferCtxt<'tcx> {
if value.has_infer_regions() {
let guar = self
.tcx
.sess
.dcx()
.span_delayed_bug(DUMMY_SP, format!("`{value:?}` is not fully resolved"));
Ok(self.tcx.fold_regions(value, |re, _| {
if re.is_var() { ty::Region::new_error(self.tcx, guar) } else { re }

View file

@ -157,7 +157,7 @@ impl<'tcx> InferCtxt<'tcx> {
if let Some(OpaqueTyOrigin::TyAlias { .. }) =
b_def_id.as_local().and_then(|b_def_id| self.opaque_type_origin(b_def_id))
{
self.tcx.sess.emit_err(OpaqueHiddenTypeDiag {
self.tcx.dcx().emit_err(OpaqueHiddenTypeDiag {
span: cause.span,
hidden_type: self.tcx.def_span(b_def_id),
opaque_type: self.tcx.def_span(def_id),

View file

@ -41,7 +41,7 @@ impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
fn drop(&mut self) {
if !self.opaque_types.is_empty() {
ty::tls::with(|tcx| {
tcx.sess.span_delayed_bug(DUMMY_SP, format!("{:?}", self.opaque_types))
tcx.dcx().span_delayed_bug(DUMMY_SP, format!("{:?}", self.opaque_types))
});
}
}

View file

@ -254,7 +254,7 @@ where
// ignore this, we presume it will yield an error
// later, since if a type variable is not resolved by
// this point it never will be
self.tcx.sess.span_delayed_bug(
self.tcx.dcx().span_delayed_bug(
origin.span(),
format!("unresolved inference variable in outlives: {v:?}"),
);

View file

@ -175,7 +175,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
// ignore this, we presume it will yield an error
// later, since if a type variable is not resolved by
// this point it never will be
self.tcx.sess.span_delayed_bug(
self.tcx.dcx().span_delayed_bug(
rustc_span::DUMMY_SP,
format!("unresolved inference variable in outlives: {v:?}"),
);

View file

@ -180,7 +180,7 @@ impl<'tcx> InferCtxt<'tcx> {
&mut OriginalQueryValues::default(),
);
self.tcx.check_tys_might_be_eq(canonical).map_err(|_| {
self.tcx.sess.span_delayed_bug(
self.tcx.dcx().span_delayed_bug(
DUMMY_SP,
format!("cannot relate consts of different types (a={a:?}, b={b:?})",),
)

View file

@ -496,7 +496,7 @@ where
// shouldn't ever fail. Instead, it unconditionally emits an
// alias-relate goal.
assert!(!self.infcx.next_trait_solver());
self.tcx().sess.span_delayed_bug(
self.tcx().dcx().span_delayed_bug(
self.delegate.span(),
"failure to relate an opaque to itself should result in an error later on",
);
@ -554,7 +554,7 @@ where
match b.kind() {
ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
// Forbid inference variables in the RHS.
self.infcx.tcx.sess.span_delayed_bug(
self.infcx.dcx().span_delayed_bug(
self.delegate.span(),
format!("unexpected inference var {b:?}",),
);

View file

@ -20,7 +20,7 @@ impl<'tcx> InferCtxt<'tcx> {
requirement: &dyn fmt::Display,
) -> DiagnosticBuilder<'tcx> {
let mut err = struct_span_err!(
self.tcx.sess,
self.tcx.dcx(),
error_span,
E0276,
"impl has stricter requirements than trait"
@ -51,7 +51,7 @@ pub fn report_object_safety_error<'tcx>(
_ => None,
});
let mut err = struct_span_err!(
tcx.sess,
tcx.dcx(),
span,
E0038,
"the trait `{}` cannot be made into an object",