Add and use generics.is_empty() and generics.is_own_empty, rather than using generics' attributes

This commit is contained in:
Santiago Pastorino 2024-05-11 11:46:25 +02:00
parent 84b9b6d16c
commit 4501ae89f1
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
18 changed files with 25 additions and 17 deletions

View File

@ -432,7 +432,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
} }
let gat_generics = tcx.generics_of(gat_def_id); let gat_generics = tcx.generics_of(gat_def_id);
// FIXME(jackh726): we can also warn in the more general case // FIXME(jackh726): we can also warn in the more general case
if gat_generics.own_params.is_empty() { if gat_generics.is_own_empty() {
continue; continue;
} }

View File

@ -1409,7 +1409,7 @@ fn generics_args_err_extend<'a>(
// it was done based on the end of assoc segment but that sometimes // it was done based on the end of assoc segment but that sometimes
// led to impossible spans and caused issues like #116473 // led to impossible spans and caused issues like #116473
let args_span = args.span_ext.with_lo(args.span_ext.lo() - BytePos(2)); let args_span = args.span_ext.with_lo(args.span_ext.lo() - BytePos(2));
if tcx.generics_of(adt_def.did()).count() == 0 { if tcx.generics_of(adt_def.did()).is_empty() {
// FIXME(estebank): we could also verify that the arguments being // FIXME(estebank): we could also verify that the arguments being
// work for the `enum`, instead of just looking if it takes *any*. // work for the `enum`, instead of just looking if it takes *any*.
err.span_suggestion_verbose( err.span_suggestion_verbose(

View File

@ -412,7 +412,7 @@ fn lower_generic_args_of_path(
// Traits always have `Self` as a generic parameter, which means they will not return early // Traits always have `Self` as a generic parameter, which means they will not return early
// here and so associated type bindings will be handled regardless of whether there are any // here and so associated type bindings will be handled regardless of whether there are any
// non-`Self` generic parameters. // non-`Self` generic parameters.
if generics.own_params.is_empty() { if generics.is_own_empty() {
return (tcx.mk_args(parent_args), arg_count); return (tcx.mk_args(parent_args), arg_count);
} }

View File

@ -181,7 +181,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
let def_kind = tcx.def_kind(item_def_id); let def_kind = tcx.def_kind(item_def_id);
match def_kind { match def_kind {
DefKind::Static { .. } => tcx.ensure().eval_static_initializer(item_def_id), DefKind::Static { .. } => tcx.ensure().eval_static_initializer(item_def_id),
DefKind::Const if tcx.generics_of(item_def_id).own_params.is_empty() => { DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty()); let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
let cid = GlobalId { instance, promoted: None }; let cid = GlobalId { instance, promoted: None };
let param_env = ty::ParamEnv::reveal_all(); let param_env = ty::ParamEnv::reveal_all();

View File

@ -99,7 +99,7 @@ fn build_constraints_for_item(&mut self, def_id: LocalDefId) {
debug!("build_constraints_for_item({})", tcx.def_path_str(def_id)); debug!("build_constraints_for_item({})", tcx.def_path_str(def_id));
// Skip items with no generics - there's nothing to infer in them. // Skip items with no generics - there's nothing to infer in them.
if tcx.generics_of(def_id).count() == 0 { if tcx.generics_of(def_id).is_empty() {
return; return;
} }

View File

@ -41,7 +41,7 @@ fn crate_variances(tcx: TyCtxt<'_>, (): ()) -> CrateVariancesMap<'_> {
fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] { fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
// Skip items with no generics - there's nothing to infer in them. // Skip items with no generics - there's nothing to infer in them.
if tcx.generics_of(item_def_id).count() == 0 { if tcx.generics_of(item_def_id).is_empty() {
return &[]; return &[];
} }

View File

@ -451,7 +451,7 @@ fn inferred_kind(
// `foo.bar::<u32>(...)` -- the `Self` type here will be the // `foo.bar::<u32>(...)` -- the `Self` type here will be the
// type of `foo` (possibly adjusted), but we don't want to // type of `foo` (possibly adjusted), but we don't want to
// include that. We want just the `[_, u32]` part. // include that. We want just the `[_, u32]` part.
if !args.is_empty() && !generics.own_params.is_empty() { if !args.is_empty() && !generics.is_own_empty() {
let user_type_annotation = self.probe(|_| { let user_type_annotation = self.probe(|_| {
let user_args = UserArgs { let user_args = UserArgs {
args: GenericArgs::for_item(self.tcx, pick.item.def_id, |param, _| { args: GenericArgs::for_item(self.tcx, pick.item.def_id, |param, _| {

View File

@ -279,7 +279,7 @@ pub(super) fn lint_fully_qualified_call_from_2018(
if !self_ty_name.contains('<') { if !self_ty_name.contains('<') {
if let ty::Adt(def, _) = self_ty.kind() { if let ty::Adt(def, _) = self_ty.kind() {
let generics = self.tcx.generics_of(def.did()); let generics = self.tcx.generics_of(def.did());
if !generics.own_params.is_empty() { if !generics.is_own_empty() {
let counts = generics.own_counts(); let counts = generics.own_counts();
self_ty_name += &format!( self_ty_name += &format!(
"<{}>", "<{}>",

View File

@ -1754,7 +1754,7 @@ fn xform_method_sig(&self, method: DefId, args: GenericArgsRef<'tcx>) -> ty::FnS
let generics = self.tcx.generics_of(method); let generics = self.tcx.generics_of(method);
assert_eq!(args.len(), generics.parent_count); assert_eq!(args.len(), generics.parent_count);
let xform_fn_sig = if generics.own_params.is_empty() { let xform_fn_sig = if generics.is_own_empty() {
fn_sig.instantiate(self.tcx, args) fn_sig.instantiate(self.tcx, args)
} else { } else {
let args = GenericArgs::for_item(self.tcx, method, |param, _| { let args = GenericArgs::for_item(self.tcx, method, |param, _| {

View File

@ -391,6 +391,14 @@ pub fn check_concrete_type_after_default(
} }
false false
} }
pub fn is_empty(&'tcx self) -> bool {
self.count() == 0
}
pub fn is_own_empty(&'tcx self) -> bool {
self.own_params.is_empty()
}
} }
/// Bounds on generics. /// Bounds on generics.

View File

@ -160,7 +160,7 @@ fn default_print_def_path(
// If we have any generic arguments to print, we do that // If we have any generic arguments to print, we do that
// on top of the same path, but without its own generics. // on top of the same path, but without its own generics.
_ => { _ => {
if !generics.own_params.is_empty() && args.len() >= generics.count() { if !generics.is_own_empty() && args.len() >= generics.count() {
let args = generics.own_args_no_defaults(self.tcx(), args); let args = generics.own_args_no_defaults(self.tcx(), args);
return self.path_generic_args( return self.path_generic_args(
|cx| cx.print_def_path(def_id, parent_args), |cx| cx.print_def_path(def_id, parent_args),

View File

@ -1429,7 +1429,7 @@ fn process_item(&mut self, id: hir::ItemId) {
match self.tcx.def_kind(id.owner_id) { match self.tcx.def_kind(id.owner_id) {
DefKind::Enum | DefKind::Struct | DefKind::Union => { DefKind::Enum | DefKind::Struct | DefKind::Union => {
if self.strategy == MonoItemCollectionStrategy::Eager if self.strategy == MonoItemCollectionStrategy::Eager
&& self.tcx.generics_of(id.owner_id).count() == 0 && self.tcx.generics_of(id.owner_id).is_empty()
{ {
debug!("RootCollector: ADT drop-glue for `{id:?}`",); debug!("RootCollector: ADT drop-glue for `{id:?}`",);

View File

@ -51,7 +51,7 @@ fn unused_generic_params<'tcx>(
debug!(?generics); debug!(?generics);
// Exit early when there are no parameters to be unused. // Exit early when there are no parameters to be unused.
if generics.count() == 0 { if generics.is_empty() {
return UnusedGenericParams::new_all_used(); return UnusedGenericParams::new_all_used();
} }

View File

@ -398,7 +398,7 @@ pub fn object_safety_violations_for_assoc_item(
// Associated types can only be object safe if they have `Self: Sized` bounds. // Associated types can only be object safe if they have `Self: Sized` bounds.
ty::AssocKind::Type => { ty::AssocKind::Type => {
if !tcx.features().generic_associated_types_extended if !tcx.features().generic_associated_types_extended
&& !tcx.generics_of(item.def_id).own_params.is_empty() && !tcx.generics_of(item.def_id).is_own_empty()
&& !item.is_impl_trait_in_trait() && !item.is_impl_trait_in_trait()
{ {
vec![ObjectSafetyViolation::GAT(item.name, item.ident(tcx).span)] vec![ObjectSafetyViolation::GAT(item.name, item.ident(tcx).span)]

View File

@ -619,7 +619,7 @@ fn confirm_object_candidate(
// higher-ranked things. // higher-ranked things.
// Prevent, e.g., `dyn Iterator<Item = str>`. // Prevent, e.g., `dyn Iterator<Item = str>`.
for bound in self.tcx().item_bounds(assoc_type).transpose_iter() { for bound in self.tcx().item_bounds(assoc_type).transpose_iter() {
let arg_bound = if defs.count() == 0 { let arg_bound = if defs.is_empty() {
bound.instantiate(tcx, trait_predicate.trait_ref.args) bound.instantiate(tcx, trait_predicate.trait_ref.args)
} else { } else {
let mut args = smallvec::SmallVec::with_capacity(defs.count()); let mut args = smallvec::SmallVec::with_capacity(defs.count());

View File

@ -1781,7 +1781,7 @@ pub(super) fn match_projection_projections(
// FIXME(generic-associated-types): This only detects one layer of inference, // FIXME(generic-associated-types): This only detects one layer of inference,
// which is probably not what we actually want, but fixing it causes some ambiguity: // which is probably not what we actually want, but fixing it causes some ambiguity:
// <https://github.com/rust-lang/rust/issues/125196>. // <https://github.com/rust-lang/rust/issues/125196>.
if !generics.own_params.is_empty() if !generics.is_own_empty()
&& obligation.predicate.args[generics.parent_count..].iter().any(|&p| { && obligation.predicate.args[generics.parent_count..].iter().any(|&p| {
p.has_non_region_infer() p.has_non_region_infer()
&& match p.unpack() { && match p.unpack() {

View File

@ -225,7 +225,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'
&& let ImplItemKind::Fn(sig, _) = item.kind && let ImplItemKind::Fn(sig, _) = item.kind
&& let FnRetTy::Return(ret) = sig.decl.output && let FnRetTy::Return(ret) = sig.decl.output
&& is_nameable_in_impl_trait(ret) && is_nameable_in_impl_trait(ret)
&& cx.tcx.generics_of(item_did).own_params.is_empty() && cx.tcx.generics_of(item_did).is_own_empty()
&& sig.decl.implicit_self == expected_implicit_self && sig.decl.implicit_self == expected_implicit_self
&& sig.decl.inputs.len() == 1 && sig.decl.inputs.len() == 1
&& let Some(imp) = get_parent_as_impl(cx.tcx, item.hir_id()) && let Some(imp) = get_parent_as_impl(cx.tcx, item.hir_id())

View File

@ -176,7 +176,7 @@ fn qpath_certainty(cx: &LateContext<'_>, qpath: &QPath<'_>, resolves_to_type: bo
.get(*lang_item) .get(*lang_item)
.map_or(Certainty::Uncertain, |def_id| { .map_or(Certainty::Uncertain, |def_id| {
let generics = cx.tcx.generics_of(def_id); let generics = cx.tcx.generics_of(def_id);
if generics.parent_count == 0 && generics.own_params.is_empty() { if generics.is_empty() {
Certainty::Certain(if resolves_to_type { Some(def_id) } else { None }) Certainty::Certain(if resolves_to_type { Some(def_id) } else { None })
} else { } else {
Certainty::Uncertain Certainty::Uncertain