Skip to content

Commit

Permalink
Auto merge of #124953 - compiler-errors:own-params, r=lcnr
Browse files Browse the repository at this point in the history
Rename `Generics::params` to `Generics::own_params`

I hope this makes it slightly more obvious that `generics.own_params` is insufficient when considering nested items. I didn't actually audit any of the usages, for the record.

r? lcnr
  • Loading branch information
bors committed May 10, 2024
2 parents a6e87c5 + 1c19b6a commit 98dabb6
Show file tree
Hide file tree
Showing 54 changed files with 179 additions and 171 deletions.
55 changes: 29 additions & 26 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,36 +317,39 @@ impl<'tcx> BorrowExplanation<'tcx> {
let mut has_dyn = false;
let mut failed = false;

let elaborated_args = std::iter::zip(*args, &generics.params).map(|(arg, param)| {
if let Some(ty::Dynamic(obj, _, ty::Dyn)) = arg.as_type().map(Ty::kind) {
let default = tcx.object_lifetime_default(param.def_id);

let re_static = tcx.lifetimes.re_static;

let implied_region = match default {
// This is not entirely precise.
ObjectLifetimeDefault::Empty => re_static,
ObjectLifetimeDefault::Ambiguous => {
failed = true;
re_static
}
ObjectLifetimeDefault::Param(param_def_id) => {
let index = generics.param_def_id_to_index[&param_def_id] as usize;
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(|| {
let elaborated_args =
std::iter::zip(*args, &generics.own_params).map(|(arg, param)| {
if let Some(ty::Dynamic(obj, _, ty::Dyn)) = arg.as_type().map(Ty::kind) {
let default = tcx.object_lifetime_default(param.def_id);

let re_static = tcx.lifetimes.re_static;

let implied_region = match default {
// This is not entirely precise.
ObjectLifetimeDefault::Empty => re_static,
ObjectLifetimeDefault::Ambiguous => {
failed = true;
re_static
})
}
ObjectLifetimeDefault::Static => re_static,
};
}
ObjectLifetimeDefault::Param(param_def_id) => {
let index = generics.param_def_id_to_index[&param_def_id] as usize;
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(
|| {
failed = true;
re_static
},
)
}
ObjectLifetimeDefault::Static => re_static,
};

has_dyn = true;
has_dyn = true;

Ty::new_dynamic(tcx, obj, implied_region, ty::Dyn).into()
} else {
arg
}
});
Ty::new_dynamic(tcx, obj, implied_region, ty::Dyn).into()
} else {
arg
}
});
let elaborated_ty = Ty::new_adt(tcx, *def, tcx.mk_args_from_iter(elaborated_args));

if has_dyn && !failed {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ fn build_generic_type_param_di_nodes<'ll, 'tcx>(
let mut names = generics
.parent
.map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
names.extend(generics.params.iter().map(|param| param.name));
names.extend(generics.own_params.iter().map(|param| param.name));
names
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
let mut names = generics.parent.map_or_else(Vec::new, |def_id| {
get_parameter_names(cx, cx.tcx.generics_of(def_id))
});
names.extend(generics.params.iter().map(|param| param.name));
names.extend(generics.own_params.iter().map(|param| param.name));
names
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
let generics = tcx.generics_of(generics);
def_id = generics.parent;

for param in &generics.params {
for param in &generics.own_params {
if expected_captures.contains(&param.def_id) {
assert_eq!(
variances[param.index as usize],
Expand Down Expand Up @@ -778,7 +778,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let def_id = item.id.owner_id.def_id;
let generics = tcx.generics_of(def_id);
let own_counts = generics.own_counts();
if generics.params.len() - own_counts.lifetimes != 0 {
if generics.own_params.len() - own_counts.lifetimes != 0 {
let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts)
{
(_, 0) => ("type", "types", Some("u32")),
Expand Down Expand Up @@ -1544,7 +1544,7 @@ fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalD
.collect::<FxIndexMap<_, _>>()
});

let mut params_used = BitSet::new_empty(generics.params.len());
let mut params_used = BitSet::new_empty(generics.own_params.len());
for leaf in ty.walk() {
if let GenericArgKind::Type(leaf_ty) = leaf.unpack()
&& let ty::Param(param) = leaf_ty.kind()
Expand All @@ -1554,7 +1554,7 @@ fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalD
}
}

for param in &generics.params {
for param in &generics.own_params {
if !params_used.contains(param.index)
&& let ty::GenericParamDefKind::Type { .. } = param.kind
{
Expand Down
24 changes: 13 additions & 11 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
// since we previously enforce that the trait method and impl method have the
// same generics.
let num_trait_args = trait_to_impl_args.len();
let num_impl_args = tcx.generics_of(impl_m.container_id(tcx)).params.len();
let num_impl_args = tcx.generics_of(impl_m.container_id(tcx)).own_params.len();
let ty = match ty.try_fold_with(&mut RemapHiddenTyRegions {
tcx,
map,
Expand Down Expand Up @@ -1494,14 +1494,16 @@ fn compare_synthetic_generics<'tcx>(
let mut error_found = None;
let impl_m_generics = tcx.generics_of(impl_m.def_id);
let trait_m_generics = tcx.generics_of(trait_m.def_id);
let impl_m_type_params = impl_m_generics.params.iter().filter_map(|param| match param.kind {
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => None,
});
let trait_m_type_params = trait_m_generics.params.iter().filter_map(|param| match param.kind {
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => None,
});
let impl_m_type_params =
impl_m_generics.own_params.iter().filter_map(|param| match param.kind {
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => None,
});
let trait_m_type_params =
trait_m_generics.own_params.iter().filter_map(|param| match param.kind {
GenericParamDefKind::Type { synthetic, .. } => Some((param.def_id, synthetic)),
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => None,
});
for ((impl_def_id, impl_synthetic), (trait_def_id, trait_synthetic)) in
iter::zip(impl_m_type_params, trait_m_type_params)
{
Expand Down Expand Up @@ -1639,7 +1641,7 @@ fn compare_generic_param_kinds<'tcx>(
assert_eq!(impl_item.kind, trait_item.kind);

let ty_const_params_of = |def_id| {
tcx.generics_of(def_id).params.iter().filter(|param| {
tcx.generics_of(def_id).own_params.iter().filter(|param| {
matches!(
param.kind,
GenericParamDefKind::Const { .. } | GenericParamDefKind::Type { .. }
Expand Down Expand Up @@ -2140,7 +2142,7 @@ fn param_env_with_gat_bounds<'tcx>(
};

let mut bound_vars: smallvec::SmallVec<[ty::BoundVariableKind; 8]> =
smallvec::SmallVec::with_capacity(tcx.generics_of(impl_ty.def_id).params.len());
smallvec::SmallVec::with_capacity(tcx.generics_of(impl_ty.def_id).own_params.len());
// Extend the impl's identity args with late-bound GAT vars
let normalize_impl_ty_args = ty::GenericArgs::identity_for_item(tcx, container_id)
.extend_to(tcx, impl_ty.def_id, |param, _| match param.kind {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
}
let gat_generics = tcx.generics_of(gat_def_id);
// FIXME(jackh726): we can also warn in the more general case
if gat_generics.params.is_empty() {
if gat_generics.own_params.is_empty() {
continue;
}

Expand Down Expand Up @@ -1400,7 +1400,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
// struct Foo<T = Vec<[u32]>> { .. }
//
// Here, the default `Vec<[u32]>` is not WF because `[u32]: Sized` does not hold.
for param in &generics.params {
for param in &generics.own_params {
match param.kind {
GenericParamDefKind::Type { .. } => {
if is_our_default(param) {
Expand Down Expand Up @@ -1879,7 +1879,7 @@ fn check_variances_for_type_defn<'tcx>(
continue;
}

let ty_param = &ty_generics.params[index];
let ty_param = &ty_generics.own_params[index];
let hir_param = &hir_generics.params[index];

if ty_param.def_id != hir_param.def_id.into() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for TyVarReplacer<'cx, 'tcx> {
if let Some(def_id) = origin.param_def_id {
// The generics of an `impl` don't have a parent, we can index directly.
let index = self.generics.param_def_id_to_index[&def_id];
let name = self.generics.params[index as usize].name;
let name = self.generics.own_params[index as usize].name;

Ty::new_param(self.infcx.tcx, index, name)
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/coherence/unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(super) fn check_item(
trait_def: &TraitDef,
) -> Result<(), ErrorGuaranteed> {
let unsafe_attr =
tcx.generics_of(def_id).params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
tcx.generics_of(def_id).own_params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
let trait_ref = trait_header.trait_ref.instantiate_identity();

match (trait_def.unsafety, unsafe_attr, trait_header.unsafety, trait_header.polarity) {
Expand Down
43 changes: 22 additions & 21 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
let trait_def_id = tcx.parent(fn_def_id);
let opaque_ty_generics = tcx.generics_of(opaque_def_id);
let opaque_ty_parent_count = opaque_ty_generics.parent_count;
let mut params = opaque_ty_generics.params.clone();
let mut own_params = opaque_ty_generics.own_params.clone();

let parent_generics = tcx.generics_of(trait_def_id);
let parent_count = parent_generics.parent_count + parent_generics.params.len();
let parent_count = parent_generics.parent_count + parent_generics.own_params.len();

let mut trait_fn_params = tcx.generics_of(fn_def_id).params.clone();
let mut trait_fn_params = tcx.generics_of(fn_def_id).own_params.clone();

for param in &mut params {
for param in &mut own_params {
param.index = param.index + parent_count as u32 + trait_fn_params.len() as u32
- opaque_ty_parent_count as u32;
}

trait_fn_params.extend(params);
params = trait_fn_params;
trait_fn_params.extend(own_params);
own_params = trait_fn_params;

let param_def_id_to_index =
params.iter().map(|param| (param.def_id, param.index)).collect();
own_params.iter().map(|param| (param.def_id, param.index)).collect();

return ty::Generics {
parent: Some(trait_def_id),
parent_count,
params,
own_params,
param_def_id_to_index,
has_self: opaque_ty_generics.has_self,
has_late_bound_regions: opaque_ty_generics.has_late_bound_regions,
Expand Down Expand Up @@ -124,17 +124,17 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
let generics = tcx.generics_of(parent_def_id.to_def_id());
let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
// In the above example this would be .params[..N#0]
let params = generics.params_to(param_def_idx as usize, tcx).to_owned();
let own_params = generics.params_to(param_def_idx as usize, tcx).to_owned();
let param_def_id_to_index =
params.iter().map(|param| (param.def_id, param.index)).collect();
own_params.iter().map(|param| (param.def_id, param.index)).collect();

return ty::Generics {
// we set the parent of these generics to be our parent's parent so that we
// dont end up with args: [N, M, N] for the const default on a struct like this:
// struct Foo<const N: usize, const M: usize = { ... }>;
parent: generics.parent,
parent_count: generics.parent_count,
params,
own_params,
param_def_id_to_index,
has_self: generics.has_self,
has_late_bound_regions: generics.has_late_bound_regions,
Expand Down Expand Up @@ -274,17 +274,17 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
parent_has_self = generics.has_self;
host_effect_index = generics.host_effect_index;
own_start = generics.count() as u32;
generics.parent_count + generics.params.len()
generics.parent_count + generics.own_params.len()
});

let mut params: Vec<_> = Vec::with_capacity(hir_generics.params.len() + has_self as usize);
let mut own_params: Vec<_> = Vec::with_capacity(hir_generics.params.len() + has_self as usize);

if let Some(opt_self) = opt_self {
params.push(opt_self);
own_params.push(opt_self);
}

let early_lifetimes = super::early_bound_lifetimes_from_generics(tcx, hir_generics);
params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
own_params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
name: param.name.ident().name,
index: own_start + i as u32,
def_id: param.def_id.to_def_id(),
Expand All @@ -293,7 +293,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
}));

// Now create the real type and const parameters.
let type_start = own_start - has_self as u32 + params.len() as u32;
let type_start = own_start - has_self as u32 + own_params.len() as u32;
let mut i: u32 = 0;
let mut next_index = || {
let prev = i;
Expand All @@ -304,7 +304,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \
`struct`, `enum`, `type`, or `trait` definitions";

params.extend(hir_generics.params.iter().filter_map(|param| match param.kind {
own_params.extend(hir_generics.params.iter().filter_map(|param| match param.kind {
GenericParamKind::Lifetime { .. } => None,
GenericParamKind::Type { default, synthetic, .. } => {
if default.is_some() {
Expand Down Expand Up @@ -404,7 +404,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
][..],
};

params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
own_params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
index: next_index(),
name: Symbol::intern(arg),
def_id: def_id.to_def_id(),
Expand All @@ -415,7 +415,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {

// provide junk type parameter defs for const blocks.
if let Node::ConstBlock(_) = node {
params.push(ty::GenericParamDef {
own_params.push(ty::GenericParamDef {
index: next_index(),
name: Symbol::intern("<const_ty>"),
def_id: def_id.to_def_id(),
Expand All @@ -424,12 +424,13 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
});
}

let param_def_id_to_index = params.iter().map(|param| (param.def_id, param.index)).collect();
let param_def_id_to_index =
own_params.iter().map(|param| (param.def_id, param.index)).collect();

ty::Generics {
parent: parent_def_id,
parent_count,
params,
own_params,
param_def_id_to_index,
has_self: has_self || parent_has_self,
has_late_bound_regions: has_late_bound_regions(tcx, node),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
// opaque lifetimes, which explains the slicing below.
compute_bidirectional_outlives_predicates(
tcx,
&tcx.generics_of(def_id.to_def_id()).params
[tcx.generics_of(fn_def_id).params.len()..],
&tcx.generics_of(def_id.to_def_id()).own_params
[tcx.generics_of(fn_def_id).own_params.len()..],
&mut predicates,
);

Expand Down Expand Up @@ -300,7 +300,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
};
debug!(?lifetimes);

compute_bidirectional_outlives_predicates(tcx, &generics.params, &mut predicates);
compute_bidirectional_outlives_predicates(tcx, &generics.own_params, &mut predicates);
debug!(?predicates);
}

Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
ObjectLifetimeDefault::Ambiguous => None,
};
generics
.params
.own_params
.iter()
.filter_map(|param| {
match self.tcx.def_kind(param.def_id) {
Expand Down Expand Up @@ -1668,7 +1668,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
binding.ident,
ty::AssocKind::Fn,
) {
bound_vars.extend(self.tcx.generics_of(assoc_fn.def_id).params.iter().map(
bound_vars.extend(self.tcx.generics_of(assoc_fn.def_id).own_params.iter().map(
|param| match param.kind {
ty::GenericParamDefKind::Lifetime => ty::BoundVariableKind::Region(
ty::BoundRegionKind::BrNamed(param.def_id, param.name),
Expand Down Expand Up @@ -2003,7 +2003,8 @@ fn is_late_bound_map(
// just consider args to be unconstrained.
let generics = self.tcx.generics_of(alias_def);
let mut walker = ConstrainedCollectorPostHirTyLowering {
arg_is_constrained: vec![false; generics.params.len()].into_boxed_slice(),
arg_is_constrained: vec![false; generics.own_params.len()]
.into_boxed_slice(),
};
walker.visit_ty(self.tcx.type_of(alias_def).instantiate_identity());

Expand Down
Loading

0 comments on commit 98dabb6

Please sign in to comment.