use crate::infer::{SubregionOrigin, TypeTrace};
use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
use rustc_data_structures::stable_set::FxHashSet;
-use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
+use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{walk_ty, Visitor};
use rustc_hir::{self as hir, GenericBound, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind};
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// Print the error message for lifetime errors when the return type is a static `impl Trait`,
/// `dyn Trait` or if a method call on a trait object introduces a static requirement.
- pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> {
+ pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorGuaranteed> {
debug!("try_report_static_impl_trait(error={:?})", self.error);
let tcx = self.tcx();
let (var_origin, sub_origin, sub_r, sup_origin, sup_r, spans) = match self.error.as_ref()? {
),
);
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt) {
- err.emit();
- return Some(ErrorReported);
+ let reported = err.emit();
+ return Some(reported);
} else {
err.cancel();
}
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.def_id);
let mut override_error_code = None;
- if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin {
- if let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code() {
- // Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
- // `'static` lifetime when called as a method on a binding: `bar.qux()`.
- if self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt) {
- override_error_code = Some(ctxt.assoc_item.name);
- }
- }
+ if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin
+ && let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code()
+ // Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
+ // `'static` lifetime when called as a method on a binding: `bar.qux()`.
+ && self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt)
+ {
+ override_error_code = Some(ctxt.assoc_item.name);
}
- if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sub_origin {
- let code = match cause.code() {
+
+ if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sub_origin
+ && let code = match cause.code() {
ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
_ => cause.code(),
- };
- if let (ObligationCauseCode::ItemObligation(item_def_id), None) =
- (code, override_error_code)
+ }
+ && let (&ObligationCauseCode::ItemObligation(item_def_id), None) = (code, override_error_code)
+ {
+ // Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
+ // lifetime as above, but called using a fully-qualified path to the method:
+ // `Foo::qux(bar)`.
+ let mut v = TraitObjectVisitor(FxHashSet::default());
+ v.visit_ty(param.param_ty);
+ if let Some((ident, self_ty)) =
+ self.get_impl_ident_and_self_ty_from_trait(item_def_id, &v.0)
+ && self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty)
{
- // Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
- // lifetime as above, but called using a fully-qualified path to the method:
- // `Foo::qux(bar)`.
- let mut v = TraitObjectVisitor(FxHashSet::default());
- v.visit_ty(param.param_ty);
- if let Some((ident, self_ty)) =
- self.get_impl_ident_and_self_ty_from_trait(*item_def_id, &v.0)
- {
- if self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty) {
- override_error_code = Some(ident.name);
- }
- }
+ override_error_code = Some(ident.name);
}
}
if let (Some(ident), true) = (override_error_code, fn_returns.is_empty()) {
Some((param.param_ty_span, param.param_ty.to_string())),
);
- err.emit();
- Some(ErrorReported)
+ let reported = err.emit();
+ Some(reported)
}
}
pub fn suggest_new_region_bound(
tcx: TyCtxt<'_>,
- err: &mut DiagnosticBuilder<'_>,
+ err: &mut Diagnostic,
fn_returns: Vec<&rustc_hir::Ty<'_>>,
lifetime_name: String,
arg: Option<String>,
/// `'static` obligation. Suggest relaxing that implicit bound.
fn find_impl_on_dyn_trait(
&self,
- err: &mut DiagnosticBuilder<'_>,
+ err: &mut Diagnostic,
ty: Ty<'_>,
ctxt: &UnifyReceiverContext<'tcx>,
) -> bool {
let tcx = self.tcx();
// Find the method being called.
- let instance = match ty::Instance::resolve(
+ let Ok(Some(instance)) = ty::Instance::resolve(
tcx,
ctxt.param_env,
ctxt.assoc_item.def_id,
self.infcx.resolve_vars_if_possible(ctxt.substs),
- ) {
- Ok(Some(instance)) => instance,
- _ => return false,
+ ) else {
+ return false;
};
let mut v = TraitObjectVisitor(FxHashSet::default());
// Get the `Ident` of the method being called and the corresponding `impl` (to point at
// `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
- let (ident, self_ty) =
- match self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &v.0) {
- Some((ident, self_ty)) => (ident, self_ty),
- None => return false,
- };
+ let Some((ident, self_ty)) = self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &v.0) else {
+ return false;
+ };
// Find the trait object types in the argument, so we point at *only* the trait object.
self.suggest_constrain_dyn_trait_in_impl(err, &v.0, ident, self_ty)
fn suggest_constrain_dyn_trait_in_impl(
&self,
- err: &mut DiagnosticBuilder<'_>,
+ err: &mut Diagnostic,
found_dids: &FxHashSet<DefId>,
ident: Ident,
self_ty: &hir::Ty<'_>,