Skip to content

Commit 1884249

Browse files
authored
Rollup merge of rust-lang#116144 - lcnr:subst-less, r=oli-obk
subst -> instantiate continues rust-lang#110793, there are still quite a few uses of `subst` and `substitute`, but changing them all in the same PR was a bit too much, so I've stopped here for now.
2 parents 40ef3af + 3c52a3e commit 1884249

File tree

25 files changed

+66
-75
lines changed

25 files changed

+66
-75
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16641664
lifetime.ident,
16651665
));
16661666

1667-
// Now make an arg that we can use for the substs of the opaque tykind.
1667+
// Now make an arg that we can use for the generic params of the opaque tykind.
16681668
let id = self.next_node_id();
16691669
let lifetime_arg = self.new_named_lifetime_with_res(id, lifetime.ident, res);
16701670
let duplicated_lifetime_def_id = self.local_def_id(duplicated_lifetime_node_id);

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(crate) struct RegionName {
2727
/// This helps to print the right kinds of diagnostics.
2828
#[derive(Debug, Clone)]
2929
pub(crate) enum RegionNameSource {
30-
/// A bound (not free) region that was substituted at the def site (not an HRTB).
30+
/// A bound (not free) region that was instantiated at the def site (not an HRTB).
3131
NamedEarlyBoundRegion(Span),
3232
/// A free region that the user has a name (`'a`) for.
3333
NamedFreeRegion(Span),
@@ -619,7 +619,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
619619
// programs, so we need to use delay_span_bug here. See #82126.
620620
self.infcx.tcx.sess.delay_span_bug(
621621
hir_arg.span(),
622-
format!("unmatched subst and hir arg: found {kind:?} vs {hir_arg:?}"),
622+
format!("unmatched arg and hir arg: found {kind:?} vs {hir_arg:?}"),
623623
);
624624
}
625625
}

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22502250

22512251
pub(crate) fn universe_info(&self, universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
22522252
// Query canonicalization can create local superuniverses (for example in
2253-
// `InferCtx::query_response_substitution_guess`), but they don't have an associated
2253+
// `InferCtx::query_response_instantiation_guess`), but they don't have an associated
22542254
// `UniverseInfo` explaining why they were created.
22552255
// This can cause ICEs if these causes are accessed in diagnostics, for example in issue
22562256
// #114907 where this happens via liveness and dropck outlives results.

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10071007
}
10081008

10091009
pub(super) fn register_predefined_opaques_in_new_solver(&mut self) {
1010-
// OK to use the identity substitutions for each opaque type key, since
1010+
// OK to use the identity arguments for each opaque type key, since
10111011
// we remap opaques from HIR typeck back to their definition params.
10121012
let opaques: Vec<_> = self
10131013
.infcx

compiler/rustc_borrowck/src/universal_regions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
639639
};
640640

641641
let global_mapping = iter::once((tcx.lifetimes.re_static, fr_static));
642-
let subst_mapping =
643-
iter::zip(identity_args.regions(), fr_args.regions().map(|r| r.as_var()));
642+
let arg_mapping = iter::zip(identity_args.regions(), fr_args.regions().map(|r| r.as_var()));
644643

645-
UniversalRegionIndices { indices: global_mapping.chain(subst_mapping).collect(), fr_static }
644+
UniversalRegionIndices { indices: global_mapping.chain(arg_mapping).collect(), fr_static }
646645
}
647646

648647
fn compute_inputs_and_output(

compiler/rustc_codegen_cranelift/scripts/filter_profile.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
100100
stack = &stack[..index + ENCODE_METADATA.len()];
101101
}
102102

103-
const SUBST_AND_NORMALIZE_ERASING_REGIONS: &str = "rustc_middle::ty::normalize_erasing_regions::<impl rustc_middle::ty::context::TyCtxt>::subst_and_normalize_erasing_regions";
104-
if let Some(index) = stack.find(SUBST_AND_NORMALIZE_ERASING_REGIONS) {
105-
stack = &stack[..index + SUBST_AND_NORMALIZE_ERASING_REGIONS.len()];
103+
const INSTANTIATE_AND_NORMALIZE_ERASING_REGIONS: &str = "rustc_middle::ty::normalize_erasing_regions::<impl rustc_middle::ty::context::TyCtxt>::instantiate_and_normalize_erasing_regions";
104+
if let Some(index) = stack.find(INSTANTIATE_AND_NORMALIZE_ERASING_REGIONS) {
105+
stack = &stack[..index + INSTANTIATE_AND_NORMALIZE_ERASING_REGIONS.len()];
106106
}
107107

108108
const NORMALIZE_ERASING_LATE_BOUND_REGIONS: &str = "rustc_middle::ty::normalize_erasing_regions::<impl rustc_middle::ty::context::TyCtxt>::normalize_erasing_late_bound_regions";

compiler/rustc_codegen_cranelift/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
359359
where
360360
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
361361
{
362-
self.instance.subst_mir_and_normalize_erasing_regions(
362+
self.instance.instantiate_mir_and_normalize_erasing_regions(
363363
self.tcx,
364364
ty::ParamEnv::reveal_all(),
365365
ty::EarlyBinder::bind(value),

compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn make_mir_scope<'ll, 'tcx>(
9090
Some((callee, _)) => {
9191
// FIXME(eddyb) this would be `self.monomorphize(&callee)`
9292
// if this is moved to `rustc_codegen_ssa::mir::debuginfo`.
93-
let callee = cx.tcx.subst_and_normalize_erasing_regions(
93+
let callee = cx.tcx.instantiate_and_normalize_erasing_regions(
9494
instance.args,
9595
ty::ParamEnv::reveal_all(),
9696
ty::EarlyBinder::bind(callee),

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
529529
if let Some(impl_def_id) = cx.tcx.impl_of_method(instance.def_id()) {
530530
// If the method does *not* belong to a trait, proceed
531531
if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
532-
let impl_self_ty = cx.tcx.subst_and_normalize_erasing_regions(
532+
let impl_self_ty = cx.tcx.instantiate_and_normalize_erasing_regions(
533533
instance.args,
534534
ty::ParamEnv::reveal_all(),
535535
cx.tcx.type_of(impl_def_id),

compiler/rustc_codegen_ssa/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
118118
T: Copy + TypeFoldable<TyCtxt<'tcx>>,
119119
{
120120
debug!("monomorphize: self.instance={:?}", self.instance);
121-
self.instance.subst_mir_and_normalize_erasing_regions(
121+
self.instance.instantiate_mir_and_normalize_erasing_regions(
122122
self.cx.tcx(),
123123
ty::ParamEnv::reveal_all(),
124124
ty::EarlyBinder::bind(value),

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
569569
) -> Result<T, ErrorHandled> {
570570
frame
571571
.instance
572-
.try_subst_mir_and_normalize_erasing_regions(
572+
.try_instantiate_mir_and_normalize_erasing_regions(
573573
*self.tcx,
574574
self.param_env,
575575
ty::EarlyBinder::bind(value),

compiler/rustc_const_eval/src/interpret/util.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::ty::{
44
};
55
use std::ops::ControlFlow;
66

7-
/// Checks whether a type contains generic parameters which require substitution.
7+
/// Checks whether a type contains generic parameters which must be instantiated.
88
///
99
/// In case it does, returns a `TooGeneric` const eval error. Note that due to polymorphization
1010
/// types may be "concrete enough" even though they still contain generic parameters in
@@ -43,7 +43,8 @@ where
4343
.try_into()
4444
.expect("more generic parameters than can fit into a `u32`");
4545
// Only recurse when generic parameters in fns, closures and generators
46-
// are used and require substitution.
46+
// are used and have to be instantiated.
47+
//
4748
// Just in case there are closures or generators within this subst,
4849
// recurse.
4950
if unused_params.is_used(index) && subst.has_param() {

compiler/rustc_const_eval/src/transform/validate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
558558
}
559559
}
560560

561-
/// A faster version of the validation pass that only checks those things which may break when apply
562-
/// generic substitutions.
561+
/// A faster version of the validation pass that only checks those things which may break when
562+
/// instantiating any generic parameters.
563563
pub fn validate_types<'tcx>(
564564
tcx: TyCtxt<'tcx>,
565565
mir_phase: MirPhase,

compiler/rustc_error_codes/src/error_codes/E0038.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,13 @@ fn foo<T>(x: T) {
162162
```
163163

164164
The machine code for `foo::<u8>()`, `foo::<bool>()`, `foo::<String>()`, or any
165-
other type substitution is different. Hence the compiler generates the
165+
other instantiation is different. Hence the compiler generates the
166166
implementation on-demand. If you call `foo()` with a `bool` parameter, the
167167
compiler will only generate code for `foo::<bool>()`. When we have additional
168168
type parameters, the number of monomorphized implementations the compiler
169169
generates does not grow drastically, since the compiler will only generate an
170-
implementation if the function is called with unparameterized substitutions
171-
(i.e., substitutions where none of the substituted types are themselves
172-
parameterized).
170+
implementation if the function is called with fully concrete arguments
171+
(i.e., arguments which do not contain any generic parameters).
173172

174173
However, with trait objects we have to make a table containing _every_ object
175174
that implements the trait. Now, if it has type parameters, we need to add

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
427427
let bound_vars = tcx.late_bound_vars(binding.hir_id);
428428
ty::Binder::bind_with_vars(subst_output, bound_vars)
429429
} else {
430-
// Include substitutions for generic parameters of associated types
430+
// Append the generic arguments of the associated type to the `trait_ref`.
431431
candidate.map_bound(|trait_ref| {
432432
let ident = Ident::new(assoc_item.name, binding.item_name.span);
433433
let item_segment = hir::PathSegment {

compiler/rustc_hir_analysis/src/astconv/generics.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,22 @@ fn generic_arg_mismatch_err(
139139
err.emit()
140140
}
141141

142-
/// Creates the relevant generic argument substitutions
142+
/// Creates the relevant generic arguments
143143
/// corresponding to a set of generic parameters. This is a
144144
/// rather complex function. Let us try to explain the role
145145
/// of each of its parameters:
146146
///
147-
/// To start, we are given the `def_id` of the thing we are
148-
/// creating the substitutions for, and a partial set of
149-
/// substitutions `parent_args`. In general, the substitutions
150-
/// for an item begin with substitutions for all the "parents" of
147+
/// To start, we are given the `def_id` of the thing whose generic
148+
/// parameters we are instantiating, and a partial set of
149+
/// arguments `parent_args`. In general, the generic arguments
150+
/// for an item begin with arguments for all the "parents" of
151151
/// that item -- e.g., for a method it might include the
152152
/// parameters from the impl.
153153
///
154154
/// Therefore, the method begins by walking down these parents,
155155
/// starting with the outermost parent and proceed inwards until
156156
/// it reaches `def_id`. For each parent `P`, it will check `parent_args`
157-
/// first to see if the parent's substitutions are listed in there. If so,
157+
/// first to see if the parent's arguments are listed in there. If so,
158158
/// we can append those and move on. Otherwise, it invokes the
159159
/// three callback functions:
160160
///
@@ -188,15 +188,16 @@ pub fn create_args_for_parent_generic_args<'tcx, 'a>(
188188
stack.push((def_id, parent_defs));
189189
}
190190

191-
// We manually build up the substitution, rather than using convenience
191+
// We manually build up the generic arguments, rather than using convenience
192192
// methods in `subst.rs`, so that we can iterate over the arguments and
193193
// parameters in lock-step linearly, instead of trying to match each pair.
194194
let mut args: SmallVec<[ty::GenericArg<'tcx>; 8]> = SmallVec::with_capacity(count);
195195
// Iterate over each segment of the path.
196196
while let Some((def_id, defs)) = stack.pop() {
197197
let mut params = defs.params.iter().peekable();
198198

199-
// If we have already computed substitutions for parents, we can use those directly.
199+
// If we have already computed the generic arguments for parents,
200+
// we can use those directly.
200201
while let Some(&param) = params.peek() {
201202
if let Some(&kind) = parent_args.get(param.index as usize) {
202203
args.push(kind);

compiler/rustc_hir_analysis/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
289289
}
290290

291291
/// Given a path `path` that refers to an item `I` with the declared generics `decl_generics`,
292-
/// returns an appropriate set of substitutions for this particular reference to `I`.
292+
/// returns an appropriate set of generic arguments for this particular reference to `I`.
293293
pub fn ast_path_args_for_ty(
294294
&self,
295295
span: Span,
@@ -315,7 +315,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
315315

316316
/// Given the type/lifetime/const arguments provided to some path (along with
317317
/// an implicit `Self`, if this is a trait reference), returns the complete
318-
/// set of substitutions. This may involve applying defaulted type parameters.
318+
/// set of generic arguments. This may involve applying defaulted type parameters.
319319
/// Constraints on associated types are created from `create_assoc_bindings_for_generic_args`.
320320
///
321321
/// Example:

compiler/rustc_middle/src/ty/instance.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx> Instance<'tcx> {
118118
/// lifetimes erased, allowing a `ParamEnv` to be specified for use during normalization.
119119
pub fn ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> {
120120
let ty = tcx.type_of(self.def.def_id());
121-
tcx.subst_and_normalize_erasing_regions(self.args, param_env, ty)
121+
tcx.instantiate_and_normalize_erasing_regions(self.args, param_env, ty)
122122
}
123123

124124
/// Finds a crate that contains a monomorphization of this instance that
@@ -580,7 +580,7 @@ impl<'tcx> Instance<'tcx> {
580580
self.def.has_polymorphic_mir_body().then_some(self.args)
581581
}
582582

583-
pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: EarlyBinder<&T>) -> T
583+
pub fn instantiate_mir<T>(&self, tcx: TyCtxt<'tcx>, v: EarlyBinder<&T>) -> T
584584
where
585585
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
586586
{
@@ -593,7 +593,7 @@ impl<'tcx> Instance<'tcx> {
593593
}
594594

595595
#[inline(always)]
596-
pub fn subst_mir_and_normalize_erasing_regions<T>(
596+
pub fn instantiate_mir_and_normalize_erasing_regions<T>(
597597
&self,
598598
tcx: TyCtxt<'tcx>,
599599
param_env: ty::ParamEnv<'tcx>,
@@ -603,14 +603,14 @@ impl<'tcx> Instance<'tcx> {
603603
T: TypeFoldable<TyCtxt<'tcx>> + Clone,
604604
{
605605
if let Some(args) = self.args_for_mir_body() {
606-
tcx.subst_and_normalize_erasing_regions(args, param_env, v)
606+
tcx.instantiate_and_normalize_erasing_regions(args, param_env, v)
607607
} else {
608608
tcx.normalize_erasing_regions(param_env, v.skip_binder())
609609
}
610610
}
611611

612612
#[inline(always)]
613-
pub fn try_subst_mir_and_normalize_erasing_regions<T>(
613+
pub fn try_instantiate_mir_and_normalize_erasing_regions<T>(
614614
&self,
615615
tcx: TyCtxt<'tcx>,
616616
param_env: ty::ParamEnv<'tcx>,
@@ -620,7 +620,7 @@ impl<'tcx> Instance<'tcx> {
620620
T: TypeFoldable<TyCtxt<'tcx>> + Clone,
621621
{
622622
if let Some(args) = self.args_for_mir_body() {
623-
tcx.try_subst_and_normalize_erasing_regions(args, param_env, v)
623+
tcx.try_instantiate_and_normalize_erasing_regions(args, param_env, v)
624624
} else {
625625
tcx.try_normalize_erasing_regions(param_env, v.skip_binder())
626626
}

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ impl<'tcx> TyCtxt<'tcx> {
134134
/// in-scope substitutions and then normalizing any associated
135135
/// types.
136136
/// Panics if normalization fails. In case normalization might fail
137-
/// use `try_subst_and_normalize_erasing_regions` instead.
138-
pub fn subst_and_normalize_erasing_regions<T>(
137+
/// use `try_instantiate_and_normalize_erasing_regions` instead.
138+
#[instrument(level = "debug", skip(self))]
139+
pub fn instantiate_and_normalize_erasing_regions<T>(
139140
self,
140141
param_args: GenericArgsRef<'tcx>,
141142
param_env: ty::ParamEnv<'tcx>,
@@ -144,22 +145,16 @@ impl<'tcx> TyCtxt<'tcx> {
144145
where
145146
T: TypeFoldable<TyCtxt<'tcx>>,
146147
{
147-
debug!(
148-
"subst_and_normalize_erasing_regions(\
149-
param_args={:?}, \
150-
value={:?}, \
151-
param_env={:?})",
152-
param_args, value, param_env,
153-
);
154148
let substituted = value.instantiate(self, param_args);
155149
self.normalize_erasing_regions(param_env, substituted)
156150
}
157151

158152
/// Monomorphizes a type from the AST by first applying the
159153
/// in-scope substitutions and then trying to normalize any associated
160-
/// types. Contrary to `subst_and_normalize_erasing_regions` this does
154+
/// types. Contrary to `instantiate_and_normalize_erasing_regions` this does
161155
/// not assume that normalization succeeds.
162-
pub fn try_subst_and_normalize_erasing_regions<T>(
156+
#[instrument(level = "debug", skip(self))]
157+
pub fn try_instantiate_and_normalize_erasing_regions<T>(
163158
self,
164159
param_args: GenericArgsRef<'tcx>,
165160
param_env: ty::ParamEnv<'tcx>,
@@ -168,13 +163,6 @@ impl<'tcx> TyCtxt<'tcx> {
168163
where
169164
T: TypeFoldable<TyCtxt<'tcx>>,
170165
{
171-
debug!(
172-
"subst_and_normalize_erasing_regions(\
173-
param_args={:?}, \
174-
value={:?}, \
175-
param_env={:?})",
176-
param_args, value, param_env,
177-
);
178166
let substituted = value.instantiate(self, param_args);
179167
self.try_normalize_erasing_regions(param_env, substituted)
180168
}

0 commit comments

Comments
 (0)