Rollup merge of #96854 - jackh726:subst-cleanup, r=compiler-errors
Some subst cleanup Two separate things here. Both changes are useful for some refactoring I'm doing to add an "EarlyBinder" newtype. (Part of chalkification). 1) Remove `subst_spanned` and just use `subst`. It wasn't used much anyways. In practice, I think we can probably get most of the info just from the actual error message. If not, outputting logs should do the trick. (The specific line probably wouldn't help much anyways). 2) Call `.subst()` before `replace_bound_vars_with_fresh_vars` and `erase_late_bound_regions` in three places that do the opposite. I think there might have been some time in the past that the order here matter for something, but this shouldn't be the case anymore. Conceptually, it makes more sense to the of the *early bound* vars on `fn`s as "outside" the late bound vars.
This commit is contained in:
commit
0e00ed5f48
6 changed files with 26 additions and 73 deletions
|
@ -10,7 +10,7 @@ use rustc_data_structures::intern::{Interned, WithStableHash};
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
use rustc_macros::HashStable;
|
use rustc_macros::HashStable;
|
||||||
use rustc_serialize::{self, Decodable, Encodable};
|
use rustc_serialize::{self, Decodable, Encodable};
|
||||||
use rustc_span::{Span, DUMMY_SP};
|
use rustc_span::DUMMY_SP;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
use core::intrinsics;
|
use core::intrinsics;
|
||||||
|
@ -498,34 +498,14 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
// Just call `foo.subst(tcx, substs)` to perform a substitution across `foo`.
|
||||||
// Public trait `Subst`
|
|
||||||
//
|
|
||||||
// Just call `foo.subst(tcx, substs)` to perform a substitution across
|
|
||||||
// `foo`. Or use `foo.subst_spanned(tcx, substs, Some(span))` when
|
|
||||||
// there is more information available (for better errors).
|
|
||||||
|
|
||||||
pub trait Subst<'tcx>: Sized {
|
pub trait Subst<'tcx>: Sized {
|
||||||
fn subst(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> Self {
|
fn subst(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> Self;
|
||||||
self.subst_spanned(tcx, substs, None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn subst_spanned(
|
|
||||||
self,
|
|
||||||
tcx: TyCtxt<'tcx>,
|
|
||||||
substs: &[GenericArg<'tcx>],
|
|
||||||
span: Option<Span>,
|
|
||||||
) -> Self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx, T: TypeFoldable<'tcx>> Subst<'tcx> for T {
|
impl<'tcx, T: TypeFoldable<'tcx>> Subst<'tcx> for T {
|
||||||
fn subst_spanned(
|
fn subst(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> T {
|
||||||
self,
|
let mut folder = SubstFolder { tcx, substs, binders_passed: 0 };
|
||||||
tcx: TyCtxt<'tcx>,
|
|
||||||
substs: &[GenericArg<'tcx>],
|
|
||||||
span: Option<Span>,
|
|
||||||
) -> T {
|
|
||||||
let mut folder = SubstFolder { tcx, substs, span, binders_passed: 0 };
|
|
||||||
self.fold_with(&mut folder)
|
self.fold_with(&mut folder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -537,9 +517,6 @@ struct SubstFolder<'a, 'tcx> {
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
substs: &'a [GenericArg<'tcx>],
|
substs: &'a [GenericArg<'tcx>],
|
||||||
|
|
||||||
/// The location for which the substitution is performed, if available.
|
|
||||||
span: Option<Span>,
|
|
||||||
|
|
||||||
/// Number of region binders we have passed through while doing the substitution
|
/// Number of region binders we have passed through while doing the substitution
|
||||||
binders_passed: u32,
|
binders_passed: u32,
|
||||||
}
|
}
|
||||||
|
@ -571,13 +548,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
|
||||||
match rk {
|
match rk {
|
||||||
Some(GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
|
Some(GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
|
||||||
_ => {
|
_ => {
|
||||||
let span = self.span.unwrap_or(DUMMY_SP);
|
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"Region parameter out of range \
|
"Region parameter out of range \
|
||||||
when substituting in region {} (index={})",
|
when substituting in region {} (index={})",
|
||||||
data.name, data.index
|
data.name, data.index
|
||||||
);
|
);
|
||||||
span_bug!(span, "{}", msg);
|
span_bug!(DUMMY_SP, "{}", msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -617,9 +593,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
|
||||||
let ty = match opt_ty {
|
let ty = match opt_ty {
|
||||||
Some(GenericArgKind::Type(ty)) => ty,
|
Some(GenericArgKind::Type(ty)) => ty,
|
||||||
Some(kind) => {
|
Some(kind) => {
|
||||||
let span = self.span.unwrap_or(DUMMY_SP);
|
|
||||||
span_bug!(
|
span_bug!(
|
||||||
span,
|
DUMMY_SP,
|
||||||
"expected type for `{:?}` ({:?}/{}) but found {:?} \
|
"expected type for `{:?}` ({:?}/{}) but found {:?} \
|
||||||
when substituting, substs={:?}",
|
when substituting, substs={:?}",
|
||||||
p,
|
p,
|
||||||
|
@ -630,9 +605,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let span = self.span.unwrap_or(DUMMY_SP);
|
|
||||||
span_bug!(
|
span_bug!(
|
||||||
span,
|
DUMMY_SP,
|
||||||
"type parameter `{:?}` ({:?}/{}) out of range \
|
"type parameter `{:?}` ({:?}/{}) out of range \
|
||||||
when substituting, substs={:?}",
|
when substituting, substs={:?}",
|
||||||
p,
|
p,
|
||||||
|
@ -652,9 +626,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
|
||||||
let ct = match opt_ct {
|
let ct = match opt_ct {
|
||||||
Some(GenericArgKind::Const(ct)) => ct,
|
Some(GenericArgKind::Const(ct)) => ct,
|
||||||
Some(kind) => {
|
Some(kind) => {
|
||||||
let span = self.span.unwrap_or(DUMMY_SP);
|
|
||||||
span_bug!(
|
span_bug!(
|
||||||
span,
|
DUMMY_SP,
|
||||||
"expected const for `{:?}` ({:?}/{}) but found {:?} \
|
"expected const for `{:?}` ({:?}/{}) but found {:?} \
|
||||||
when substituting substs={:?}",
|
when substituting substs={:?}",
|
||||||
p,
|
p,
|
||||||
|
@ -665,9 +638,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let span = self.span.unwrap_or(DUMMY_SP);
|
|
||||||
span_bug!(
|
span_bug!(
|
||||||
span,
|
DUMMY_SP,
|
||||||
"const parameter `{:?}` ({:?}/{}) out of range \
|
"const parameter `{:?}` ({:?}/{}) out of range \
|
||||||
when substituting substs={:?}",
|
when substituting substs={:?}",
|
||||||
p,
|
p,
|
||||||
|
|
|
@ -523,11 +523,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
||||||
self.astconv
|
self.astconv
|
||||||
.normalize_ty(
|
.normalize_ty(
|
||||||
self.span,
|
self.span,
|
||||||
tcx.at(self.span).type_of(param.def_id).subst_spanned(
|
tcx.at(self.span).type_of(param.def_id).subst(tcx, substs),
|
||||||
tcx,
|
|
||||||
substs,
|
|
||||||
Some(self.span),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
@ -547,9 +543,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
||||||
GenericParamDefKind::Const { has_default } => {
|
GenericParamDefKind::Const { has_default } => {
|
||||||
let ty = tcx.at(self.span).type_of(param.def_id);
|
let ty = tcx.at(self.span).type_of(param.def_id);
|
||||||
if !infer_args && has_default {
|
if !infer_args && has_default {
|
||||||
tcx.const_param_default(param.def_id)
|
tcx.const_param_default(param.def_id).subst(tcx, substs.unwrap()).into()
|
||||||
.subst_spanned(tcx, substs.unwrap(), Some(self.span))
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
if infer_args {
|
if infer_args {
|
||||||
self.astconv.ct_infer(ty, Some(param), self.span).into()
|
self.astconv.ct_infer(ty, Some(param), self.span).into()
|
||||||
|
|
|
@ -1403,10 +1403,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
// is missing.
|
// is missing.
|
||||||
let default = tcx.type_of(param.def_id);
|
let default = tcx.type_of(param.def_id);
|
||||||
self.fcx
|
self.fcx
|
||||||
.normalize_ty(
|
.normalize_ty(self.span, default.subst(tcx, substs.unwrap()))
|
||||||
self.span,
|
|
||||||
default.subst_spanned(tcx, substs.unwrap(), Some(self.span)),
|
|
||||||
)
|
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
// If no type arguments were provided, we have to infer them.
|
// If no type arguments were provided, we have to infer them.
|
||||||
|
@ -1418,9 +1415,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
GenericParamDefKind::Const { has_default } => {
|
GenericParamDefKind::Const { has_default } => {
|
||||||
if !infer_args && has_default {
|
if !infer_args && has_default {
|
||||||
tcx.const_param_default(param.def_id)
|
tcx.const_param_default(param.def_id).subst(tcx, substs.unwrap()).into()
|
||||||
.subst_spanned(tcx, substs.unwrap(), Some(self.span))
|
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
self.fcx.var_for_def(self.span, param)
|
self.fcx.var_for_def(self.span, param)
|
||||||
}
|
}
|
||||||
|
|
|
@ -462,19 +462,13 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
|
||||||
|
|
||||||
let sig = self.tcx.fn_sig(def_id);
|
let sig = self.tcx.fn_sig(def_id);
|
||||||
|
|
||||||
// Instantiate late-bound regions and substitute the trait
|
let sig = sig.subst(self.tcx, all_substs);
|
||||||
// parameters into the method type to get the actual method type.
|
debug!("type scheme substituted, sig={:?}", sig);
|
||||||
//
|
|
||||||
// N.B., instantiate late-bound regions first so that
|
|
||||||
// `instantiate_type_scheme` can normalize associated types that
|
|
||||||
// may reference those regions.
|
|
||||||
let method_sig = self.replace_bound_vars_with_fresh_vars(sig);
|
|
||||||
debug!("late-bound lifetimes from method instantiated, method_sig={:?}", method_sig);
|
|
||||||
|
|
||||||
let method_sig = method_sig.subst(self.tcx, all_substs);
|
let sig = self.replace_bound_vars_with_fresh_vars(sig);
|
||||||
debug!("type scheme substituted, method_sig={:?}", method_sig);
|
debug!("late-bound lifetimes from method instantiated, sig={:?}", sig);
|
||||||
|
|
||||||
(method_sig, method_predicates)
|
(sig, method_predicates)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_obligations(
|
fn add_obligations(
|
||||||
|
|
|
@ -461,8 +461,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
// `instantiate_type_scheme` can normalize associated types that
|
// `instantiate_type_scheme` can normalize associated types that
|
||||||
// may reference those regions.
|
// may reference those regions.
|
||||||
let fn_sig = tcx.fn_sig(def_id);
|
let fn_sig = tcx.fn_sig(def_id);
|
||||||
let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig).0;
|
|
||||||
let fn_sig = fn_sig.subst(self.tcx, substs);
|
let fn_sig = fn_sig.subst(self.tcx, substs);
|
||||||
|
let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig).0;
|
||||||
|
|
||||||
let InferOk { value, obligations: o } = if is_op {
|
let InferOk { value, obligations: o } = if is_op {
|
||||||
self.normalize_op_associated_types_in_as_infer_ok(span, fn_sig, opt_input_expr)
|
self.normalize_op_associated_types_in_as_infer_ok(span, fn_sig, opt_input_expr)
|
||||||
|
|
|
@ -1784,12 +1784,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||||
let generics = self.tcx.generics_of(method);
|
let generics = self.tcx.generics_of(method);
|
||||||
assert_eq!(substs.len(), generics.parent_count as usize);
|
assert_eq!(substs.len(), generics.parent_count as usize);
|
||||||
|
|
||||||
// Erase any late-bound regions from the method and substitute
|
let xform_fn_sig = if generics.params.is_empty() {
|
||||||
// in the values from the substitution.
|
fn_sig.subst(self.tcx, substs)
|
||||||
let xform_fn_sig = self.erase_late_bound_regions(fn_sig);
|
|
||||||
|
|
||||||
if generics.params.is_empty() {
|
|
||||||
xform_fn_sig.subst(self.tcx, substs)
|
|
||||||
} else {
|
} else {
|
||||||
let substs = InternalSubsts::for_item(self.tcx, method, |param, _| {
|
let substs = InternalSubsts::for_item(self.tcx, method, |param, _| {
|
||||||
let i = param.index as usize;
|
let i = param.index as usize;
|
||||||
|
@ -1807,8 +1803,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
xform_fn_sig.subst(self.tcx, substs)
|
fn_sig.subst(self.tcx, substs)
|
||||||
}
|
};
|
||||||
|
|
||||||
|
self.erase_late_bound_regions(xform_fn_sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the type of an impl and generate substitutions with placeholders.
|
/// Gets the type of an impl and generate substitutions with placeholders.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue