1
Fork 0

Rollup merge of #104156 - oli-obk:autoderef, r=estebank

Cleanups in autoderef impl

Just something I noticed. Turns out the `overloaded_span` is not actually used separately from the main span, so I merged them.
This commit is contained in:
Manish Goregaokar 2022-11-09 15:39:06 -05:00 committed by GitHub
commit a709cc1f32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 8 additions and 35 deletions

View file

@ -1708,8 +1708,7 @@ fn receiver_is_valid<'tcx>(
return true; return true;
} }
let mut autoderef = let mut autoderef = Autoderef::new(infcx, wfcx.param_env, wfcx.body_id, span, receiver_ty);
Autoderef::new(infcx, wfcx.param_env, wfcx.body_id, span, receiver_ty, span);
// The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`. // The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`.
if arbitrary_self_types_enabled { if arbitrary_self_types_enabled {

View file

@ -12,18 +12,7 @@ use std::iter;
impl<'a, 'tcx> FnCtxt<'a, 'tcx> { impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'tcx> { pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'tcx> {
Autoderef::new(self, self.param_env, self.body_id, span, base_ty, span) Autoderef::new(self, self.param_env, self.body_id, span, base_ty)
}
/// Like `autoderef`, but provides a custom `Span` to use for calls to
/// an overloaded `Deref` operator
pub fn autoderef_overloaded_span(
&'a self,
span: Span,
base_ty: Ty<'tcx>,
overloaded_span: Span,
) -> Autoderef<'a, 'tcx> {
Autoderef::new(self, self.param_env, self.body_id, span, base_ty, overloaded_span)
} }
pub fn try_overloaded_deref( pub fn try_overloaded_deref(
@ -55,11 +44,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|InferOk { value: method, obligations: o }| { |InferOk { value: method, obligations: o }| {
obligations.extend(o); obligations.extend(o);
if let ty::Ref(region, _, mutbl) = *method.sig.output().kind() { if let ty::Ref(region, _, mutbl) = *method.sig.output().kind() {
Some(OverloadedDeref { Some(OverloadedDeref { region, mutbl, span: autoderef.span() })
region,
mutbl,
span: autoderef.overloaded_span(),
})
} else { } else {
None None
} }

View file

@ -151,8 +151,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
) -> Ty<'tcx> { ) -> Ty<'tcx> {
// Commit the autoderefs by calling `autoderef` again, but this // Commit the autoderefs by calling `autoderef` again, but this
// time writing the results into the various typeck results. // time writing the results into the various typeck results.
let mut autoderef = let mut autoderef = self.autoderef(self.call_expr.span, unadjusted_self_ty);
self.autoderef_overloaded_span(self.span, unadjusted_self_ty, self.call_expr.span);
let Some((ty, n)) = autoderef.nth(pick.autoderefs) else { let Some((ty, n)) = autoderef.nth(pick.autoderefs) else {
return self.tcx.ty_error_with_message( return self.tcx.ty_error_with_message(
rustc_span::DUMMY_SP, rustc_span::DUMMY_SP,

View file

@ -475,10 +475,9 @@ fn method_autoderef_steps<'tcx>(
let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal); let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
let ParamEnvAnd { param_env, value: self_ty } = goal; let ParamEnvAnd { param_env, value: self_ty } = goal;
let mut autoderef = let mut autoderef = Autoderef::new(infcx, param_env, hir::CRATE_HIR_ID, DUMMY_SP, self_ty)
Autoderef::new(infcx, param_env, hir::CRATE_HIR_ID, DUMMY_SP, self_ty, DUMMY_SP) .include_raw_pointers()
.include_raw_pointers() .silence_errors();
.silence_errors();
let mut reached_raw_pointer = false; let mut reached_raw_pointer = false;
let mut steps: Vec<_> = autoderef let mut steps: Vec<_> = autoderef
.by_ref() .by_ref()

View file

@ -19,7 +19,6 @@ use rustc_span::lev_distance::find_best_match_for_name;
use rustc_span::source_map::{Span, Spanned}; use rustc_span::source_map::{Span, Spanned};
use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{BytePos, DUMMY_SP}; use rustc_span::{BytePos, DUMMY_SP};
use rustc_trait_selection::autoderef::Autoderef;
use rustc_trait_selection::traits::{ObligationCause, Pattern}; use rustc_trait_selection::traits::{ObligationCause, Pattern};
use ty::VariantDef; use ty::VariantDef;
@ -2132,7 +2131,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&& let ty::Array(..) | ty::Slice(..) = ty.kind() && let ty::Array(..) | ty::Slice(..) = ty.kind()
{ {
err.help("the semantics of slice patterns changed recently; see issue #62254"); err.help("the semantics of slice patterns changed recently; see issue #62254");
} else if Autoderef::new(&self.infcx, self.param_env, self.body_id, span, expected_ty, span) } else if self.autoderef(span, expected_ty)
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..))) .any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
&& let (Some(span), true) = (ti.span, ti.origin_expr) && let (Some(span), true) = (ti.span, ti.origin_expr)
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)

View file

@ -27,7 +27,6 @@ pub struct Autoderef<'a, 'tcx> {
// Meta infos: // Meta infos:
infcx: &'a InferCtxt<'tcx>, infcx: &'a InferCtxt<'tcx>,
span: Span, span: Span,
overloaded_span: Span,
body_id: hir::HirId, body_id: hir::HirId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
@ -99,12 +98,10 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
body_id: hir::HirId, body_id: hir::HirId,
span: Span, span: Span,
base_ty: Ty<'tcx>, base_ty: Ty<'tcx>,
overloaded_span: Span,
) -> Autoderef<'a, 'tcx> { ) -> Autoderef<'a, 'tcx> {
Autoderef { Autoderef {
infcx, infcx,
span, span,
overloaded_span,
body_id, body_id,
param_env, param_env,
state: AutoderefSnapshot { state: AutoderefSnapshot {
@ -193,10 +190,6 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
self.span self.span
} }
pub fn overloaded_span(&self) -> Span {
self.overloaded_span
}
pub fn reached_recursion_limit(&self) -> bool { pub fn reached_recursion_limit(&self) -> bool {
self.state.reached_recursion_limit self.state.reached_recursion_limit
} }

View file

@ -714,7 +714,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
obligation.cause.body_id, obligation.cause.body_id,
span, span,
base_ty, base_ty,
span,
); );
if let Some(steps) = autoderef.find_map(|(ty, steps)| { if let Some(steps) = autoderef.find_map(|(ty, steps)| {
// Re-add the `&` // Re-add the `&`