Rollup merge of #137183 - compiler-errors:dead-regionck-code, r=lcnr
Prune dead regionck code We never encounter `ObligationCauseCode`s that correspond to region obligations that originate from "within" a body, since we don't do HIR regionck anymore on bodies. So prune some dead code.
This commit is contained in:
commit
72535fec55
7 changed files with 9 additions and 241 deletions
|
@ -11,7 +11,7 @@ use rustc_hir_analysis::hir_ty_lowering::{
|
|||
};
|
||||
use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk};
|
||||
use rustc_lint::builtin::SUPERTRAIT_ITEM_SHADOWING_USAGE;
|
||||
use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
|
||||
use rustc_middle::traits::ObligationCauseCode;
|
||||
use rustc_middle::ty::adjustment::{
|
||||
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
|
||||
};
|
||||
|
@ -136,7 +136,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
|
|||
"confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}",
|
||||
self_ty, method_sig_rcvr, method_sig, method_predicates
|
||||
);
|
||||
self.unify_receivers(self_ty, method_sig_rcvr, pick, all_args);
|
||||
self.unify_receivers(self_ty, method_sig_rcvr, pick);
|
||||
|
||||
let (method_sig, method_predicates) =
|
||||
self.normalize(self.span, (method_sig, method_predicates));
|
||||
|
@ -525,20 +525,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
|
|||
self_ty: Ty<'tcx>,
|
||||
method_self_ty: Ty<'tcx>,
|
||||
pick: &probe::Pick<'tcx>,
|
||||
args: GenericArgsRef<'tcx>,
|
||||
) {
|
||||
debug!(
|
||||
"unify_receivers: self_ty={:?} method_self_ty={:?} span={:?} pick={:?}",
|
||||
self_ty, method_self_ty, self.span, pick
|
||||
);
|
||||
let cause = self.cause(
|
||||
self.self_expr.span,
|
||||
ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext {
|
||||
assoc_item: pick.item,
|
||||
param_env: self.param_env,
|
||||
args,
|
||||
})),
|
||||
);
|
||||
let cause = self.cause(self.self_expr.span, ObligationCauseCode::Misc);
|
||||
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) {
|
||||
Ok(InferOk { obligations, value: () }) => {
|
||||
self.register_predicates(obligations);
|
||||
|
|
|
@ -144,14 +144,6 @@ impl<'tcx> ObligationCause<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
#[derive(TypeVisitable, TypeFoldable)]
|
||||
pub struct UnifyReceiverContext<'tcx> {
|
||||
pub assoc_item: ty::AssocItem,
|
||||
pub param_env: ty::ParamEnv<'tcx>,
|
||||
pub args: GenericArgsRef<'tcx>,
|
||||
}
|
||||
|
||||
/// A compact form of `ObligationCauseCode`.
|
||||
#[derive(Clone, PartialEq, Eq, Default, HashStable)]
|
||||
#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
|
||||
|
@ -360,8 +352,6 @@ pub enum ObligationCauseCode<'tcx> {
|
|||
/// Method receiver
|
||||
MethodReceiver,
|
||||
|
||||
UnifyReceiver(Box<UnifyReceiverContext<'tcx>>),
|
||||
|
||||
/// `return` with no expression
|
||||
ReturnNoExpression,
|
||||
|
||||
|
|
|
@ -225,14 +225,6 @@ trait_selection_mismatched_static_lifetime = incompatible lifetime on type
|
|||
trait_selection_missing_options_for_on_unimplemented_attr = missing options for `on_unimplemented` attribute
|
||||
.help = at least one of the `message`, `note` and `label` options are expected
|
||||
|
||||
trait_selection_more_targeted = {$has_param_name ->
|
||||
[true] `{$param_name}`
|
||||
*[false] `fn` parameter
|
||||
} has {$has_lifetime ->
|
||||
[true] lifetime `{$lifetime}`
|
||||
*[false] an anonymous lifetime `'_`
|
||||
} but calling `{$ident}` introduces an implicit `'static` lifetime requirement
|
||||
|
||||
trait_selection_msl_introduces_static = introduces a `'static` lifetime requirement
|
||||
trait_selection_msl_unmet_req = because this has an unmet lifetime requirement
|
||||
|
||||
|
|
|
@ -33,11 +33,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
};
|
||||
// If we added a "points at argument expression" obligation, we remove it here, we care
|
||||
// about the original obligation only.
|
||||
let code = match cause.code() {
|
||||
ObligationCauseCode::FunctionArg { parent_code, .. } => &*parent_code,
|
||||
code => code,
|
||||
};
|
||||
let ObligationCauseCode::MatchImpl(parent, impl_def_id) = code else {
|
||||
let ObligationCauseCode::MatchImpl(parent, impl_def_id) = cause.code() else {
|
||||
return None;
|
||||
};
|
||||
let (ObligationCauseCode::WhereClause(_, binding_span)
|
||||
|
|
|
@ -1,27 +1,21 @@
|
|||
//! Error Reporting for static impl Traits.
|
||||
|
||||
use rustc_data_structures::fx::FxIndexSet;
|
||||
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, Subdiagnostic};
|
||||
use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::intravisit::{Visitor, VisitorExt, walk_ty};
|
||||
use rustc_hir::{
|
||||
self as hir, AmbigArg, GenericBound, GenericParam, GenericParamKind, Item, ItemKind, Lifetime,
|
||||
LifetimeName, LifetimeParamKind, MissingLifetimeKind, Node, TyKind,
|
||||
};
|
||||
use rustc_middle::ty::{
|
||||
self, AssocItemContainer, StaticLifetimeVisitor, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor,
|
||||
};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::{Ident, Span};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::error_reporting::infer::nice_region_error::NiceRegionError;
|
||||
use crate::errors::{
|
||||
ButCallingIntroduces, ButNeedsToSatisfy, DynTraitConstraintSuggestion, MoreTargeted,
|
||||
ReqIntroducedLocations,
|
||||
};
|
||||
use crate::infer::{RegionResolutionError, SubregionOrigin, TypeTrace};
|
||||
use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
|
||||
use crate::errors::ButNeedsToSatisfy;
|
||||
use crate::infer::{RegionResolutionError, SubregionOrigin};
|
||||
|
||||
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
||||
/// Print the error message for lifetime errors when the return type is a static `impl Trait`,
|
||||
|
@ -39,52 +33,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
sup_r,
|
||||
spans,
|
||||
) if sub_r.is_static() => (var_origin, sub_origin, sub_r, sup_origin, sup_r, spans),
|
||||
RegionResolutionError::ConcreteFailure(
|
||||
SubregionOrigin::Subtype(box TypeTrace { cause, .. }),
|
||||
sub_r,
|
||||
sup_r,
|
||||
) if sub_r.is_static() => {
|
||||
// This is for an implicit `'static` requirement coming from `impl dyn Trait {}`.
|
||||
if let ObligationCauseCode::UnifyReceiver(ctxt) = cause.code() {
|
||||
// This may have a closure and it would cause ICE
|
||||
// through `find_param_with_region` (#78262).
|
||||
let anon_reg_sup = tcx.is_suitable_region(self.generic_param_scope, *sup_r)?;
|
||||
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
|
||||
if fn_returns.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let param = self.find_param_with_region(*sup_r, *sub_r)?;
|
||||
let simple_ident = param.param.pat.simple_ident();
|
||||
|
||||
let (has_impl_path, impl_path) = match ctxt.assoc_item.container {
|
||||
AssocItemContainer::Trait => {
|
||||
let id = ctxt.assoc_item.container_id(tcx);
|
||||
(true, tcx.def_path_str(id))
|
||||
}
|
||||
AssocItemContainer::Impl => (false, String::new()),
|
||||
};
|
||||
|
||||
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(),
|
||||
param_name: simple_ident.map(|x| x.to_string()).unwrap_or_default(),
|
||||
has_lifetime: sup_r.has_name(),
|
||||
lifetime: sup_r.to_string(),
|
||||
assoc_item: ctxt.assoc_item.name,
|
||||
has_impl_path,
|
||||
impl_path,
|
||||
});
|
||||
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, ctxt) {
|
||||
let reported = err.emit();
|
||||
return Some(reported);
|
||||
} else {
|
||||
err.cancel()
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
debug!(
|
||||
|
@ -140,39 +88,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
None
|
||||
};
|
||||
|
||||
let mut subdiag = None;
|
||||
|
||||
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = sub_origin {
|
||||
if let ObligationCauseCode::ReturnValue(hir_id)
|
||||
| ObligationCauseCode::BlockTailExpression(hir_id, ..) = cause.code()
|
||||
{
|
||||
let parent_id = tcx.hir_get_parent_item(*hir_id);
|
||||
if let Some(fn_decl) = tcx.hir_fn_decl_by_hir_id(parent_id.into()) {
|
||||
let mut span: MultiSpan = fn_decl.output.span().into();
|
||||
let mut spans = Vec::new();
|
||||
let mut add_label = true;
|
||||
if let hir::FnRetTy::Return(ty) = fn_decl.output {
|
||||
let mut v = StaticLifetimeVisitor(vec![], tcx.hir());
|
||||
v.visit_ty_unambig(ty);
|
||||
if !v.0.is_empty() {
|
||||
span = v.0.clone().into();
|
||||
spans = v.0;
|
||||
add_label = false;
|
||||
}
|
||||
}
|
||||
let fn_decl_span = fn_decl.output.span();
|
||||
|
||||
subdiag = Some(ReqIntroducedLocations {
|
||||
span,
|
||||
spans,
|
||||
fn_decl_span,
|
||||
cause_span: cause.span,
|
||||
add_label,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let diag = ButNeedsToSatisfy {
|
||||
sp,
|
||||
influencer_point,
|
||||
|
@ -183,7 +98,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
require_span_as_note: require_as_note.then_some(require_span),
|
||||
// We don't need a note, it's already at the end, it can be shown as a `span_label`.
|
||||
require_span_as_label: (!require_as_note).then_some(require_span),
|
||||
req_introduces_loc: subdiag,
|
||||
|
||||
has_lifetime: sup_r.has_name(),
|
||||
lifetime: lifetime_name.clone(),
|
||||
|
@ -197,45 +111,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
|
||||
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.scope);
|
||||
|
||||
let mut override_error_code = None;
|
||||
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() {
|
||||
ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
|
||||
_ => cause.code(),
|
||||
}
|
||||
&& let (
|
||||
&ObligationCauseCode::WhereClause(item_def_id, _)
|
||||
| &ObligationCauseCode::WhereClauseInExpr(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(FxIndexSet::default());
|
||||
v.visit_ty(param.param_ty);
|
||||
if let Some((ident, self_ty)) =
|
||||
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, item_def_id, &v.0)
|
||||
&& self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty)
|
||||
{
|
||||
override_error_code = Some(ident.name);
|
||||
}
|
||||
}
|
||||
if let (Some(ident), true) = (override_error_code, fn_returns.is_empty()) {
|
||||
// Provide a more targeted error code and description.
|
||||
let retarget_subdiag = MoreTargeted { ident };
|
||||
retarget_subdiag.add_to_diag(&mut err);
|
||||
}
|
||||
|
||||
let arg = match param.param.pat.simple_ident() {
|
||||
Some(simple_ident) => format!("argument `{simple_ident}`"),
|
||||
None => "the argument".to_string(),
|
||||
|
@ -495,8 +370,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
|
||||
}) = tcx.hir_node_by_def_id(impl_did)
|
||||
&& trait_objects.iter().all(|did| {
|
||||
// FIXME: we should check `self_ty` against the receiver
|
||||
// type in the `UnifyReceiver` context, but for now, use
|
||||
// FIXME: we should check `self_ty`, but for now, use
|
||||
// this imperfect proxy. This will fail if there are
|
||||
// multiple `impl`s for the same trait like
|
||||
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
|
||||
|
@ -516,62 +390,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// When we call a method coming from an `impl Foo for dyn Bar`, `dyn Bar` introduces a default
|
||||
/// `'static` obligation. Suggest relaxing that implicit bound.
|
||||
fn find_impl_on_dyn_trait(
|
||||
&self,
|
||||
err: &mut Diag<'_>,
|
||||
ty: Ty<'_>,
|
||||
ctxt: &UnifyReceiverContext<'tcx>,
|
||||
) -> bool {
|
||||
let tcx = self.tcx();
|
||||
|
||||
// Find the method being called.
|
||||
let Ok(Some(instance)) = ty::Instance::try_resolve(
|
||||
tcx,
|
||||
self.cx.typing_env(ctxt.param_env),
|
||||
ctxt.assoc_item.def_id,
|
||||
self.cx.resolve_vars_if_possible(ctxt.args),
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
let mut v = TraitObjectVisitor(FxIndexSet::default());
|
||||
v.visit_ty(ty);
|
||||
|
||||
// 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 Some((ident, self_ty)) =
|
||||
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, 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 Diag<'_>,
|
||||
found_dids: &FxIndexSet<DefId>,
|
||||
ident: Ident,
|
||||
self_ty: &hir::Ty<'_>,
|
||||
) -> bool {
|
||||
let mut suggested = false;
|
||||
for found_did in found_dids {
|
||||
let mut traits = vec![];
|
||||
let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
|
||||
hir_v.visit_ty_unambig(self_ty);
|
||||
for &span in &traits {
|
||||
let subdiag = DynTraitConstraintSuggestion { span, ident };
|
||||
subdiag.add_to_diag(err);
|
||||
suggested = true;
|
||||
}
|
||||
}
|
||||
suggested
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect all the trait objects in a type that could have received an implicit `'static` lifetime.
|
||||
|
|
|
@ -2680,7 +2680,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
|||
| ObligationCauseCode::IntrinsicType
|
||||
| ObligationCauseCode::MethodReceiver
|
||||
| ObligationCauseCode::ReturnNoExpression
|
||||
| ObligationCauseCode::UnifyReceiver(..)
|
||||
| ObligationCauseCode::Misc
|
||||
| ObligationCauseCode::WellFormed(..)
|
||||
| ObligationCauseCode::MatchImpl(..)
|
||||
|
|
|
@ -1119,22 +1119,6 @@ impl Subdiagnostic for ReqIntroducedLocations {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct MoreTargeted {
|
||||
pub ident: Symbol,
|
||||
}
|
||||
|
||||
impl Subdiagnostic for MoreTargeted {
|
||||
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
|
||||
self,
|
||||
diag: &mut Diag<'_, G>,
|
||||
_f: &F,
|
||||
) {
|
||||
diag.code(E0772);
|
||||
diag.primary_message(fluent::trait_selection_more_targeted);
|
||||
diag.arg("ident", self.ident);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(trait_selection_but_needs_to_satisfy, code = E0759)]
|
||||
pub struct ButNeedsToSatisfy {
|
||||
|
@ -1151,9 +1135,6 @@ pub struct ButNeedsToSatisfy {
|
|||
#[note(trait_selection_introduced_by_bound)]
|
||||
pub bound: Option<Span>,
|
||||
|
||||
#[subdiagnostic]
|
||||
pub req_introduces_loc: Option<ReqIntroducedLocations>,
|
||||
|
||||
pub has_param_name: bool,
|
||||
pub param_name: String,
|
||||
pub spans_empty: bool,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue