2024-07-05 15:00:40 +00:00
|
|
|
use std::ops::ControlFlow;
|
|
|
|
|
2023-02-06 18:38:52 +00:00
|
|
|
use crate::middle::resolve_bound_vars as rbv;
|
2022-09-29 09:31:46 +00:00
|
|
|
use hir::{
|
|
|
|
intravisit::{self, Visitor},
|
|
|
|
GenericParamKind, HirId, Node,
|
|
|
|
};
|
|
|
|
use rustc_hir as hir;
|
2023-01-12 06:07:53 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-03-13 18:54:05 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2022-09-29 09:31:46 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
|
|
|
use rustc_session::lint;
|
|
|
|
use rustc_span::symbol::{kw, Symbol};
|
2023-12-04 14:22:05 +00:00
|
|
|
use rustc_span::Span;
|
2022-09-29 09:31:46 +00:00
|
|
|
|
2024-07-17 02:07:36 +00:00
|
|
|
#[instrument(level = "debug", skip(tcx), ret)]
|
2023-03-13 18:54:05 +00:00
|
|
|
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
|
2022-09-29 09:31:46 +00:00
|
|
|
use rustc_hir::*;
|
|
|
|
|
2024-03-05 15:55:04 +00:00
|
|
|
// For an RPITIT, synthesize generics which are equal to the opaque's generics
|
|
|
|
// and parent fn's generics compressed into one list.
|
|
|
|
if let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id }) =
|
|
|
|
tcx.opt_rpitit_info(def_id.to_def_id())
|
|
|
|
{
|
|
|
|
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;
|
2024-05-10 00:56:44 +00:00
|
|
|
let mut own_params = opaque_ty_generics.own_params.clone();
|
2024-03-05 15:55:04 +00:00
|
|
|
|
|
|
|
let parent_generics = tcx.generics_of(trait_def_id);
|
2024-05-10 00:56:44 +00:00
|
|
|
let parent_count = parent_generics.parent_count + parent_generics.own_params.len();
|
2024-03-05 15:55:04 +00:00
|
|
|
|
2024-05-10 00:56:44 +00:00
|
|
|
let mut trait_fn_params = tcx.generics_of(fn_def_id).own_params.clone();
|
2024-03-05 15:55:04 +00:00
|
|
|
|
2024-05-10 00:56:44 +00:00
|
|
|
for param in &mut own_params {
|
2024-03-05 15:55:04 +00:00
|
|
|
param.index = param.index + parent_count as u32 + trait_fn_params.len() as u32
|
|
|
|
- opaque_ty_parent_count as u32;
|
|
|
|
}
|
|
|
|
|
2024-05-10 00:56:44 +00:00
|
|
|
trait_fn_params.extend(own_params);
|
|
|
|
own_params = trait_fn_params;
|
2024-03-05 15:55:04 +00:00
|
|
|
|
|
|
|
let param_def_id_to_index =
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params.iter().map(|param| (param.def_id, param.index)).collect();
|
2024-03-05 15:55:04 +00:00
|
|
|
|
|
|
|
return ty::Generics {
|
|
|
|
parent: Some(trait_def_id),
|
|
|
|
parent_count,
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params,
|
2024-03-05 15:55:04 +00:00
|
|
|
param_def_id_to_index,
|
|
|
|
has_self: opaque_ty_generics.has_self,
|
|
|
|
has_late_bound_regions: opaque_ty_generics.has_late_bound_regions,
|
|
|
|
host_effect_index: parent_generics.host_effect_index,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:28:19 +00:00
|
|
|
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
2022-09-29 09:31:46 +00:00
|
|
|
|
2023-12-01 13:28:34 +00:00
|
|
|
let node = tcx.hir_node(hir_id);
|
2022-09-29 09:31:46 +00:00
|
|
|
let parent_def_id = match node {
|
|
|
|
Node::ImplItem(_)
|
|
|
|
| Node::TraitItem(_)
|
|
|
|
| Node::Variant(_)
|
|
|
|
| Node::Ctor(..)
|
|
|
|
| Node::Field(_) => {
|
|
|
|
let parent_id = tcx.hir().get_parent_item(hir_id);
|
|
|
|
Some(parent_id.to_def_id())
|
|
|
|
}
|
|
|
|
// FIXME(#43408) always enable this once `lazy_normalization` is
|
|
|
|
// stable enough and does not need a feature gate anymore.
|
|
|
|
Node::AnonConst(_) => {
|
2024-06-10 13:32:50 +00:00
|
|
|
let parent_did = tcx.parent(def_id.to_def_id());
|
|
|
|
|
|
|
|
// We don't do this unconditionally because the `DefId` parent of an anon const
|
|
|
|
// might be an implicitly created closure during `async fn` desugaring. This would
|
|
|
|
// have the wrong generics.
|
|
|
|
//
|
|
|
|
// i.e. `async fn foo<'a>() { let a = [(); { 1 + 2 }]; bar().await() }`
|
|
|
|
// would implicitly have a closure in its body that would be the parent of
|
|
|
|
// the `{ 1 + 2 }` anon const. This closure's generics is simply a witness
|
|
|
|
// instead of `['a]`.
|
|
|
|
let parent_did = if let DefKind::AnonConst = tcx.def_kind(parent_did) {
|
|
|
|
parent_did
|
|
|
|
} else {
|
|
|
|
tcx.hir().get_parent_item(hir_id).to_def_id()
|
|
|
|
};
|
|
|
|
debug!(?parent_did);
|
2022-09-29 09:31:46 +00:00
|
|
|
|
|
|
|
let mut in_param_ty = false;
|
|
|
|
for (_parent, node) in tcx.hir().parent_iter(hir_id) {
|
|
|
|
if let Some(generics) = node.generics() {
|
2024-07-05 15:00:40 +00:00
|
|
|
let mut visitor = AnonConstInParamTyDetector { in_param_ty: false, ct: hir_id };
|
2022-09-29 09:31:46 +00:00
|
|
|
|
2024-07-05 15:00:40 +00:00
|
|
|
in_param_ty = visitor.visit_generics(generics).is_break();
|
2022-09-29 09:31:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if in_param_ty {
|
|
|
|
// We do not allow generic parameters in anon consts if we are inside
|
|
|
|
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
|
|
|
|
None
|
2023-06-01 19:56:46 +00:00
|
|
|
} else if tcx.features().generic_const_exprs {
|
2024-02-09 20:58:36 +00:00
|
|
|
let parent_node = tcx.parent_hir_node(hir_id);
|
2024-07-17 02:07:36 +00:00
|
|
|
debug!(?parent_node);
|
2023-05-05 20:31:00 +00:00
|
|
|
if let Node::Variant(Variant { disr_expr: Some(constant), .. }) = parent_node
|
|
|
|
&& constant.hir_id == hir_id
|
|
|
|
{
|
|
|
|
// enum variant discriminants are not allowed to use any kind of generics
|
|
|
|
None
|
|
|
|
} else if let Some(param_id) =
|
|
|
|
tcx.hir().opt_const_param_default_param_def_id(hir_id)
|
|
|
|
{
|
2022-09-29 09:31:46 +00:00
|
|
|
// If the def_id we are calling generics_of on is an anon ct default i.e:
|
|
|
|
//
|
|
|
|
// struct Foo<const N: usize = { .. }>;
|
|
|
|
// ^^^ ^ ^^^^^^ def id of this anon const
|
|
|
|
// ^ ^ param_id
|
|
|
|
// ^ parent_def_id
|
|
|
|
//
|
|
|
|
// then we only want to return generics for params to the left of `N`. If we don't do that we
|
2023-07-11 21:35:29 +00:00
|
|
|
// end up with that const looking like: `ty::ConstKind::Unevaluated(def_id, args: [N#0])`.
|
2022-09-29 09:31:46 +00:00
|
|
|
//
|
2023-07-11 21:35:29 +00:00
|
|
|
// This causes ICEs (#86580) when building the args for Foo in `fn foo() -> Foo { .. }` as
|
2024-02-12 06:39:32 +00:00
|
|
|
// we instantiate the defaults with the partially built args when we build the args. Instantiating
|
2023-07-11 21:35:29 +00:00
|
|
|
// the `N#0` on the unevaluated const indexes into the empty args we're in the process of building.
|
2022-09-29 09:31:46 +00:00
|
|
|
//
|
|
|
|
// We fix this by having this function return the parent's generics ourselves and truncating the
|
|
|
|
// generics to only include non-forward declared params (with the exception of the `Self` ty)
|
|
|
|
//
|
2023-07-11 21:35:29 +00:00
|
|
|
// For the above code example that means we want `args: []`
|
|
|
|
// For the following struct def we want `args: [N#0]` when generics_of is called on
|
2022-09-29 09:31:46 +00:00
|
|
|
// the def id of the `{ N + 1 }` anon const
|
|
|
|
// struct Foo<const N: usize, const M: usize = { N + 1 }>;
|
|
|
|
//
|
|
|
|
// This has some implications for how we get the predicates available to the anon const
|
|
|
|
// see `explicit_predicates_of` for more information on this
|
2024-06-10 13:32:50 +00:00
|
|
|
let generics = tcx.generics_of(parent_did);
|
2022-11-06 18:26:36 +00:00
|
|
|
let param_def_idx = generics.param_def_id_to_index[¶m_id.to_def_id()];
|
2022-09-29 09:31:46 +00:00
|
|
|
// In the above example this would be .params[..N#0]
|
2024-05-10 00:56:44 +00:00
|
|
|
let own_params = generics.params_to(param_def_idx as usize, tcx).to_owned();
|
2022-09-29 09:31:46 +00:00
|
|
|
let param_def_id_to_index =
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params.iter().map(|param| (param.def_id, param.index)).collect();
|
2022-09-29 09:31:46 +00:00
|
|
|
|
|
|
|
return ty::Generics {
|
|
|
|
// we set the parent of these generics to be our parent's parent so that we
|
2023-07-11 21:35:29 +00:00
|
|
|
// dont end up with args: [N, M, N] for the const default on a struct like this:
|
2022-09-29 09:31:46 +00:00
|
|
|
// struct Foo<const N: usize, const M: usize = { ... }>;
|
|
|
|
parent: generics.parent,
|
|
|
|
parent_count: generics.parent_count,
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params,
|
2022-09-29 09:31:46 +00:00
|
|
|
param_def_id_to_index,
|
|
|
|
has_self: generics.has_self,
|
|
|
|
has_late_bound_regions: generics.has_late_bound_regions,
|
2023-07-04 13:49:08 +00:00
|
|
|
host_effect_index: None,
|
2022-09-29 09:31:46 +00:00
|
|
|
};
|
2023-05-05 20:31:00 +00:00
|
|
|
} else {
|
|
|
|
// HACK(eddyb) this provides the correct generics when
|
|
|
|
// `feature(generic_const_expressions)` is enabled, so that const expressions
|
|
|
|
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
|
|
|
|
//
|
|
|
|
// Note that we do not supply the parent generics when using
|
|
|
|
// `min_const_generics`.
|
2024-06-10 13:32:50 +00:00
|
|
|
Some(parent_did)
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-09 20:58:36 +00:00
|
|
|
let parent_node = tcx.parent_hir_node(hir_id);
|
2024-07-17 02:07:36 +00:00
|
|
|
let parent_node = match parent_node {
|
|
|
|
Node::ConstArg(ca) => tcx.parent_hir_node(ca.hir_id),
|
|
|
|
_ => parent_node,
|
|
|
|
};
|
2022-09-29 09:31:46 +00:00
|
|
|
match parent_node {
|
|
|
|
// HACK(eddyb) this provides the correct generics for repeat
|
|
|
|
// expressions' count (i.e. `N` in `[x; N]`), and explicit
|
|
|
|
// `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
|
|
|
|
// as they shouldn't be able to cause query cycle errors.
|
2024-07-17 02:07:36 +00:00
|
|
|
Node::Expr(Expr { kind: ExprKind::Repeat(_, ArrayLen::Body(ct)), .. })
|
|
|
|
if ct.anon_const_hir_id() == Some(hir_id) =>
|
2022-09-29 09:31:46 +00:00
|
|
|
{
|
2024-06-10 13:32:50 +00:00
|
|
|
Some(parent_did)
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
// Exclude `GlobalAsm` here which cannot have generics.
|
|
|
|
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
|
|
|
|
if asm.operands.iter().any(|(op, _op_sp)| match op {
|
|
|
|
hir::InlineAsmOperand::Const { anon_const }
|
|
|
|
| hir::InlineAsmOperand::SymFn { anon_const } => {
|
|
|
|
anon_const.hir_id == hir_id
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}) =>
|
|
|
|
{
|
2024-06-10 13:32:50 +00:00
|
|
|
Some(parent_did)
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-03 09:11:58 +00:00
|
|
|
Node::ConstBlock(_)
|
|
|
|
| Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
|
|
|
|
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
|
|
|
|
}
|
2022-09-29 09:31:46 +00:00
|
|
|
Node::Item(item) => match item.kind {
|
2023-06-22 23:53:20 +00:00
|
|
|
ItemKind::OpaqueTy(&hir::OpaqueTy {
|
2023-01-12 06:07:53 +00:00
|
|
|
origin:
|
|
|
|
hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
|
|
|
|
in_trait,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
if in_trait {
|
|
|
|
assert!(matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn))
|
|
|
|
} else {
|
|
|
|
assert!(matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn))
|
|
|
|
}
|
|
|
|
Some(fn_def_id.to_def_id())
|
|
|
|
}
|
2024-04-18 01:58:27 +00:00
|
|
|
ItemKind::OpaqueTy(&hir::OpaqueTy {
|
|
|
|
origin: hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty },
|
2023-04-17 10:19:41 +00:00
|
|
|
..
|
|
|
|
}) => {
|
2024-04-18 01:58:27 +00:00
|
|
|
if in_assoc_ty {
|
|
|
|
assert!(matches!(tcx.def_kind(parent), DefKind::AssocTy));
|
|
|
|
} else {
|
|
|
|
assert!(matches!(tcx.def_kind(parent), DefKind::TyAlias));
|
|
|
|
}
|
|
|
|
debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent);
|
2022-09-29 09:31:46 +00:00
|
|
|
// Opaque types are always nested within another item, and
|
|
|
|
// inherit the generics of the item.
|
2024-04-18 01:58:27 +00:00
|
|
|
Some(parent.to_def_id())
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Defaults {
|
|
|
|
Allowed,
|
|
|
|
// See #36887
|
|
|
|
FutureCompatDisallowed,
|
|
|
|
Deny,
|
|
|
|
}
|
|
|
|
|
2024-02-11 08:24:35 +00:00
|
|
|
let hir_generics = node.generics().unwrap_or(hir::Generics::empty());
|
2022-09-29 09:31:46 +00:00
|
|
|
let (opt_self, allow_defaults) = match node {
|
|
|
|
Node::Item(item) => {
|
|
|
|
match item.kind {
|
|
|
|
ItemKind::Trait(..) | ItemKind::TraitAlias(..) => {
|
|
|
|
// Add in the self type parameter.
|
|
|
|
//
|
|
|
|
// Something of a hack: use the node id for the trait, also as
|
|
|
|
// the node id for the Self type parameter.
|
|
|
|
let opt_self = Some(ty::GenericParamDef {
|
|
|
|
index: 0,
|
|
|
|
name: kw::SelfUpper,
|
2023-03-13 18:54:05 +00:00
|
|
|
def_id: def_id.to_def_id(),
|
2022-09-29 09:31:46 +00:00
|
|
|
pure_wrt_drop: false,
|
|
|
|
kind: ty::GenericParamDefKind::Type {
|
|
|
|
has_default: false,
|
|
|
|
synthetic: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
(opt_self, Defaults::Allowed)
|
|
|
|
}
|
|
|
|
ItemKind::TyAlias(..)
|
|
|
|
| ItemKind::Enum(..)
|
|
|
|
| ItemKind::Struct(..)
|
|
|
|
| ItemKind::OpaqueTy(..)
|
|
|
|
| ItemKind::Union(..) => (None, Defaults::Allowed),
|
2023-07-02 16:48:43 +00:00
|
|
|
ItemKind::Const(..) => (None, Defaults::Deny),
|
2022-09-29 09:31:46 +00:00
|
|
|
_ => (None, Defaults::FutureCompatDisallowed),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GATs
|
|
|
|
Node::TraitItem(item) if matches!(item.kind, TraitItemKind::Type(..)) => {
|
|
|
|
(None, Defaults::Deny)
|
|
|
|
}
|
2022-10-09 07:09:57 +00:00
|
|
|
Node::ImplItem(item) if matches!(item.kind, ImplItemKind::Type(..)) => {
|
2022-09-29 09:31:46 +00:00
|
|
|
(None, Defaults::Deny)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => (None, Defaults::FutureCompatDisallowed),
|
|
|
|
};
|
|
|
|
|
|
|
|
let has_self = opt_self.is_some();
|
|
|
|
let mut parent_has_self = false;
|
|
|
|
let mut own_start = has_self as u32;
|
2023-07-04 13:49:08 +00:00
|
|
|
let mut host_effect_index = None;
|
2022-09-29 09:31:46 +00:00
|
|
|
let parent_count = parent_def_id.map_or(0, |def_id| {
|
|
|
|
let generics = tcx.generics_of(def_id);
|
|
|
|
assert!(!has_self);
|
|
|
|
parent_has_self = generics.has_self;
|
2023-07-04 13:49:08 +00:00
|
|
|
host_effect_index = generics.host_effect_index;
|
2022-09-29 09:31:46 +00:00
|
|
|
own_start = generics.count() as u32;
|
2024-05-10 00:56:44 +00:00
|
|
|
generics.parent_count + generics.own_params.len()
|
2022-09-29 09:31:46 +00:00
|
|
|
});
|
|
|
|
|
2024-05-10 00:56:44 +00:00
|
|
|
let mut own_params: Vec<_> = Vec::with_capacity(hir_generics.params.len() + has_self as usize);
|
2022-09-29 09:31:46 +00:00
|
|
|
|
|
|
|
if let Some(opt_self) = opt_self {
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params.push(opt_self);
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 08:24:35 +00:00
|
|
|
let early_lifetimes = super::early_bound_lifetimes_from_generics(tcx, hir_generics);
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
|
2022-09-29 09:31:46 +00:00
|
|
|
name: param.name.ident().name,
|
|
|
|
index: own_start + i as u32,
|
2022-11-06 18:26:36 +00:00
|
|
|
def_id: param.def_id.to_def_id(),
|
2022-09-29 09:31:46 +00:00
|
|
|
pure_wrt_drop: param.pure_wrt_drop,
|
|
|
|
kind: ty::GenericParamDefKind::Lifetime,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Now create the real type and const parameters.
|
2024-05-10 00:56:44 +00:00
|
|
|
let type_start = own_start - has_self as u32 + own_params.len() as u32;
|
2023-07-04 13:49:08 +00:00
|
|
|
let mut i: u32 = 0;
|
2022-10-24 11:58:45 +00:00
|
|
|
let mut next_index = || {
|
|
|
|
let prev = i;
|
|
|
|
i += 1;
|
2023-07-04 13:49:08 +00:00
|
|
|
prev + type_start
|
2022-10-24 11:58:45 +00:00
|
|
|
};
|
2022-09-29 09:31:46 +00:00
|
|
|
|
|
|
|
const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \
|
|
|
|
`struct`, `enum`, `type`, or `trait` definitions";
|
|
|
|
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params.extend(hir_generics.params.iter().filter_map(|param| match param.kind {
|
2022-09-29 09:31:46 +00:00
|
|
|
GenericParamKind::Lifetime { .. } => None,
|
2023-01-09 16:30:40 +00:00
|
|
|
GenericParamKind::Type { default, synthetic, .. } => {
|
2022-09-29 09:31:46 +00:00
|
|
|
if default.is_some() {
|
|
|
|
match allow_defaults {
|
|
|
|
Defaults::Allowed => {}
|
|
|
|
Defaults::FutureCompatDisallowed
|
|
|
|
if tcx.features().default_type_parameter_fallback => {}
|
|
|
|
Defaults::FutureCompatDisallowed => {
|
2024-01-16 05:14:33 +00:00
|
|
|
tcx.node_span_lint(
|
2022-09-29 09:31:46 +00:00
|
|
|
lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
|
|
|
|
param.hir_id,
|
|
|
|
param.span,
|
2024-05-22 14:46:05 +00:00
|
|
|
|lint| {
|
|
|
|
lint.primary_message(TYPE_DEFAULT_NOT_ALLOWED);
|
|
|
|
},
|
2022-09-29 09:31:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Defaults::Deny => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(param.span, TYPE_DEFAULT_NOT_ALLOWED);
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let kind = ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic };
|
|
|
|
|
2022-10-24 11:58:45 +00:00
|
|
|
Some(ty::GenericParamDef {
|
|
|
|
index: next_index(),
|
2022-09-29 09:31:46 +00:00
|
|
|
name: param.name.ident().name,
|
2022-11-06 18:26:36 +00:00
|
|
|
def_id: param.def_id.to_def_id(),
|
2022-09-29 09:31:46 +00:00
|
|
|
pure_wrt_drop: param.pure_wrt_drop,
|
|
|
|
kind,
|
2022-10-24 11:58:45 +00:00
|
|
|
})
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
2024-06-14 12:16:15 +00:00
|
|
|
GenericParamKind::Const { ty: _, default, is_host_effect, synthetic } => {
|
2023-06-30 15:50:58 +00:00
|
|
|
if !matches!(allow_defaults, Defaults::Allowed)
|
|
|
|
&& default.is_some()
|
2023-12-04 14:22:05 +00:00
|
|
|
// `host` effect params are allowed to have defaults.
|
|
|
|
&& !is_host_effect
|
2023-06-30 15:50:58 +00:00
|
|
|
{
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_err(
|
2022-09-29 09:31:46 +00:00
|
|
|
param.span,
|
|
|
|
"defaults for const parameters are only allowed in \
|
|
|
|
`struct`, `enum`, `type`, or `trait` definitions",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:49:08 +00:00
|
|
|
let index = next_index();
|
|
|
|
|
2023-12-04 14:22:05 +00:00
|
|
|
if is_host_effect {
|
2023-07-04 13:49:08 +00:00
|
|
|
if let Some(idx) = host_effect_index {
|
2024-02-22 03:41:32 +00:00
|
|
|
tcx.dcx().span_delayed_bug(
|
2024-01-02 00:11:21 +00:00
|
|
|
param.span,
|
|
|
|
format!("parent also has host effect param? index: {idx}, def: {def_id:?}"),
|
|
|
|
);
|
2023-07-04 13:49:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 05:58:53 +00:00
|
|
|
host_effect_index = Some(index as usize);
|
2023-07-04 13:49:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 11:58:45 +00:00
|
|
|
Some(ty::GenericParamDef {
|
2023-07-04 13:49:08 +00:00
|
|
|
index,
|
2022-09-29 09:31:46 +00:00
|
|
|
name: param.name.ident().name,
|
2022-11-06 18:26:36 +00:00
|
|
|
def_id: param.def_id.to_def_id(),
|
2022-09-29 09:31:46 +00:00
|
|
|
pure_wrt_drop: param.pure_wrt_drop,
|
2023-09-11 13:18:36 +00:00
|
|
|
kind: ty::GenericParamDefKind::Const {
|
|
|
|
has_default: default.is_some(),
|
2023-12-04 14:22:05 +00:00
|
|
|
is_host_effect,
|
2024-06-14 12:16:15 +00:00
|
|
|
synthetic,
|
2023-09-11 13:18:36 +00:00
|
|
|
},
|
2022-10-24 11:58:45 +00:00
|
|
|
})
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// provide junk type parameter defs - the only place that
|
|
|
|
// cares about anything but the length is instantiation,
|
|
|
|
// and we don't do that for closures.
|
|
|
|
if let Node::Expr(&hir::Expr {
|
2023-12-22 21:29:12 +00:00
|
|
|
kind: hir::ExprKind::Closure(hir::Closure { kind, .. }), ..
|
2022-09-29 09:31:46 +00:00
|
|
|
}) = node
|
|
|
|
{
|
2024-02-07 16:06:52 +00:00
|
|
|
// See `ClosureArgsParts`, `CoroutineArgsParts`, and `CoroutineClosureArgsParts`
|
|
|
|
// for info on the usage of each of these fields.
|
2023-12-22 21:29:12 +00:00
|
|
|
let dummy_args = match kind {
|
|
|
|
ClosureKind::Closure => &["<closure_kind>", "<closure_signature>", "<upvars>"][..],
|
2024-02-07 16:06:52 +00:00
|
|
|
ClosureKind::Coroutine(_) => &[
|
|
|
|
"<coroutine_kind>",
|
|
|
|
"<resume_ty>",
|
|
|
|
"<yield_ty>",
|
|
|
|
"<return_ty>",
|
|
|
|
"<witness>",
|
|
|
|
"<upvars>",
|
|
|
|
][..],
|
2024-01-24 18:01:56 +00:00
|
|
|
ClosureKind::CoroutineClosure(_) => &[
|
|
|
|
"<closure_kind>",
|
|
|
|
"<closure_signature_parts>",
|
|
|
|
"<upvars>",
|
|
|
|
"<bound_captures_by_ref>",
|
|
|
|
"<witness>",
|
|
|
|
][..],
|
2022-09-29 09:31:46 +00:00
|
|
|
};
|
|
|
|
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
|
2022-10-24 11:58:45 +00:00
|
|
|
index: next_index(),
|
2022-09-29 09:31:46 +00:00
|
|
|
name: Symbol::intern(arg),
|
2023-03-13 18:54:05 +00:00
|
|
|
def_id: def_id.to_def_id(),
|
2022-09-29 09:31:46 +00:00
|
|
|
pure_wrt_drop: false,
|
|
|
|
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// provide junk type parameter defs for const blocks.
|
2024-06-03 09:11:58 +00:00
|
|
|
if let Node::ConstBlock(_) = node {
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params.push(ty::GenericParamDef {
|
2023-02-25 19:53:37 +00:00
|
|
|
index: next_index(),
|
|
|
|
name: Symbol::intern("<const_ty>"),
|
|
|
|
def_id: def_id.to_def_id(),
|
|
|
|
pure_wrt_drop: false,
|
|
|
|
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
|
|
|
|
});
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
|
2024-05-10 00:56:44 +00:00
|
|
|
let param_def_id_to_index =
|
|
|
|
own_params.iter().map(|param| (param.def_id, param.index)).collect();
|
2022-09-29 09:31:46 +00:00
|
|
|
|
|
|
|
ty::Generics {
|
|
|
|
parent: parent_def_id,
|
|
|
|
parent_count,
|
2024-05-10 00:56:44 +00:00
|
|
|
own_params,
|
2022-09-29 09:31:46 +00:00
|
|
|
param_def_id_to_index,
|
|
|
|
has_self: has_self || parent_has_self,
|
|
|
|
has_late_bound_regions: has_late_bound_regions(tcx, node),
|
2023-07-04 13:49:08 +00:00
|
|
|
host_effect_index,
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<Span> {
|
|
|
|
struct LateBoundRegionsDetector<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
outer_index: ty::DebruijnIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for LateBoundRegionsDetector<'tcx> {
|
2024-07-05 15:00:40 +00:00
|
|
|
type Result = ControlFlow<Span>;
|
|
|
|
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) -> ControlFlow<Span> {
|
2022-09-29 09:31:46 +00:00
|
|
|
match ty.kind {
|
|
|
|
hir::TyKind::BareFn(..) => {
|
|
|
|
self.outer_index.shift_in(1);
|
2024-07-05 15:00:40 +00:00
|
|
|
let res = intravisit::walk_ty(self, ty);
|
2022-09-29 09:31:46 +00:00
|
|
|
self.outer_index.shift_out(1);
|
2024-07-05 15:00:40 +00:00
|
|
|
res
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
_ => intravisit::walk_ty(self, ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 15:00:40 +00:00
|
|
|
fn visit_poly_trait_ref(&mut self, tr: &'tcx hir::PolyTraitRef<'tcx>) -> ControlFlow<Span> {
|
2022-09-29 09:31:46 +00:00
|
|
|
self.outer_index.shift_in(1);
|
2024-07-05 15:00:40 +00:00
|
|
|
let res = intravisit::walk_poly_trait_ref(self, tr);
|
2022-09-29 09:31:46 +00:00
|
|
|
self.outer_index.shift_out(1);
|
2024-07-05 15:00:40 +00:00
|
|
|
res
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 15:00:40 +00:00
|
|
|
fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) -> ControlFlow<Span> {
|
2023-02-06 18:38:52 +00:00
|
|
|
match self.tcx.named_bound_var(lt.hir_id) {
|
2024-07-05 15:00:40 +00:00
|
|
|
Some(rbv::ResolvedArg::StaticLifetime | rbv::ResolvedArg::EarlyBound(..)) => {
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
2023-02-06 18:38:52 +00:00
|
|
|
Some(rbv::ResolvedArg::LateBound(debruijn, _, _))
|
2024-07-05 15:00:40 +00:00
|
|
|
if debruijn < self.outer_index =>
|
|
|
|
{
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
2023-02-18 03:28:43 +00:00
|
|
|
Some(
|
|
|
|
rbv::ResolvedArg::LateBound(..)
|
|
|
|
| rbv::ResolvedArg::Free(..)
|
|
|
|
| rbv::ResolvedArg::Error(_),
|
|
|
|
)
|
2024-07-05 15:00:40 +00:00
|
|
|
| None => ControlFlow::Break(lt.ident.span),
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_late_bound_regions<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
generics: &'tcx hir::Generics<'tcx>,
|
|
|
|
decl: &'tcx hir::FnDecl<'tcx>,
|
|
|
|
) -> Option<Span> {
|
2024-07-05 15:00:40 +00:00
|
|
|
let mut visitor = LateBoundRegionsDetector { tcx, outer_index: ty::INNERMOST };
|
2022-09-29 09:31:46 +00:00
|
|
|
for param in generics.params {
|
|
|
|
if let GenericParamKind::Lifetime { .. } = param.kind {
|
|
|
|
if tcx.is_late_bound(param.hir_id) {
|
|
|
|
return Some(param.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-05 15:00:40 +00:00
|
|
|
visitor.visit_fn_decl(decl).break_value()
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 12:16:22 +00:00
|
|
|
let decl = node.fn_decl()?;
|
|
|
|
let generics = node.generics()?;
|
|
|
|
has_late_bound_regions(tcx, generics, decl)
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AnonConstInParamTyDetector {
|
|
|
|
in_param_ty: bool,
|
|
|
|
ct: HirId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'v> Visitor<'v> for AnonConstInParamTyDetector {
|
2024-07-05 15:00:40 +00:00
|
|
|
type Result = ControlFlow<()>;
|
|
|
|
|
|
|
|
fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) -> Self::Result {
|
2024-06-14 12:16:15 +00:00
|
|
|
if let GenericParamKind::Const { ty, default: _, is_host_effect: _, synthetic: _ } = p.kind
|
|
|
|
{
|
2022-09-29 09:31:46 +00:00
|
|
|
let prev = self.in_param_ty;
|
|
|
|
self.in_param_ty = true;
|
2024-07-05 15:00:40 +00:00
|
|
|
let res = self.visit_ty(ty);
|
2022-09-29 09:31:46 +00:00
|
|
|
self.in_param_ty = prev;
|
2024-07-05 15:00:40 +00:00
|
|
|
res
|
|
|
|
} else {
|
|
|
|
ControlFlow::Continue(())
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 15:00:40 +00:00
|
|
|
fn visit_anon_const(&mut self, c: &'v hir::AnonConst) -> Self::Result {
|
2022-09-29 09:31:46 +00:00
|
|
|
if self.in_param_ty && self.ct == c.hir_id {
|
2024-07-05 15:00:40 +00:00
|
|
|
return ControlFlow::Break(());
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
2024-07-05 15:00:40 +00:00
|
|
|
intravisit::walk_anon_const(self, c)
|
2022-09-29 09:31:46 +00:00
|
|
|
}
|
|
|
|
}
|