2020-06-20 10:29:13 +00:00
|
|
|
use rustc_infer::infer::InferCtxt;
|
2024-10-09 00:02:55 +00:00
|
|
|
use rustc_infer::traits::PredicateObligations;
|
2022-11-17 11:21:39 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
|
2022-03-20 19:02:18 +00:00
|
|
|
use rustc_session::Limit;
|
2020-06-20 10:29:13 +00:00
|
|
|
use rustc_span::Span;
|
2021-05-10 16:23:32 +00:00
|
|
|
use rustc_span::def_id::{LOCAL_CRATE, LocalDefId};
|
2024-05-01 21:22:39 +00:00
|
|
|
use rustc_trait_selection::traits::ObligationCtxt;
|
2024-08-29 23:02:58 +00:00
|
|
|
use tracing::{debug, instrument};
|
2020-06-20 10:29:13 +00:00
|
|
|
|
2022-08-26 18:08:58 +00:00
|
|
|
use crate::errors::AutoDerefReachedRecursionLimit;
|
2024-05-01 21:22:39 +00:00
|
|
|
use crate::traits;
|
2020-06-20 10:29:13 +00:00
|
|
|
use crate::traits::query::evaluate_obligation::InferCtxtExt;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2020-06-20 10:29:13 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum AutoderefKind {
|
2024-01-23 15:10:23 +00:00
|
|
|
/// A true pointer type, such as `&T` and `*mut T`.
|
2020-06-20 10:29:13 +00:00
|
|
|
Builtin,
|
2024-01-23 15:10:23 +00:00
|
|
|
/// A type which must dispatch to a `Deref` implementation.
|
2020-06-20 10:29:13 +00:00
|
|
|
Overloaded,
|
|
|
|
}
|
|
|
|
struct AutoderefSnapshot<'tcx> {
|
|
|
|
at_start: bool,
|
|
|
|
reached_recursion_limit: bool,
|
|
|
|
steps: Vec<(Ty<'tcx>, AutoderefKind)>,
|
|
|
|
cur_ty: Ty<'tcx>,
|
2024-10-09 00:02:55 +00:00
|
|
|
obligations: PredicateObligations<'tcx>,
|
2020-06-20 10:29:13 +00:00
|
|
|
}
|
|
|
|
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
/// Recursively dereference a type, considering both built-in
|
|
|
|
/// dereferences (`*`) and the `Deref` trait.
|
|
|
|
/// Although called `Autoderef` it can be configured to use the
|
|
|
|
/// `Receiver` trait instead of the `Deref` trait.
|
2020-06-20 10:29:13 +00:00
|
|
|
pub struct Autoderef<'a, 'tcx> {
|
|
|
|
// Meta infos:
|
2022-09-09 18:01:06 +00:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2020-06-20 10:29:13 +00:00
|
|
|
span: Span,
|
2023-01-15 11:58:46 +00:00
|
|
|
body_id: LocalDefId,
|
2020-06-20 10:29:13 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
|
|
|
// Current state:
|
|
|
|
state: AutoderefSnapshot<'tcx>,
|
|
|
|
|
|
|
|
// Configurations:
|
|
|
|
include_raw_pointers: bool,
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
use_receiver_trait: bool,
|
2020-06-20 10:29:13 +00:00
|
|
|
silence_errors: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Iterator for Autoderef<'a, 'tcx> {
|
|
|
|
type Item = (Ty<'tcx>, usize);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
|
|
|
|
debug!("autoderef: steps={:?}, cur_ty={:?}", self.state.steps, self.state.cur_ty);
|
|
|
|
if self.state.at_start {
|
|
|
|
self.state.at_start = false;
|
|
|
|
debug!("autoderef stage #0 is {:?}", self.state.cur_ty);
|
|
|
|
return Some((self.state.cur_ty, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have reached the recursion limit, error gracefully.
|
2021-07-04 18:02:51 +00:00
|
|
|
if !tcx.recursion_limit().value_within_limit(self.state.steps.len()) {
|
2020-06-20 10:29:13 +00:00
|
|
|
if !self.silence_errors {
|
|
|
|
report_autoderef_recursion_limit_error(tcx, self.span, self.state.cur_ty);
|
|
|
|
}
|
|
|
|
self.state.reached_recursion_limit = true;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.state.cur_ty.is_ty_var() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, deref if type is derefable:
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
// NOTE: in the case of self.use_receiver_trait = true, you might think it would
|
|
|
|
// be better to skip this clause and use the Overloaded case only, since &T
|
|
|
|
// and &mut T implement Receiver. But built-in derefs apply equally to Receiver
|
|
|
|
// and Deref, and this has benefits for const and the emitted MIR.
|
2024-05-10 02:45:14 +00:00
|
|
|
let (kind, new_ty) =
|
|
|
|
if let Some(ty) = self.state.cur_ty.builtin_deref(self.include_raw_pointers) {
|
|
|
|
debug_assert_eq!(ty, self.infcx.resolve_vars_if_possible(ty));
|
|
|
|
// NOTE: we may still need to normalize the built-in deref in case
|
|
|
|
// we have some type like `&<Ty as Trait>::Assoc`, since users of
|
|
|
|
// autoderef expect this type to have been structurally normalized.
|
|
|
|
if self.infcx.next_trait_solver()
|
|
|
|
&& let ty::Alias(..) = ty.kind()
|
|
|
|
{
|
|
|
|
let (normalized_ty, obligations) = self.structurally_normalize(ty)?;
|
|
|
|
self.state.obligations.extend(obligations);
|
|
|
|
(AutoderefKind::Builtin, normalized_ty)
|
|
|
|
} else {
|
|
|
|
(AutoderefKind::Builtin, ty)
|
|
|
|
}
|
|
|
|
} else if let Some(ty) = self.overloaded_deref_ty(self.state.cur_ty) {
|
|
|
|
// The overloaded deref check already normalizes the pointee type.
|
|
|
|
(AutoderefKind::Overloaded, ty)
|
2020-06-20 10:29:13 +00:00
|
|
|
} else {
|
2024-05-10 02:45:14 +00:00
|
|
|
return None;
|
|
|
|
};
|
2020-06-20 10:29:13 +00:00
|
|
|
|
|
|
|
self.state.steps.push((self.state.cur_ty, kind));
|
|
|
|
debug!(
|
|
|
|
"autoderef stage #{:?} is {:?} from {:?}",
|
|
|
|
self.step_count(),
|
|
|
|
new_ty,
|
|
|
|
(self.state.cur_ty, kind)
|
|
|
|
);
|
|
|
|
self.state.cur_ty = new_ty;
|
|
|
|
|
|
|
|
Some((self.state.cur_ty, self.step_count()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Autoderef<'a, 'tcx> {
|
|
|
|
pub fn new(
|
2022-09-09 18:01:06 +00:00
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
2020-06-20 10:29:13 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2023-01-15 11:58:46 +00:00
|
|
|
body_def_id: LocalDefId,
|
2020-06-20 10:29:13 +00:00
|
|
|
span: Span,
|
|
|
|
base_ty: Ty<'tcx>,
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
) -> Self {
|
2020-06-20 10:29:13 +00:00
|
|
|
Autoderef {
|
|
|
|
infcx,
|
|
|
|
span,
|
2023-01-15 11:58:46 +00:00
|
|
|
body_id: body_def_id,
|
2020-06-20 10:29:13 +00:00
|
|
|
param_env,
|
|
|
|
state: AutoderefSnapshot {
|
|
|
|
steps: vec![],
|
2020-10-24 00:21:18 +00:00
|
|
|
cur_ty: infcx.resolve_vars_if_possible(base_ty),
|
2024-10-09 00:02:55 +00:00
|
|
|
obligations: PredicateObligations::new(),
|
2020-06-20 10:29:13 +00:00
|
|
|
at_start: true,
|
|
|
|
reached_recursion_limit: false,
|
|
|
|
},
|
|
|
|
include_raw_pointers: false,
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
use_receiver_trait: false,
|
2020-06-20 10:29:13 +00:00
|
|
|
silence_errors: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
|
|
|
debug!("overloaded_deref_ty({:?})", ty);
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
|
2024-02-11 22:26:06 +00:00
|
|
|
if ty.references_error() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
// <ty as Deref>, or whatever the equivalent trait is that we've been asked to walk.
|
|
|
|
let (trait_def_id, trait_target_def_id) = if self.use_receiver_trait {
|
|
|
|
(tcx.lang_items().receiver_trait()?, tcx.lang_items().receiver_target()?)
|
|
|
|
} else {
|
|
|
|
(tcx.lang_items().deref_trait()?, tcx.lang_items().deref_target()?)
|
|
|
|
};
|
|
|
|
let trait_ref = ty::TraitRef::new(tcx, trait_def_id, [ty]);
|
2020-06-20 10:29:13 +00:00
|
|
|
let cause = traits::ObligationCause::misc(self.span, self.body_id);
|
|
|
|
let obligation = traits::Obligation::new(
|
2022-11-09 10:49:28 +00:00
|
|
|
tcx,
|
2020-06-20 10:29:13 +00:00
|
|
|
cause.clone(),
|
|
|
|
self.param_env,
|
2022-11-18 21:29:26 +00:00
|
|
|
ty::Binder::dummy(trait_ref),
|
2020-06-20 10:29:13 +00:00
|
|
|
);
|
|
|
|
if !self.infcx.predicate_may_hold(&obligation) {
|
|
|
|
debug!("overloaded_deref_ty: cannot match obligation");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
let (normalized_ty, obligations) =
|
|
|
|
self.structurally_normalize(Ty::new_projection(tcx, trait_target_def_id, [ty]))?;
|
2023-04-23 19:58:24 +00:00
|
|
|
debug!("overloaded_deref_ty({:?}) = ({:?}, {:?})", ty, normalized_ty, obligations);
|
|
|
|
self.state.obligations.extend(obligations);
|
|
|
|
|
|
|
|
Some(self.infcx.resolve_vars_if_possible(normalized_ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
|
|
|
pub fn structurally_normalize(
|
|
|
|
&self,
|
|
|
|
ty: Ty<'tcx>,
|
2024-10-09 00:02:55 +00:00
|
|
|
) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
|
2024-05-01 21:22:39 +00:00
|
|
|
let ocx = ObligationCtxt::new(self.infcx);
|
|
|
|
let Ok(normalized_ty) = ocx.structurally_normalize(
|
|
|
|
&traits::ObligationCause::misc(self.span, self.body_id),
|
|
|
|
self.param_env,
|
|
|
|
ty,
|
|
|
|
) else {
|
|
|
|
// We shouldn't have errors here, except for evaluate/fulfill mismatches,
|
|
|
|
// but that's not a reason for an ICE (`predicate_may_hold` is conservative
|
|
|
|
// by design).
|
|
|
|
// FIXME(-Znext-solver): This *actually* shouldn't happen then.
|
|
|
|
return None;
|
2023-04-23 19:58:24 +00:00
|
|
|
};
|
2024-05-01 21:22:39 +00:00
|
|
|
let errors = ocx.select_where_possible();
|
2021-11-08 15:35:23 +00:00
|
|
|
if !errors.is_empty() {
|
2020-06-20 10:29:13 +00:00
|
|
|
// This shouldn't happen, except for evaluate/fulfill mismatches,
|
|
|
|
// but that's not a reason for an ICE (`predicate_may_hold` is conservative
|
|
|
|
// by design).
|
2023-04-23 19:58:24 +00:00
|
|
|
debug!(?errors, "encountered errors while fulfilling");
|
2020-06-20 10:29:13 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-05-02 02:39:49 +00:00
|
|
|
Some((normalized_ty, ocx.into_pending_obligations()))
|
2020-06-20 10:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the final type we ended up with, which may be an inference
|
|
|
|
/// variable (we will resolve it first, if we want).
|
|
|
|
pub fn final_ty(&self, resolve: bool) -> Ty<'tcx> {
|
|
|
|
if resolve {
|
2020-10-24 00:21:18 +00:00
|
|
|
self.infcx.resolve_vars_if_possible(self.state.cur_ty)
|
2020-06-20 10:29:13 +00:00
|
|
|
} else {
|
|
|
|
self.state.cur_ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step_count(&self) -> usize {
|
|
|
|
self.state.steps.len()
|
|
|
|
}
|
|
|
|
|
2024-10-09 00:02:55 +00:00
|
|
|
pub fn into_obligations(self) -> PredicateObligations<'tcx> {
|
2020-06-20 10:29:13 +00:00
|
|
|
self.state.obligations
|
|
|
|
}
|
|
|
|
|
2024-10-09 00:02:55 +00:00
|
|
|
pub fn current_obligations(&self) -> PredicateObligations<'tcx> {
|
2022-12-27 00:39:36 +00:00
|
|
|
self.state.obligations.clone()
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:29:13 +00:00
|
|
|
pub fn steps(&self) -> &[(Ty<'tcx>, AutoderefKind)] {
|
|
|
|
&self.state.steps
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span(&self) -> Span {
|
2020-08-07 22:27:46 +00:00
|
|
|
self.span
|
2020-07-25 11:04:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 10:29:13 +00:00
|
|
|
pub fn reached_recursion_limit(&self) -> bool {
|
|
|
|
self.state.reached_recursion_limit
|
|
|
|
}
|
|
|
|
|
|
|
|
/// also dereference through raw pointer types
|
|
|
|
/// e.g., assuming ptr_to_Foo is the type `*const Foo`
|
|
|
|
/// fcx.autoderef(span, ptr_to_Foo) => [*const Foo]
|
|
|
|
/// fcx.autoderef(span, ptr_to_Foo).include_raw_ptrs() => [*const Foo, Foo]
|
|
|
|
pub fn include_raw_pointers(mut self) -> Self {
|
|
|
|
self.include_raw_pointers = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
Arbitrary self types v2: use Receiver trait
In this new version of Arbitrary Self Types, we no longer use the Deref trait
exclusively when working out which self types are valid. Instead, we follow a
chain of Receiver traits. This enables methods to be called on smart pointer
types which fundamentally cannot support Deref (for instance because they are
wrappers for pointers that don't follow Rust's aliasing rules).
This includes:
* Changes to tests appropriately
* New tests for:
* The basics of the feature
* Ensuring lifetime elision works properly
* Generic Receivers
* A copy of the method subst test enhanced with Receiver
This is really the heart of the 'arbitrary self types v2' feature, and
is the most critical commit in the current PR.
Subsequent commits are focused on:
* Detecting "shadowing" problems, where a smart pointer type can hide
methods in the pointee.
* Diagnostics and cleanup.
Naming: in this commit, the "Autoderef" type is modified so that it no
longer solely focuses on the "Deref" trait, but can now consider the
"Receiver" trait instead. Should it be renamed, to something like
"TraitFollower"? This was considered, but rejected, because
* even in the Receiver case, it still considers built-in derefs
* the name Autoderef is short and snappy.
2024-10-25 11:08:58 +00:00
|
|
|
/// Use `core::ops::Receiver` and `core::ops::Receiver::Target` as
|
|
|
|
/// the trait and associated type to iterate, instead of
|
|
|
|
/// `core::ops::Deref` and `core::ops::Deref::Target`
|
|
|
|
pub fn use_receiver_trait(mut self) -> Self {
|
|
|
|
self.use_receiver_trait = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:29:13 +00:00
|
|
|
pub fn silence_errors(mut self) -> Self {
|
|
|
|
self.silence_errors = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
|
|
|
|
// We've reached the recursion limit, error gracefully.
|
2021-09-28 20:16:42 +00:00
|
|
|
let suggested_limit = match tcx.recursion_limit() {
|
|
|
|
Limit(0) => Limit(2),
|
|
|
|
limit => limit * 2,
|
|
|
|
};
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(AutoDerefReachedRecursionLimit {
|
2022-03-20 19:02:18 +00:00
|
|
|
span,
|
2022-08-26 18:08:58 +00:00
|
|
|
ty,
|
2022-03-20 19:02:18 +00:00
|
|
|
suggested_limit,
|
2022-08-26 18:08:58 +00:00
|
|
|
crate_name: tcx.crate_name(LOCAL_CRATE),
|
|
|
|
});
|
2020-06-20 10:29:13 +00:00
|
|
|
}
|