Skip to content

Commit 0e00ed5

Browse files
authored
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.
2 parents 84a8f8d + 657499d commit 0e00ed5

File tree

6 files changed

+26
-73
lines changed

6 files changed

+26
-73
lines changed

compiler/rustc_middle/src/ty/subst.rs

+10-38
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::intern::{Interned, WithStableHash};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_macros::HashStable;
1212
use rustc_serialize::{self, Decodable, Encodable};
13-
use rustc_span::{Span, DUMMY_SP};
13+
use rustc_span::DUMMY_SP;
1414
use smallvec::SmallVec;
1515

1616
use core::intrinsics;
@@ -498,34 +498,14 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
498498
}
499499
}
500500

501-
///////////////////////////////////////////////////////////////////////////
502-
// Public trait `Subst`
503-
//
504-
// Just call `foo.subst(tcx, substs)` to perform a substitution across
505-
// `foo`. Or use `foo.subst_spanned(tcx, substs, Some(span))` when
506-
// there is more information available (for better errors).
507-
501+
// Just call `foo.subst(tcx, substs)` to perform a substitution across `foo`.
508502
pub trait Subst<'tcx>: Sized {
509-
fn subst(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> Self {
510-
self.subst_spanned(tcx, substs, None)
511-
}
512-
513-
fn subst_spanned(
514-
self,
515-
tcx: TyCtxt<'tcx>,
516-
substs: &[GenericArg<'tcx>],
517-
span: Option<Span>,
518-
) -> Self;
503+
fn subst(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> Self;
519504
}
520505

521506
impl<'tcx, T: TypeFoldable<'tcx>> Subst<'tcx> for T {
522-
fn subst_spanned(
523-
self,
524-
tcx: TyCtxt<'tcx>,
525-
substs: &[GenericArg<'tcx>],
526-
span: Option<Span>,
527-
) -> T {
528-
let mut folder = SubstFolder { tcx, substs, span, binders_passed: 0 };
507+
fn subst(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> T {
508+
let mut folder = SubstFolder { tcx, substs, binders_passed: 0 };
529509
self.fold_with(&mut folder)
530510
}
531511
}
@@ -537,9 +517,6 @@ struct SubstFolder<'a, 'tcx> {
537517
tcx: TyCtxt<'tcx>,
538518
substs: &'a [GenericArg<'tcx>],
539519

540-
/// The location for which the substitution is performed, if available.
541-
span: Option<Span>,
542-
543520
/// Number of region binders we have passed through while doing the substitution
544521
binders_passed: u32,
545522
}
@@ -571,13 +548,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
571548
match rk {
572549
Some(GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
573550
_ => {
574-
let span = self.span.unwrap_or(DUMMY_SP);
575551
let msg = format!(
576552
"Region parameter out of range \
577553
when substituting in region {} (index={})",
578554
data.name, data.index
579555
);
580-
span_bug!(span, "{}", msg);
556+
span_bug!(DUMMY_SP, "{}", msg);
581557
}
582558
}
583559
}
@@ -617,9 +593,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
617593
let ty = match opt_ty {
618594
Some(GenericArgKind::Type(ty)) => ty,
619595
Some(kind) => {
620-
let span = self.span.unwrap_or(DUMMY_SP);
621596
span_bug!(
622-
span,
597+
DUMMY_SP,
623598
"expected type for `{:?}` ({:?}/{}) but found {:?} \
624599
when substituting, substs={:?}",
625600
p,
@@ -630,9 +605,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
630605
);
631606
}
632607
None => {
633-
let span = self.span.unwrap_or(DUMMY_SP);
634608
span_bug!(
635-
span,
609+
DUMMY_SP,
636610
"type parameter `{:?}` ({:?}/{}) out of range \
637611
when substituting, substs={:?}",
638612
p,
@@ -652,9 +626,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
652626
let ct = match opt_ct {
653627
Some(GenericArgKind::Const(ct)) => ct,
654628
Some(kind) => {
655-
let span = self.span.unwrap_or(DUMMY_SP);
656629
span_bug!(
657-
span,
630+
DUMMY_SP,
658631
"expected const for `{:?}` ({:?}/{}) but found {:?} \
659632
when substituting substs={:?}",
660633
p,
@@ -665,9 +638,8 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> {
665638
);
666639
}
667640
None => {
668-
let span = self.span.unwrap_or(DUMMY_SP);
669641
span_bug!(
670-
span,
642+
DUMMY_SP,
671643
"const parameter `{:?}` ({:?}/{}) out of range \
672644
when substituting substs={:?}",
673645
p,

compiler/rustc_typeck/src/astconv/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
523523
self.astconv
524524
.normalize_ty(
525525
self.span,
526-
tcx.at(self.span).type_of(param.def_id).subst_spanned(
527-
tcx,
528-
substs,
529-
Some(self.span),
530-
),
526+
tcx.at(self.span).type_of(param.def_id).subst(tcx, substs),
531527
)
532528
.into()
533529
}
@@ -547,9 +543,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
547543
GenericParamDefKind::Const { has_default } => {
548544
let ty = tcx.at(self.span).type_of(param.def_id);
549545
if !infer_args && has_default {
550-
tcx.const_param_default(param.def_id)
551-
.subst_spanned(tcx, substs.unwrap(), Some(self.span))
552-
.into()
546+
tcx.const_param_default(param.def_id).subst(tcx, substs.unwrap()).into()
553547
} else {
554548
if infer_args {
555549
self.astconv.ct_infer(ty, Some(param), self.span).into()

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1403,10 +1403,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14031403
// is missing.
14041404
let default = tcx.type_of(param.def_id);
14051405
self.fcx
1406-
.normalize_ty(
1407-
self.span,
1408-
default.subst_spanned(tcx, substs.unwrap(), Some(self.span)),
1409-
)
1406+
.normalize_ty(self.span, default.subst(tcx, substs.unwrap()))
14101407
.into()
14111408
} else {
14121409
// If no type arguments were provided, we have to infer them.
@@ -1418,9 +1415,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14181415
}
14191416
GenericParamDefKind::Const { has_default } => {
14201417
if !infer_args && has_default {
1421-
tcx.const_param_default(param.def_id)
1422-
.subst_spanned(tcx, substs.unwrap(), Some(self.span))
1423-
.into()
1418+
tcx.const_param_default(param.def_id).subst(tcx, substs.unwrap()).into()
14241419
} else {
14251420
self.fcx.var_for_def(self.span, param)
14261421
}

compiler/rustc_typeck/src/check/method/confirm.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -462,19 +462,13 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
462462

463463
let sig = self.tcx.fn_sig(def_id);
464464

465-
// Instantiate late-bound regions and substitute the trait
466-
// parameters into the method type to get the actual method type.
467-
//
468-
// N.B., instantiate late-bound regions first so that
469-
// `instantiate_type_scheme` can normalize associated types that
470-
// may reference those regions.
471-
let method_sig = self.replace_bound_vars_with_fresh_vars(sig);
472-
debug!("late-bound lifetimes from method instantiated, method_sig={:?}", method_sig);
465+
let sig = sig.subst(self.tcx, all_substs);
466+
debug!("type scheme substituted, sig={:?}", sig);
473467

474-
let method_sig = method_sig.subst(self.tcx, all_substs);
475-
debug!("type scheme substituted, method_sig={:?}", method_sig);
468+
let sig = self.replace_bound_vars_with_fresh_vars(sig);
469+
debug!("late-bound lifetimes from method instantiated, sig={:?}", sig);
476470

477-
(method_sig, method_predicates)
471+
(sig, method_predicates)
478472
}
479473

480474
fn add_obligations(

compiler/rustc_typeck/src/check/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
461461
// `instantiate_type_scheme` can normalize associated types that
462462
// may reference those regions.
463463
let fn_sig = tcx.fn_sig(def_id);
464-
let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig).0;
465464
let fn_sig = fn_sig.subst(self.tcx, substs);
465+
let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig).0;
466466

467467
let InferOk { value, obligations: o } = if is_op {
468468
self.normalize_op_associated_types_in_as_infer_ok(span, fn_sig, opt_input_expr)

compiler/rustc_typeck/src/check/method/probe.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1784,12 +1784,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
17841784
let generics = self.tcx.generics_of(method);
17851785
assert_eq!(substs.len(), generics.parent_count as usize);
17861786

1787-
// Erase any late-bound regions from the method and substitute
1788-
// in the values from the substitution.
1789-
let xform_fn_sig = self.erase_late_bound_regions(fn_sig);
1790-
1791-
if generics.params.is_empty() {
1792-
xform_fn_sig.subst(self.tcx, substs)
1787+
let xform_fn_sig = if generics.params.is_empty() {
1788+
fn_sig.subst(self.tcx, substs)
17931789
} else {
17941790
let substs = InternalSubsts::for_item(self.tcx, method, |param, _| {
17951791
let i = param.index as usize;
@@ -1807,8 +1803,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18071803
}
18081804
}
18091805
});
1810-
xform_fn_sig.subst(self.tcx, substs)
1811-
}
1806+
fn_sig.subst(self.tcx, substs)
1807+
};
1808+
1809+
self.erase_late_bound_regions(xform_fn_sig)
18121810
}
18131811

18141812
/// Gets the type of an impl and generate substitutions with placeholders.

0 commit comments

Comments
 (0)